projects/07/src/codewriter.h
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 |
#ifndef _CODEWRITER_H #define _CODEWRITER_H // 'codewriter.h' roughly corresponds to the 'CodeWriter' module specified in // nand2tetris, with a few liberties taken. #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "parser.h" #include "util.h" #define _DEBUG // memory mapping: // 0-15 virtual registers R0-R15 // 16-255 static variables // 256-2047 stack #define SP (0) // points to word ahead of top of stack #define LCL (1) // points to local segment #define ARG (2) // points to argument segment #define POINTER (3) #define THIS (3) // #define THAT (4) // #define TEMP (5) // #define R13 (13) // R13-R15 are scratch space that #define R14 (14) // VM-generated assembly can use #define R15 (15) // for whatever. #define STATIC (16) // start of static variables segment (240 words, 16-255) char vm_init[] = "@256\n" // starting address of stack (nothing pushed yet) "D=A\n" // D = 256 "@SP\n" // A = <constant representing address of SP> "M=D\n" // <memory pointed to by SP> = 256 "\n"; // TODO: add initializers for argument, local, static, constant, this, that char *static_sym_name; // static segment index indexes into map, retrieves hack assembly symbol offset #define MAX_STATIC_SYMBOLS (240) // for whatever. uint16_t static_symbol_map[MAX_STATIC_SYMBOLS]; uint8_t g_symbol_offset_bump = 0; // holds current largest symbol offset, bumps void write_vm_init(FILE *fp) { //fprintf(fp, vm_init); fprintf(fp, "// TODO eventually output VM initialization assembly\n"); } static bool write_arithmetic(struct vm_instruction_t *vm_instr, FILE *fp) { // TODO: write assembly code for 'add' and 'neg' instructions, turn into // codegen (depend on if unary/binary operation) // Binary: load RAM[SP], D=M, RAM[SP]--, M=D<op>M (I _think_ this is good) // Unary: load RAM[SP], M=<op>M (I _think_ this is good) fprintf(fp, "ARITHMETIC INSTRUCTION: "); print_vm_instruction(vm_instr); return true; } static bool resolve_static_address(struct vm_instruction_t *vm_instr, uint16_t *addr) { uint16_t symbol_offset; if (vm_instr->arg2 >= MAX_STATIC_SYMBOLS) { err("error: arg2 too large, >= %u\n", MAX_STATIC_SYMBOLS); return false; } symbol_offset = static_symbol_map[vm_instr->arg2]; if (symbol_offset == 0xffff) { // new offset not in map (-1 special) if (g_symbol_offset_bump >= MAX_STATIC_SYMBOLS) { err("error: symbol offset grew too large (>= %u), " "too many static variables\n", MAX_STATIC_SYMBOLS); return false; } static_symbol_map[vm_instr->arg2] = g_symbol_offset_bump; *addr = g_symbol_offset_bump; ++g_symbol_offset_bump; // bump global symbol offset } else { // offset was found in map, return symbol value/index *addr = symbol_offset; } return true; } // returns addr static bool resolve_segment_offset(struct vm_instruction_t *vm_instr, uint16_t *addr) { // TODO implement // resolve base (address) of segment // check index is valid for that segment // add index vm_instr->arg2 to address // set to *addr return true; } // push 16-bit value from segment offset onto top of stack static bool write_push(struct vm_instruction_t *vm_instr, FILE *fp) { uint16_t addr, arg2 = vm_instr->arg2; // TODO: maybe add SP counter/check to catch overflows // TODO: if vm_instr->arg1 == "constant", push_constant, else push_segment char const_template[] = "@%hu\n" // A = constant "D=A\n%s"; // D = constant char addr_template[] = "@%hu\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char static_template[] = "@%s.%hu\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char indirect_template[] = "@%hu\n" // A = segment "D=A\n" // D = segment "@%hu\n" // A = index "A=A+D\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char push_boilerplate[] = "@SP\n" "M=M+1\n" // RAM[SP]++ // inc SP "A=M-1\n" // A = RAM[SP] - 1 // prev top "M=D\n"; // RAM[SP] = constant // TODO: move segment resolution to separate function if (!strcmp(vm_instr->arg1, "constant")) { // TODO: check size of constant (allowed to be > 32,767?) // TODO: look up in nand2tetris forums in case issue already noted fprintf(fp, const_template, arg2, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "argument")) { fprintf(fp, indirect_template, ARG, arg2, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "local")) { fprintf(fp, indirect_template, LCL, arg2, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "static")) { if (!resolve_static_address(vm_instr, &addr)) { return false; } fprintf(fp, static_template, static_sym_name, addr, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "this")) { fprintf(fp, indirect_template, THIS, arg2, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "that")) { fprintf(fp, indirect_template, THAT, arg2, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "pointer")) { addr = POINTER + vm_instr->arg2; fprintf(fp, addr_template, addr, push_boilerplate); } else if (!strcmp(vm_instr->arg1, "temp")) { addr = TEMP + vm_instr->arg2; fprintf(fp, addr_template, addr, push_boilerplate); } else { err("error: invalid segment name \"%s\"\n", vm_instr->arg1); return false; } return true; } // pop 16-bit value from top of stack into segment offset static bool write_pop(struct vm_instruction_t *vm_instr, FILE *fp) { // TODO: maybe add SP counter/check to catch overflows // can use R13-R15 for scratch space uint16_t addr, arg2 = vm_instr->arg2; char addr_template[] = "@%hu\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char static_template[] = "@%s.%hu\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char indirect_template[] = "@%hu\n" // @segment "D=A\n" // D = segment "@%hu\n" // @index "A=A+D\n" // A = segment + index "D=M\n%s"; // D = RAM[segment + index] char pop_boilerplate[] = "@SP\n" "AM=M-1\n" // RAM[SP]--, A = RAM[SP] "M=D\n"; // RAM[SP] = D // TODO: move segment resolution to separate function if (!strcmp(vm_instr->arg1, "argument")) { fprintf(fp, indirect_template, ARG, arg2, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "local")) { fprintf(fp, indirect_template, LCL, arg2, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "static")) { if (!resolve_static_address(vm_instr, &addr)) { return false; } fprintf(fp, static_template, static_sym_name, addr, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "this")) { fprintf(fp, indirect_template, THIS, arg2, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "that")) { fprintf(fp, indirect_template, THAT, arg2, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "pointer")) { addr = POINTER + vm_instr->arg2; fprintf(fp, addr_template, addr, pop_boilerplate); } else if (!strcmp(vm_instr->arg1, "temp")) { addr = TEMP + vm_instr->arg2; fprintf(fp, addr_template, addr, pop_boilerplate); } else { err("error: invalid segment name \"%s\"\n", vm_instr->arg1); return false; } return true; } bool write_instruction(struct vm_instruction_t *vm_instr, FILE *fp) { fprintf(fp, "\n// %lu: %s\n", file_line_no, vm_instr->line); if (vm_instr->cmd == C_ARITHMETIC) { write_arithmetic(vm_instr, fp); } else if (vm_instr->cmd == C_PUSH) { write_push(vm_instr, fp); } else if (vm_instr->cmd == C_POP) { write_pop(vm_instr, fp); } else { // TODO: eventually error if unrecognized instruction print_vm_instruction(vm_instr); } return true; } #endif // _CODEWRITER_H |