summaryrefslogtreecommitdiff
path: root/tnslc/compile/codegen.tnsl
blob: f057f83e4ac66129b43a84cc7535b1429f685912 (plain)
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
/; _indent (int idt)
	/; loop (int i = 0; i < idt) [i++]
		_printf("  \0")
	;/
;/

/; generate (~utils.File fin, fout)
	# Parse files into AST
	parse.Node ast = parse.generate_ast(fin)
	ast.update_children()

	# parse.print_ast(~ast)

	# Create output buffer
	CompBuf buffer
	buffer.init()

	# Transform into a module tree
	Module mod
	mod.init(~ast, ~buffer)
	mod.update_children()

	# Compile code
	_gen_prims(~mod)
	mod.compile(~buffer)

	mod.print()

	# Write assembly to output file
	fout.create()
	buffer.write_to(fout)
	fout.close()
	
	# Free all structs
	mod.end()
	buffer.end()
	ast.end()
;/

/; _gen_prim(int size, ~uint8 id) [Struct]
	Var t
	Struct s
	s.size = size
	s.methods = NULL
	s.members.init(len t)
	s.name = utils.strcpy(id)
	s._up = NULL
	return s
;/

/; _gen_prims (~Module m)

	Struct s

	# One byte prims
	s = _gen_prim(1, "bool\0")
	m`.structs.push(~s)
	s = _gen_prim(1, "uint8\0")
	m`.structs.push(~s)
	s = _gen_prim(1, "int8\0")
	m`.structs.push(~s)

	# Two byte prims
	s = _gen_prim(2, "uint16\0")
	m`.structs.push(~s)
	s = _gen_prim(2, "int16\0")
	m`.structs.push(~s)

	# Four byte prims
	s = _gen_prim(4, "uint32\0")
	m`.structs.push(~s)
	s = _gen_prim(4, "int32\0")
	m`.structs.push(~s)
	s = _gen_prim(4, "float32\0")
	m`.structs.push(~s)

	# Eight byte prims
	s = _gen_prim(8, "uint64\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "int64\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "float64\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "uint\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "int\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "float\0")
	m`.structs.push(~s)
	s = _gen_prim(8, "void\0")
	m`.structs.push(~s)
;/