vm_t *vm_new() { vm_t *vm; vm = malloc(sizeof(struct vm)); if (!vm) return NULL; memset(vm, '\0', sizeof(struct vm)); // Allocate 64k for code vm->code = malloc(0xFFFF); if (vm->code == NULL) { return NULL; } memset(vm->code, '\0', 0xFFFF); vm->code_size = 0; // Allocate a 64k heap GCState* gc = gc_init(0xFFFF * 2); vm->gc = gc; vm->function_names = strings_new(); vm->intern_pool = strings_new(); vm->ip = 0; vm->current_frame = 0; vm->current_function = NULL; memset(vm->functions, NO_FUNCTION, MAX_FUNCTIONS * sizeof(uint64_t)); opcode_init(vm); return vm; }
void select_processor(char *name) { struct px *found = NULL; if (state.cmd_line.processor == 1) { gpwarning(GPW_CMDLINE_PROC, NULL); } else { found = gp_find_processor(name); if (found) { if (state.processor == none) { state.processor = found->tag; state.processor_info = found; set_global(found->defined_as, 1, PERMANENT, gvt_constant); } else if (state.processor != found->tag ) { gpwarning(GPW_REDEFINING_PROC, NULL); gperror(GPE_EXTRA_PROC, NULL); } } else { if (state.pass) { gperror(GPE_UNKNOWN_PROC, NULL); } else { printf("Didn't find any processor named: %s\nHere are the supported processors:\n",name); gp_dump_processor_list(); exit(1); } } /* load the instruction sets if necessary */ if ((state.processor_chosen == 0) && (state.processor != no_processor)) { opcode_init(1); /* General directives */ /* seperate the directives from the opcodes */ state.stBuiltin = push_symbol_table(state.stBuiltin, 1); opcode_init(2); /* Processor-specific */ if (!_16bit_core && !_17cxx_core) { opcode_init(3); /* Special pseudo ops for 12 and 14 bit devices */ } state.processor_chosen = 1; } } }
/* Run yyparse and return a pointer to the first instruction if no errors occur, otherwise return NULL */ Instruction* parse() { opcode_init(); yyparse(); if (error_found) { free_instructions(first_instruction); return NULL; } else { return first_instruction; } }
int main( int argc, char *argv[] ) { extern char *optarg; extern int optind; int c; int usage = 0; int memory_dump = 0; int dos_newlines = 0; char *pc; int cmd_processor = 0; char *processor_name = NULL; state.i_memory = i_memory_create(); state.pass = 0; state.quiet = 0; #ifdef PARSE_DEBUG { extern int yydebug; yydebug = 1; /* enable parse debug */ } #endif #ifdef USE_GPASM_HEADER_PATH /* add the header path to the include paths list */ #ifndef __MSDOS__ include_paths[n_include_paths++] = GPASM_HEADER_PATH; #else include_paths[n_include_paths++] = "c:\\gputils\\header"; #endif #endif while ((c = GETOPT_FUNC) != EOF) { switch (c) { case '?': case 'h': usage = 1; break; case 'a': select_hexformat(optarg); state.cmd_line.hex_format = 1; break; case 'c': state.case_insensitive = 1; if (state.stDefines != NULL) { printf("Warning: The -c option must be called before the -d option.\n"); } break; case 'D': case 'd': if ((optarg != NULL) && (strlen(optarg) > 0)) { struct symbol *sym; char *lhs, *rhs; /* the Defines symbol table is not yet defined*/ if (state.stDefines == NULL) { state.stDefines = push_symbol_table(NULL, state.case_insensitive); } lhs = strdup(optarg); rhs = strchr(lhs, '='); if (rhs != NULL) { *rhs = '\0'; /* Terminate the left-hand side */ rhs++; /* right-hand side begins after the '=' */ } sym = get_symbol(state.stDefines, lhs); if (sym == NULL) sym = add_symbol(state.stDefines, lhs); if (rhs) annotate_symbol(sym, rhs); } break; case 'e': select_expand(optarg); state.cmd_line.macro_expand = 1; break; case 'I': if(n_include_paths < MAX_INCLUDE_PATHS) { include_paths[n_include_paths++] = optarg; } else { fprintf(stderr, "too many -I paths\n"); exit(1); } break; case 'l': dump_processor_list(); exit(0); break; case 'm': memory_dump = 1; break; case 'n': #ifndef __MSDOS__ dos_newlines = 1; #endif break; case 'o': strcpy(state.hexfilename, optarg); strcpy(state.basefilename, optarg); pc = strrchr(state.basefilename, '.'); if (pc) *pc = 0; break; case 'p': cmd_processor = 1; processor_name = optarg; break; case 'q': state.quiet = 1; break; case 'r': select_radix(optarg); state.cmd_line.radix = 1; break; case 'w': select_errorlevel(atoi(optarg)); state.cmd_line.error_level = 1; break; case 'v': fprintf(stderr, "%s\n", GPASM_VERSION_STRING); exit(0); } if (usage) break; } if (optind < argc) state.srcfilename = argv[optind]; else usage = 1; if (usage) { show_usage(); } if(state.basefilename[0] == '\0') { strcpy(state.basefilename, state.srcfilename); pc = strrchr(state.basefilename, '.'); if (pc) *pc = 0; } /* the Defines symbol table is not yet defined*/ if (state.stDefines == NULL) { state.stDefines = push_symbol_table(NULL, state.case_insensitive); } /* Builtins are always case insensitive */ state.stBuiltin = push_symbol_table(NULL, 1); state.stDirective = state.stBuiltin; state.stMacros = push_symbol_table(NULL, state.case_insensitive); state.stTop = state.stGlobal = push_symbol_table(NULL, state.case_insensitive); opcode_init(0); /* the tables are built, select the processor if -p was used */ if (cmd_processor) { select_processor(processor_name); state.cmd_line.processor = 1; } state.maxram = (MAX_RAM - 1); open_src(state.srcfilename, 0); state.pass = 1; yyparse(); open_src(state.srcfilename, 0); state.pass++; state.org = 0; state.cblock = 0; if (state.cmd_line.radix != 1) state.radix = 16; cod_init(); lst_init(); yyparse(); assert(state.pass == 2); pop_symbol_table(state.stBuiltin); if (check_writehex(state.i_memory, state.hex_format)) { gperror(GPE_IHEX,NULL); } else { int byte_words; if (state.device.core_size > 0xff) { byte_words = 0; } else { byte_words = 1; if (state.hex_format != inhx8m) { gpwarning(GPW_UNKNOWN,"Must use inhx8m format for EEPROM8"); state.hex_format = inhx8m; } } if (writehex(state.basefilename, state.i_memory, state.hex_format, state.num.errors, byte_words, dos_newlines)) { gperror(GPE_UNKNOWN,"Error generating hex file"); } } if(memory_dump) print_i_memory(state.i_memory); /* Maybe produce a symbol table */ if (state.lst.symboltable) { lst_throw(); /* Start symbol table on a fresh page */ lst_symbol_table(state.stGlobal); lst_defines_table(state.stDefines); } /* Maybe produce a memory map */ if (state.lst.memorymap) { lst_memory_map(state.i_memory); } /* Finish off the listing and symbol files*/ lst_close(); if (state.processor_info) cod_close_file(); free_files(); if (state.num.errors > 0) return 1; else return 0; }
int assemble(void) { char *pc; struct symbol_table *cmd_defines; /* store the command line defines to restore on second pass */ cmd_defines = state.stDefines; state.c_memory = state.i_memory = i_memory_create(); if(state.basefilename[0] == '\0') { strcpy(state.basefilename, state.srcfilename); pc = strrchr(state.basefilename, '.'); if (pc) *pc = 0; } /* Builtins are always case insensitive */ state.stBuiltin = push_symbol_table(NULL, 1); state.stDirective = state.stBuiltin; state.stMacros = push_symbol_table(NULL, state.case_insensitive); state.stTop = state.stGlobal = push_symbol_table(NULL, state.case_insensitive); state.stTopDefines = state.stDefines = push_symbol_table(cmd_defines, state.case_insensitive); opcode_init(0); /* the tables are built, select the processor if -p was used */ if (cmd_processor) { select_processor(processor_name); state.cmd_line.processor = 1; } state.pass = 1; open_src(state.srcfilename, 0); yyparse(); state.pass++; state.org = 0; state.cblock = 0; /* clean out defines for second pass */ state.stTopDefines = state.stDefines = push_symbol_table(cmd_defines, state.case_insensitive); if (state.cmd_line.radix != 1) state.radix = 16; state.obj.symbol_num = 0; state.obj.section_num = 0; state.obj.org_num = 0; state.found_config = 0; state.found_devid = 0; state.found_idlocs = 0; coff_init(); cod_init(); lst_init(); open_src(state.srcfilename, 0); yyparse(); assert(state.pass == 2); pop_symbol_table(state.stBuiltin); hex_init(); if(state.memory_dump) print_i_memory(state.i_memory, state.device.class == PROC_CLASS_PIC16E ? 1 : 0); /* Maybe produce a symbol table */ if (state.lst.symboltable) { lst_throw(); /* Start symbol table on a fresh page */ lst_symbol_table(state.stGlobal); lst_defines_table(state.stDefines); } /* Maybe produce a memory map */ if ((state.mode == absolute) && (state.lst.memorymap)) { lst_memory_map(state.i_memory); } /* Finish off the object, listing, and symbol files*/ coff_close_file(); lst_close(); if (state.processor_info) cod_close_file(); free_files(); if ((state.num.errors > 0) || (gp_num_errors > 0)) return EXIT_FAILURE; else return EXIT_SUCCESS; }