git.haldean.org cvm / master
There's nothing like implementing malloc to find a bunch of bugs Will Brown 9 years ago
8 changed file(s) with 41 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
212212 return NIL;
213213 }
214214
215 int start_addr = 1;
215 int start_addr = NIL + 1;
216216 int offset;
217217
218218 while (start_addr + size < mem->size) {
234234 }
235235
236236 printf("Out of memory: could not find a memory block of size %d\n", size);
237 return (uint) -1;
237 return NIL;
238238 }
239239
240240 void machine_run_instruction(
280280 goto skip_pc_incr;
281281
282282 case ALLOC:
283 if (argument == -1) {
284 argument = RS_POP;
285 }
283286 RS_PUSH(find_free_memory_of_size(argument, machine->memory));
284287 break;
285288
1717 write_binary(parse_instructions(source), binout)
1818 else:
1919 tree = parse(preprocess(source))
20 if args.write_ast:
20 if args.print_ast:
2121 print_tree(tree)
2222 if not tree:
2323 return
2424
2525 instructions = link(*translate(tree))
26 if args.write_assembly:
26 if args.print_assembly:
2727 print_instructions(instructions)
2828
2929 with open(args.output, 'w') as binout:
1010 __repr__ = __str__
1111
1212 class cvmptr(object):
13 bytecount = 1
14
1315 def __init__(self, pointsto):
1416 self.pointsto = pointsto
1517
1010 for item in root:
1111 if item[0] == 'fun':
1212 func = translate_function(item, glob, funcs)
13 funcs[item[2][0][0]] = func
13 funcs[func.name] = func
1414 elif item[0] == 'declare':
1515 for var, init in item[2]:
1616 cvmtype, name = cvmtypes.declarator((item[1], var))
2525 return glob, funcs, init_code
2626
2727 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
3037 fcode, flocals = translate_compound(ftree[3], glob, funcs)
3138 if 'void' in ftree[1]:
3239 fret = None
3340 else:
3441 fret = cvmtypes.typefor(ftree[1])
42 if ptr:
43 fret = cvmtypes.cvmptr(fret)
3544
3645 func = function(fname, [], fargs, fret, flocals)
3746
4857 func.frame_size += cvmtype.bytecount
4958
5059 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
5660 for cvmtype, name in reversed(fargs):
5761 prepend.append(('lstore', local_addr_offsets[name]))
5862 fcode = prepend + fcode
9599 code.extend(translate_declaration(var, data, glob, funcs))
96100 for node in ftree[first_statement:]:
97101 code.extend(translate_statement(node, glob, funcs))
98
99102 return code, loc
100103
101104 def translate_declaration(var, data, glob, funcs):
1010 '-o', '--output', default='out.cvm', metavar='BYTECODE_FILE',
1111 help='output bytecode file')
1212 ap.add_argument(
13 '--write-assembly', action='store_true',
13 '--print-assembly', action='store_true',
1414 help='write out generated assembly instructions')
1515 ap.add_argument(
16 '--write-ast', action='store_true',
16 '--print-ast', action='store_true',
1717 help='write out AST')
1818 ap.add_argument('input_file', metavar='INPUT_FILE')
1919
2020 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)
2525
2626 main()
00 void putc(char c) {
1 c;
1 asm("LLOAD 0");
22 asm("PRINT");
33 return;
44 }
0 char *malloc(unsigned int size) {
1 asm("LLOAD 0");
2 asm("ALLOC -1");
3 asm("RETURN");
4 }
0 #include <cvmstdlib.c>
1 #include <cvmstdio.c>
2
3 int main() {
4 char* addr = malloc(10);
5 putc('0' + addr);
6 putc('\n');
7 }