2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // RISCA.C - GPU/DSP Assembler
4 // Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
20 #define DEF_MR // Declare keyword values
21 #include "risckw.h" // Incl. generated risc keywords
23 #define DEF_KW // Declare keyword values
24 #include "kwtab.h" // Incl. generated keyword tables & defs
26 #define MAXINTERNCC 26 // Maximum internal condition codes
29 #define EVAL_REG_RETURN_IF_ERROR(x, y) \
30 x = EvaluateRegisterFromTokenStream(y); \
35 #define EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(x, y) \
36 x = EvaluateRegisterFromTokenStream(y); \
38 if ((x == ERROR) || (ErrorIfNotAtEOL() == ERROR)) \
42 if (ErrorIfNotAtEOL() == ERROR) \
45 unsigned altbankok = 0; // Ok to use alternate register bank
46 unsigned orgactive = 0; // RISC/6502 org directive active
47 unsigned orgaddr = 0; // Org'd address
48 unsigned orgwarning = 0; // Has an ORG warning been issued
49 int lastOpcode = -1; // Last RISC opcode assembled
50 uint8_t riscImmTokenSeen; // The '#' (immediate) token was seen
52 static const char reg_err[] = "missing register R0...R31";
54 // Jaguar jump condition names
55 static const char condname[MAXINTERNCC][5] = {
56 "NZ", "Z", "NC", "NCNZ", "NCZ", "C", "CNZ", "CZ", "NN", "NNNZ", "NNZ",
57 "N", "N_NZ", "N_Z", "T", "A", "NE", "EQ", "CC", "HS", "HI", "CS", "LO",
61 // Jaguar jump condition numbers
62 static const char condnumber[] = {
63 1, 2, 4, 5, 6, 8, 9, 10, 20, 21, 22, 24, 25, 26,
64 0, 0, 1, 2, 4, 4, 5, 8, 8, 20, 24, 31
67 // Opcode Specific Data
69 uint16_t state; // Opcode Name (unused)
70 uint16_t type; // Opcode Type
71 uint16_t param; // Opcode Parameter
74 static const struct opcoderecord roptbl[] = {
75 { MR_ADD, RI_TWO, 0 },
76 { MR_ADDC, RI_TWO, 1 },
77 { MR_ADDQ, RI_NUM_32, 2 },
78 { MR_ADDQT, RI_NUM_32, 3 },
79 { MR_SUB, RI_TWO, 4 },
80 { MR_SUBC, RI_TWO, 5 },
81 { MR_SUBQ, RI_NUM_32, 6 },
82 { MR_SUBQT, RI_NUM_32, 7 },
83 { MR_NEG, RI_ONE, 8 },
84 { MR_AND, RI_TWO, 9 },
85 { MR_OR, RI_TWO, 10 },
86 { MR_XOR, RI_TWO, 11 },
87 { MR_NOT, RI_ONE, 12 },
88 { MR_BTST, RI_NUM_31, 13 },
89 { MR_BSET, RI_NUM_31, 14 },
90 { MR_BCLR, RI_NUM_31, 15 },
91 { MR_MULT, RI_TWO, 16 },
92 { MR_IMULT, RI_TWO, 17 },
93 { MR_IMULTN, RI_TWO, 18 },
94 { MR_RESMAC, RI_ONE, 19 },
95 { MR_IMACN, RI_TWO, 20 },
96 { MR_DIV, RI_TWO, 21 },
97 { MR_ABS, RI_ONE, 22 },
98 { MR_SH, RI_TWO, 23 },
99 { MR_SHLQ, RI_NUM_32, 24 + SUB32 },
100 { MR_SHRQ, RI_NUM_32, 25 },
101 { MR_SHA, RI_TWO, 26 },
102 { MR_SHARQ, RI_NUM_32, 27 },
103 { MR_ROR, RI_TWO, 28 },
104 { MR_RORQ, RI_NUM_32, 29 },
105 { MR_ROLQ, RI_NUM_32, 29 + SUB32 },
106 { MR_CMP, RI_TWO, 30 },
107 { MR_CMPQ, RI_NUM_15, 31 },
108 { MR_SAT8, RI_ONE, 32 + GPUONLY },
109 { MR_SUBQMOD, RI_NUM_32, 32 + DSPONLY },
110 { MR_SAT16, RI_ONE, 33 + GPUONLY },
111 { MR_SAT16S, RI_ONE, 33 + DSPONLY },
112 { MR_MOVEQ, RI_NUM_31, 35 },
113 { MR_MOVETA, RI_TWO, 36 },
114 { MR_MOVEFA, RI_TWO, 37 },
115 { MR_MOVEI, RI_MOVEI, 38 },
116 { MR_LOADB, RI_LOADN, 39 },
117 { MR_LOADW, RI_LOADN, 40 },
118 { MR_LOADP, RI_LOADN, 42 + GPUONLY },
119 { MR_SAT32S, RI_ONE, 42 + DSPONLY },
120 { MR_STOREB, RI_STOREN, 45 },
121 { MR_STOREW, RI_STOREN, 46 },
122 { MR_STOREP, RI_STOREN, 48 + GPUONLY },
123 { MR_MIRROR, RI_ONE, 48 + DSPONLY },
124 { MR_JUMP, RI_JUMP, 52 },
125 { MR_JR, RI_JR, 53 },
126 { MR_MMULT, RI_TWO, 54 },
127 { MR_MTOI, RI_TWO, 55 },
128 { MR_NORMI, RI_TWO, 56 },
129 { MR_NOP, RI_NONE, 57 },
130 { MR_SAT24, RI_ONE, 62 },
131 { MR_UNPACK, RI_ONE, 63 + GPUONLY | (0 << 6) },
132 { MR_PACK, RI_ONE, 63 + GPUONLY | (1 << 6) },
133 { MR_ADDQMOD, RI_NUM_32, 63 + DSPONLY },
134 { MR_MOVE, RI_MOVE, 0 },
135 { MR_LOAD, RI_LOAD, 0 },
136 { MR_STORE, RI_STORE, 0 }
141 #define MALF_LPAREN 2
142 #define MALF_RPAREN 3
144 static const char malform1[] = "missing '#'";
145 static const char malform2[] = "bad expression";
146 static const char malform3[] = "missing ')'";
147 static const char malform4[] = "missing '('";
149 static const char * malformErr[] = {
150 malform1, malform2, malform3, malform4
154 // Function to return "malformed expression" error
155 // This is done mainly to remove a bunch of GOTO statements in the parser
157 static inline int MalformedOpcode(int signal)
159 return error("Malformed opcode, %s", malformErr[signal]);
163 // Function to return "Illegal Indexed Register" error
164 // Anyone trying to index something other than R14 or R15
166 static inline int IllegalIndexedRegister(int reg)
168 return error("Attempted index reference with non-indexable register (r%d)", reg - KW_R0);
172 // Function to return "Illegal Indexed Register" error for EQUR scenarios
173 // Trying to use register value within EQUR that isn't 14 or 15
175 static inline int IllegalIndexedRegisterEqur(SYM * sy)
177 return error("Attempted index reference with non-indexable register within EQUR (%s = r%d)", sy->sname, sy->svalue);
181 // Build up & deposit RISC instruction word
183 static void DepositRISCInstructionWord(uint16_t opcode, int reg1, int reg2)
185 // Check for absolute address setting
186 if (!orgwarning && !orgactive)
188 warn("RISC code generated with no origin defined");
192 int value = ((opcode & 0x3F) << 10) + ((reg1 & 0x1F) << 5) + (reg2 & 0x1F);
197 // Evaluate the RISC register from the token stream. Passed in value is the
198 // FIXUP attribute to use if the expression comes back as undefined.
200 static int EvaluateRegisterFromTokenStream(uint32_t fixup)
202 // Firstly, check to see if it's a register token and return that. No
203 // need to invoke expr() for easy cases like this.
204 int reg = *tok & 255;
205 if (reg >= KW_R0 && reg <= KW_R31)
207 // Check register bank usage in the case of .equr register
208 // (encoded as bits 30 and 31 in *tok).
209 // (I still think that this is superfluous and should be taken out, see my note in token.c)
210 if (*tok & 0x80000000)
212 // Oops, with all the evaluation in token.c we now lost the equated symbol reference, so we
213 // don't know its name or anything else. Oh well...
214 if ((regbank == BANK_0) && (*tok & 0x40000000) && !altbankok)
215 return error("equated symbol cannot be used in register bank 0");
217 if ((regbank == BANK_1) && !(*tok & 0x40000000) && !altbankok)
218 return error("equated symbol cannot be used in register bank 1");
227 // If at this point we don't have a symbol then it's garbage. Punt.
228 return error("Expected register number or EQUREG");
231 uint64_t eval; // Expression value
232 WORD eattr; // Expression attributes
233 SYM * esym; // External symbol involved in expr.
234 TOKEN r_expr[EXPRSIZE]; // Expression token list
236 // Evaluate what's in the global "tok" buffer
237 // N.B.: We should either get a fixup or a register name from EQUR
238 if (expr(r_expr, &eval, &eattr, &esym) != OK)
241 if (!(eattr & DEFINED))
243 AddFixup(FU_WORD | fixup, sloc, r_expr);
247 // We shouldn't get here, that should not be legal
250 // If we got a register in range (0-31), return it
251 //if (eattr & RISCREG)
252 // return (int)eval - KW_R0;
254 //// Otherwise, it's out of range & we flag an error
255 //return error(reg_err);
259 // Do RISC code generation
261 int GenerateRISCCode(int state)
263 int reg1; // Register 1
264 int reg2; // Register 2
265 int val = 0; // Constructed value
272 int indexed; // Indexed register flag
274 uint64_t eval; // Expression value
275 uint16_t eattr; // Expression attributes
276 SYM * esym = NULL; // External symbol involved in expr.
277 TOKEN r_expr[EXPRSIZE]; // Expression token list
279 // Get opcode parameter and type
280 uint16_t parm = roptbl[state - 3000].param;
281 uint16_t type = roptbl[state - 3000].type;
282 riscImmTokenSeen = 0; // Set to "token not seen yet"
284 // Detect whether the opcode parmeter passed determines that the opcode is
285 // specific to only one of the RISC processors and ensure it is legal in
286 // the current code section. If not then show error and return.
287 if (((parm & GPUONLY) && rdsp) || ((parm & DSPONLY) && rgpu))
288 return error("Opcode is not valid in this code section");
290 // Process RISC opcode
293 // No operand instructions
296 DepositRISCInstructionWord(parm, 0, 0);
299 // Single operand instructions (Rd)
300 // ABS, MIRROR, NEG, NOT, PACK, RESMAC, SAT8, SAT16, SAT16S, SAT24, SAT32S,
303 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
304 DepositRISCInstructionWord(parm, parm >> 6, reg2);
307 // Two operand instructions (Rs,Rd)
308 // ADD, ADDC, AND, CMP, DIV, IMACN, IMULT, IMULTN, MOVEFA, MOVETA, MULT,
309 // MMULT, MTOI, NORMI, OR, ROR, SH, SHA, SUB, SUBC, XOR
312 altbankok = 1; // MOVEFA
314 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
318 altbankok = 1; // MOVETA
320 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
321 DepositRISCInstructionWord(parm, reg1, reg2);
324 // Numeric operand (n,Rd) where n = -16..+15
328 // Numeric operand (n,Rd) where n = 0..31
329 // BCLR, BSET, BTST, MOVEQ
332 // Numeric operand (n,Rd) where n = 1..32
333 // ADDQ, ADDQMOD, ADDQT, SHARQ, SHLQ, SHRQ, SUBQ, SUBQMOD, SUBQT, ROLQ,
339 reg1 = -16; reg2 = 15; attrflg = FU_NUM15;
343 reg1 = 0; reg2 = 31; attrflg = FU_NUM31;
346 reg1 = 1; reg2 = 32; attrflg = FU_NUM32;
354 return MalformedOpcode(MALF_NUM);
357 riscImmTokenSeen = 1;
359 if (expr(r_expr, &eval, &eattr, &esym) != OK)
360 return MalformedOpcode(MALF_EXPR);
362 if (!(eattr & DEFINED))
364 AddFixup((WORD)(FU_WORD | attrflg), sloc, r_expr);
369 if (esym && (esym->sattre & EQUATEDREG))
370 return error("equated register seen for immediate value");
373 return error("register seen for immediate value");
375 if (((int)eval < reg1) || ((int)eval > reg2))
376 return error("constant out of range (%d to %d)", reg1, reg2);
379 reg1 = 32 - (int)eval;
380 else if (type == RI_NUM_32)
381 reg1 = (reg1 == 32 ? 0 : (int)eval);
387 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
388 DepositRISCInstructionWord(parm, reg1, reg2);
391 // Move Immediate--n,Rn--n in Second Word
394 return MalformedOpcode(MALF_NUM);
397 riscImmTokenSeen = 1;
399 // Check for equated register after # and return error if so
402 sy = lookup(string[tok[1]], LABEL, 0);
404 if (sy && (sy->sattre & EQUATEDREG))
405 return error("equated register in 1st operand of MOVEI instruction");
408 if (expr(r_expr, &eval, &eattr, &esym) != OK)
409 return MalformedOpcode(MALF_EXPR);
411 if ((lastOpcode == RI_JUMP) || (lastOpcode == RI_JR))
415 // User doesn't care, emit a NOP to fix
416 DepositRISCInstructionWord(57, 0, 0);
417 warn("MOVEI following JUMP, inserting NOP to fix your BROKEN CODE");
420 warn("MOVEI immediately follows JUMP");
423 if (!(eattr & DEFINED))
425 AddFixup(FU_LONG | FU_MOVEI, sloc + 2, r_expr);
431 MarkRelocatable(cursect, sloc + 2, (eattr & TDB), (MLONG | MMOVEI), NULL);
435 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
437 DepositRISCInstructionWord(parm, 0, reg2);
438 val = WORDSWAP32(eval);
453 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
457 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
458 DepositRISCInstructionWord(parm, reg1, reg2);
461 // (Rn),Rn = 41 / (R14/R15+n),Rn = 43/44 / (R14/R15+Rn),Rn = 58/59
467 return MalformedOpcode(MALF_LPAREN);
471 if ((tok[1] == '+') || (tok[1] == '-'))
473 // Trying to make indexed call
474 // Note: no bank check for .equr symbol here, but the original code for .equr
475 // below also didn't check for banks. Even more reasons to throw away
477 if (((*tok & 0xff) == KW_R14) || ((*tok &0xff) == KW_R15))
478 indexed = ((*tok & 0xff) - KW_R0);
480 return IllegalIndexedRegister(*tok);
483 //if (*tok == SYMBOL)
485 // sy = lookup(string[tok[1]], LABEL, 0);
493 // if (sy->sattre & EQUATEDREG)
495 // if ((tok[2] == '+') || (tok[2] == '-'))
497 // if ((sy->svalue - KW_R0) == 14 || (sy->svalue - KW_R0) == 15) {
498 // indexed = (sy->svalue - KW_R0);
502 // return IllegalIndexedRegisterEqur(sy);
509 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
519 parm = (WORD)(reg1 - 14 + 58);
522 if (((*tok & 0xff) >= KW_R0) && ((*tok & 0xff) <= KW_R31))
527 sy = lookup(string[tok[1]], LABEL, 0);
535 if (sy->sattre & EQUATEDREG)
541 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
545 if (expr(r_expr, &eval, &eattr, &esym) != OK)
546 return MalformedOpcode(MALF_EXPR);
548 if (!(eattr & DEFINED))
549 return error("constant expected after '+'");
555 reg1 = 14 + (parm - 58);
557 warn("NULL offset in LOAD ignored");
561 if ((reg1 < 1) || (reg1 > 32))
562 return error("constant in LOAD out of range (1-32)");
567 parm = (WORD)(parm - 58 + 43);
573 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
578 return MalformedOpcode(MALF_RPAREN);
582 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
583 DepositRISCInstructionWord(parm, reg1, reg2);
586 // Rn,(Rn) = 47 / Rn,(R14/R15+n) = 49/50 / Rn,(R14/R15+Rn) = 60/61
589 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
593 return MalformedOpcode(MALF_LPAREN);
598 // Again, no bank checks here or in the original code.
599 // This check has more holes than a very hole-y thing.
600 if ((((*tok & 0xff) == KW_R14) || ((*tok & 0xff) == KW_R15)) && (tok[1] != ')'))
601 indexed = (*tok & 0xff) - KW_R0;
603 //if (*tok == SYMBOL)
605 // sy = lookup(string[tok[1]], LABEL, 0);
613 // if (sy->sattre & EQUATEDREG)
615 // if (((sy->svalue - KW_R0) == 14 || (sy->svalue - KW_R0) == 15)
616 // && (tok[2] != ')'))
618 // indexed = (sy->svalue - KW_R0);
626 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
636 parm = (WORD)(reg2 - 14 + 60);
639 if (((*tok & 0xff) >= KW_R0) && ((*tok & 0xff) <= KW_R31))
644 sy = lookup(string[tok[1]], LABEL, 0);
652 if (sy->sattre & EQUATEDREG)
658 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
662 if (expr(r_expr, &eval, &eattr, &esym) != OK)
663 return MalformedOpcode(MALF_EXPR);
665 if (!(eattr & DEFINED))
667 AddFixup(FU_WORD | FU_REGTWO, sloc, r_expr);
676 reg2 = 14 + (parm - 60);
678 warn("NULL offset in STORE ignored");
682 if ((reg2 < 1) || (reg2 > 32))
683 return error("constant in STORE out of range (1-32)");
688 parm = (WORD)(parm - 60 + 49);
695 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
700 return MalformedOpcode(MALF_RPAREN);
704 DepositRISCInstructionWord(parm, reg2, reg1);
707 // LOADB/LOADP/LOADW (Rn),Rn
710 return MalformedOpcode(MALF_LPAREN);
713 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
716 return MalformedOpcode(MALF_RPAREN);
720 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
721 DepositRISCInstructionWord(parm, reg1, reg2);
724 // STOREB/STOREP/STOREW Rn,(Rn)
726 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
730 return MalformedOpcode(MALF_LPAREN);
733 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
736 return MalformedOpcode(MALF_RPAREN);
740 DepositRISCInstructionWord(parm, reg2, reg1);
743 // Jump Relative - cc,n - n=-16..+15 words, reg2=cc
746 // Jump Absolute - cc,(Rs) - reg2=cc
748 // Check to see if there is a comma in the token string. If not then
749 // the JR or JUMP should default to 0, Jump Always
752 for(t=tok; *t!=EOL; t++)
765 // CC using a constant number (O_o)
772 else if (*tok == SYMBOL)
775 strcpy(scratch, string[tok[1]]);
778 for(i=0; i<MAXINTERNCC; i++)
780 // Look for the condition code & break if found
781 if (strcmp(condname[i], scratch) == 0)
788 // Standard CC was not found, look for an equated one
791 ccsym = lookup(string[tok[1]], LABEL, 0);
793 if (ccsym && (ccsym->sattre & EQUATEDCC) && !(ccsym->sattre & UNDEF_CC))
794 val = (int)ccsym->svalue;
796 return error("unknown condition code");
802 else if (*tok == '(')
804 // Set CC to "Jump Always"
810 // Set CC to "Jump Always"
814 if ((val < 0) || (val > 31))
815 return error("condition constant out of range");
817 // Store condition code
823 if (expr(r_expr, &eval, &eattr, &esym) != OK)
824 return MalformedOpcode(MALF_EXPR);
826 if (!(eattr & DEFINED))
828 AddFixup(FU_WORD | FU_JR, sloc, r_expr);
833 reg2 = ((int)(eval - ((orgactive ? orgaddr : sloc) + 2))) / 2;
835 if ((reg2 < -16) || (reg2 > 15))
836 error("PC relative overflow in JR (outside of -16 to 15)");
843 return MalformedOpcode(MALF_LPAREN);
846 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
849 return MalformedOpcode(MALF_RPAREN);
855 DepositRISCInstructionWord(parm, reg2, reg1);
858 // We should never get here. If we do, somebody done fucked up. :-D
860 return error("Unknown RISC opcode type");