1: \ asm.fs assembler file (for MIPS R3000)
2: \
3: \ Copyright (C) 1995-97 Martin Anton Ertl, Christian Pirker
4: \
5: \ This file is part of RAFTS.
6: \
7: \ RAFTS is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11: \
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16: \
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21: $20 constant asm-registers
22:
23: : asm-register ( n n "name" ... "name" -- )
24: ?do
25: i constant
26: loop ;
27:
28: $08 $00 asm-register @zero @at @v0 @v1 @a0 @a1 @a2 @a3
29: $10 $08 asm-register @t0 @t1 @t2 @t3 @t4 @t5 @t6 @t7
30: $18 $10 asm-register @s0 @s1 @s2 @s3 @s4 @s5 @s6 @s7
31: $20 $18 asm-register @t8 @t9 @k0 @k1 @gp @sp @s8 @ra
32:
33: $00 constant asm-init-code
34:
35: : asm-bitmask ( n -- code )
36: $1 swap lshift 1- ;
37:
38: : asm-bmask ( n n "name" ... "name" -- )
39: ?do
40: i asm-bitmask constant
41: loop ;
42:
43: $09 $01 asm-bmask asm-bm01 asm-bm02 asm-bm03 asm-bm04 asm-bm05 asm-bm06 asm-bm07 asm-bm08
44: $11 $09 asm-bmask asm-bm09 asm-bm0A asm-bm0B asm-bm0C asm-bm0D asm-bm0E asm-bm0F asm-bm10
45: $19 $11 asm-bmask asm-bm11 asm-bm12 asm-bm13 asm-bm14 asm-bm15 asm-bm16 asm-bm17 asm-bm18
46: $20 $19 asm-bmask asm-bm19 asm-bm1A asm-bm1B asm-bm1C asm-bm1D asm-bm1E asm-bm1F
47: $FFFFFFFF constant asm-bm20
48:
49: : asm-expand ( x -- x )
50: dup $0000ffff > if
51: $ffff0000 or
52: endif ;
53:
54: : asm-op ( n -- code )
55: asm-bm06 and $1a lshift ;
56:
57: : asm-rs ( n code -- code )
58: swap asm-bm05 and $15 lshift or ;
59:
60: : asm-rt ( n code -- code )
61: swap asm-bm05 and $10 lshift or ;
62:
63: : asm-imm ( n code -- code )
64: swap asm-bm10 and or ;
65: ' asm-imm alias asm-offset
66:
67: : asm-target ( n code -- code )
68: swap 2 rshift asm-bm1A and or ;
69:
70: : asm-rd ( n code -- code )
71: swap asm-bm05 and $b lshift or ;
72:
73: : asm-shamt ( n code -- code )
74: swap asm-bm05 and $6 lshift or ;
75: ' asm-shamt alias asm-sa
76:
77: : asm-funct ( n code -- code )
78: swap asm-bm06 and or ;
79:
80: \ ***** I-types
81: : asm-I-type ( code -- )
82: a, ;
83:
84: : (asm-I-rt,imm) ( rt imm addr -- )
85: @ asm-imm asm-rt a, ;
86:
87: : asm-I-rt,imm
88: create asm-I-type
89: does>
90: (asm-I-rt,imm) ;
91:
92: : (asm-I-rs,imm) ( rs imm addr -- )
93: @ swap 2 rshift swap asm-imm asm-rs a, ;
94:
95: : asm-I-rs,imm
96: create asm-I-type
97: does>
98: (asm-I-rs,imm) ;
99:
100: : (asm-I-rt,rs,imm) ( rt rs imm addr -- )
101: @ asm-imm asm-rs asm-rt a, ;
102:
103: : asm-I-rt,rs,imm
104: create asm-I-type
105: does>
106: (asm-I-rt,rs,imm) ;
107:
108: : (asm-I-rs,rt,imm) ( rs rt imm addr -- )
109: @ swap 2 rshift swap asm-imm asm-rt asm-rs a, ;
110:
111: : asm-I-rs,rt,imm
112: create asm-I-type
113: does>
114: (asm-I-rs,rt,imm) ;
115:
116: : (asm-I-rt,offset,rs) ( rt offset rs addr -- )
117: @ asm-rs asm-offset asm-rt a, ;
118:
119: : asm-I-rt,offset,rs
120: create asm-I-type
121: does>
122: (asm-I-rt,offset,rs) ;
123:
124: \ ***** regimm types
125: : asm-regimm-rs,imm ( funct -- )
126: $01 asm-op asm-rt asm-I-rs,imm ;
127:
128: \ ***** copz types 1
129: : (asm-I-imm,z) ( imm z addr -- )
130: @ swap asm-op or swap 2 rshift swap asm-imm a, ;
131:
132: : asm-I-imm,z
133: create asm-I-type
134: does>
135: (asm-I-imm,z) ;
136:
137: : asm-copz-imm ( code -- )
138: $10 asm-op or asm-I-imm,z ;
139:
140: : (asm-I-rt,offset,rs,z) ( rt offset rs z addr -- )
141: @ swap asm-op or asm-rs asm-offset asm-rt a, ;
142:
143: : asm-I-rt,offset,rs,z
144: create asm-I-type
145: does>
146: (asm-I-rt,offset,rs,z) ;
147:
148: : asm-copz-rt,offset,rs ( code -- )
149: asm-op asm-I-rt,offset,rs,z ;
150:
151: $00 constant asm-copz-MF
152: $02 constant asm-copz-CF
153: $04 constant asm-copz-MT
154: $06 constant asm-copz-CT
155: $08 constant asm-copz-BC
156: $10 constant asm-copz-C0
157:
158: $00 constant asm-copz-BCF
159: $01 constant asm-copz-BCT
160:
161: \ ***** J-types
162: : asm-J-type ( code -- )
163: a, ;
164:
165: : (asm-J-target) ( target addr -- )
166: @ asm-target a, ;
167:
168: : asm-J-target
169: create asm-J-type
170: does>
171: (asm-J-target) ;
172:
173: \ ***** R-types
174: : asm-R-type ( code -- )
175: a, ;
176:
177: : (asm-R-nothing) ( addr -- )
178: @ a, ;
179:
180: : asm-R-nothing
181: create asm-R-type
182: does>
183: (asm-R-nothing) ;
184:
185: : (asm-R-rd) ( rd addr -- )
186: @ asm-rd a, ;
187:
188: : asm-R-rd
189: create asm-R-type
190: does>
191: (asm-R-rd) ;
192:
193: : (asm-R-rs) ( rs addr -- )
194: @ asm-rs a, ;
195:
196: : asm-R-rs
197: create asm-R-type
198: does>
199: (asm-R-rs) ;
200:
201: : (asm-R-rd,rs) ( rd rs addr -- )
202: @ asm-rs asm-rd a, ;
203:
204: : asm-R-rd,rs
205: create asm-R-type
206: does>
207: (asm-R-rd,rs) ;
208:
209: : (asm-R-rs,rt) ( rs rt addr -- )
210: @ asm-rt asm-rs a, ;
211:
212: : asm-R-rs,rt
213: create asm-R-type
214: does>
215: (asm-R-rs,rt) ;
216:
217: : (asm-R-rd,rs,rt) ( rd rs rt addr -- )
218: @ asm-rt asm-rs asm-rd a, ;
219:
220: : asm-R-rd,rs,rt
221: create asm-R-type
222: does>
223: (asm-R-rd,rs,rt) ;
224:
225: : (asm-R-rd,rt,rs) ( rd rt rs addr -- )
226: @ asm-rs asm-rt asm-rd a, ;
227:
228: : asm-R-rd,rt,rs
229: create asm-R-type
230: does>
231: (asm-R-rd,rt,rs) ;
232:
233: : (asm-R-rd,rt,sa) ( rd rt sa addr -- )
234: @ asm-sa asm-rt asm-rd a, ;
235:
236: : asm-R-rd,rt,sa
237: create asm-R-type
238: does>
239: (asm-R-rd,rt,sa) ;
240:
241: \ ***** special types
242: : asm-special-nothing ( funct -- )
243: asm-init-code asm-funct asm-R-nothing ;
244:
245: : asm-special-rd ( funct -- )
246: asm-init-code asm-funct asm-R-rd ;
247:
248: : asm-special-rs ( funct -- )
249: asm-init-code asm-funct asm-R-rs ;
250:
251: : asm-special-rd,rs ( funct -- )
252: asm-init-code asm-funct asm-R-rd,rs ;
253:
254: : asm-special-rs,rt ( funct -- )
255: asm-init-code asm-funct asm-R-rs,rt ;
256:
257: : asm-special-rd,rs,rt ( funct -- )
258: asm-init-code asm-funct asm-R-rd,rs,rt ;
259:
260: : asm-special-rd,rt,rs ( funct -- )
261: asm-init-code asm-funct asm-R-rd,rt,rs ;
262:
263: : asm-special-rd,rt,sa ( funct -- )
264: asm-init-code asm-funct asm-R-rd,rt,sa ;
265:
266: \ ***** copz types 2
267: : asm-copz0 ( funct -- )
268: $10 $10 asm-op asm-rs asm-funct asm-R-nothing ;
269:
270: : (asm-R-rt,rd,z) ( rt rd z addr -- )
271: @ swap asm-op or asm-rd asm-rt a, ;
272:
273: : asm-R-rt,rd,z
274: create asm-R-type
275: does>
276: (asm-R-rt,rd,z) ;
277:
278: : asm-copz-rt,rd ( funct -- )
279: $10 asm-op or asm-R-rt,rd,z ;
280:
281: : nop, ( -- )
282: 0 a, ;
283:
284: $04 asm-op asm-I-rs,rt,imm beq,
285: $05 asm-op asm-I-rs,rt,imm bne,
286: $00 $06 asm-op asm-rt asm-I-rs,imm blez,
287: $00 $07 asm-op asm-rt asm-I-rs,imm bgtz,
288: $08 asm-op asm-I-rt,rs,imm addi,
289: $09 asm-op asm-I-rt,rs,imm addiu,
290: $0a asm-op asm-I-rt,rs,imm slti,
291: $0b asm-op asm-I-rt,rs,imm sltiu,
292: $0c asm-op asm-I-rt,rs,imm andi,
293: $0d asm-op asm-I-rt,rs,imm ori,
294: $0e asm-op asm-I-rt,rs,imm xori,
295: $0f asm-op asm-I-rt,imm lui,
296: $20 asm-op asm-I-rt,offset,rs lb,
297: $21 asm-op asm-I-rt,offset,rs lh,
298: $22 asm-op asm-I-rt,offset,rs lwl,
299: $23 asm-op asm-I-rt,offset,rs lw,
300: $24 asm-op asm-I-rt,offset,rs lbu,
301: $25 asm-op asm-I-rt,offset,rs lhu,
302: $26 asm-op asm-I-rt,offset,rs lwr,
303: $28 asm-op asm-I-rt,offset,rs sb,
304: $29 asm-op asm-I-rt,offset,rs sh,
305: $2a asm-op asm-I-rt,offset,rs swl,
306: $2b asm-op asm-I-rt,offset,rs sw,
307: $2e asm-op asm-I-rt,offset,rs swr,
308:
309: $02 asm-op asm-J-target j,
310: $03 asm-op asm-J-target jal,
311:
312: $00 asm-special-rd,rt,sa sll,
313: $02 asm-special-rd,rt,sa srl,
314: $03 asm-special-rd,rt,sa sra,
315: $04 asm-special-rd,rt,rs sllv,
316: $06 asm-special-rd,rt,rs srlv,
317: $07 asm-special-rd,rt,rs srav,
318: $08 asm-special-rs jr,
319: $09 asm-special-rd,rs jalr,
320: $0c asm-special-nothing syscall,
321: $0d asm-special-nothing break,
322: $10 asm-special-rd mfhi,
323: $11 asm-special-rs mthi,
324: $12 asm-special-rd mflo,
325: $13 asm-special-rs mtlo,
326: $18 asm-special-rs,rt mult,
327: $19 asm-special-rs,rt multu,
328: $1a asm-special-rs,rt div,
329: $1b asm-special-rs,rt divu,
330: $20 asm-special-rd,rs,rt add,
331: $21 asm-special-rd,rs,rt addu,
332: $22 asm-special-rd,rs,rt sub,
333: $23 asm-special-rd,rs,rt subu,
334: $24 asm-special-rd,rs,rt and,
335: $25 asm-special-rd,rs,rt or,
336: $26 asm-special-rd,rs,rt xor,
337: $27 asm-special-rd,rs,rt nor,
338: $2a asm-special-rd,rs,rt slt,
339: $2b asm-special-rd,rs,rt sltu,
340:
341: $00 asm-regimm-rs,imm bltz,
342: $01 asm-regimm-rs,imm bgez,
343: $10 asm-regimm-rs,imm bltzal,
344: $11 asm-regimm-rs,imm bgezal,
345:
346: $30 asm-copz-rt,offset,rs lwcz,
347: $38 asm-copz-rt,offset,rs swcz,
348: asm-copz-MF $00 asm-rs asm-copz-rt,rd mfcz,
349: asm-copz-CF $00 asm-rs asm-copz-rt,rd cfcz,
350: asm-copz-MT $00 asm-rs asm-copz-rt,rd mtcz,
351: asm-copz-CT $00 asm-rs asm-copz-rt,rd ctcz,
352: asm-copz-BC $00 asm-rs asm-copz-BCF swap asm-rt asm-copz-imm bczf,
353: asm-copz-BC $00 asm-rs asm-copz-BCT swap asm-rt asm-copz-imm bczt,
354: $01 asm-copz0 tlbr,
355: $02 asm-copz0 tlbwi,
356: $06 asm-copz0 tlbwr,
357: $08 asm-copz0 tlbl,
358:
359: : move, ( rd rs -- )
360: @zero addu, ;
361:
362: : abs, ( rd rs -- )
363: dup $0008 bgez,
364: 2dup move,
365: @zero swap subu, ;
366:
367: : neg, ( rd rs -- )
368: @zero swap subu, ;
369:
370: : negu, ( rd rs -- )
371: @zero swap subu, ;
372:
373: : not, ( rd rs -- )
374: @zero nor, ;
375:
376: : li, ( rd imm -- )
377: dup 0= if
378: drop dup @zero = if
379: drop nop, assert( false )
380: else
381: @zero move,
382: endif
383: else
384: dup $8000 u< if
385: @zero swap addiu,
386: else
387: dup $10000 u< if
388: @zero swap ori,
389: else
390: dup $ffff and 0= if
391: $10 rshift lui,
392: else
393: dup $ffff8000 and $ffff8000 = if
394: @zero swap addiu,
395: else
396: 2dup $10 rshift lui,
397: over swap ori,
398: endif
399: endif
400: endif
401: endif
402: endif ;
403:
404: : blt, ( rs rt imm -- ) \ <
405: >r @at rot rot slt,
406: @at @zero r> bne, ;
407:
408: : ble, ( rs rt imm -- ) \ <=
409: >r @at rot rot swap slt,
410: @at @zero r> beq, ;
411:
412: : bgt, ( rs rt imm -- ) \ >
413: >r @at rot rot swap slt,
414: @at @zero r> bne, ;
415:
416: : bge, ( rs rt imm -- ) \ >=
417: >r @at rot rot slt,
418: @at @zero r> beq, ;
419:
420: : bltu, ( rs rt imm -- ) \ < unsigned
421: >r @at rot rot sltu,
422: @at @zero r> bne, ;
423:
424: : bleu, ( rs rt imm -- ) \ <= unsigned
425: >r @at rot rot swap sltu,
426: @at @zero r> beq, ;
427:
428: : bgtu, ( rs rt imm -- ) \ > unsigned
429: >r @at rot rot swap sltu,
430: @at @zero r> bne, ;
431:
432: : bgeu, ( rs rt imm -- ) \ >= unsigned
433: >r @at rot rot sltu,
434: @at @zero r> beq, ;
435:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>