about summary refs log tree commit diff stats
path: root/029tools.cc
blob: 244b72b25cb251bffa07b1c286dd8c8f3fcd89dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//: Allow Mu programs to log facts just like we've been doing in C++ so far.

:(scenario trace)
def main [
  trace 1, [foo], [this is a trace in Mu]
]
+foo: this is a trace in Mu

:(before "End Primitive Recipe Declarations")
TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "trace", TRACE);
:(before "End Primitive Recipe Checks")
case TRACE: {
  if (SIZE(inst.ingredients) < 3) {
    raise << maybe(get(Recipe, r).name) << "'trace' takes three or more ingredients rather than '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_number(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "first ingredient of 'trace' should be a number (depth), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
    break;
  }
  if (!is_literal_text(inst.ingredients.at(1))) {
    raise << maybe(get(Recipe, r).name) << "second ingredient of 'trace' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case TRACE: {
  int depth = ingredients.at(0).at(0);
  string label = current_instruction().ingredients.at(1).name;
  ostringstream out;
  for (int i = 2;  i < SIZE(current_instruction().ingredients);  ++i) {
    if (i > 2) out << ' ';
    out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  trace(depth, label) << out.str() << end();
  break;
}

//: simpler limited version of 'trace'

:(before "End Primitive Recipe Declarations")
STASH,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "stash", STASH);
:(before "End Primitive Recipe Checks")
case STASH: {
  break;
}
:(before "End Primitive Recipe Implementations")
case STASH: {
  ostringstream out;
  for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i) {
    if (i) out << ' ';
    out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  trace(2, "app") << out.str() << end();
  break;
}

:(scenario stash_literal_string)
def main [
  stash [foo]
]
+app: foo

:(scenario stash_literal_number)
def main [
  stash [foo:], 4
]
+app: foo: 4

:(scenario stash_number)
def main [
  1:num <- copy 34
  stash [foo:], 1:num
]
+app: foo: 34

:(code)
string inspect(const reagent& r, const vector<double>& data) {
  if (is_literal(r))
    return r.name;
  // End inspect Special-cases(r, data)
  ostringstream out;
  for (long long i = 0;  i < SIZE(data);  ++i) {
    if (i) out << ' ';
    out << no_scientific(data.at(i));
  }
  return out.str();
}

:(before "End Primitive Recipe Declarations")
HIDE_ERRORS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "hide-errors", HIDE_ERRORS);
:(before "End Primitive Recipe Checks")
case HIDE_ERRORS: {
  break;
}
:(before "End Primitive Recipe Implementations")
case HIDE_ERRORS: {
  Hide_errors = true;
  break;
}

:(before "End Primitive Recipe Declarations")
SHOW_ERRORS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "show-errors", SHOW_ERRORS);
:(before "End Primitive Recipe Checks")
case SHOW_ERRORS: {
  break;
}
:(before "End Primitive Recipe Implementations")
case SHOW_ERRORS: {
  Hide_errors = false;
  break;
}

:(before "End Primitive Recipe Declarations")
TRACE_UNTIL,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "trace-until", TRACE_UNTIL);
:(before "End Primitive Recipe Checks")
case TRACE_UNTIL: {
  break;
}
:(before "End Primitive Recipe Implementations")
case TRACE_UNTIL: {
  if (Trace_stream) {
    Trace_stream->collect_depth = ingredients.at(0).at(0);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
_DUMP_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-trace", _DUMP_TRACE);
:(before "End Primitive Recipe Checks")
case _DUMP_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_TRACE: {
  if (ingredients.empty()) {
    DUMP("");
  }
  else {
    DUMP(current_instruction().ingredients.at(0).name);
  }
  break;
}

:(before "End Primitive Recipe Declarations")
_CLEAR_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$clear-trace", _CLEAR_TRACE);
:(before "End Primitive Recipe Checks")
case _CLEAR_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _CLEAR_TRACE: {
  if (Trace_stream) Trace_stream->past_lines.clear();
  break;
}

:(before "End Primitive Recipe Declarations")
_SAVE_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$save-trace", _SAVE_TRACE);
:(before "End Primitive Recipe Checks")
case _SAVE_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _SAVE_TRACE: {
  if (Save_trace) Trace_stream->dump();
  break;
}

//:: 'cheating' by using the host system

:(before "End Primitive Recipe Declarations")
_PRINT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$print", _PRINT);
:(before "End Primitive Recipe Checks")
case _PRINT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _PRINT: {
  for (int i = 0;  i < SIZE(ingredients);  ++i) {
    if (is_literal(current_instruction().ingredients.at(i))) {
      trace(9998, "run") << "$print: " << current_instruction().ingredients.at(i).name << end();
      if (!has_property(current_instruction().ingredients.at(i), "newline")) {
        cout << current_instruction().ingredients.at(i).name;
      }
      // hack: '$print 10' prints '10', but '$print 10/newline' prints '\n'
      // End $print 10/newline Special-cases
      else {
        cout << '\n';
      }
    }
    // End $print Special-cases
    else {
      for (int j = 0;  j < SIZE(ingredients.at(i));  ++j) {
        trace(9998, "run") << "$print: " << ingredients.at(i).at(j) << end();
        if (j > 0) cout << " ";
        cout << no_scientific(ingredients.at(i).at(j));
      }
    }
  }
  cout.flush();
  break;
}

:(before "End Primitive Recipe Declarations")
_EXIT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$exit", _EXIT);
:(before "End Primitive Recipe Checks")
case _EXIT: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _EXIT: {
  exit(0);
  break;
}

:(before "End Primitive Recipe Declarations")
_SYSTEM,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$system", _SYSTEM);
:(before "End Primitive Recipe Checks")
case _SYSTEM: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'$system' requires exactly one ingredient, but got '" << to_string(inst) << "'\n" << end();
    break;
  }
  if (!is_literal_text(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "ingredient to '$system' must be a literal text, but got '" << to_string(inst) << "'\n" << end();
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case _SYSTEM: {
  int status = system(current_instruction().ingredients.at(0).name.c_str());
  products.resize(1);
  products.at(0).push_back(status);
  break;
}

:(before "End Primitive Recipe Declarations")
_DUMP_MEMORY,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump-memory", _DUMP_MEMORY);
:(before "End Primitive Recipe Checks")
case _DUMP_MEMORY: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _DUMP_MEMORY: {
  dump_memory();
  break;
}

//: In times of real extremis we need to create a whole new modality for debug
//: logs, independent of other changes to the screen or Trace_stream.

:(before "End Globals")
ofstream LOG;
:(before "End One-time Setup")
//? LOG.open("log");

:(before "End Primitive Recipe Declarations")
_LOG,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$log", _LOG);
:(before "End Primitive Recipe Checks")
case _LOG: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _LOG: {
  ostringstream out;
  for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i) {
    out << inspect(current_instruction().ingredients.at(i), ingredients.at(i));
  }
  LOG << out.str() << '\n';
  break;
}

//: set a variable from within Mu code
//: useful for selectively tracing or printing after some point
:(before "End Globals")
bool Foo = false;
:(before "End Primitive Recipe Declarations")
_FOO,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$foo", _FOO);
:(before "End Primitive Recipe Checks")
case _FOO: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _FOO: {
  Foo = true;
  break;
}
pan>uint32_t addr, int32_t val) { int32_t* handle = mem_addr_i32(addr); if (handle != NULL) *handle = val; } inline bool already_allocated(uint32_t addr) { bool result = false; for (int i = 0; i < SIZE(Mem); ++i) { if (Mem.at(i).match(addr)) { if (result) raise << "address 0x" << HEXWORD << addr << " is in two segments\n" << end(); result = true; } } return result; } //:: core interpreter loop :(code) // skeleton of how x86 instructions are decoded void run_one_instruction() { uint8_t op=0, op2=0, op3=0; // Run One Instruction if (Trace_file.is_open()) { dump_registers(); // End Dump Info for Instruction } uint32_t inst_start_address = EIP; op = next(); trace(Callstack_depth+1, "run") << "0x" << HEXWORD << inst_start_address << " opcode: " << HEXBYTE << NUM(op) << end(); switch (op) { case 0xf4: // hlt EIP = End_of_program; break; // End Single-Byte Opcodes case 0x0f: switch(op2 = next()) { // End Two-Byte Opcodes Starting With 0f default: cerr << "unrecognized second opcode after 0f: " << HEXBYTE << NUM(op2) << '\n'; exit(1); } break; case 0xf2: switch(op2 = next()) { // End Two-Byte Opcodes Starting With f2 case 0x0f: switch(op3 = next()) { // End Three-Byte Opcodes Starting With f2 0f default: cerr << "unrecognized third opcode after f2 0f: " << HEXBYTE << NUM(op3) << '\n'; exit(1); } break; default: cerr << "unrecognized second opcode after f2: " << HEXBYTE << NUM(op2) << '\n'; exit(1); } break; case 0xf3: switch(op2 = next()) { // End Two-Byte Opcodes Starting With f3 case 0x0f: switch(op3 = next()) { // End Three-Byte Opcodes Starting With f3 0f default: cerr << "unrecognized third opcode after f3 0f: " << HEXBYTE << NUM(op3) << '\n'; exit(1); } break; default: cerr << "unrecognized second opcode after f3: " << HEXBYTE << NUM(op2) << '\n'; exit(1); } break; default: cerr << "unrecognized opcode: " << HEXBYTE << NUM(op) << '\n'; exit(1); } } inline uint8_t next() { return read_mem_u8(EIP++); } void dump_registers() { ostringstream out; out << "regs: "; for (int i = 0; i < NUM_INT_REGISTERS; ++i) { if (i > 0) out << " "; out << i << ": " << std::hex << std::setw(8) << std::setfill('_') << Reg[i].u; } out << " -- SF: " << SF << "; ZF: " << ZF << "; CF: " << CF << "; OF: " << OF; trace(Callstack_depth+1, "run") << out.str() << end(); } //: start tracking supported opcodes :(before "End Globals") map</*op*/string, string> Name; map</*op*/string, string> Name_0f; map</*op*/string, string> Name_f3; map</*op*/string, string> Name_f3_0f; :(before "End One-time Setup") init_op_names(); :(code) void init_op_names() { put(Name, "f4", "halt (hlt)"); // End Initialize Op Names } :(before "End Help Special-cases(key)") if (key == "opcodes") { cerr << "Opcodes currently supported by SubX:\n"; for (map<string, string>::iterator p = Name.begin(); p != Name.end(); ++p) cerr << " " << p->first << ": " << p->second << '\n'; for (map<string, string>::iterator p = Name_0f.begin(); p != Name_0f.end(); ++p) cerr << " 0f " << p->first << ": " << p->second << '\n'; for (map<string, string>::iterator p = Name_f3.begin(); p != Name_f3.end(); ++p) cerr << " f3 " << p->first << ": " << p->second << '\n'; for (map<string, string>::iterator p = Name_f3_0f.begin(); p != Name_f3_0f.end(); ++p) cerr << " f3 0f " << p->first << ": " << p->second << '\n'; cerr << "Run `bootstrap help instructions` for details on words like 'r32' and 'disp8'.\n" "For complete details on these instructions, consult the IA-32 manual (volume 2).\n" "There's various versions of it online, such as https://c9x.me/x86.\n" "The mnemonics in brackets will help you locate each instruction.\n"; return 0; } :(before "End Help Contents") cerr << " opcodes\n"; //: Helpers for managing trace depths //: //: We're going to use trace depths primarily to segment code running at //: different frames of the call stack. This will make it easy for the trace //: browser to collapse over entire calls. //: //: Errors will be at depth 0. //: Warnings will be at depth 1. //: SubX instructions will occupy depth 2 and up to Max_depth, organized by //: stack frames. Each instruction's internal details will be one level deeper //: than its 'main' depth. So 'call' instruction details will be at the same //: depth as the instructions of the function it calls. :(before "End Globals") extern const int Initial_callstack_depth = 2; int Callstack_depth = Initial_callstack_depth; :(before "End Reset") Callstack_depth = Initial_callstack_depth; :(before "End Includes") #include <iomanip> #define HEXBYTE std::hex << std::setw(2) << std::setfill('0') #define HEXWORD std::hex << std::setw(8) << std::setfill('0') // ugly that iostream doesn't print uint8_t as an integer #define NUM(X) static_cast<int>(X) #include <stdint.h>