static int scala_init_state(Data* data) { int prev_mc = -1; for (int mp = 0; data; data = data->next, mp++) { if (data->v) { int mc = mp / 1000; while (prev_mc != mc) { if (prev_mc != -1) { dec_indent(); emit_line("}"); } prev_mc++; emit_line("def init%d(): Unit = {", prev_mc); inc_indent(); } emit_line("mem(%d) = %d", mp, data->v); } } if (prev_mc != -1) { dec_indent(); emit_line("}"); } return prev_mc + 1; }
static void scala_emit_func_epilogue(void) { dec_indent(); emit_line("}"); emit_line("pc += 1"); dec_indent(); emit_line("}"); dec_indent(); emit_line("}"); }
static void js_emit_func_epilogue(void) { dec_indent(); emit_line("}"); emit_line("pc++;"); dec_indent(); emit_line("}"); dec_indent(); emit_line("};"); }
static void lua_emit_func_epilogue(void) { dec_indent(); emit_line("end"); emit_line("pc = pc + 1"); dec_indent(); emit_line("end"); dec_indent(); emit_line("end"); }
void PeekStatementAST::generate(string& code, Type* return_type_ptr) const { code += indent_str() + "{\n"; // Start scope inc_indent(); g_sym_table.pushFrame(); Type* msg_type_ptr = m_type_ptr->lookupType(); // Add new local var to symbol table g_sym_table.newTempSym(new Var("in_msg", getLocation(), msg_type_ptr, "(*in_msg_ptr)", getPairs())); // Check the queue type m_queue_name_ptr->assertType("InPort"); // Declare the new "in_msg_ptr" variable code += indent_str() + "const " + msg_type_ptr->cIdent() + "* in_msg_ptr;\n"; // Declare message // code += indent_str() + "in_msg_ptr = static_cast<const "; code += indent_str() + "in_msg_ptr = dynamic_cast<const "; code += msg_type_ptr->cIdent() + "*>("; code += "(" + m_queue_name_ptr->getVar()->getCode() + ")"; code += "."; code += m_method; code += "());\n"; code += indent_str() + "assert(in_msg_ptr != NULL);\n"; // Check the cast result m_statementlist_ptr->generate(code, return_type_ptr); // The other statements dec_indent(); g_sym_table.popFrame(); code += indent_str() + "}\n"; // End scope }
static void js_emit_pc_change(int pc) { emit_line("break;"); emit_line(""); dec_indent(); emit_line("case %d:", pc); inc_indent(); }
void target_scala(Module* module) { emit_line("object Main {"); inc_indent(); for (int i = 0; i < 7; i++) { emit_line("var %s: Int = _", reg_names[i]); } emit_line("var mem: Array[Int] = new Array(1 << 24)"); int num_inits = scala_init_state(module->data); CHUNKED_FUNC_SIZE = 128; int num_funcs = emit_chunked_main_loop(module->text, scala_emit_func_prologue, scala_emit_func_epilogue, scala_emit_pc_change, scala_emit_inst); emit_line("def main(args: Array[String]): Unit = {"); inc_indent(); for (int i = 0; i < num_inits; i++) { emit_line("init%d()", i); } emit_line(""); emit_line("while (true) {"); inc_indent(); emit_line("(pc / %d | 0) match {", CHUNKED_FUNC_SIZE); inc_indent(); for (int i = 0; i < num_funcs; i++) { emit_line("case %d => func%d()", i, i); } dec_indent(); emit_line("}"); dec_indent(); emit_line("}"); dec_indent(); emit_line("}"); dec_indent(); emit_line("}"); }
static void scala_emit_func_prologue(int func_id) { emit_line(""); emit_line("private def func%d(): Unit = {", func_id); inc_indent(); emit_line("while (%d <= pc && pc < %d) {", func_id * CHUNKED_FUNC_SIZE, (func_id + 1) * CHUNKED_FUNC_SIZE); inc_indent(); emit_line("pc match {"); inc_indent(); emit_line("case -1 => () /* dummy */"); dec_indent(); inc_indent(); }
void target_js(Module* module) { init_state_js(module->data); emit_line("var running = true;"); int num_funcs = emit_chunked_main_loop(module->text, js_emit_func_prologue, js_emit_func_epilogue, js_emit_pc_change, js_emit_inst); emit_line(""); emit_line("while (running) {"); inc_indent(); emit_line("switch (pc / %d | 0) {", CHUNKED_FUNC_SIZE); for (int i = 0; i < num_funcs; i++) { emit_line("case %d:", i); emit_line(" func%d();", i); emit_line(" break;"); } emit_line("}"); dec_indent(); emit_line("}"); emit_line("};"); // For nodejs emit_line("if (typeof require != 'undefined') {"); emit_line(" var sys = require('sys');"); emit_line(" var input = null;"); emit_line(" var ip = 0;"); emit_line(" var getchar = function() {"); emit_line(" if (input === null)"); emit_line(" input = require('fs').readFileSync('/dev/stdin');"); emit_line(" return input[ip++] | 0;"); emit_line(" };"); emit_line(" var putchar = function(c) {"); emit_line(" sys.print(String.fromCharCode(c & 255));"); emit_line(" };"); emit_line(" main(getchar, putchar);"); emit_line("}"); }
void target_lua(Module* module) { init_state_lua(module->data); int num_funcs = emit_chunked_main_loop(module->text, lua_emit_func_prologue, lua_emit_func_epilogue, lua_emit_pc_change, lua_emit_inst); emit_line(""); emit_line("while true do"); inc_indent(); emit_line("if false then"); for (int i = 0; i < num_funcs; i++) { emit_line("elseif pc < %d then func%d();", (i + 1) * CHUNKED_FUNC_SIZE, i); } emit_line("end"); dec_indent(); emit_line("end"); }
void dump_tree() { uint16_t n_internal_nodes = bitshift_read(); //Internal node if (n_internal_nodes) { uint32_t relocation_bitmask; //Simply all of the ones with the same prefix for (int i = 0; i < n_internal_nodes; ++i) { uint32_t n_node_bytes; uint32_t cur_relocation_bitmask; n_node_bytes = read_byte(); //Only allowed 32 bytes if (n_node_bytes > 0x20u) err("Too many bytes, leading max 0x20, found 0x%02X\n", n_node_bytes); cur_relocation_bitmask = 1 << (n_node_bytes - 1); if (n_node_bytes >= 0x10) relocation_bitmask = read_relocation_bitmask(); else relocation_bitmask = bitshift_read(); //Relocations don't appear until the end printf_indented(""); for (uint32_t j = 0; j < n_node_bytes; ++j) { if ( cur_relocation_bitmask & relocation_bitmask) printf(".."); else printf("%02X", read_byte()); cur_relocation_bitmask >>= 1; } printf(":\n"); inc_indent(); dump_tree(); dec_indent(); } //Leaf node } else {
void CFGPrinterOutput::print_end(const char* tag) { dec_indent(); output()->indent(); output()->print_cr("end_%s", tag); }
static void scala_emit_pc_change(int pc) { emit_line(""); dec_indent(); emit_line("case %d => ", pc); inc_indent(); }
static void lua_emit_pc_change(int pc) { emit_line(""); dec_indent(); emit_line("elseif pc == %d then", pc); inc_indent(); }