| 1 | /* |
| 2 | * (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com> |
| 3 | * All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <string.h> |
| 10 | #include <errno.h> |
| 11 | #include <stdint.h> |
| 12 | #include <sys/select.h> |
| 13 | #include <getopt.h> |
| 14 | |
| 15 | #define VERSION 1 |
| 16 | |
| 17 | #define STACKSIZE 1024 |
| 18 | #define HEAPSIZE 1024 |
| 19 | #define RETURNSTACKSIZE 1024 |
| 20 | |
| 21 | void |
| 22 | print_usage(char ** argv) |
| 23 | { |
| 24 | printf( "VVhitespace Interpreter v%d (www.subgeniuskitty.com)\n" |
| 25 | "Usage: %s -i <file>\n" |
| 26 | " -h Help (prints this message)\n" |
| 27 | " -i <file> Specify a VVhitespace source file to interpret.\n" |
| 28 | , VERSION, argv[0] |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | int |
| 33 | stdin_empty(void) |
| 34 | { |
| 35 | fd_set read_fds; |
| 36 | FD_ZERO(&read_fds); |
| 37 | FD_SET(STDIN_FILENO, &read_fds); |
| 38 | |
| 39 | struct timeval timeout; |
| 40 | timeout.tv_sec = 0; |
| 41 | timeout.tv_usec = 0; |
| 42 | |
| 43 | int retval = select(1, &read_fds, NULL, NULL, &timeout); |
| 44 | /* retval could be -1. Ignoring that for now. */ |
| 45 | if (retval > 0) return 0; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ws_die(size_t * pc, char * msg) |
| 51 | { |
| 52 | printf("\n"); |
| 53 | printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg); |
| 54 | fflush(stdout); |
| 55 | exit(EXIT_FAILURE); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | stack_push(int32_t ** sp, int32_t word) |
| 60 | { |
| 61 | *((*sp)++) = word; |
| 62 | } |
| 63 | |
| 64 | int32_t |
| 65 | stack_pop(int32_t ** sp) |
| 66 | { |
| 67 | return *(--(*sp)); |
| 68 | } |
| 69 | |
| 70 | int32_t |
| 71 | stack_peek(int32_t ** sp, size_t offset) |
| 72 | /* offset=0 peeks TOS, offset=1 peeks NOS, etc. */ |
| 73 | { |
| 74 | return *((*sp)-offset-1); |
| 75 | } |
| 76 | |
| 77 | uint8_t |
| 78 | next_code_byte(uint8_t * code, size_t * pc) |
| 79 | { |
| 80 | return code[(*pc)++]; |
| 81 | } |
| 82 | |
| 83 | uint16_t |
| 84 | parse_label(uint8_t * code, size_t * pc) |
| 85 | { |
| 86 | // TODO: Check for invalid label and die. |
| 87 | uint16_t label = 0; |
| 88 | uint8_t c; |
| 89 | while ((c = code[(*pc)++]) != '\n') { |
| 90 | label = label << 1; |
| 91 | if (c == ' ') label++; |
| 92 | } |
| 93 | return label; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | populate_labels(uint32_t * labels, uint8_t * code, size_t code_size) |
| 98 | { |
| 99 | // TODO |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp) |
| 104 | { |
| 105 | switch (next_code_byte(code,pc)) { |
| 106 | case ' ': |
| 107 | /* Push number onto TOS. */ |
| 108 | { |
| 109 | /* First, pick off the sign */ |
| 110 | int32_t sign = 0; |
| 111 | switch (next_code_byte(code,pc)) { |
| 112 | case ' ' : sign = 1; break; |
| 113 | case '\t': sign = -1; break; |
| 114 | default : ws_die(pc, "expected sign"); break; |
| 115 | } |
| 116 | |
| 117 | /* Now, construct the number and push to TOS. */ |
| 118 | /* I'm assuming the numbers are read MSb first. */ |
| 119 | int32_t temp, number = 0; |
| 120 | while ((temp = next_code_byte(code,pc)) != '\n') { |
| 121 | if (temp == '\v') ws_die(pc, "non-binary digit in number"); |
| 122 | number <<= 1; |
| 123 | if (temp == '\t') number++; |
| 124 | } |
| 125 | stack_push(sp, number*sign); |
| 126 | } |
| 127 | break; |
| 128 | case '\n': |
| 129 | /* Stack sub-command */ |
| 130 | { |
| 131 | switch (next_code_byte(code,pc)) { |
| 132 | /* Duplicate the TOS. */ |
| 133 | case ' ': |
| 134 | stack_push(sp, stack_peek(sp,0)); |
| 135 | break; |
| 136 | /* Swap TOS and NOS. */ |
| 137 | case '\t': |
| 138 | { |
| 139 | int32_t t1 = stack_pop(sp); |
| 140 | int32_t t2 = stack_pop(sp); |
| 141 | stack_push(sp, t1); |
| 142 | stack_push(sp, t2); |
| 143 | } |
| 144 | break; |
| 145 | /* Discard TOS. */ |
| 146 | case '\n': |
| 147 | stack_pop(sp); |
| 148 | break; |
| 149 | default: |
| 150 | ws_die(pc, "malformed stack IMP"); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | break; |
| 155 | default: ws_die(pc, "malformed stack IMP"); break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp) |
| 161 | { |
| 162 | int32_t temp; |
| 163 | switch (next_code_byte(code,pc)) { |
| 164 | case ' ': |
| 165 | { |
| 166 | switch (next_code_byte(code,pc)) { |
| 167 | case ' ': |
| 168 | /* Addition */ |
| 169 | stack_push(sp, stack_pop(sp)+stack_pop(sp)); |
| 170 | break; |
| 171 | case '\t': |
| 172 | /* Subtraction */ |
| 173 | temp = stack_pop(sp); |
| 174 | stack_push(sp, stack_pop(sp)-temp); |
| 175 | break; |
| 176 | case '\n': |
| 177 | /* Multiplication */ |
| 178 | stack_push(sp, stack_pop(sp)*stack_pop(sp)); |
| 179 | break; |
| 180 | default: |
| 181 | ws_die(pc, "malformed arithmetic IMP"); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | break; |
| 186 | case '\t': |
| 187 | { |
| 188 | switch (next_code_byte(code,pc)) { |
| 189 | case ' ': |
| 190 | /* Division */ |
| 191 | temp = stack_pop(sp); |
| 192 | stack_push(sp, stack_pop(sp)/temp); |
| 193 | break; |
| 194 | case '\t': |
| 195 | /* Modulo */ |
| 196 | temp = stack_pop(sp); |
| 197 | stack_push(sp, stack_pop(sp)%temp); |
| 198 | break; |
| 199 | default: ws_die(pc, "malformed arithmetic IMP"); break; |
| 200 | } |
| 201 | } |
| 202 | break; |
| 203 | default: ws_die(pc, "malformed arithmetic IMP"); break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * labels, |
| 209 | uint32_t ** rsp) |
| 210 | { |
| 211 | switch (next_code_byte(code,pc)) { |
| 212 | case '\n': |
| 213 | /* Technically another LF is required but we ignore it. */ |
| 214 | printf("\n"); |
| 215 | fflush(stdout); |
| 216 | exit(EXIT_SUCCESS); |
| 217 | case ' ': |
| 218 | { |
| 219 | switch (next_code_byte(code,pc)) { |
| 220 | case ' ': |
| 221 | /* Mark a location in the program. */ |
| 222 | // TODO: Verify this is a label, but do nothing else. |
| 223 | labels[parse_label(code, pc)] = *pc; |
| 224 | break; |
| 225 | case '\t': |
| 226 | /* Call a subroutine. */ |
| 227 | *((*rsp)++) = *pc; |
| 228 | *pc = labels[parse_label(code, pc)]; |
| 229 | break; |
| 230 | case '\n': |
| 231 | /* Jump unconditionally to a label. */ |
| 232 | *pc = labels[parse_label(code, pc)]; |
| 233 | break; |
| 234 | default: |
| 235 | ws_die(pc, "malformed flow control IMP"); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | case '\t': |
| 241 | { |
| 242 | switch (next_code_byte(code,pc)) { |
| 243 | case ' ': |
| 244 | /* Jump to a label if TOS == 0 */ |
| 245 | if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)]; |
| 246 | break; |
| 247 | case '\t': |
| 248 | /* Jump to a label if TOS < 0. */ |
| 249 | if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)]; |
| 250 | break; |
| 251 | case '\n': |
| 252 | /* Return from subroutine. */ |
| 253 | *pc = *(--(*rsp)); |
| 254 | break; |
| 255 | default: |
| 256 | ws_die(pc, "malformed flow control IMP"); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | break; |
| 261 | default: |
| 262 | ws_die(pc, "malformed flow control IMP"); |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp) |
| 269 | { |
| 270 | switch (next_code_byte(code,pc)) { |
| 271 | case ' ' : /* Store to heap */ *(*hp + *((*sp)-1)) = **sp; *sp -= 2; break; |
| 272 | case '\t': /* Retrieve from heap */ **sp = *(*hp + **sp); break; |
| 273 | default: ws_die(pc, "malformed heap IMP"); break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp) |
| 279 | { |
| 280 | switch (next_code_byte(code,pc)) { |
| 281 | case ' ': |
| 282 | /* Output */ |
| 283 | { |
| 284 | switch (next_code_byte(code,pc)) { |
| 285 | case ' ' : /* Output character from TOS */ printf("%c", stack_pop(sp)); break; |
| 286 | case '\t': /* Output number from TOS */ printf("%d", stack_pop(sp)); break; |
| 287 | default: ws_die(pc, "malformed output IMP"); break; |
| 288 | } |
| 289 | fflush(stdout); |
| 290 | } |
| 291 | break; |
| 292 | case '\t': |
| 293 | /* Input */ |
| 294 | { |
| 295 | while (stdin_empty()) continue; |
| 296 | char c = getchar(); |
| 297 | switch (next_code_byte(code,pc)) { |
| 298 | case '\t': /* Input digit */ c -= '0'; /* fallthrough */ |
| 299 | case ' ' : /* Input character */ *(*hp + *((*sp)--)) = c; break; |
| 300 | default: ws_die(pc, "malformed input IMP"); break; |
| 301 | } |
| 302 | } |
| 303 | break; |
| 304 | default: ws_die(pc, "malformed i/o IMP"); break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | int |
| 309 | main(int argc, char ** argv) |
| 310 | { |
| 311 | /* |
| 312 | * Process command line arguments |
| 313 | */ |
| 314 | int c; |
| 315 | FILE * input = NULL; |
| 316 | while ((c = getopt(argc,argv,"i:h")) != -1) { |
| 317 | switch (c) { |
| 318 | case 'i': |
| 319 | if ((input = fopen(optarg, "r")) == NULL) { |
| 320 | fprintf(stderr, "ERROR: %s: %s\n", optarg, strerror(errno)); |
| 321 | } |
| 322 | break; |
| 323 | case 'h': |
| 324 | print_usage(argv); |
| 325 | exit(EXIT_SUCCESS); |
| 326 | break; |
| 327 | default: |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | if (input == NULL) { |
| 332 | fprintf(stderr, "ERROR: Must specify a VVhitespace source file with -f flag.\n"); |
| 333 | print_usage(argv); |
| 334 | exit(EXIT_FAILURE); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Read just the VVhitespace source code into memory. |
| 339 | * We will use the array indices as addresses for the virtual PC when jumping to labels. |
| 340 | */ |
| 341 | size_t ws_code_size = 0; |
| 342 | uint8_t temp_byte; |
| 343 | while (fread(&temp_byte, 1, 1, input)) { |
| 344 | if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') { |
| 345 | ws_code_size++; |
| 346 | } |
| 347 | } |
| 348 | rewind(input); |
| 349 | uint8_t * ws_code_space = malloc(ws_code_size); |
| 350 | ws_code_size = 0; |
| 351 | while (fread(&temp_byte, 1, 1, input)) { |
| 352 | if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') { |
| 353 | ws_code_space[ws_code_size++] = temp_byte; |
| 354 | } |
| 355 | } |
| 356 | fclose(input); |
| 357 | |
| 358 | /* |
| 359 | * Setup a stack and heap. |
| 360 | * Assume a 32-bit word size. |
| 361 | */ |
| 362 | // TODO: Make everything 64-bit. |
| 363 | int32_t * hp = malloc(HEAPSIZE*4); |
| 364 | int32_t * sp = malloc(STACKSIZE*4); |
| 365 | |
| 366 | /* |
| 367 | * Setup the return stack and the label array. |
| 368 | */ |
| 369 | uint32_t * rsp = malloc(RETURNSTACKSIZE*4); |
| 370 | uint32_t labels[65536] = {0}; |
| 371 | populate_labels(labels, ws_code_space, ws_code_size); |
| 372 | |
| 373 | /* |
| 374 | * Main Loop |
| 375 | */ |
| 376 | |
| 377 | size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */ |
| 378 | while (1) { |
| 379 | if (pc >= ws_code_size) { |
| 380 | fprintf(stderr, "SIM_ERROR: PC Overrun\n Requested PC: %lu\n Max Address: %lu\n", |
| 381 | pc, ws_code_size-1); |
| 382 | exit(EXIT_FAILURE); |
| 383 | } |
| 384 | |
| 385 | /* Decode the IMPs */ |
| 386 | switch (ws_code_space[pc++]) { |
| 387 | case ' ': |
| 388 | /* Stack Manipulation */ |
| 389 | process_imp_stack(ws_code_space, &pc, &sp); |
| 390 | break; |
| 391 | case '\n': |
| 392 | /* Flow Control */ |
| 393 | process_imp_flowcontrol(ws_code_space, &pc, &sp, labels, &rsp); |
| 394 | break; |
| 395 | case '\t': |
| 396 | /* Arithmetic, Heap Access, or I/O */ |
| 397 | { |
| 398 | switch (ws_code_space[pc++]) { |
| 399 | case ' ': |
| 400 | /* Arithmetic */ |
| 401 | process_imp_arithmetic(ws_code_space, &pc, &sp); |
| 402 | break; |
| 403 | case '\t': |
| 404 | /* Heap Access */ |
| 405 | process_imp_heap(ws_code_space, &pc, &sp, &hp); |
| 406 | break; |
| 407 | case '\n': |
| 408 | /* I/O */ |
| 409 | process_imp_io(ws_code_space, &pc, &sp, &hp); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | break; |
| 414 | default: ws_die(pc, "unexpected VTab"); break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | printf("\n"); |
| 419 | printf("Program executed.\n"); |
| 420 | |
| 421 | exit(EXIT_SUCCESS); |
| 422 | } |