]> Shamusworld >> Repos - rmac/blob - riscasm.c
Harden RISC register parser and simplify expression evaluator.
[rmac] / riscasm.c
1 //
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
7 //
8
9 #include "riscasm.h"
10 #include "amode.h"
11 #include "direct.h"
12 #include "error.h"
13 #include "expr.h"
14 #include "mark.h"
15 #include "procln.h"
16 #include "rmac.h"
17 #include "sect.h"
18 #include "token.h"
19
20 #define DEF_MR                          // Declare keyword values
21 #include "risckw.h"                     // Incl. generated risc keywords
22
23 #define DEF_KW                          // Declare keyword values
24 #include "kwtab.h"                      // Incl. generated keyword tables & defs
25
26 #define MAXINTERNCC 26          // Maximum internal condition codes
27
28 // Useful macros
29 #define EVAL_REG_RETURN_IF_ERROR(x, y) \
30 x = EvaluateRegisterFromTokenStream(y); \
31 \
32 if (x == ERROR) \
33         return ERROR;
34
35 #define EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(x, y) \
36 x = EvaluateRegisterFromTokenStream(y); \
37 \
38 if ((x == ERROR) || (ErrorIfNotAtEOL() == ERROR)) \
39         return ERROR;
40
41 #define CHECK_EOL \
42 if (ErrorIfNotAtEOL() == ERROR) \
43         return ERROR;
44
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
51
52 static const char reg_err[] = "missing register R0...R31";
53
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",
58         "PL", "MI", "F"
59 };
60
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
65 };
66
67 // Opcode Specific Data
68 struct opcoderecord {
69         uint16_t state;         // Opcode Name (unused)
70         uint16_t type;          // Opcode Type
71         uint16_t param;         // Opcode Parameter
72 };
73
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 }
137 };
138
139 #define MALF_NUM                0
140 #define MALF_EXPR               1
141 #define MALF_LPAREN             2
142 #define MALF_RPAREN             3
143
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 '('";
148
149 static const char * malformErr[] = {
150         malform1, malform2, malform3, malform4
151 };
152
153 //
154 // Function to return "malformed expression" error
155 // This is done mainly to remove a bunch of GOTO statements in the parser
156 //
157 static inline int MalformedOpcode(int signal)
158 {
159         return error("Malformed opcode, %s", malformErr[signal]);
160 }
161
162 //
163 // Function to return "Illegal Indexed Register" error
164 // Anyone trying to index something other than R14 or R15
165 //
166 static inline int IllegalIndexedRegister(int reg)
167 {
168         return error("Attempted index reference with non-indexable register (r%d)", reg - KW_R0);
169 }
170
171 //
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
174 //
175 static inline int IllegalIndexedRegisterEqur(SYM * sy)
176 {
177         return error("Attempted index reference with non-indexable register within EQUR (%s = r%d)", sy->sname, sy->svalue);
178 }
179
180 //
181 // Build up & deposit RISC instruction word
182 //
183 static void DepositRISCInstructionWord(uint16_t opcode, int reg1, int reg2)
184 {
185         // Check for absolute address setting
186         if (!orgwarning && !orgactive)
187         {
188                 warn("RISC code generated with no origin defined");
189                 orgwarning = 1;
190         }
191
192         int value = ((opcode & 0x3F) << 10) + ((reg1 & 0x1F) << 5) + (reg2 & 0x1F);
193         D_word(value);
194 }
195
196 //
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.
199 //
200 static int EvaluateRegisterFromTokenStream(uint32_t fixup)
201 {
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         if (*tok >= KW_R0 && *tok <= KW_R31)
205         {
206                 int reg = *tok - KW_R0;
207                 tok++;
208                 return reg;
209         }
210
211         if (*tok != SYMBOL)
212         {
213                 // If at this point we don't have a symbol then it's garbage.  Punt.
214                 return error("Expected register number or EQUREG");
215         }
216
217         uint64_t eval;                          // Expression value
218         WORD eattr;                                     // Expression attributes
219         SYM * esym;                                     // External symbol involved in expr.
220         TOKEN r_expr[EXPRSIZE];         // Expression token list
221
222         // Evaluate what's in the global "tok" buffer
223         // N.B.: We should either get a fixup or a register name from EQUR
224         if (expr(r_expr, &eval, &eattr, &esym) != OK)
225                 return ERROR;
226
227         if (!(eattr & DEFINED))
228         {
229                 AddFixup(FU_WORD | fixup, sloc, r_expr);
230                 return 0;
231         }
232
233         // If we got a register in range (0-31), return it
234         if (eval <= 31)
235                 return (int)eval;
236
237         // Otherwise, it's out of range & we flag an error
238         return error(reg_err);
239 }
240
241 //
242 // Do RISC code generation
243 //
244 int GenerateRISCCode(int state)
245 {
246         int reg1;                                       // Register 1
247         int reg2;                                       // Register 2
248         int val = 0;                            // Constructed value
249         char scratch[80];
250         SYM * ccsym;
251         SYM * sy;
252         int i, commaFound;
253         TOKEN * t;
254         uint16_t attrflg;
255         int indexed;                            // Indexed register flag
256
257         uint64_t eval;                          // Expression value
258         uint16_t eattr;                         // Expression attributes
259         SYM * esym = NULL;                      // External symbol involved in expr.
260         TOKEN r_expr[EXPRSIZE];         // Expression token list
261
262         // Get opcode parameter and type
263         uint16_t parm = roptbl[state - 3000].param;
264         uint16_t type = roptbl[state - 3000].type;
265         riscImmTokenSeen = 0;           // Set to "token not seen yet"
266
267         // Detect whether the opcode parmeter passed determines that the opcode is
268         // specific to only one of the RISC processors and ensure it is legal in
269         // the current code section. If not then show error and return.
270         if (((parm & GPUONLY) && rdsp) || ((parm & DSPONLY) && rgpu))
271                 return error("Opcode is not valid in this code section");
272
273         // Process RISC opcode
274         switch (type)
275         {
276         // No operand instructions
277         // NOP (57)
278         case RI_NONE:
279                 DepositRISCInstructionWord(parm, 0, 0);
280                 break;
281
282         // Single operand instructions (Rd)
283         // ABS, MIRROR, NEG, NOT, PACK, RESMAC, SAT8, SAT16, SAT16S, SAT24, SAT32S,
284         // UNPACK
285         case RI_ONE:
286                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
287                 DepositRISCInstructionWord(parm, parm >> 6, reg2);
288                 break;
289
290         // Two operand instructions (Rs,Rd)
291         // ADD, ADDC, AND, CMP, DIV, IMACN, IMULT, IMULTN, MOVEFA, MOVETA, MULT,
292         // MMULT, MTOI, NORMI, OR, ROR, SH, SHA, SUB, SUBC, XOR
293         case RI_TWO:
294                 if (parm == 37)
295                         altbankok = 1;                      // MOVEFA
296
297                 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
298                 CHECK_COMMA;
299
300                 if (parm == 36)
301                         altbankok = 1;                      // MOVETA
302
303                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
304                 DepositRISCInstructionWord(parm, reg1, reg2);
305                 break;
306
307         // Numeric operand (n,Rd) where n = -16..+15
308         // CMPQ
309         case RI_NUM_15:
310
311         // Numeric operand (n,Rd) where n = 0..31
312         // BCLR, BSET, BTST, MOVEQ
313         case RI_NUM_31:
314
315         // Numeric operand (n,Rd) where n = 1..32
316         // ADDQ, ADDQMOD, ADDQT, SHARQ, SHLQ, SHRQ, SUBQ, SUBQMOD, SUBQT, ROLQ,
317         // RORQ
318         case RI_NUM_32:
319                 switch (type)
320                 {
321                 case RI_NUM_15:
322                         reg1 = -16; reg2 = 15; attrflg = FU_NUM15;
323                         break;
324                 default:
325                 case RI_NUM_31:
326                         reg1 =   0; reg2 = 31; attrflg = FU_NUM31;
327                         break;
328                 case RI_NUM_32:
329                         reg1 =   1; reg2 = 32; attrflg = FU_NUM32;
330                         break;
331                 }
332
333                 if (parm & SUB32)
334                         attrflg |= FU_SUB32;
335
336                 if (*tok != '#')
337                         return MalformedOpcode(MALF_NUM);
338
339                 tok++;
340                 riscImmTokenSeen = 1;
341
342                 if (expr(r_expr, &eval, &eattr, &esym) != OK)
343                         return MalformedOpcode(MALF_EXPR);
344
345                 if (!(eattr & DEFINED))
346                 {
347                         AddFixup((WORD)(FU_WORD | attrflg), sloc, r_expr);
348                         reg1 = 0;
349                 }
350                 else
351                 {
352                         if (esym && (esym->sattre & EQUATEDREG))
353                                 return error("equated register seen for immediate value");
354
355                         if (eattr & RISCREG)
356                                 return error("register seen for immediate value");
357
358                         if (((int)eval < reg1) || ((int)eval > reg2))
359                                 return error("constant out of range (%d to %d)", reg1, reg2);
360
361                         if (parm & SUB32)
362                                 reg1 = 32 - (int)eval;
363                         else if (type == RI_NUM_32)
364                                 reg1 = (reg1 == 32 ? 0 : (int)eval);
365                         else
366                                 reg1 = (int)eval;
367                 }
368
369                 CHECK_COMMA;
370                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
371                 DepositRISCInstructionWord(parm, reg1, reg2);
372                 break;
373
374         // Move Immediate--n,Rn--n in Second Word
375         case RI_MOVEI:
376                 if (*tok != '#')
377                         return MalformedOpcode(MALF_NUM);
378
379                 tok++;
380                 riscImmTokenSeen = 1;
381
382                 // Check for equated register after # and return error if so
383                 if (*tok == SYMBOL)
384                 {
385                         sy = lookup(string[tok[1]], LABEL, 0);
386
387                         if (sy && (sy->sattre & EQUATEDREG))
388                                 return error("equated register in 1st operand of MOVEI instruction");
389                 }
390
391                 if (expr(r_expr, &eval, &eattr, &esym) != OK)
392                         return MalformedOpcode(MALF_EXPR);
393
394                 if ((lastOpcode == RI_JUMP) || (lastOpcode == RI_JR))
395                 {
396                         if (legacy_flag)
397                         {
398                                 // User doesn't care, emit a NOP to fix
399                                 DepositRISCInstructionWord(57, 0, 0);
400                                 warn("MOVEI following JUMP, inserting NOP to fix your BROKEN CODE");
401                         }
402                         else
403                                 warn("MOVEI immediately follows JUMP");
404                 }
405
406                 if (!(eattr & DEFINED))
407                 {
408                         AddFixup(FU_LONG | FU_MOVEI, sloc + 2, r_expr);
409                         eval = 0;
410                 }
411                 else
412                 {
413                         if (eattr & TDB)
414                                 MarkRelocatable(cursect, sloc + 2, (eattr & TDB), (MLONG | MMOVEI), NULL);
415                 }
416
417                 CHECK_COMMA;
418                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
419
420                 DepositRISCInstructionWord(parm, 0, reg2);
421                 val = WORDSWAP32(eval);
422                 D_long(val);
423                 break;
424
425         // PC,Rd or Rs,Rd
426         case RI_MOVE:
427                 if (*tok == KW_PC)
428                 {
429                         parm = 51;
430                         reg1 = 0;
431                         tok++;
432                 }
433                 else
434                 {
435                         parm = 34;
436                         EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
437                 }
438
439                 CHECK_COMMA;
440                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
441                 DepositRISCInstructionWord(parm, reg1, reg2);
442                 break;
443
444         // (Rn),Rn = 41 / (R14/R15+n),Rn = 43/44 / (R14/R15+Rn),Rn = 58/59
445         case RI_LOAD:
446                 indexed = 0;
447                 parm = 41;
448
449                 if (*tok != '(')
450                         return MalformedOpcode(MALF_LPAREN);
451
452                 tok++;
453
454         if ((tok[1] == '+') || (tok[1] == '-'))
455                 {
456                         // Trying to make indexed call
457                         if ((*tok == KW_R14) || (*tok == KW_R15))
458                                 indexed = (*tok - KW_R0);
459                         else
460                                 return IllegalIndexedRegister(*tok);
461                 }
462
463                 if (*tok == SYMBOL)
464                 {
465                         sy = lookup(string[tok[1]], LABEL, 0);
466
467                         if (!sy)
468                         {
469                                 error(reg_err);
470                                 return ERROR;
471                         }
472
473                         if (sy->sattre & EQUATEDREG)
474                         {
475                                 if ((tok[2] == '+') || (tok[2] == '-'))
476                                 {
477                                         if ((sy->svalue & 0x1F) == 14 || (sy->svalue & 0x1F) == 15) {
478                                                 indexed = (sy->svalue & 0x1F);
479                                                 tok++;
480                                         }
481                                         else
482                                                 return IllegalIndexedRegisterEqur(sy);
483                                 }
484                         }
485                 }
486
487                 if (!indexed)
488                 {
489                         EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
490                 }
491                 else
492                 {
493                         reg1 = indexed;
494                         indexed = 0;
495                         tok++;
496
497                         if (*tok == '+')
498                         {
499                                 parm = (WORD)(reg1 - 14 + 58);
500                                 tok++;
501
502                                 if ((*tok >= KW_R0) && (*tok <= KW_R31))
503                                         indexed = 1;
504
505                                 if (*tok == SYMBOL)
506                                 {
507                                         sy = lookup(string[tok[1]], LABEL, 0);
508
509                                         if (!sy)
510                                         {
511                                                 error(reg_err);
512                                                 return ERROR;
513                                         }
514
515                                         if (sy->sattre & EQUATEDREG)
516                                                 indexed = 1;
517                                 }
518
519                                 if (indexed)
520                                 {
521                                         EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
522                                 }
523                                 else
524                                 {
525                                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
526                                                 return MalformedOpcode(MALF_EXPR);
527
528                                         if (!(eattr & DEFINED))
529                                                 return error("constant expected after '+'");
530
531                                         reg1 = (int)eval;
532
533                                         if (reg1 == 0)
534                                         {
535                                                 reg1 = 14 + (parm - 58);
536                                                 parm = 41;
537                                                 warn("NULL offset in LOAD ignored");
538                                         }
539                                         else
540                                         {
541                                                 if ((reg1 < 1) || (reg1 > 32))
542                                                         return error("constant in LOAD out of range (1-32)");
543
544                                                 if (reg1 == 32)
545                                                         reg1 = 0;
546
547                                                 parm = (WORD)(parm - 58 + 43);
548                                         }
549                                 }
550                         }
551                         else
552                         {
553                                 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
554                         }
555                 }
556
557                 if (*tok != ')')
558                         return MalformedOpcode(MALF_RPAREN);
559
560                 tok++;
561                 CHECK_COMMA;
562                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
563                 DepositRISCInstructionWord(parm, reg1, reg2);
564                 break;
565
566         // Rn,(Rn) = 47 / Rn,(R14/R15+n) = 49/50 / Rn,(R14/R15+Rn) = 60/61
567         case RI_STORE:
568                 parm = 47;
569                 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
570                 CHECK_COMMA;
571
572                 if (*tok != '(')
573                         return MalformedOpcode(MALF_LPAREN);
574
575                 tok++;
576                 indexed = 0;
577
578                 if (((*tok == KW_R14) || (*tok == KW_R15)) && (tok[1] != ')'))
579                         indexed = *tok - KW_R0;
580
581                 if (*tok == SYMBOL)
582                 {
583                         sy = lookup(string[tok[1]], LABEL, 0);
584
585                         if (!sy)
586                         {
587                                 error(reg_err);
588                                 return ERROR;
589                         }
590
591                         if (sy->sattre & EQUATEDREG)
592                         {
593                                 if (((sy->svalue & 0x1F) == 14 || (sy->svalue & 0x1F) == 15)
594                                         && (tok[2] != ')'))
595                                 {
596                                         indexed = (sy->svalue & 0x1F);
597                                         tok++;
598                                 }
599                         }
600                 }
601
602                 if (!indexed)
603                 {
604                         EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
605                 }
606                 else
607                 {
608                         reg2 = indexed;
609                         indexed = 0;
610                         tok++;
611
612                         if (*tok == '+')
613                         {
614                                 parm = (WORD)(reg2 - 14 + 60);
615                                 tok++;
616
617                                 if ((*tok >= KW_R0) && (*tok <= KW_R31))
618                                         indexed = 1;
619
620                                 if (*tok == SYMBOL)
621                                 {
622                                         sy = lookup(string[tok[1]], LABEL, 0);
623
624                                         if (!sy)
625                                         {
626                                                 error(reg_err);
627                                                 return ERROR;
628                                         }
629
630                                         if (sy->sattre & EQUATEDREG)
631                                                 indexed = 1;
632                                 }
633
634                                 if (indexed)
635                                 {
636                                         EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
637                                 }
638                                 else
639                                 {
640                                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
641                                                 return MalformedOpcode(MALF_EXPR);
642
643                                         if (!(eattr & DEFINED))
644                                         {
645                                                 AddFixup(FU_WORD | FU_REGTWO, sloc, r_expr);
646                                                 reg2 = 0;
647                                         }
648                                         else
649                                         {
650                                                 reg2 = (int)eval;
651
652                                                 if (reg2 == 0)
653                                                 {
654                                                         reg2 = 14 + (parm - 60);
655                                                         parm = 47;
656                                                         warn("NULL offset in STORE ignored");
657                                                 }
658                                                 else
659                                                 {
660                                                         if ((reg2 < 1) || (reg2 > 32))
661                                                                 return error("constant in STORE out of range (1-32)");
662
663                                                         if (reg2 == 32)
664                                                                 reg2 = 0;
665
666                                                         parm = (WORD)(parm - 60 + 49);
667                                                 }
668                                         }
669                                 }
670                         }
671                         else
672                         {
673                                 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
674                         }
675                 }
676
677                 if (*tok != ')')
678                         return MalformedOpcode(MALF_RPAREN);
679
680                 tok++;
681                 CHECK_EOL;
682                 DepositRISCInstructionWord(parm, reg2, reg1);
683                 break;
684
685         // LOADB/LOADP/LOADW (Rn),Rn
686         case RI_LOADN:
687                 if (*tok != '(')
688                         return MalformedOpcode(MALF_LPAREN);
689
690                 tok++;
691                 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
692
693                 if (*tok != ')')
694                         return MalformedOpcode(MALF_RPAREN);
695
696                 tok++;
697                 CHECK_COMMA;
698                 EVAL_REG_RETURN_IF_ERROR_OR_NO_EOL(reg2, FU_REGTWO);
699                 DepositRISCInstructionWord(parm, reg1, reg2);
700                 break;
701
702         // STOREB/STOREP/STOREW Rn,(Rn)
703         case RI_STOREN:
704                 EVAL_REG_RETURN_IF_ERROR(reg1, FU_REGONE);
705                 CHECK_COMMA;
706
707                 if (*tok != '(')
708                         return MalformedOpcode(MALF_LPAREN);
709
710                 tok++;
711                 EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
712
713                 if (*tok != ')')
714                         return MalformedOpcode(MALF_RPAREN);
715
716                 tok++;
717                 CHECK_EOL;
718                 DepositRISCInstructionWord(parm, reg2, reg1);
719                 break;
720
721         // Jump Relative - cc,n - n=-16..+15 words, reg2=cc
722         case RI_JR:
723
724         // Jump Absolute - cc,(Rs) - reg2=cc
725         case RI_JUMP:
726                 // Check to see if there is a comma in the token string. If not then
727                 // the JR or JUMP should default to 0, Jump Always
728                 commaFound = 0;
729
730                 for(t=tok; *t!=EOL; t++)
731                 {
732                         if (*t == ',')
733                         {
734                                 commaFound = 1;
735                                 break;
736                         }
737                 }
738
739                 if (commaFound)
740                 {
741                         if (*tok == CONST)
742                         {
743                                 // CC using a constant number (O_o)
744                                 PTR tp;
745                                 tp.tk = tok + 1;
746                                 val = *tp.i64++;
747                                 tok = tp.tk;
748                                 CHECK_COMMA;
749                         }
750                         else if (*tok == SYMBOL)
751                         {
752                                 val = 9999;
753                                 strcpy(scratch, string[tok[1]]);
754                                 strtoupper(scratch);
755
756                                 for(i=0; i<MAXINTERNCC; i++)
757                                 {
758                                         // Look for the condition code & break if found
759                                         if (strcmp(condname[i], scratch) == 0)
760                                         {
761                                                 val = condnumber[i];
762                                                 break;
763                                         }
764                                 }
765
766                                 // Standard CC was not found, look for an equated one
767                                 if (val == 9999)
768                                 {
769                                         ccsym = lookup(string[tok[1]], LABEL, 0);
770
771                                         if (ccsym && (ccsym->sattre & EQUATEDCC) && !(ccsym->sattre & UNDEF_CC))
772                                                 val = (int)ccsym->svalue;
773                                         else
774                                                 return error("unknown condition code");
775                                 }
776
777                                 tok += 2;
778                                 CHECK_COMMA;
779                         }
780                         else if (*tok == '(')
781                         {
782                                 // Set CC to "Jump Always"
783                                 val = 0;
784                         }
785                 }
786                 else
787                 {
788                         // Set CC to "Jump Always"
789                         val = 0;
790                 }
791
792                 if ((val < 0) || (val > 31))
793                         return error("condition constant out of range");
794
795                 // Store condition code
796                 reg1 = val;
797
798                 if (type == RI_JR)
799                 {
800                         // JR cc,n
801                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
802                                 return MalformedOpcode(MALF_EXPR);
803
804                         if (!(eattr & DEFINED))
805                         {
806                                 AddFixup(FU_WORD | FU_JR, sloc, r_expr);
807                                 reg2 = 0;
808                         }
809                         else
810                         {
811                                 reg2 = ((int)(eval - ((orgactive ? orgaddr : sloc) + 2))) / 2;
812
813                                 if ((reg2 < -16) || (reg2 > 15))
814                                         error("PC relative overflow in JR (outside of -16 to 15)");
815                         }
816                 }
817                 else
818                 {
819                         // JUMP cc, (Rn)
820                         if (*tok != '(')
821                                 return MalformedOpcode(MALF_LPAREN);
822
823                         tok++;
824                         EVAL_REG_RETURN_IF_ERROR(reg2, FU_REGTWO);
825
826                         if (*tok != ')')
827                                 return MalformedOpcode(MALF_RPAREN);
828
829                         tok++;
830                         CHECK_EOL;
831                 }
832
833                 DepositRISCInstructionWord(parm, reg2, reg1);
834                 break;
835
836         // We should never get here. If we do, somebody done fucked up. :-D
837         default:
838                 return error("Unknown RISC opcode type");
839         }
840
841         lastOpcode = type;
842         return 0;
843 }