All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
chunk.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cassert>
3 
4 #include "chunk.h"
5 
6 void uscript::Chunk::Write(uint8_t instruction) {
7  code.push_back(instruction);
8 }
9 
11  constants.push_back(constant);
12  return constants.size() - 1;
13 }
14 
15 void uscript::Chunk::Disassemble(const std::string &name) const {
16  std::cout << "== " << name << " ==" << std::endl;
17  for (unsigned index = 0; index < code.size();) {
18  index = DisassembleInstruction(index);
19  }
20 }
21 
22 unsigned simpleInstruction(const char *name, unsigned index) {
23  std::cout << name << std::endl;
24  return index + 1;
25 }
26 
27 unsigned jumpInstruction(const char *name, int sign, unsigned index, uint8_t jumpa, uint8_t jumpb) {
28  uint16_t jump = ((uint16_t)jumpa) << 8;
29  jump |= jumpb;
30 
31  std::cout << name << " " << index << " " << (index + 3 + sign * jump) << std::endl;
32  return index + 3;
33 }
34 
35 unsigned constantInstruction(const char *name, unsigned index, uint8_t constant, const uscript::Value &value) {
36  std::cout << name << " " << (unsigned)constant << " ";
37  value.Print();
38  std::cout << std::endl;
39  return index + 2;
40 }
41 
42 unsigned byteInstruction(const char *name, unsigned index, uint8_t slot) {
43  std::cout << name << " " << (unsigned)slot << std::endl;
44  return index + 2;
45 }
46 
47 unsigned uscript::Chunk::DisassembleInstruction(unsigned index) const {
48  std::cout << index << " ";
49  uint8_t instruction = code[index];
50  switch (instruction) {
51  case uscript::OP_RETURN:
52  return simpleInstruction("OP_RETURN", index);
53  case uscript::OP_PRINT:
54  return simpleInstruction("OP_PRINT", index);
55  case uscript::OP_FIELDS:
56  return simpleInstruction("OP_FIELDS", index);
57  case uscript::OP_LENGTH:
58  return simpleInstruction("OP_LENGTH", index);
60  return constantInstruction("OP_CONSTANT", index, code[index+1], constants[code[index+1]]);
61  case uscript::OP_ADD:
62  return simpleInstruction("OP_ADD", index);
64  return simpleInstruction("OP_SUBTRACT", index);
66  return simpleInstruction("OP_MULTIPLY", index);
67  case uscript::OP_DIVIDE:
68  return simpleInstruction("OP_DIVIDE", index);
69  case uscript::OP_NEGATE:
70  return simpleInstruction("OP_NEGATE", index);
71  case uscript::OP_NIL:
72  return simpleInstruction("OP_NIL", index);
73  case uscript::OP_NOT:
74  return simpleInstruction("OP_NOT", index);
75  case uscript::OP_TRUE:
76  return simpleInstruction("OP_TRUE", index);
77  case uscript::OP_FALSE:
78  return simpleInstruction("OP_FALSE", index);
79  case uscript::OP_POP:
80  return simpleInstruction("OP_POP", index);
81  case uscript::OP_LOOP:
82  return jumpInstruction("OP_LOOP", -1, index, code[index+1], code[index+2]);
83  case uscript::OP_JUMP:
84  return jumpInstruction("OP_JUMP", 1, index, code[index+1], code[index+2]);
86  return jumpInstruction("OP_JUMP_IF_FALSE", 1, index, code[index+1], code[index+2]);
88  return constantInstruction("OP_GET_PROPERTY", index, code[index+1], constants[code[index+1]]);
89  case uscript::OP_CALL:
90  return byteInstruction("OP_CALL", index, code[index+1]);
91  case uscript::OP_INDEX:
92  return simpleInstruction("OP_INDEX", index);
94  return byteInstruction("OP_SET_LOCAL", index, code[index+1]);
96  return byteInstruction("OP_GET_LOCAL", index, code[index+1]);
98  return constantInstruction("OP_DEFINE_GLOBAL", index, code[index+1], constants[code[index+1]]);
100  return constantInstruction("OP_GET_GLOBAL", index, code[index+1], constants[code[index+1]]);
102  return constantInstruction("OP_SET_GLOBAL", index, code[index+1], constants[code[index+1]]);
103  case uscript::OP_EQUAL:
104  return simpleInstruction("OP_EQUAL", index);
105  case uscript::OP_GREATER:
106  return simpleInstruction("OP_GREATER", index);
107  case uscript::OP_LESS:
108  return simpleInstruction("OP_LESS", index);
109  default:
110  std::cout << "Unknown Instruction: " << instruction << std::endl;
111  return index + 1;
112  }
113 }
114 
unsigned jumpInstruction(const char *name, int sign, unsigned index, uint8_t jumpa, uint8_t jumpb)
Definition: chunk.cc:27
unsigned simpleInstruction(const char *name, unsigned index)
Definition: chunk.cc:22
unsigned constantInstruction(const char *name, unsigned index, uint8_t constant, const uscript::Value &value)
Definition: chunk.cc:35
void Write(uint8_t instruction)
Definition: chunk.cc:6
std::vector< uint8_t > code
Definition: chunk.h:52
unsigned AddConstant(Value constant)
Definition: chunk.cc:10
void Disassemble(const std::string &name) const
Definition: chunk.cc:15
int sign(double val)
Definition: UtilFunc.cxx:104
void Print() const
Definition: value.cc:5
unsigned DisassembleInstruction(unsigned index) const
Definition: chunk.cc:47
then echo fcl name
temporary value
BEGIN_PROLOG could also be cout
unsigned byteInstruction(const char *name, unsigned index, uint8_t slot)
Definition: chunk.cc:42