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 228 229 |
#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 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 long) char vm_init[] = "@256\n" // starting address of stack "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 // 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 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 = STATIC + g_symbol_offset_bump; ++g_symbol_offset_bump; // bump global symbol offset } else { // offset was found in map, return symbol value/index *addr = STATIC + 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 push_const_boilerplate[] = "@%hu\n" // A = constant "D=A\n" // D = constant "@SP\n" "M=M+1\n" // RAM[SP]++ // inc SP "A=M\n" // A = RAM[SP] "M=D\n"; // RAM[SP] = constant char push_addr_boilerplate[] = "@%hu\n" // A = segment + index "D=M\n" // D = RAM[segment + index] "@SP\n" "M=M+1\n" // RAM[SP]++ // inc SP "A=M\n" // A = RAM[SP] "M=D\n"; // RAM[SP] = constant char push_indirect_boilerplate[] = "@%hu\n" // A = segment "D=A\n" // D = segment "@%hu\n" // A = index "A=A+D\n" // A = segment + index "D=M\n" // D = RAM[segment + index] "@SP\n" "M=M+1\n" // RAM[SP]++ "A=M\n" // A = RAM[SP] "M=D\n"; 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, push_const_boilerplate, arg2); } else { // TODO: move segment resolution to separate function if (!strcmp(vm_instr->arg1, "argument")) { fprintf(fp, push_indirect_boilerplate, ARG, arg2); } else if (!strcmp(vm_instr->arg1, "local")) { fprintf(fp, push_indirect_boilerplate, LCL, arg2); } else if (!strcmp(vm_instr->arg1, "static")) { if (!resolve_static_address(vm_instr, &addr)) { return false; } fprintf(fp, push_addr_boilerplate, addr); } else if (!strcmp(vm_instr->arg1, "this")) { fprintf(fp, push_indirect_boilerplate, THIS, arg2); } else if (!strcmp(vm_instr->arg1, "that")) { fprintf(fp, push_indirect_boilerplate, THAT, arg2); } else if (!strcmp(vm_instr->arg1, "pointer")) { addr = POINTER + vm_instr->arg2; fprintf(fp, push_addr_boilerplate, addr); } else if (!strcmp(vm_instr->arg1, "temp")) { addr = TEMP + vm_instr->arg2; fprintf(fp, push_addr_boilerplate, addr); } 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 pop_addr_boilerplate[] = "@%hu\n" // A = segment + index "D=M\n" // D = RAM[segment + index] "@SP\n" "AM=M-1\n" // RAM[SP]--, A = RAM[SP] "M=D\n"; // D = RAM[SP] char pop_indirect_boilerplate[] = "@%hu\n" // @segment "D=A\n" // D = segment "@%hu\n" // @index "A=A+D\n" // A = segment + index "D=M\n" // D = RAM[segment + index] "@SP\n" "AM=M-1\n" // RAM[SP]--; A = RAM[SP] "M=D\n"; // TODO: move segment resolution to separate function if (!strcmp(vm_instr->arg1, "argument")) { fprintf(fp, pop_indirect_boilerplate, ARG, arg2); } else if (!strcmp(vm_instr->arg1, "local")) { fprintf(fp, pop_indirect_boilerplate, LCL, arg2); } else if (!strcmp(vm_instr->arg1, "static")) { if (!resolve_static_address(vm_instr, &addr)) { return false; } fprintf(fp, pop_addr_boilerplate, addr); } else if (!strcmp(vm_instr->arg1, "this")) { fprintf(fp, pop_indirect_boilerplate, THIS, arg2); } else if (!strcmp(vm_instr->arg1, "that")) { fprintf(fp, pop_indirect_boilerplate, THAT, arg2); } else if (!strcmp(vm_instr->arg1, "pointer")) { addr = POINTER + vm_instr->arg2; fprintf(fp, pop_addr_boilerplate, addr); } else if (!strcmp(vm_instr->arg1, "temp")) { addr = TEMP + vm_instr->arg2; fprintf(fp, pop_addr_boilerplate, addr); } 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 |