summaryrefslogtreecommitdiff
path: root/tnslc/compile/isa_x86.tnsl
blob: 3fda4866df29fb681f456c3d9f993a0a8f33ddcf (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/##
	Copyright 2021-2022 Kyle Gunger

	This file is licensed under the CDDL 1.0 (the License)
	and may only be used in accordance with the License.
	You should have received a copy of the License with this
	software/source code. If you did not, a copy can be found
	at the following URL:

	https://opensource.org/licenses/CDDL-1.0

	THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
	WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
	EXPRESS OR IMPLIED
#/

/; construct_statement({}uint8 base, {}{}uint8 args) [{}uint8]
	/; loop (int i = 0; i < len args) [i++]
		;add_strings(~base, ~(args{i}))
		/; if (i < len args - 1)
			;base.append(',')
			;base.append(' ')
		;/
	;/
	;base.append('\n')
	;return base
;/

/; literal_num ({}uint8 num) [{}uint8]
	;{}uint8 out = "$"
	;add_strings(~out, ~num)
	;return out
;/

/; add_asm ({}uint8 from, to) [{}uint8]
	;return construct_statement("\tadd ", {from, to})
;/

/; sub_asm({}uint8 from, to) [{}uint8]
	;return construct_statement("\tsub ", {from, to})
;/

/; push_asm ({}uint8 reg) [{}uint8]
	;return construct_statement("\tpush ", {reg})
;/

/; pop_asm ({}uint8 reg) [{}uint8]
	;return construct_statement("\tpop ", {reg})
;/

/; cmp_asm ({}uint8 a, b) [{}uint8]
	;return construct_statement("\tcmp ", {a, b})
;/

/; mov_asm ({}uint8 a, b) [{}uint8]
	;return construct_statement("\tmov ", {a, b})
;/

/; jmp_asm ({}uint8 pos) [{}uint8]
	;return construct_statement("\tjmp ", {pos})
;/

/; call_asm ({}uint8 pos) [{}uint8]
	;return construct_statement("\tcall ", {pos})
;/

/; cjmp_asm ({}uint8 suffix, pos) [{}uint8]
	;{}uint8 p = "\tj"
	;add_strings(~p, ~suffix)
	;p.append(' ')
	;return construct_statement(p, {pos})
;/

/; mem_offset ({}uint8 pos, offset, scale) [{}uint8]
	;{}uint8 tmp = construct_statement("(", {pos, offset, scale})
	;tmp{len tmp - 1} = ')'
	;return tmp
;/

/; header_guard (~{}uint8 csec) [{}uint8]
	;{}uint8 out = "", tmp = ""
	;tmp = push_asm("%r8")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r9")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r10")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r11")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r12")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r13")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r14")
	;add_strings(~out, ~tmp)
	;tmp = push_asm("%r15")
	;add_strings(~out, ~tmp)
	;add_strings(csec, ~out)
;/

/; tail_guard (~{}uint8 csec) [{}uint8]
	;{}uint8 out = "", tmp = ""
	;tmp = pop_asm("%r15")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r14")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r13")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r12")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r11")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r10")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r9")
	;add_strings(~out, ~tmp)
	;tmp = pop_asm("%r8")
	;add_strings(~out, ~tmp)
	;add_strings(csec, ~out)
;/

/; is_common_reg ({}uint8 n) [bool]
	;return string_equate(n, "ax") || string_equate(n, "bx") || string_equate(n, "cx") || string_equate(n, "dx")
			|| string_equate(n, "sp")  || string_equate(n, "bp") ||  string_equate(n, "si")  || string_equate(n, "di")
			|| string_equate(n, "8") || string_equate(n, "9") || string_equate(n, "10") || string_equate(n, "11")
			|| string_equate(n, "12") || string_equate(n, "13") || string_equate(n, "14") || string_equate(n, "15")
;/

/# Accepted common names:
#    - ax
#    - bx
#    - cx
#    - dx
#    - si
#    - di
#    - bp
#    - sp
#    - 8-15
#/
/; get_reg (uint size, {}uint8 common) [{}uint8]
	;{}uint8 out = "%"

	/; if (common{0} !< 'a')

		/; if (size == 1)
			/; if(common{1} == 'x')
				;common{1} = 'l'
			;; else
				;common.append('l')
			;/
		;; else if (size == 4)
			;out.append('e')
		;; else if (size == 8)
			;out.append('r')
		;/

		;add_strings(~out, ~common)

	;; else

		;{}uint8 out = "r"
		;add_strings(~out, ~common)
		/; if (size == 1)
			;out.append('b')
		;; else if (size == 2)
			;out.append('w')
		;; else if (size == 4)
			;out.append('d')
		;/
		;return out
	;/

	;return out
;/

/; make_label ({}uint8 func_name, func_place, ~{}uint8 csec)
	;func_name.append("_")
	;add_strings(~func_name, ~func_place)
	;func_name.append(':')
	;func_name.append('\n')
	;add_strings(csec, ~func_name)
;/