There's nothing like implementing malloc to find a bunch of bugs
Will Brown
10 years ago
212 | 212 | return NIL; |
213 | 213 | } |
214 | 214 | |
215 | int start_addr = 1; | |
215 | int start_addr = NIL + 1; | |
216 | 216 | int offset; |
217 | 217 | |
218 | 218 | while (start_addr + size < mem->size) { |
234 | 234 | } |
235 | 235 | |
236 | 236 | printf("Out of memory: could not find a memory block of size %d\n", size); |
237 | return (uint) -1; | |
237 | return NIL; | |
238 | 238 | } |
239 | 239 | |
240 | 240 | void machine_run_instruction( |
280 | 280 | goto skip_pc_incr; |
281 | 281 | |
282 | 282 | case ALLOC: |
283 | if (argument == -1) { | |
284 | argument = RS_POP; | |
285 | } | |
283 | 286 | RS_PUSH(find_free_memory_of_size(argument, machine->memory)); |
284 | 287 | break; |
285 | 288 |
17 | 17 | write_binary(parse_instructions(source), binout) |
18 | 18 | else: |
19 | 19 | tree = parse(preprocess(source)) |
20 | if args.write_ast: | |
20 | if args.print_ast: | |
21 | 21 | print_tree(tree) |
22 | 22 | if not tree: |
23 | 23 | return |
24 | 24 | |
25 | 25 | instructions = link(*translate(tree)) |
26 | if args.write_assembly: | |
26 | if args.print_assembly: | |
27 | 27 | print_instructions(instructions) |
28 | 28 | |
29 | 29 | with open(args.output, 'w') as binout: |
10 | 10 | __repr__ = __str__ |
11 | 11 | |
12 | 12 | class cvmptr(object): |
13 | bytecount = 1 | |
14 | ||
13 | 15 | def __init__(self, pointsto): |
14 | 16 | self.pointsto = pointsto |
15 | 17 |
10 | 10 | for item in root: |
11 | 11 | if item[0] == 'fun': |
12 | 12 | func = translate_function(item, glob, funcs) |
13 | funcs[item[2][0][0]] = func | |
13 | funcs[func.name] = func | |
14 | 14 | elif item[0] == 'declare': |
15 | 15 | for var, init in item[2]: |
16 | 16 | cvmtype, name = cvmtypes.declarator((item[1], var)) |
25 | 25 | return glob, funcs, init_code |
26 | 26 | |
27 | 27 | def translate_function(ftree, glob, funcs): |
28 | fname = ftree[2][0][0] | |
29 | fargs = list(map(cvmtypes.declarator, ftree[2][1])) | |
28 | if ftree[2][0][0] == '*': | |
29 | ptr = True | |
30 | fname = ftree[2][1][0][0] | |
31 | fargs = list(map(cvmtypes.declarator, ftree[2][1][1])) | |
32 | else: | |
33 | ptr = False | |
34 | fname = ftree[2][0][0] | |
35 | fargs = list(map(cvmtypes.declarator, ftree[2][1])) | |
36 | ||
30 | 37 | fcode, flocals = translate_compound(ftree[3], glob, funcs) |
31 | 38 | if 'void' in ftree[1]: |
32 | 39 | fret = None |
33 | 40 | else: |
34 | 41 | fret = cvmtypes.typefor(ftree[1]) |
42 | if ptr: | |
43 | fret = cvmtypes.cvmptr(fret) | |
35 | 44 | |
36 | 45 | func = function(fname, [], fargs, fret, flocals) |
37 | 46 | |
48 | 57 | func.frame_size += cvmtype.bytecount |
49 | 58 | |
50 | 59 | prepend = [] |
51 | for name, init in map(lambda x: (x[0], x[1][1]), flocals.items()): | |
52 | if init: | |
53 | prepend += translate_statement(init, glob, funcs) | |
54 | prepend.append(('lstore', local_addr_offsets[name])) | |
55 | ||
56 | 60 | for cvmtype, name in reversed(fargs): |
57 | 61 | prepend.append(('lstore', local_addr_offsets[name])) |
58 | 62 | fcode = prepend + fcode |
95 | 99 | code.extend(translate_declaration(var, data, glob, funcs)) |
96 | 100 | for node in ftree[first_statement:]: |
97 | 101 | code.extend(translate_statement(node, glob, funcs)) |
98 | ||
99 | 102 | return code, loc |
100 | 103 | |
101 | 104 | def translate_declaration(var, data, glob, funcs): |
10 | 10 | '-o', '--output', default='out.cvm', metavar='BYTECODE_FILE', |
11 | 11 | help='output bytecode file') |
12 | 12 | ap.add_argument( |
13 | '--write-assembly', action='store_true', | |
13 | '--print-assembly', action='store_true', | |
14 | 14 | help='write out generated assembly instructions') |
15 | 15 | ap.add_argument( |
16 | '--write-ast', action='store_true', | |
16 | '--print-ast', action='store_true', | |
17 | 17 | help='write out AST') |
18 | 18 | ap.add_argument('input_file', metavar='INPUT_FILE') |
19 | 19 | |
20 | 20 | args = ap.parse_args() |
21 | try: | |
22 | run(args) | |
23 | except Exception as e: | |
24 | print(e) | |
21 | #try: | |
22 | run(args) | |
23 | #except Exception as e: | |
24 | # print(e) | |
25 | 25 | |
26 | 26 | main() |