summaryrefslogtreecommitdiff
path: root/tnslc/compile/isa_x86.tnsl
blob: e342c90e74e428acba9538a8e1dd00bfc1305a05 (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
/##
	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({}charp base, {}{}charp args) [{}charp]
	/; 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 ({}charp num) [{}charp]
	;{}charp out = "$"
	;add_strings(~out, ~num)
	;return out
;/

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

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

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

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

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

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

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

/; header_guard (~{}charp csec) [{}charp]
	;{}charp 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 (~{}charp csec) [{}charp]
	;{}charp 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)
;/

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

	/; if (string_equate(common, "ax") || string_equate(common, "bx") || string_equate(common, "cx") || string_equate(common, "dx"))

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

		;add_strings(~out, ~common)

	;; else if (string_equate(common, "sp") || string_equate(common, "bp") || string_equate(common, "si") || string_equate(common, "di"))
		
		/; if (size == 1)
			;common.append('l')
		;; else if (size == 4)
			;out.append('e')
		;; else if (size == 8)
			;out.append('r')
		;/

		;add_strings(~out, ~common)

	;; else

		;{}charp 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 ({}charp func_name, func_place, ~{}charp csec)
	;func_name.append("_")
	;add_strings(~func_name, ~func_place)
	;add_strings(csec, ~func_name)
	;csec`.append(':')
	;csec`.append('\n')
;/