OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [AS64/] [source/] [dsd9.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2016  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// A64 - Assembler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
#include "DSD9.h"
28
 
29
static void process_shifti(int oc, int funct3, int funct7);
30
static void ProcessEOL(int opt);
31
extern void process_message();
32
static void mem_operand(int64_t *disp, int *regA, int *regB, int *Sc);
33
 
34
extern bool bGenListing;
35
extern int first_rodata;
36
extern int first_data;
37
extern int first_bss;
38
extern int htable[100000];
39
extern int htblcnt[100000];
40
extern int htblmax;
41
extern int pass;
42
__int8 bytebuf[80];     // size of a cache line
43
 
44
static int64_t ca;
45
static char *lptr;
46
DSD9_Section cs;
47
 
48
extern int use_gp;
49
 
50
#define OPT64     0
51
#define OPTX32    1
52
#define OPTLUI0   0
53
#define LB16    -31653LL
54
#define IMAX20  524287LL
55
#define IMIN20  -524288LL
56
 
57
// ----------------------------------------------------------------------------
58
// Return the register number or -1 if not a register.
59
// Parses pretty register names like SP or BP in addition to r1,r2,etc.
60
// ----------------------------------------------------------------------------
61
 
62
static int getRegisterX()
63
{
64
    int reg;
65
 
66
    while(isspace(*inptr)) inptr++;
67
    switch(*inptr) {
68
    case 'r': case 'R':
69
        if ((inptr[1]=='a' || inptr[1]=='A') && !isIdentChar(inptr[2])) {
70
            inptr += 2;
71
            NextToken();
72
            return 29;
73
        }
74
         if (isdigit(inptr[1])) {
75
             reg = inptr[1]-'0';
76
             if (isdigit(inptr[2])) {
77
                 reg = 10 * reg + (inptr[2]-'0');
78
                 if (isdigit(inptr[3])) {
79
                     reg = 10 * reg + (inptr[3]-'0');
80
                     if (isIdentChar(inptr[4]))
81
                         return -1;
82
                     inptr += 4;
83
                     NextToken();
84
                     return reg;
85
                 }
86
                 else if (isIdentChar(inptr[3]))
87
                     return -1;
88
                 else {
89
                     inptr += 3;
90
                     NextToken();
91
                     return reg;
92
                 }
93
             }
94
             else if (isIdentChar(inptr[2]))
95
                 return -1;
96
             else {
97
                 inptr += 2;
98
                 NextToken();
99
                 return reg;
100
             }
101
         }
102
         else return -1;
103
    case 'a': case 'A':
104
         if (isdigit(inptr[1])) {
105
             reg = inptr[1]-'0' + 18;
106
             if (isIdentChar(inptr[2]))
107
                 return -1;
108
             else {
109
                 inptr += 2;
110
                 NextToken();
111
                 return reg;
112
             }
113
         }
114
         else return -1;
115
    case 'b': case 'B':
116
        if ((inptr[1]=='P' || inptr[1]=='p') && !isIdentChar(inptr[2])) {
117
            inptr += 2;
118
            NextToken();
119
            return 59;
120
        }
121
        break;
122
    case 'f': case 'F':
123
        if ((inptr[1]=='P' || inptr[1]=='p') && !isIdentChar(inptr[2])) {
124
            inptr += 2;
125
            NextToken();
126
            return 59;
127
        }
128
        break;
129
    case 'g': case 'G':
130
        if ((inptr[1]=='P' || inptr[1]=='p') && !isIdentChar(inptr[2])) {
131
            inptr += 2;
132
            NextToken();
133
            return 57;
134
        }
135
        break;
136
    case 'p': case 'P':
137
        if ((inptr[1]=='C' || inptr[1]=='c') && !isIdentChar(inptr[2])) {
138
            inptr += 2;
139
            NextToken();
140
            return 63;
141
        }
142
        break;
143
    case 's': case 'S':
144
        if ((inptr[1]=='P' || inptr[1]=='p') && !isIdentChar(inptr[2])) {
145
            inptr += 2;
146
            NextToken();
147
            return 63;
148
        }
149
        break;
150
    case 't': case 'T':
151
         if (isdigit(inptr[1])) {
152
             reg = inptr[1]-'0' + 26;
153
             if (isIdentChar(inptr[2]))
154
                 return -1;
155
             else {
156
                 inptr += 2;
157
                 NextToken();
158
                 return reg;
159
             }
160
         }
161
        if ((inptr[1]=='P' || inptr[1]=='p') && !isIdentChar(inptr[2])) {
162
            inptr += 2;
163
            NextToken();
164
            return 56;
165
        }
166
        /*
167
        if ((inptr[1]=='R' || inptr[1]=='r') && !isIdentChar(inptr[2])) {
168
            inptr += 2;
169
            NextToken();
170
            return 24;
171
        }
172
        */
173
        break;
174
        // lr
175
    case 'l': case 'L':
176
        if ((inptr[1]=='R' || inptr[1]=='r') && !isIdentChar(inptr[2])) {
177
            inptr += 2;
178
            NextToken();
179
            return 55;
180
        }
181
        break;
182
        // xlr
183
    case 'x': case 'X':
184
        if ((inptr[1]=='L' || inptr[1]=='l') && (inptr[2]=='R' || inptr[2]=='r') &&
185
                        !isIdentChar(inptr[3])) {
186
            inptr += 3;
187
            NextToken();
188
            return 58;
189
        }
190
        break;
191
    default:
192
        return -1;
193
    }
194
    return -1;
195
}
196
 
197
static int isdelim(char ch)
198
{
199
    return ch==',' || ch=='[' || ch=='(' || ch==']' || ch==')' || ch=='.';
200
}
201
 
202
// ----------------------------------------------------------------------------
203
// Get the friendly name of a special purpose register.
204
// ----------------------------------------------------------------------------
205
 
206
static int DSD9_getSprRegister()
207
{
208
    int reg;
209
    int pr;
210
 
211
    while(isspace(*inptr)) inptr++;
212
    if (inptr[0]=='p' || inptr[0]=='P') {
213
         if (isdigit(inptr[1]) && isdigit(inptr[2])) {
214
              pr = ((inptr[1]-'0' * 10) + (inptr[2]-'0'));
215
              if (!isIdentChar(inptr[3])) {
216
                  inptr += 3;
217
                  NextToken();
218
                  return pr | 0x40;
219
              }
220
         }
221
         else if (isdigit(inptr[1])) {
222
              pr = (inptr[1]-'0');
223
              if (!isIdentChar(inptr[2])) {
224
                  inptr += 2;
225
                  NextToken();
226
                  return pr | 0x40;
227
              }
228
         }
229
     }
230
 
231
    while(isspace(*inptr)) inptr++;
232
    switch(*inptr) {
233
 
234
    case '0':
235
    case '1':
236
    case '2':
237
    case '3':
238
    case '4':
239
    case '5':
240
    case '6':
241
    case '7':
242
    case '8':
243
    case '9':
244
         NextToken();
245
         NextToken();
246
         return (int)ival.low & 0xFFF;
247
 
248
    // arg1
249
    case 'a': case 'A':
250
         if ((inptr[1]=='r' || inptr[1]=='R') &&
251
             (inptr[2]=='g' || inptr[2]=='G') &&
252
             (inptr[3]=='1' || inptr[3]=='1') &&
253
             !isIdentChar(inptr[4])) {
254
             inptr += 4;
255
             NextToken();
256
             return 58;
257
         }
258
         break;
259
    // bear
260
    case 'b': case 'B':
261
         if ((inptr[1]=='e' || inptr[1]=='E') &&
262
             (inptr[2]=='a' || inptr[2]=='A') &&
263
             (inptr[3]=='r' || inptr[3]=='R') &&
264
             !isIdentChar(inptr[4])) {
265
             inptr += 4;
266
             NextToken();
267
             return 11;
268
         }
269
         break;
270
    // cas clk cr0 cr3 cs CPL
271
    case 'c': case 'C':
272
         if ((inptr[1]=='a' || inptr[1]=='A') &&
273
             (inptr[2]=='s' || inptr[2]=='S') &&
274
             !isIdentChar(inptr[3])) {
275
             inptr += 3;
276
             NextToken();
277
             return 44;
278
         }
279
         if ((inptr[1]=='l' || inptr[1]=='L') &&
280
             (inptr[2]=='k' || inptr[2]=='K') &&
281
             !isIdentChar(inptr[3])) {
282
             inptr += 3;
283
             NextToken();
284
             return 0x06;
285
         }
286
         if ((inptr[1]=='r' || inptr[1]=='R') &&
287
             (inptr[2]=='0') &&
288
             !isIdentChar(inptr[3])) {
289
             inptr += 3;
290
             NextToken();
291
             return 0x00;
292
         }
293
         if ((inptr[1]=='r' || inptr[1]=='R') &&
294
             (inptr[2]=='3') &&
295
             !isIdentChar(inptr[3])) {
296
             inptr += 3;
297
             NextToken();
298
             return 0x03;
299
         }
300
        if ((inptr[1]=='s' || inptr[1]=='S') &&
301
            !isIdentChar(inptr[2])) {
302
            if (inptr[2]=='.') {
303
               if ((inptr[3]=='l' || inptr[3]=='L') &&
304
                   (inptr[4]=='m' || inptr[4]=='M') &&
305
                   (inptr[5]=='t' || inptr[5]=='T') &&
306
                   !isIdentChar(inptr[6])) {
307
                       inptr += 6;
308
                       NextToken();
309
                       return 0x2F;
310
               }
311
            }
312
            inptr += 2;
313
            NextToken();
314
            return 0x27;
315
        }
316
         if ((inptr[1]=='p' || inptr[1]=='P') &&
317
             (inptr[2]=='l' || inptr[2]=='L') &&
318
             !isIdentChar(inptr[3])) {
319
             inptr += 3;
320
             NextToken();
321
             return 42;
322
         }
323
         break;
324
 
325
    // dbad0 dbad1 dbctrl dpc dsp ds
326
    case 'd': case 'D':
327
         if ((inptr[1]=='b' || inptr[1]=='B') &&
328
             (inptr[2]=='a' || inptr[2]=='A') &&
329
             (inptr[3]=='d' || inptr[3]=='D') &&
330
             (inptr[4]=='0' || inptr[4]=='0') &&
331
             !isIdentChar(inptr[5])) {
332
             inptr += 5;
333
             NextToken();
334
             return 50;
335
         }
336
         if ((inptr[1]=='b' || inptr[1]=='B') &&
337
             (inptr[2]=='a' || inptr[2]=='A') &&
338
             (inptr[3]=='d' || inptr[3]=='D') &&
339
             (inptr[4]=='1' || inptr[4]=='1') &&
340
             !isIdentChar(inptr[5])) {
341
             inptr += 5;
342
             NextToken();
343
             return 51;
344
         }
345
         if ((inptr[1]=='b' || inptr[1]=='B') &&
346
             (inptr[2]=='a' || inptr[2]=='A') &&
347
             (inptr[3]=='d' || inptr[3]=='D') &&
348
             (inptr[4]=='2' || inptr[4]=='2') &&
349
             !isIdentChar(inptr[5])) {
350
             inptr += 5;
351
             NextToken();
352
             return 52;
353
         }
354
         if ((inptr[1]=='b' || inptr[1]=='B') &&
355
             (inptr[2]=='a' || inptr[2]=='A') &&
356
             (inptr[3]=='d' || inptr[3]=='D') &&
357
             (inptr[4]=='3' || inptr[4]=='3') &&
358
             !isIdentChar(inptr[5])) {
359
             inptr += 5;
360
             NextToken();
361
             return 53;
362
         }
363
         if ((inptr[1]=='b' || inptr[1]=='B') &&
364
             (inptr[2]=='c' || inptr[2]=='C') &&
365
             (inptr[3]=='t' || inptr[3]=='T') &&
366
             (inptr[4]=='r' || inptr[4]=='R') &&
367
             (inptr[5]=='l' || inptr[5]=='L') &&
368
             !isIdentChar(inptr[6])) {
369
             inptr += 6;
370
             NextToken();
371
             return 54;
372
         }
373
         if ((inptr[1]=='p' || inptr[1]=='P') &&
374
             (inptr[2]=='c' || inptr[2]=='C') &&
375
             !isIdentChar(inptr[3])) {
376
             inptr += 3;
377
             NextToken();
378
             return 7;
379
         }
380
         if (
381
             (inptr[1]=='b' || inptr[1]=='B') &&
382
             (inptr[2]=='p' || inptr[2]=='P') &&
383
             (inptr[3]=='c' || inptr[3]=='C') &&
384
             !isIdentChar(inptr[4])) {
385
             inptr += 4;
386
             NextToken();
387
             return 7;
388
         }
389
         if ((inptr[1]=='s' || inptr[1]=='S') &&
390
             (inptr[2]=='p' || inptr[2]=='P') &&
391
             !isIdentChar(inptr[3])) {
392
             inptr += 3;
393
             NextToken();
394
             return 16;
395
         }
396
        if ((inptr[1]=='s' || inptr[1]=='S') &&
397
            !isIdentChar(inptr[2])) {
398
            if (inptr[2]=='.') {
399
               if ((inptr[3]=='l' || inptr[3]=='L') &&
400
                   (inptr[4]=='m' || inptr[4]=='M') &&
401
                   (inptr[5]=='t' || inptr[5]=='T') &&
402
                   !isIdentChar(inptr[6])) {
403
                       inptr += 6;
404
                       NextToken();
405
                       return 0x29;
406
               }
407
            }
408
            inptr += 2;
409
            NextToken();
410
            return 0x21;
411
        }
412
         break;
413
 
414
    // ea epc esp es
415
    case 'e': case 'E':
416
         if ((inptr[1]=='a' || inptr[1]=='A') &&
417
             !isIdentChar(inptr[2])) {
418
             inptr += 2;
419
             NextToken();
420
             return 40;
421
         }
422
         if ((inptr[1]=='p' || inptr[1]=='P') &&
423
             (inptr[2]=='c' || inptr[2]=='C') &&
424
             !isIdentChar(inptr[3])) {
425
             inptr += 3;
426
             NextToken();
427
             return 9;
428
         }
429
         if ((inptr[1]=='s' || inptr[1]=='S') &&
430
             (inptr[2]=='p' || inptr[2]=='P') &&
431
             !isIdentChar(inptr[3])) {
432
             inptr += 3;
433
             NextToken();
434
             return 17;
435
         }
436
        if ((inptr[1]=='s' || inptr[1]=='S') &&
437
            !isIdentChar(inptr[2])) {
438
            if (inptr[2]=='.') {
439
               if ((inptr[3]=='l' || inptr[3]=='L') &&
440
                   (inptr[4]=='m' || inptr[4]=='M') &&
441
                   (inptr[5]=='t' || inptr[5]=='T') &&
442
                   !isIdentChar(inptr[6])) {
443
                       inptr += 6;
444
                       NextToken();
445
                       return 0x2A;
446
               }
447
            }
448
            inptr += 2;
449
            NextToken();
450
            return 0x22;
451
        }
452
         break;
453
 
454
    // fault_pc fs
455
    case 'f': case 'F':
456
         if ((inptr[1]=='a' || inptr[1]=='A') &&
457
             (inptr[2]=='u' || inptr[2]=='U') &&
458
             (inptr[3]=='l' || inptr[3]=='L') &&
459
             (inptr[4]=='t' || inptr[4]=='T') &&
460
             (inptr[5]=='_' || inptr[5]=='_') &&
461
             (inptr[6]=='p' || inptr[6]=='P') &&
462
             (inptr[7]=='c' || inptr[7]=='C') &&
463
             !isIdentChar(inptr[8])) {
464
             inptr += 8;
465
             NextToken();
466
             return 0x08;
467
         }
468
        if ((inptr[1]=='s' || inptr[1]=='S') &&
469
            !isIdentChar(inptr[2])) {
470
            if (inptr[2]=='.') {
471
               if ((inptr[3]=='l' || inptr[3]=='L') &&
472
                   (inptr[4]=='m' || inptr[4]=='M') &&
473
                   (inptr[5]=='t' || inptr[5]=='T') &&
474
                   !isIdentChar(inptr[6])) {
475
                       inptr += 6;
476
                       NextToken();
477
                       return 0x2B;
478
               }
479
            }
480
            inptr += 2;
481
            NextToken();
482
            return 0x23;
483
        }
484
         break;
485
 
486
    // gs GDT
487
    case 'g': case 'G':
488
        if ((inptr[1]=='s' || inptr[1]=='S') &&
489
            !isIdentChar(inptr[2])) {
490
            if (inptr[2]=='.') {
491
               if ((inptr[3]=='l' || inptr[3]=='L') &&
492
                   (inptr[4]=='m' || inptr[4]=='M') &&
493
                   (inptr[5]=='t' || inptr[5]=='T') &&
494
                   !isIdentChar(inptr[6])) {
495
                       inptr += 6;
496
                       NextToken();
497
                       return 0x2C;
498
               }
499
            }
500
            inptr += 2;
501
            NextToken();
502
            return 0x24;
503
        }
504
        if ((inptr[1]=='d' || inptr[1]=='D') &&
505
           (inptr[2]=='t' || inptr[2]=='T') &&
506
            !isIdentChar(inptr[3])) {
507
            inptr += 3;
508
            NextToken();
509
            return 41;
510
        }
511
        break;
512
 
513
    // history
514
    case 'h': case 'H':
515
         if ((inptr[1]=='i' || inptr[1]=='I') &&
516
             (inptr[2]=='s' || inptr[2]=='S') &&
517
             (inptr[3]=='t' || inptr[3]=='T') &&
518
             (inptr[4]=='o' || inptr[4]=='O') &&
519
             (inptr[5]=='r' || inptr[5]=='R') &&
520
             (inptr[6]=='y' || inptr[6]=='Y') &&
521
             !isIdentChar(inptr[7])) {
522
             inptr += 7;
523
             NextToken();
524
             return 0x0D;
525
         }
526
        if ((inptr[1]=='s' || inptr[1]=='S') &&
527
            !isIdentChar(inptr[2])) {
528
            if (inptr[2]=='.') {
529
               if ((inptr[3]=='l' || inptr[3]=='L') &&
530
                   (inptr[4]=='m' || inptr[4]=='M') &&
531
                   (inptr[5]=='t' || inptr[5]=='T') &&
532
                   !isIdentChar(inptr[6])) {
533
                       inptr += 6;
534
                       NextToken();
535
                       return 0x2D;
536
               }
537
            }
538
            inptr += 2;
539
            NextToken();
540
            return 0x25;
541
        }
542
         break;
543
 
544
    // ipc isp ivno
545
    case 'i': case 'I':
546
         if ((inptr[1]=='p' || inptr[1]=='P') &&
547
             (inptr[2]=='c' || inptr[2]=='C') &&
548
             !isIdentChar(inptr[3])) {
549
             inptr += 3;
550
             NextToken();
551
             return 8;
552
         }
553
         if ((inptr[1]=='s' || inptr[1]=='S') &&
554
             (inptr[2]=='p' || inptr[2]=='P') &&
555
             !isIdentChar(inptr[3])) {
556
             inptr += 3;
557
             NextToken();
558
             return 15;
559
         }
560
         if ((inptr[1]=='v' || inptr[1]=='V') &&
561
             (inptr[2]=='n' || inptr[2]=='N') &&
562
             (inptr[3]=='o' || inptr[3]=='O') &&
563
             !isIdentChar(inptr[4])) {
564
             inptr += 4;
565
             NextToken();
566
             return 0x0C;
567
         }
568
         break;
569
 
570
 
571
    // LC LDT
572
    case 'l': case 'L':
573
         if ((inptr[1]=='c' || inptr[1]=='C') &&
574
             !isIdentChar(inptr[2])) {
575
             inptr += 2;
576
             NextToken();
577
             return 0x33;
578
         }
579
         if ((inptr[1]=='d' || inptr[1]=='D') &&
580
            (inptr[2]=='t' || inptr[2]=='T') &&
581
             !isIdentChar(inptr[3])) {
582
             inptr += 3;
583
             NextToken();
584
             return 40;
585
         }
586
         break;
587
 
588
    // pregs
589
    case 'p': case 'P':
590
         if ((inptr[1]=='r' || inptr[1]=='R') &&
591
             (inptr[2]=='e' || inptr[2]=='E') &&
592
             (inptr[3]=='g' || inptr[3]=='G') &&
593
             (inptr[4]=='s' || inptr[4]=='S') &&
594
             !isIdentChar(inptr[5])) {
595
             inptr += 5;
596
             NextToken();
597
             return 52;
598
         }
599
         break;
600
 
601
    // rand
602
    case 'r': case 'R':
603
         if ((inptr[1]=='a' || inptr[1]=='A') &&
604
             (inptr[2]=='n' || inptr[2]=='N') &&
605
             (inptr[3]=='d' || inptr[3]=='D') &&
606
             !isIdentChar(inptr[4])) {
607
             inptr += 4;
608
             NextToken();
609
             return 0x12;
610
         }
611
         break;
612
    // ss_ll srand1 srand2 ss segsw segbase seglmt segacr
613
    case 's': case 'S':
614
         if ((inptr[1]=='s' || inptr[1]=='S') &&
615
             (inptr[2]=='_' || inptr[2]=='_') &&
616
             (inptr[3]=='l' || inptr[3]=='L') &&
617
             (inptr[4]=='l' || inptr[4]=='L') &&
618
             !isIdentChar(inptr[5])) {
619
             inptr += 5;
620
             NextToken();
621
             return 0x1A;
622
         }
623
         if ((inptr[1]=='r' || inptr[1]=='R') &&
624
             (inptr[2]=='a' || inptr[2]=='A') &&
625
             (inptr[3]=='n' || inptr[3]=='N') &&
626
             (inptr[4]=='d' || inptr[4]=='D') &&
627
             (inptr[5]=='1') &&
628
             !isIdentChar(inptr[6])) {
629
             inptr += 6;
630
             NextToken();
631
             return 0x10;
632
         }
633
         if ((inptr[1]=='r' || inptr[1]=='R') &&
634
             (inptr[2]=='a' || inptr[2]=='A') &&
635
             (inptr[3]=='n' || inptr[3]=='N') &&
636
             (inptr[4]=='d' || inptr[4]=='D') &&
637
             (inptr[5]=='2') &&
638
             !isIdentChar(inptr[6])) {
639
             inptr += 6;
640
             NextToken();
641
             return 0x11;
642
         }
643
         if ((inptr[1]=='p' || inptr[1]=='P') &&
644
             (inptr[2]=='r' || inptr[2]=='R') &&
645
             isdigit(inptr[3]) && isdigit(inptr[4]) &&
646
             !isIdentChar(inptr[5])) {
647
             inptr += 5;
648
             NextToken();
649
             return (inptr[3]-'0')*10 + (inptr[4]-'0');
650
         }
651
        if ((inptr[1]=='s' || inptr[1]=='S') &&
652
            !isIdentChar(inptr[2])) {
653
            if (inptr[2]=='.') {
654
               if ((inptr[3]=='l' || inptr[3]=='L') &&
655
                   (inptr[4]=='m' || inptr[4]=='M') &&
656
                   (inptr[5]=='t' || inptr[5]=='T') &&
657
                   !isIdentChar(inptr[6])) {
658
                       inptr += 6;
659
                       NextToken();
660
                       return 0x2E;
661
               }
662
            }
663
            inptr += 2;
664
            NextToken();
665
            return 0x26;
666
         }
667
         // segxxx
668
         if ((inptr[1]=='e' || inptr[1]=='E') &&
669
             (inptr[2]=='g' || inptr[2]=='G')) {
670
             // segsw
671
             if ((inptr[3]=='s' || inptr[3]=='S') &&
672
                  (inptr[4]=='w' || inptr[4]=='W') &&
673
                  !isIdentChar(inptr[5])) {
674
               inptr += 5;
675
               NextToken();
676
               return 43;
677
             }
678
             // segbase
679
             if ((inptr[3]=='b' || inptr[3]=='B') &&
680
                  (inptr[4]=='a' || inptr[4]=='A') &&
681
                  (inptr[5]=='s' || inptr[5]=='S') &&
682
                  (inptr[6]=='e' || inptr[6]=='E') &&
683
                  !isIdentChar(inptr[7])) {
684
               inptr += 7;
685
               NextToken();
686
               return 44;
687
             }
688
             // seglmt
689
             if ((inptr[3]=='l' || inptr[3]=='L') &&
690
                  (inptr[4]=='m' || inptr[4]=='M') &&
691
                  (inptr[5]=='t' || inptr[5]=='T') &&
692
                  !isIdentChar(inptr[6])) {
693
               inptr += 6;
694
               NextToken();
695
               return 45;
696
             }
697
             // segacr
698
             if ((inptr[3]=='a' || inptr[3]=='A') &&
699
                  (inptr[4]=='c' || inptr[4]=='C') &&
700
                  (inptr[5]=='r' || inptr[5]=='R') &&
701
                  !isIdentChar(inptr[6])) {
702
               inptr += 6;
703
               NextToken();
704
               return 47;
705
             }
706
         }
707
         break;
708
 
709
    // tag tick 
710
    case 't': case 'T':
711
         if ((inptr[1]=='i' || inptr[1]=='I') &&
712
             (inptr[2]=='c' || inptr[2]=='C') &&
713
             (inptr[3]=='k' || inptr[3]=='K') &&
714
             !isIdentChar(inptr[4])) {
715
             inptr += 4;
716
             NextToken();
717
             return 0x32;
718
         }
719
         if ((inptr[1]=='a' || inptr[1]=='A') &&
720
             (inptr[2]=='g' || inptr[2]=='G') &&
721
             !isIdentChar(inptr[3])) {
722
             inptr += 3;
723
             NextToken();
724
             return 41;
725
         }
726
         break;
727
 
728
    // vbr
729
    case 'v': case 'V':
730
         if ((inptr[1]=='b' || inptr[1]=='B') &&
731
             (inptr[2]=='r' || inptr[2]=='R') &&
732
             !isIdentChar(inptr[3])) {
733
             inptr += 3;
734
             NextToken();
735
             return 10;
736
         }
737
         break;
738
    case 'z': case 'Z':
739
        if ((inptr[1]=='s' || inptr[1]=='S') &&
740
            !isIdentChar(inptr[2])) {
741
            if (inptr[2]=='.') {
742
               if ((inptr[3]=='l' || inptr[3]=='L') &&
743
                   (inptr[4]=='m' || inptr[4]=='M') &&
744
                   (inptr[5]=='t' || inptr[5]=='T') &&
745
                   !isIdentChar(inptr[6])) {
746
                       inptr += 6;
747
                       NextToken();
748
                       return 0x28;
749
               }
750
            }
751
            inptr += 2;
752
            NextToken();
753
            return 0x20;
754
        }
755
        break;
756
    }
757
    return -1;
758
}
759
 
760
// ---------------------------------------------------------------------------
761
// Process the size specifier for a FP instruction.
762
// ---------------------------------------------------------------------------
763
static int GetFPSize()
764
{
765
        int sz;
766
 
767
    sz = 'q';
768
    if (*inptr=='.') {
769
        inptr++;
770
        if (strchr("sdtqSDTQ",*inptr)) {
771
            sz = tolower(*inptr);
772
            inptr++;
773
        }
774
        else
775
            printf("Illegal float size.\r\n");
776
    }
777
        switch(sz) {
778
        case 's':       sz = 0; break;
779
        case 'd':       sz = 2; break;
780
        case 't':       sz = 2; break;
781
        case 'q':       sz = 3; break;
782
        default:        sz = 3; break;
783
        }
784
        return (sz);
785
}
786
 
787
// ---------------------------------------------------------------------------
788
// ---------------------------------------------------------------------------
789
 
790
static void emit_insn(int64_t oc, int can_compress, int sz)
791
{
792
    int ndx;
793
        static __int8 bbndx = 0;
794
 
795
    if (pass==3 && can_compress && gCanCompress) {
796
       for (ndx = 0; ndx < htblmax; ndx++) {
797
         if ((int)oc == hTable[ndx].opcode) {
798
           hTable[ndx].count++;
799
           return;
800
         }
801
       }
802
       if (htblmax < 100000) {
803
          hTable[htblmax].opcode = (int)oc;
804
          hTable[htblmax].count = 1;
805
          htblmax++;
806
          return;
807
       }
808
       printf("Too many instructions.\r\n");
809
       return;
810
    }
811
    if (pass > 3) {
812
     if (can_compress && gCanCompress) {
813
       for (ndx = 0; ndx < htblmax; ndx++) {
814
         if ((int)oc == hTable[ndx].opcode) {
815
           emitCode((ndx << 6)|0x1F);
816
                   num_bytes += 2;
817
                   num_insns += 1;
818
           return;
819
         }
820
       }
821
     }
822
        }
823
        emitCode(oc&0xFF);
824
        if (sz>1)
825
                emitCode((oc >> 8) & 0xff);
826
        if (sz>2)
827
                emitCode((oc >> 16) & 0xff);
828
        if (sz>3)
829
                emitCode((oc >> 24) & 0xff);
830
        if (sz>4)
831
                emitCode((oc >> 32) & 0xff);
832
        if ((code_address & 15)==15)
833
                emitCode(0x00);
834
 
835
        //cs.Add(oc,sz,lptr);
836
        /*
837
         if (bbndx > 80) {
838
                 for (nn = 0; nn < bbndx >> 1; nn++)
839
                         emitCode(bytebuf[nn]);
840
                 bbndx &= 1;
841
                 memmove(&bytebuf[0], &bytebuf[bbndx>>1], (bbndx>>1) + 1);
842
                 memset(&bytebuf[bbndx>>1],0,sizeof(bytebuf-(bbndx>>1)));
843
         }
844
                if (bbndx & 1) {
845
                        switch(sz) {
846
                        case 1:
847
                                        bytebuf[bbndx>>2] |= ((oc & 0xF) << 4);
848
                                        bbndx += 1;
849
                                        bytebuf[bbndx>>2] = (oc >> 4);
850
                                        bbndx += 2;
851
                                        bytebuf[bbndx>>2] = (oc >> 12);
852
                                        bbndx += 2;
853
                                        break;
854
                        case 2:
855
                                        bytebuf[bbndx>>2] |= ((oc & 0xF) << 4);
856
                                        bbndx += 1;
857
                                        bytebuf[bbndx>>2] = (oc >> 4);
858
                                        bbndx += 2;
859
                                        bytebuf[bbndx>>2] = (oc >> 12);
860
                                        bbndx += 2;
861
                                        bytebuf[bbndx>>2] = (oc >> 20);
862
                                        bbndx += 2;
863
                                        bytebuf[bbndx>>2] = (oc >> 28);
864
                                        bbndx += 2;
865
                                        bytebuf[bbndx>>2] = (oc >> 36);
866
                                        bbndx += 1;
867
                                        break;
868
                        }
869
                }
870
                else {
871
                        switch(sz) {
872
                        case 1:
873
                                bytebuf[bbndx>>2] = (oc & 0x255);
874
                                bbndx += 2;
875
                                bytebuf[bbndx>>2] = (oc >> 8);
876
                                bbndx += 2;
877
                                bytebuf[bbndx>>2] = (oc >> 16) & 0xF;
878
                                bbndx += 1;
879
                                break;
880
                        case 2:
881
                                bytebuf[bbndx>>2] = (oc & 0x255);
882
                                bbndx += 2;
883
                                bytebuf[bbndx>>2] = (oc >> 8);
884
                                bbndx += 2;
885
                                bytebuf[bbndx>>2] = (oc >> 16);
886
                                bbndx += 2;
887
                                bytebuf[bbndx>>2] = (oc >> 24);
888
                                bbndx += 2;
889
                                bytebuf[bbndx>>2] = (oc >> 32);
890
                                bbndx += 2;
891
                                bytebuf[bbndx>>2] = (oc >> 40);
892
                                bbndx += 2;
893
                                break;
894
                        }
895
                }
896
                */
897
        num_bytes += sz;
898
    num_insns += 1;
899
}
900
 
901
// ---------------------------------------------------------------------------
902
// ---------------------------------------------------------------------------
903
 
904
static void emit40(Int128 cd)
905
{
906
        emitChar(cd.low & 65535LL);
907
        emitChar((cd.low >> 16) & 65535LL);
908
        emitByte(cd.high & 255LL);
909
}
910
 
911
// ---------------------------------------------------------------------------
912
// ---------------------------------------------------------------------------
913
 
914
static void emit80(Int128 cd)
915
{
916
        emitChar(cd.low & 65535LL);
917
        emitChar((cd.low >> 16) & 65535LL);
918
        emitChar((cd.low >> 32) & 65535LL);
919
        emitChar((cd.low >> 48) & 65535LL);
920
        emitChar(cd.high & 65535LL);
921
}
922
 
923
// ---------------------------------------------------------------------------
924
// Emit postfix constant extension for initial eight bit constant.
925
// ---------------------------------------------------------------------------
926
 
927
static void emit_postfix8(int64_t val)
928
{
929
        if (val < -127 || val > 127) {
930
                emit_insn(((val >> 12)<<8)|0xC0|((val >> 8)&0xF),0,5);
931
                return;
932
        }
933
        if (val < -0x7FFFFFFFFFFLL || val > 0x7FFFFFFFFFF) {
934
                emit_insn(((val >> 48)<<8)|0xC0|((val >> 44)&0xF),0,5);
935
                return;
936
        }
937
}
938
 
939
// ---------------------------------------------------------------------------
940
// Emit postfix constant extension for initial twenty bit constant.
941
// ---------------------------------------------------------------------------
942
 
943
static void emit_postfix20(int64_t val)
944
{
945
        if (val < IMIN20 || val > IMAX20) {
946
                emit_insn(((val >> 24)<<8)|0xC0|((val >> 20)&0xF),0,5);
947
                return;
948
        }
949
        if (val < -0x7FFFFFFFFFFFFFLL || val > 0x7FFFFFFFFFFFFFLL) {
950
                emit_insn(((val >> 60)<<8)|0xC0|((val >> 56)&0xF),0,5);
951
                return;
952
        }
953
}
954
 
955
// ---------------------------------------------------------------------------
956
// addi r1,r2,#1234
957
// ---------------------------------------------------------------------------
958
 
959
static void process_riop(int opcode6)
960
{
961
    int Ra;
962
    int Rt;
963
    char *p;
964
    int64_t val;
965
 
966
    p = inptr;
967
    Rt = getRegisterX();
968
    need(',');
969
    Ra = getRegisterX();
970
    need(',');
971
    NextToken();
972
    val = expr();
973
        if (opcode6==0x07)      { // subi
974
                val = -val;
975
                opcode6 = 0x04; // change to addi
976
        }
977
        /*
978
        if (opcode6==0x04) {
979
                if (Rt == Ra) {
980
                        if (val > -512 && val < 511) {
981
                                emit_insn(((val & 0x3FF) << 14)|(Rt<<8)|0x31,0,5);
982
                                return;
983
                        }
984
                }
985
        }
986
        */
987
        emit_insn(((val & 0xFFFFFLL) << 20)|(Rt << 14)|(Ra << 8)|opcode6,0,5);
988
        emit_postfix20(val);
989
}
990
 
991
// ---------------------------------------------------------------------------
992
// ---------------------------------------------------------------------------
993
 
994
static void process_rrop(int funct6)
995
{
996
    int Ra,Rb,Rt;
997
    char *p;
998
 
999
    p = inptr;
1000
    Rt = getRegisterX();
1001
    need(',');
1002
    Ra = getRegisterX();
1003
    need(',');
1004
    NextToken();
1005
    if (token=='#') {
1006
        inptr = p;
1007
        process_riop(funct6);
1008
        return;
1009
    }
1010
    prevToken();
1011
    Rb = getRegisterX();
1012
    //prevToken();
1013
    emit_insn(((__int64)funct6<<32)|(Rt<<20)|(Rb<<14)|(Ra<<8)|0x02,!expand_flag,5);
1014
}
1015
 
1016
// ---------------------------------------------------------------------------
1017
// jmp main
1018
// jsr [r19]
1019
// jmp (tbl,r2)
1020
// jsr [gp+r20]
1021
// ---------------------------------------------------------------------------
1022
 
1023
static void process_jal(int oc)
1024
{
1025
    int64_t addr;
1026
    int Ra;
1027
    int Rt;
1028
 
1029
        Ra = 0;
1030
    Rt = 0;
1031
    NextToken();
1032
    if (token=='(' || token=='[') {
1033
j1:
1034
       Ra = getRegisterX();
1035
       if (Ra==-1) {
1036
           printf("Expecting a register\r\n");
1037
           return;
1038
       }
1039
       // Simple jmp [Rn]
1040
       else {
1041
            if (token != ')' && token!=']')
1042
                printf("Missing close bracket\r\n");
1043
            emit_insn((Ra << 8)|(Rt<<14)|0x40,0,5);
1044
            return;
1045
       }
1046
    }
1047
    prevToken();
1048
    Rt = getRegisterX();
1049
    if (Rt >= 0) {
1050
        need(',');
1051
        NextToken();
1052
        // jal Rt,[Ra]
1053
        if (token=='(' || token=='[')
1054
           goto j1;
1055
    }
1056
    else
1057
        Rt = 0;
1058
        addr = expr();
1059
    // d(Rn)? 
1060
    NextToken();
1061
    if (token=='(' || token=='[') {
1062
        Ra = getRegisterX();
1063
        if (Ra==-1) {
1064
            printf("Illegal jump address mode.\r\n");
1065
            Ra = 0;
1066
        }
1067
                if (Ra==31)     // program counter relative ?
1068
                        addr -= code_address;
1069
        }
1070
 
1071
        emit_insn((addr << 20) | (Rt << 14) | (Ra << 8) | 0x40,0,5);
1072
        emit_postfix20(addr);
1073
}
1074
 
1075
// ---------------------------------------------------------------------------
1076
// fabs.d fp1,fp2[,rm]
1077
// ---------------------------------------------------------------------------
1078
 
1079
static void process_fprop(int oc)
1080
{
1081
    int Ra;
1082
    int Rt;
1083
    char *p;
1084
    int fmt;
1085
    int rm;
1086
 
1087
    rm = 0;
1088
    fmt = GetFPSize();
1089
    p = inptr;
1090
    if (oc==0x06)        // fcmp
1091
        Rt = getRegisterX();
1092
    else
1093
        Rt = getRegisterX();
1094
    need(',');
1095
    Ra = getRegisterX();
1096
    if (token==',')
1097
       rm = getFPRoundMode();
1098
    prevToken();
1099
    emit_insn(
1100
                        (fmt << 35)|
1101
                        (rm << 32)|
1102
                        (Rt << 26)|
1103
                        (oc << 20)|
1104
                        (Ra << 8) |
1105
                        0xF1,!expand_flag,5
1106
                        );
1107
}
1108
 
1109
// ---------------------------------------------------------------------------
1110
// fadd.d fp1,fp2,fp12[,rm]
1111
// fcmp.d r1,fp3,fp10[,rm]
1112
// ---------------------------------------------------------------------------
1113
 
1114
static void process_fprrop(int oc)
1115
{
1116
    int Ra;
1117
    int Rb;
1118
    int Rt;
1119
    char *p;
1120
    int fmt;
1121
    int rm;
1122
 
1123
    rm = 0;
1124
    fmt = GetFPSize();
1125
    p = inptr;
1126
    if (oc==0x06)        // fcmp
1127
        Rt = getRegisterX();
1128
    else
1129
        Rt = getRegisterX();
1130
    need(',');
1131
    Ra = getRegisterX();
1132
    need(',');
1133
    Rb = getRegisterX();
1134
    if (token==',')
1135
       rm = getFPRoundMode();
1136
    prevToken();
1137
    emit_insn(
1138
                        (fmt << 35)|
1139
                        (rm << 32)|
1140
                        (Rt << 26)|
1141
                        (oc << 20)|
1142
                        (Rb << 14)|
1143
                        (Ra << 8) |
1144
                        0xF1,!expand_flag,5
1145
                        );
1146
}
1147
 
1148
// ---------------------------------------------------------------------------
1149
// fcx r0,#2
1150
// fdx r1,#0
1151
// ---------------------------------------------------------------------------
1152
 
1153
static void process_fpstat(int oc)
1154
{
1155
    int Ra;
1156
    int64_t bits;
1157
    char *p;
1158
 
1159
    p = inptr;
1160
    bits = 0;
1161
    Ra = getRegisterX();
1162
    if (token==',') {
1163
       NextToken();
1164
       bits = expr();
1165
    }
1166
    prevToken();
1167
        emit_insn(
1168
                ((bits & 0x3F) << 20) |
1169
                (oc << 14) |
1170
                (Ra << 8) |
1171
                0xF1,!expand_flag,5
1172
                );
1173
}
1174
 
1175
// ---------------------------------------------------------------------------
1176
// not r3,r3
1177
// ---------------------------------------------------------------------------
1178
 
1179
static void process_rop(int oc)
1180
{
1181
    int Ra;
1182
    int Rt;
1183
 
1184
    Rt = getRegisterX();
1185
    need(',');
1186
    Ra = getRegisterX();
1187
        emit_insn(
1188
                ((__int64)oc << 32) |
1189
                (Rt << 22) |
1190
                (Ra << 8) |
1191
                0x02,!expand_flag,5
1192
                );
1193
        prevToken();
1194
}
1195
 
1196
// ---------------------------------------------------------------------------
1197
// beq r1,r0,label
1198
// beq r2,#1234,label
1199
// ---------------------------------------------------------------------------
1200
 
1201
static void process_bcc(int opcode6)
1202
{
1203
    int Ra, Rb;
1204
        int pred = 0;
1205
    int64_t val, imm;
1206
    int64_t disp;
1207
 
1208
    Ra = getRegisterX();
1209
    need(',');
1210
    NextToken();
1211
    if (token=='#') {
1212
        imm = expr();
1213
                need(',');
1214
                NextToken();
1215
                val = expr();
1216
                if (token==',') {
1217
                        inptr++;
1218
                        if (*inptr=='T' || *inptr=='t') {
1219
                                inptr++;
1220
                                pred = 3;
1221
                        }
1222
                        else if (*inptr=='N' || *inptr=='n') {
1223
                                inptr++;
1224
                                pred = 2;
1225
                        }
1226
                }
1227
                disp = val - code_address;
1228
                emit_insn(((disp & 0xFFFF) << 24) |
1229
                        (pred << 22) |
1230
                        ((imm & 0xff) << 14) |
1231
                        (Ra << 8) |
1232
                        opcode6+0x10,0,5
1233
                );
1234
                emit_postfix8(imm);
1235
        return;
1236
    }
1237
        prevToken();
1238
    Rb = getRegisterX();
1239
    need(',');
1240
    NextToken();
1241
 
1242
    val = expr();
1243
        if (token==',') {
1244
                inptr++;
1245
                if (*inptr=='T' || *inptr=='t') {
1246
                        inptr++;
1247
                        pred = 3;
1248
                }
1249
                else if (*inptr=='N' || *inptr=='n') {
1250
                        inptr++;
1251
                        pred = 2;
1252
                }
1253
        }
1254
    disp = val - code_address;
1255
    emit_insn(((disp & 0xFFFF) << 24) |
1256
                (pred << 22) |
1257
        (Rb << 14) |
1258
        (Ra << 8) |
1259
        opcode6,0,5
1260
    );
1261
}
1262
 
1263
// ---------------------------------------------------------------------------
1264
// beqi r1,r0,label
1265
// beqi r2,#1234,label
1266
// ---------------------------------------------------------------------------
1267
 
1268
static void process_bcci(int opcode6)
1269
{
1270
    int Ra;
1271
        int pred = 0;
1272
    int64_t val, imm;
1273
    int64_t disp;
1274
 
1275
    Ra = getRegisterX();
1276
    need(',');
1277
    NextToken();
1278
    imm = expr();
1279
        need(',');
1280
        NextToken();
1281
        val = expr();
1282
        if (token==',') {
1283
                inptr++;
1284
                if (*inptr=='T' || *inptr=='t') {
1285
                        inptr++;
1286
                        pred = 3;
1287
                }
1288
                else if (*inptr=='N' || *inptr=='n') {
1289
                        inptr++;
1290
                        pred = 2;
1291
                }
1292
        }
1293
        disp = val - code_address;
1294
        emit_insn(((disp & 0xFFFF) << 24) |
1295
                (pred << 22) |
1296
                ((imm & 0xff) << 14) |
1297
                (Ra << 8) |
1298
                opcode6,0,5
1299
        );
1300
        emit_postfix8(imm);
1301
    return;
1302
}
1303
 
1304
 
1305
// ---------------------------------------------------------------------------
1306
// bfextu r1,r2,#1,#63
1307
// ---------------------------------------------------------------------------
1308
 
1309
static void process_bitfield(int oc)
1310
{
1311
    int Ra;
1312
    int Rt;
1313
    int64_t mb;
1314
    int64_t me;
1315
 
1316
    Rt = getRegisterX();
1317
    need(',');
1318
    Ra = getRegisterX();
1319
    need(',');
1320
    NextToken();
1321
    mb = expr();
1322
    need(',');
1323
    NextToken();
1324
    me = expr();
1325
    emit_insn(
1326
        ((int64_t)oc << 36) |
1327
        (me << 28) |
1328
        (mb << 20) |
1329
        (Rt << 14) |
1330
        (Ra << 8) |
1331
        0x13,0,5 // bitfield
1332
    );
1333
}
1334
 
1335
 
1336
// ---------------------------------------------------------------------------
1337
// bra label
1338
// ---------------------------------------------------------------------------
1339
 
1340
static void process_bra(int oc)
1341
{
1342
    int Ra = 0, Rb = 0;
1343
    int64_t val;
1344
    int64_t disp;
1345
 
1346
    NextToken();
1347
    val = expr();
1348
    disp = val - code_address;
1349
    emit_insn(((disp & 0xFFFF) << 24) |
1350
                (3 << 22) |
1351
        (Rb << 14) |
1352
        (Ra << 8) |
1353
        0x46,0,5
1354
    );
1355
}
1356
 
1357
// ----------------------------------------------------------------------------
1358
// chk r1,r2,#1234
1359
// ----------------------------------------------------------------------------
1360
 
1361
static void process_chki(int opcode6)
1362
{
1363
        int Ra;
1364
        int Rb;
1365
        int64_t val;
1366
 
1367
        Ra = getRegisterX();
1368
        need(',');
1369
        Rb = getRegisterX();
1370
        need(',');
1371
        NextToken();
1372
        val = expr();
1373
        emit_insn(((val & 0xFFFFF) << 20)|(Rb << 14)|(Ra << 8)|opcode6,!expand_flag,5);
1374
        emit_postfix20(val);
1375
}
1376
 
1377
 
1378
// ---------------------------------------------------------------------------
1379
// fbeq.q fp1,fp0,label
1380
// ---------------------------------------------------------------------------
1381
 
1382
static void process_fbcc(int opcode)
1383
{
1384
    int Ra, Rb;
1385
    int64_t val;
1386
    int64_t disp;
1387
        int sz;
1388
 
1389
    sz = GetFPSize();
1390
    Ra = getRegisterX();
1391
    need(',');
1392
    Rb = getRegisterX();
1393
    need(',');
1394
    NextToken();
1395
 
1396
    val = expr();
1397
    disp = val - code_address;
1398
        if (disp < -131072 || disp > 131071) {
1399
                // Flip the test
1400
                switch(opcode) {
1401
                case 0:  opcode = 1; break;
1402
                case 1: opcode = 0; break;
1403
                case 2: opcode = 3; break;
1404
                case 3: opcode = 2; break;
1405
                case 4: opcode = 5; break;
1406
                case 5: opcode = 4; break;
1407
                case 6: opcode = 7; break;
1408
                case 7: opcode = 6; break;
1409
                }
1410
                emit_insn(
1411
                        (sz << 27) |
1412
                        (4 << 21) |
1413
                        (opcode << 18) |
1414
                        (Rb << 12) |
1415
                        (Ra << 6) |
1416
                        0x01,0,5
1417
                );
1418
                emit_insn((disp & 0x1FFF) << 19 |
1419
                        0x12,0,5
1420
                );
1421
                return;
1422
        }
1423
    emit_insn(
1424
                ((disp & 0x3FFFF) << 22) |
1425
                (sz << 20) |
1426
        (Rb << 14) |
1427
        (Ra << 8) |
1428
        0x01,0,5
1429
    );
1430
}
1431
 
1432
// ---------------------------------------------------------------------------
1433
// ---------------------------------------------------------------------------
1434
 
1435
static void process_call()
1436
{
1437
        int64_t val;
1438
        int Ra = 0;
1439
 
1440
    NextToken();
1441
        val = expr();
1442
        if (token=='[') {
1443
                Ra = getRegisterX();
1444
                need(']');
1445
                if (Ra==63) {
1446
                        val -= code_address;
1447
                }
1448
        }
1449
        if (val==0) {
1450
                emit_insn(
1451
                        (Ra << 8) |
1452
                        0x50,0,5
1453
                );
1454
                return;
1455
        }
1456
        if (Ra==0 && val < 0xFFFFFFFFLL)
1457
        {
1458
                emit_insn(
1459
                        (val << 8) |
1460
                        0x44,0,5
1461
                );
1462
                return;
1463
        }
1464
        emit_insn(
1465
                (val << 20) |
1466
                (Ra << 8) |
1467
                0x50,0,5
1468
                );
1469
        emit_postfix20(val);
1470
        return;
1471
}
1472
 
1473
static void process_ret()
1474
{
1475
        int64_t val = 10;
1476
 
1477
    NextToken();
1478
        if (token=='#') {
1479
                val = expr();
1480
        }
1481
        emit_insn(
1482
                ((val & 0xFFFFF) << 20) |
1483
                0xEF,0,5
1484
                );
1485
        emit_postfix20(val);
1486
}
1487
 
1488
// ----------------------------------------------------------------------------
1489
// inc -8[bp],#1
1490
// ----------------------------------------------------------------------------
1491
 
1492
static void process_inc(int oc)
1493
{
1494
    int Ra;
1495
    int Rb;
1496
    int64_t incamt;
1497
    int64_t disp;
1498
    char *p;
1499
    int fixup = 5;
1500
    int neg = 0;
1501
        int Sc;
1502
 
1503
    NextToken();
1504
    p = inptr;
1505
    mem_operand(&disp, &Ra, &Rb, &Sc);
1506
    incamt = 1;
1507
    if (token==']')
1508
       NextToken();
1509
    if (token==',') {
1510
        NextToken();
1511
        incamt = expr();
1512
        prevToken();
1513
    }
1514
    if (Rb >= 0) {
1515
       if (disp != 0)
1516
           printf("displacement not allowed with indexed addressing.\r\n");
1517
       oc = 0x6F;  // INCX
1518
           // ToDo: fix this
1519
       emit_insn(
1520
           ((disp & 0xFF) << 24) |
1521
           (Rb << 17) |
1522
           ((incamt & 0x1F) << 12) |
1523
           (Ra << 7) |
1524
           oc,0,5
1525
       );
1526
       return;
1527
    }
1528
    if (oc==0x25) neg = 1;
1529
    oc = 0x96;        // INC
1530
    if (Ra < 0) Ra = 0;
1531
    if (neg) incamt = -incamt;
1532
        emit_insn(
1533
                (disp << 20) |
1534
                ((incamt & 0x3f) << 14) |
1535
                (Ra << 8) |
1536
                oc,0,5);
1537
        emit_postfix20(disp);
1538
    ScanToEOL();
1539
}
1540
 
1541
// ---------------------------------------------------------------------------
1542
// ---------------------------------------------------------------------------
1543
 
1544
static void process_int()
1545
{
1546
        int64_t val;
1547
 
1548
    NextToken();
1549
        val = expr();
1550
        emit_insn(
1551
                (1 << 23) |
1552
                ((val & 0x1FF) << 8) |
1553
                0x1B,0,5
1554
                );
1555
}
1556
 
1557
// ---------------------------------------------------------------------------
1558
// ---------------------------------------------------------------------------
1559
 
1560
static void GetIndexScale(int *sc)
1561
{
1562
      int64_t val;
1563
 
1564
      NextToken();
1565
      val = expr();
1566
      prevToken();
1567
      switch(val) {
1568
      case 0: *sc = 0; break;
1569
      case 1: *sc = 0; break;
1570
      case 2: *sc = 1; break;
1571
      case 4: *sc = 2; break;
1572
      case 8: *sc = 3; break;
1573
          case 16: *sc = 4; break;
1574
      default: printf("Illegal scaling factor.\r\n");
1575
      }
1576
}
1577
 
1578
 
1579
// ---------------------------------------------------------------------------
1580
// expr
1581
// expr[Reg]
1582
// [Reg]
1583
// [Reg+Reg]
1584
// ---------------------------------------------------------------------------
1585
 
1586
static void mem_operand(int64_t *disp, int *regA, int *regB, int *Sc)
1587
{
1588
     int64_t val;
1589
 
1590
     // chech params
1591
     if (disp == (int64_t *)NULL)
1592
         return;
1593
     if (regA == (int *)NULL)
1594
         return;
1595
 
1596
     *disp = 0;
1597
     *regA = -1;
1598
         *regB = -1;
1599
         *Sc = 0;
1600
     if (token!='[') {;
1601
          val = expr();
1602
          *disp = val;
1603
     }
1604
     if (token=='[') {
1605
         *regA = getRegisterX();
1606
         if (*regA == -1) {
1607
             printf("expecting a register\r\n");
1608
         }
1609
                 if (token=='+') {
1610
                         *regB = getRegisterX();
1611
                         if (*regB == -1) {
1612
                                 printf("expecting a register\r\n");
1613
                         }
1614
              if (token=='*') {
1615
                  GetIndexScale(Sc);
1616
              }
1617
                 }
1618
         need(']');
1619
     }
1620
}
1621
 
1622
// ---------------------------------------------------------------------------
1623
// sw disp[r1],r2
1624
// sw [r1+r2],r3
1625
// ----------------------------------------------------------------------------
1626
 
1627
static void process_store(int opcode6)
1628
{
1629
    int Ra,Rb;
1630
    int Rs;
1631
    int64_t disp,val;
1632
        int Sc;
1633
 
1634
    Rs = getRegisterX();
1635
    if (Rs < 0) {
1636
        printf("Expecting a source register (%d).\r\n", lineno);
1637
        printf("Line:%.60s\r\n",inptr);
1638
        ScanToEOL();
1639
        return;
1640
    }
1641
    expect(',');
1642
    mem_operand(&disp, &Ra, &Rb, &Sc);
1643
        if (Ra > 0 && Rb > 0) {
1644
                if (disp < -1023LL || disp > 1023LL)
1645
                        printf("Index displacment (%I64d) too large (%d)\r\n", disp, lineno);
1646
                emit_insn(
1647
                        (disp << 29) |
1648
                        (Sc << 26) |
1649
                        (Rs << 20) |
1650
                        (Rb << 14) |
1651
                        (Ra << 8) |
1652
                        opcode6 + 0x20,!expand_flag,5);
1653
                return;
1654
        }
1655
    if (Ra < 0) Ra = 0;
1656
    val = disp;
1657
        emit_insn(
1658
                (val << 20) |
1659
                (Rs << 14) |
1660
                (Ra << 8) |
1661
                opcode6,!expand_flag,5);
1662
        emit_postfix20(val);
1663
    ScanToEOL();
1664
}
1665
 
1666
// ---------------------------------------------------------------------------
1667
// pea disp[r1]
1668
// ----------------------------------------------------------------------------
1669
 
1670
static void process_pea(int opcode6)
1671
{
1672
    int Ra,Rb;
1673
    int Rs=0;
1674
    int64_t disp,val;
1675
        int Sc;
1676
 
1677
    mem_operand(&disp, &Ra, &Rb, &Sc);
1678
        if (Ra > 0 && Rb > 0) {
1679
                if (disp < -1023LL || disp > 1023LL)
1680
                        printf("Index displacment (%I64d) too large (%d)\r\n", disp, lineno);
1681
                emit_insn(
1682
                        (disp << 29) |
1683
                        (Sc << 26) |
1684
                        (Rs << 20) |
1685
                        (Rb << 14) |
1686
                        (Ra << 8) |
1687
                        opcode6 + 0x20,!expand_flag,5);
1688
                return;
1689
        }
1690
    if (Ra < 0) Ra = 0;
1691
    val = disp;
1692
        emit_insn(
1693
                (val << 20) |
1694
                (Rs << 14) |
1695
                (Ra << 8) |
1696
                opcode6,!expand_flag,5);
1697
        emit_postfix20(val);
1698
    ScanToEOL();
1699
}
1700
 
1701
// ----------------------------------------------------------------------------
1702
// ----------------------------------------------------------------------------
1703
 
1704
static void process_ldi()
1705
{
1706
    int Ra = 0;
1707
    int Rt;
1708
    int64_t val;
1709
    int opcode6 = 0x09;  // ORI
1710
 
1711
    Rt = getRegisterX();
1712
    expect(',');
1713
    val = expr();
1714
        emit_insn(
1715
                (val << 20) |
1716
                (Rt << 14) |
1717
                opcode6,0,5
1718
        );
1719
        emit_postfix20(val);
1720
}
1721
 
1722
// ----------------------------------------------------------------------------
1723
// lw r1,disp[r2]
1724
// lw r1,[r2+r3]
1725
// ----------------------------------------------------------------------------
1726
 
1727
static void process_load(int opcode6)
1728
{
1729
    int Ra,Rb;
1730
    int Rt;
1731
    char *p;
1732
    int64_t disp;
1733
    int64_t val;
1734
    int fixup = 5;
1735
        int Sc;
1736
 
1737
    p = inptr;
1738
    Rt = getRegisterX();
1739
    if (Rt < 0) {
1740
        printf("Expecting a target register (%d).\r\n", lineno);
1741
        printf("Line:%.60s\r\n",p);
1742
        ScanToEOL();
1743
        inptr-=2;
1744
        return;
1745
    }
1746
    expect(',');
1747
    mem_operand(&disp, &Ra, &Rb, &Sc);
1748
        if (Ra > 0 && Rb > 0) {
1749
                if (disp < -1023LL || disp > 1023LL)
1750
                        printf("Index displacment (%I64d) too large (%d)\r\n", disp, lineno);
1751
                emit_insn(
1752
                        (disp << 29) |
1753
                        (Sc << 26) |
1754
                        (Rt << 20) |
1755
                        (Rb << 14) |
1756
                        (Ra << 8) |
1757
                        opcode6+(opcode6==0x26?0x01:0x20),!expand_flag,5);
1758
                return;
1759
        }
1760
    if (Ra < 0) Ra = 0;
1761
    val = disp;
1762
        /*
1763
        if (opcode6==0x86 && Ra==59 && val > -511 && val < 511) {
1764
                emit_insn(
1765
                        ((val & 0x3FF) << 14) |
1766
                        (Rt << 8) |
1767
                        0x72,0,5);
1768
        }
1769
        else if (opcode6==0x86 && val >- 2047 && val < 2047) {
1770
                emit_insn(
1771
                        ((val & 0xFFF) << 20) |
1772
                        (Rt << 14) |
1773
                        (Ra << 8) |
1774
                        0x73,!expand_flag,5);
1775
        }
1776
        else
1777
        */
1778
        {
1779
                emit_insn(
1780
                (val << 20) |
1781
                (Rt << 14) |
1782
                (Ra << 8) |
1783
                opcode6,0,5);
1784
                emit_postfix20(val);
1785
        }
1786
    ScanToEOL();
1787
}
1788
 
1789
static void process_lsfloat(int opcode6)
1790
{
1791
    int Ra,Rb;
1792
    int Rt;
1793
    char *p;
1794
    int64_t disp;
1795
    int64_t val;
1796
    int fixup = 5;
1797
        int Sc;
1798
    int  sz;
1799
    int rm;
1800
 
1801
    rm = 0;
1802
    sz = GetFPSize();
1803
    p = inptr;
1804
    Rt = getFPRegister();
1805
    if (Rt < 0) {
1806
        printf("Expecting a target register (1:%d).\r\n", lineno);
1807
        printf("Line:%.60s\r\n",p);
1808
        ScanToEOL();
1809
        inptr-=2;
1810
        return;
1811
    }
1812
    expect(',');
1813
    mem_operand(&disp, &Ra, &Rb, &Sc);
1814
        if (Ra > 0 && Rb > 0) {
1815
                if (disp < -1023LL || disp > 1023LL)
1816
                        printf("Index displacment (%I64d) too large (%d)\r\n", disp, lineno);
1817
                emit_insn(
1818
                        (disp << 29) |
1819
                        (Sc << 26) |
1820
                        (Rt << 20) |
1821
                        (Rb << 14) |
1822
                        (Ra << 8) |
1823
                        opcode6,!expand_flag,5);
1824
                return;
1825
        }
1826
    if (Ra < 0) Ra = 0;
1827
    val = disp;
1828
        if (disp < -1023 || disp > 15LL) {
1829
        }
1830
        else {
1831
                emit_insn(
1832
                        (sz << 27) |
1833
                        (Rt << 18) |
1834
                        ((val & 0x1f) << 11) |
1835
                        (Ra << 6) |
1836
                        opcode6,!expand_flag,5);
1837
    }
1838
    ScanToEOL();
1839
}
1840
 
1841
static void process_ld()
1842
{
1843
        int Rt;
1844
        char *p;
1845
 
1846
        p = inptr;
1847
        Rt = getRegisterX();
1848
        expect(',');
1849
//      NextToken();
1850
        if (token == '#') {
1851
                inptr = p;
1852
                process_ldi();
1853
                return;
1854
        }
1855
        // Else: do a word load
1856
        inptr = p;
1857
        process_load(0x86);
1858
}
1859
 
1860
// ----------------------------------------------------------------------------
1861
// mov r1,r2
1862
// ----------------------------------------------------------------------------
1863
 
1864
static void process_mov(int oc)
1865
{
1866
     int Ra;
1867
     int Rt;
1868
         char *p;
1869
 
1870
         p = inptr;
1871
         Rt = getFPRegister();
1872
         if (Rt==-1) {
1873
                 inptr = p;
1874
             Rt = getRegisterX();
1875
         }
1876
     need(',');
1877
         p = inptr;
1878
         Ra = getFPRegister();
1879
         if (Ra==-1) {
1880
                 inptr = p;
1881
                 Ra = getRegisterX();
1882
         }
1883
         emit_insn(
1884
                 (Rt << 14) |
1885
                 (Ra << 8) |
1886
                 oc,0,5
1887
                 );
1888
        prevToken();
1889
}
1890
 
1891
// ----------------------------------------------------------------------------
1892
// srli r1,r2,#5
1893
// ----------------------------------------------------------------------------
1894
 
1895
static void process_shifti(int funct6)
1896
{
1897
     int Ra;
1898
     int Rt;
1899
     int64_t val;
1900
 
1901
     Rt = getRegisterX();
1902
     need(',');
1903
     Ra = getRegisterX();
1904
     need(',');
1905
     NextToken();
1906
     val = expr();
1907
     emit_insn(((__int64)funct6 << 32) | (Rt << 20)| ((val & 0x3F) << 14) | (Ra << 8) | 0x02,!expand_flag,5);
1908
}
1909
 
1910
// ----------------------------------------------------------------------------
1911
// shl r1,r2,r3
1912
// ----------------------------------------------------------------------------
1913
 
1914
static void process_shift(int funct6)
1915
{
1916
     int Ra, Rb;
1917
     int Rt;
1918
     char *p;
1919
 
1920
         p = inptr;
1921
     Rt = getRegisterX();
1922
     need(',');
1923
     Ra = getRegisterX();
1924
     need(',');
1925
     NextToken();
1926
         if (token=='#') {
1927
                 inptr = p;
1928
                 process_shifti(funct6 + 0x10);
1929
         }
1930
         else {
1931
                prevToken();
1932
                Rb = getRegisterX();
1933
                emit_insn(((__int64)funct6 << 32) | (Rt << 20)| (Rb << 14) | (Ra << 8) | 0x02,!expand_flag,5);
1934
         }
1935
}
1936
 
1937
// ----------------------------------------------------------------------------
1938
// gran r1
1939
// ----------------------------------------------------------------------------
1940
 
1941
static void process_gran(int oc)
1942
{
1943
    int Rt;
1944
 
1945
    Rt = getRegisterX();
1946
//    emitAlignedCode(0x01);
1947
    emitCode(0x00);
1948
    emitCode(Rt);
1949
    emitCode(0x00);
1950
    emitCode(oc);
1951
    prevToken();
1952
}
1953
 
1954
 
1955
// ----------------------------------------------------------------------------
1956
// ----------------------------------------------------------------------------
1957
 
1958
static void process_mffp(int oc)
1959
{
1960
    int fpr;
1961
    int Rt;
1962
 
1963
    Rt = getRegisterX();
1964
    need(',');
1965
    fpr = getFPRegister();
1966
    //emitAlignedCode(0x01);
1967
    emitCode(fpr);
1968
    emitCode(Rt);
1969
    emitCode(0x00);
1970
    emitCode(oc);
1971
    if (fpr >= 0)
1972
    prevToken();
1973
}
1974
 
1975
// ----------------------------------------------------------------------------
1976
// fill.b 252,0x00
1977
// ----------------------------------------------------------------------------
1978
 
1979
static void process_fill()
1980
{
1981
    char sz = 'b';
1982
    int64_t count;
1983
    Int128 val;
1984
    int64_t nn;
1985
 
1986
    if (*inptr=='.') {
1987
        inptr++;
1988
        if (strchr("bwtpdBWTPD",*inptr)) {
1989
            sz = tolower(*inptr);
1990
            inptr++;
1991
        }
1992
        else
1993
            printf("Illegal fill size.\r\n");
1994
    }
1995
    SkipSpaces();
1996
    NextToken();
1997
    count = expr();
1998
    prevToken();
1999
    need(',');
2000
    NextToken();
2001
    val = expr128();
2002
    prevToken();
2003
    for (nn = 0; nn < count; nn++)
2004
        switch(sz) {
2005
        case 'b': emitByte(val.low); break;
2006
        case 'w': emitChar(val.low); break;
2007
        case 't': emitHalf(val.low); break;
2008
        case 'p': emit40(val); break;
2009
        case 'd': emit80(val); break;
2010
        }
2011
}
2012
 
2013
// ----------------------------------------------------------------------------
2014
// ----------------------------------------------------------------------------
2015
 
2016
static void process_fprdstat(int oc)
2017
{
2018
    int Rt;
2019
 
2020
    Rt = getRegisterX();
2021
    //emitAlignedCode(0x01);
2022
    emitCode(0x00);
2023
    emitCode(Rt);
2024
    emitCode(0x00);
2025
    emitCode(oc);
2026
}
2027
 
2028
 
2029
// ----------------------------------------------------------------------------
2030
// Four cases, two with extendable immediate constants
2031
//
2032
// csrrw        r2,#21,r3
2033
// csrrw        r4,#34,#1234
2034
// csrrw        r5,r4,#1234
2035
// csrrw        r6,r4,r5
2036
// ----------------------------------------------------------------------------
2037
 
2038
static void process_csrrw(int op)
2039
{
2040
        int Rd;
2041
        int Rs;
2042
        int Rc;
2043
        int64_t val,val2;
2044
        char *p;
2045
 
2046
        Rd = getRegisterX();
2047
        need(',');
2048
        p = inptr;
2049
        NextToken();
2050
        if (token=='#') {
2051
                val = expr();
2052
                need(',');
2053
                NextToken();
2054
                if (token=='#') {
2055
                        val2 = expr();
2056
                        emit_insn(
2057
                                ((__int64)op << 38) |
2058
                                ((val & 0x3FFF) << 22)|
2059
                                ((val2 & 0xFF) << 14) |
2060
                                (Rd << 8) | 0x0F,0,5);
2061
                        emit_postfix8(val2);
2062
                        return;
2063
                }
2064
                prevToken();
2065
                Rs = getRegisterX();
2066
                emit_insn(
2067
                        ((__int64)op << 38) |
2068
                        (3LL << 36) |
2069
                        ((val & 0x3FFF) << 22) |
2070
                        (Rd << 14) |
2071
                        (Rs << 8) |
2072
                        0x0F,!expand_flag,5);
2073
                prevToken();
2074
                return;
2075
                }
2076
        inptr = p;
2077
        Rc = getRegisterX();
2078
        need(',');
2079
        NextToken();
2080
        if (token=='#') {
2081
                val2 = expr();
2082
                emit_insn(
2083
                        ((__int64)op << 38) |
2084
                        (1LL << 36) |
2085
                        (Rd << 22) | ((val2 & 0xFF) << 14) | (Rc << 8) | 0x0F,!expand_flag,5);
2086
                emit_postfix8(val2);
2087
                return;
2088
        }
2089
        prevToken();
2090
        Rs = getRegisterX();
2091
        emit_insn(
2092
                ((__int64)op << 38) |
2093
                (2LL << 36) |
2094
                (Rd << 22) |
2095
                (Rc << 14) |
2096
                (Rs << 8) |
2097
                0x0F,!expand_flag,5);
2098
        prevToken();
2099
        return;
2100
}
2101
 
2102
// ---------------------------------------------------------------------------
2103
// com r3,r3
2104
// - alternate mnemonic for xor Rn,Rn,#-1
2105
// ---------------------------------------------------------------------------
2106
 
2107
static void process_com()
2108
{
2109
    int Ra;
2110
    int Rt;
2111
 
2112
    Rt = getRegisterX();
2113
    need(',');
2114
    Ra = getRegisterX();
2115
        emit_insn(
2116
                (0xFFFFF << 20) |
2117
                (Rt << 14) |
2118
                (Ra << 8) |
2119
                0x0A,!expand_flag,5
2120
                );
2121
        prevToken();
2122
}
2123
 
2124
// ---------------------------------------------------------------------------
2125
// com r3,r3
2126
// - alternate mnemonic for xor Rn,Rn,#-1
2127
// ---------------------------------------------------------------------------
2128
 
2129
static void process_neg()
2130
{
2131
    int Ra;
2132
    int Rt;
2133
 
2134
    Rt = getRegisterX();
2135
    need(',');
2136
    Ra = getRegisterX();
2137
        emit_insn(
2138
                (0x07 << 26) |
2139
                (Rt << 20) |
2140
                (Ra << 14) |
2141
                (0 << 8) |
2142
                0x02,!expand_flag,5
2143
                );
2144
        prevToken();
2145
}
2146
 
2147
// ----------------------------------------------------------------------------
2148
// push r1
2149
// push #123
2150
// ----------------------------------------------------------------------------
2151
 
2152
static void process_push(int func)
2153
{
2154
    int Ra,Rb;
2155
        int FRa;
2156
        int sz;
2157
    int64_t val;
2158
 
2159
    Ra = -1;
2160
    Rb = -1;
2161
    sz = GetFPSize();
2162
    NextToken();
2163
    if (token=='#') {  // Filter to PUSH
2164
       val = expr();
2165
           /*
2166
            if (val >= -15LL && val < 16LL) {
2167
                        emit_insn(
2168
                                (4 << 11) |
2169
                                ((val & 0x1f) << 6) |
2170
                                0x19,0,5
2171
                                );
2172
                        return;
2173
                }
2174
                */
2175
                emit_insn(
2176
                        ((val & 0xFFFFFLL) << 20) |
2177
                        (0x00 << 14) |
2178
                        (0x00 << 8) |
2179
                        0x9C,  // PEA
2180
                        0,5
2181
                );
2182
                emit_postfix20(val);
2183
        return;
2184
    }
2185
    prevToken();
2186
        FRa = getFPRegister();
2187
        if (FRa==-1) {
2188
                prevToken();
2189
                Ra = getRegisterX();
2190
        }
2191
        else {
2192
                emit_insn(
2193
                        (FRa << 6) |
2194
                        func,0,5
2195
                        );
2196
                prevToken();
2197
                return;
2198
        }
2199
    if (Ra == -1) {
2200
                Ra = 0;
2201
        printf("%d: unknown register.\r\n");
2202
    }
2203
        if (func==0x71) // POP
2204
                emit_insn(
2205
                        (Ra << 14) |
2206
                        func,0,5);
2207
        else
2208
                emit_insn(
2209
                        (Ra << 8) |
2210
                        func,0,5);
2211
    prevToken();
2212
}
2213
 
2214
static void process_sync(int oc)
2215
{
2216
//    emit_insn(oc,!expand_flag);
2217
}
2218
 
2219
 
2220
// ----------------------------------------------------------------------------
2221
// ----------------------------------------------------------------------------
2222
 
2223
static void ProcessEOL(int opt)
2224
{
2225
    int64_t nn,mm;
2226
    int first;
2227
    int cc;
2228
    bool sp;
2229
 
2230
     //printf("Line: %d\r", lineno);
2231
     expand_flag = 0;
2232
     compress_flag = 0;
2233
     segprefix = -1;
2234
     if (bGen && (segment==codeseg || segment==dataseg || segment==rodataseg)) {
2235
    nn = binstart;
2236
    cc = 2;
2237
    if (segment==codeseg) {
2238
       cc = 5;
2239
/*
2240
        if (sections[segment].bytes[binstart]==0x61) {
2241
            fprintf(ofp, "%06LLX ", ca);
2242
            for (nn = binstart; nn < binstart + 5 && nn < sections[segment].index; nn++) {
2243
                fprintf(ofp, "%02X ", sections[segment].bytes[nn]);
2244
            }
2245
            fprintf(ofp, "   ; imm\n");
2246
             if (((ca+5) & 15)==15) {
2247
                 ca+=6;
2248
                 binstart+=6;
2249
                 nn++;
2250
             }
2251
             else {
2252
                  ca += 5;
2253
                  binstart += 5;
2254
             }
2255
        }
2256
*/
2257
/*
2258
        if (sections[segment].bytes[binstart]==0xfd) {
2259
            fprintf(ofp, "%06LLX ", ca);
2260
            for (nn = binstart; nn < binstart + 5 && nn < sections[segment].index; nn++) {
2261
                fprintf(ofp, "%02X ", sections[segment].bytes[nn]);
2262
            }
2263
            fprintf(ofp, "   ; imm\n");
2264
             if (((ca+5) & 15)==15) {
2265
                 ca+=6;
2266
                 binstart+=6;
2267
                 nn++;
2268
             }
2269
             else {
2270
                  ca += 5;
2271
                  binstart += 5;
2272
             }
2273
        }
2274
         if (sections[segment].bytes[binstart]==0xfe) {
2275
            fprintf(ofp, "%06LLX ", ca);
2276
            for (nn = binstart; nn < binstart + 5 && nn < sections[segment].index; nn++) {
2277
                fprintf(ofp, "%02X ", sections[segment].bytes[nn]);
2278
            }
2279
            fprintf(ofp, "   ; imm\n");
2280
             if (((ca+5) & 15)==15) {
2281
                 ca+=6;
2282
                 nn++;
2283
             }
2284
             else {
2285
                  ca += 5;
2286
             }
2287
        }
2288
*/
2289
    }
2290
 
2291
    first = 1;
2292
        sp = false;
2293
    while (nn < sections[segment].index) {
2294
                sp = ((ca & 15)==10);
2295
                fprintf(ofp, "%06LLX ", ca);
2296
        for (mm = nn; nn < mm + cc && nn < sections[segment].index; ) {
2297
                        switch(sections[segment].index-nn) {
2298
                        case 1: /*fprintf(ofp, "        %02X ", sections[segment].bytes[nn]);*/ nn++; break;
2299
                        case 2: fprintf(ofp, "      %02X%02X ", sections[segment].bytes[nn+1], sections[segment].bytes[nn]); nn += 2; break;
2300
                        case 3: fprintf(ofp, "    %02X%02X%02X ",
2301
                                                sections[segment].bytes[nn+2],
2302
                                                sections[segment].bytes[nn+1],
2303
                                                sections[segment].bytes[nn]); nn += 3; break;
2304
                        case 4: fprintf(ofp, "  %02X%02X%02X%02X ",
2305
                                                sections[segment].bytes[nn+3],
2306
                                                sections[segment].bytes[nn+2],
2307
                                                sections[segment].bytes[nn+1],
2308
                                                sections[segment].bytes[nn]); nn += 4; break;
2309
                        case 5: fprintf(ofp, "%02X%02X%02X%02X%02X ",
2310
                                                sections[segment].bytes[nn+4],
2311
                                                sections[segment].bytes[nn+3],
2312
                                                sections[segment].bytes[nn+2],
2313
                                                sections[segment].bytes[nn+1],
2314
                                                sections[segment].bytes[nn]); nn += 5; break;
2315
                        default: fprintf(ofp, "%02X%02X%02X%02X%02X ",
2316
                                                sections[segment].bytes[nn+4],
2317
                                                sections[segment].bytes[nn+3],
2318
                                                sections[segment].bytes[nn+2],
2319
                                                sections[segment].bytes[nn+1],
2320
                                                sections[segment].bytes[nn]); nn += sp ? 6 : 5; break;
2321
                        }
2322
        }
2323
                for (; nn < mm + cc; nn++)
2324
                        fprintf(ofp, "  ");
2325
                if (first & opt) {
2326
                        fprintf(ofp, "\t%.*s\n", inptr-stptr-1, stptr);
2327
                        first = 0;
2328
                }
2329
                else
2330
                        fprintf(ofp, opt ? "\n" : "; NOP Ramp\n");
2331
                if (sp)
2332
                        cc = 6;
2333
                else
2334
                        cc = 5;
2335
        ca += cc;
2336
                cc = 5;
2337
    }
2338
    // empty (codeless) line
2339
    if (binstart==sections[segment].index) {
2340
        fprintf(ofp, "%24s\t%.*s", "", inptr-stptr, stptr);
2341
    }
2342
    } // bGen
2343
    if (opt) {
2344
       stptr = inptr;
2345
       lineno++;
2346
    }
2347
    binstart = sections[segment].index;
2348
    ca = sections[segment].address;
2349
}
2350
 
2351
void dsd9_GenerateListing()
2352
{
2353
        unsigned nn;
2354
        int kk;
2355
        char lnbuf[200];
2356
        char *p = nullptr;
2357
 
2358
        for (nn = 0; nn < cs.bufndx; nn++) {
2359
                fprintf(ofp, "[%08I64X] %08I64X ", ((cs.buf[nn].address * 5) >> 1), cs.buf[nn].address);
2360
                if (cs.buf[nn].size==1)
2361
                        fprintf(ofp, "%05I64X          ", cs.buf[nn].opcode & 0xFFFFFLL);
2362
                else
2363
                        fprintf(ofp, "%010I64X", cs.buf[nn].opcode & 0xFFFFFFFFFFLL);
2364
                for (kk = 0; cs.buf[nn].source[kk] != '\r' && cs.buf[nn].source[kk] != '\n' && cs.buf[nn].source[kk] && kk < 198; kk++)
2365
                        lnbuf[kk] = cs.buf[nn].source[kk];
2366
                lnbuf[kk] = '\0';
2367
                if (cs.buf[nn].source!=p)
2368
                {
2369
                        fprintf(ofp, lnbuf);
2370
                        p = cs.buf[nn].source;
2371
                }
2372
                fprintf(ofp, "\n");
2373
        }
2374
}
2375
 
2376
int dsd9_GetNextNybble(int *bndx, int *shft)
2377
{
2378
        int nyb;
2379
 
2380
        nyb = (cs.buf[*bndx].opcode >> *shft) & 0xF;
2381
        *shft += 4;
2382
        if (*shft >= cs.buf[*bndx].size * 20) {
2383
                (*bndx)++;
2384
                *shft = 0;
2385
        }
2386
        return nyb;
2387
}
2388
 
2389
void dsd9_VerilogOut(FILE *fp)
2390
{
2391
        unsigned int nn;
2392
        int bndx;
2393
        int shft;
2394
        int cnt;
2395
        int nyb;
2396
        int romndx = 0;
2397
        char buf[40];
2398
 
2399
        shft = 0;
2400
        cnt = 0;
2401
        for (bndx = 0; bndx < cs.bufndx; ) {
2402
                if (cnt==0)
2403
                        fprintf(fp, "rommem[%d] = 128'h", romndx);
2404
                nyb = dsd9_GetNextNybble(&bndx, &shft);
2405
                buf[cnt] = nyb;
2406
                cnt++;
2407
                if (cnt==32) {
2408
                        for (--cnt; cnt >= 0; --cnt)
2409
                                fprintf(fp, "%X", buf[cnt]);
2410
                        fprintf(fp, ";\n");
2411
                        romndx++;
2412
                        cnt = 0;
2413
                }
2414
        }
2415
        if (cnt > 0 && cnt < 32) {
2416
                for (; cnt < 32; cnt++)
2417
                        buf[cnt] = nyb;
2418
                for (--cnt; cnt >= 0; --cnt)
2419
                        fprintf(fp, "%X", buf[cnt]);
2420
                fprintf(fp, ";\n");
2421
        }
2422
}
2423
 
2424
 
2425
// ----------------------------------------------------------------------------
2426
// ----------------------------------------------------------------------------
2427
 
2428
void dsd9_processMaster()
2429
{
2430
    int nn;
2431
    int64_t bs1, bs2;
2432
        /*
2433
        if (bGenListing) {
2434
                dsd9_GenerateListing();
2435
                return;
2436
        }
2437
        */
2438
    lineno = 1;
2439
    binndx = 0;
2440
    binstart = 0;
2441
    bs1 = 0;
2442
    bs2 = 0;
2443
    inptr = &masterFile[0];
2444
    stptr = inptr;
2445
        lptr = inptr;
2446
    code_address = 0;
2447
    bss_address = 0;
2448
    start_address = 0;
2449
    first_org = 1;
2450
    first_rodata = 1;
2451
    first_data = 1;
2452
    first_bss = 1;
2453
        expandedBlock = 1;
2454
    if (pass<3) {
2455
    htblmax = 0;
2456
    for (nn = 0; nn < 100000; nn++) {
2457
      hTable[nn].count = 0;
2458
      hTable[nn].opcode = 0;
2459
    }
2460
    }
2461
    for (nn = 0; nn < 12; nn++) {
2462
        sections[nn].index = 0;
2463
        if (nn == 0)
2464
        sections[nn].address = 0;
2465
        else
2466
        sections[nn].address = 0;
2467
        sections[nn].start = 0;
2468
        sections[nn].end = 0;
2469
    }
2470
    ca = code_address;
2471
    segment = codeseg;
2472
    memset(current_label,0,sizeof(current_label));
2473
        memset(bytebuf,0,sizeof(bytebuf));
2474
        cs.address = 0;
2475
        cs.bufndx = 0;
2476
        num_bytes = 0;
2477
        num_insns = 0;
2478
    NextToken();
2479
    while (token != tk_eof) {
2480
//        printf("\t%.*s\n", inptr-stptr-1, stptr);
2481
//        printf("token=%d\r", token);
2482
          if (expandedBlock)
2483
             expand_flag = 1;
2484
        switch(token) {
2485
        case tk_eol:    ProcessEOL(1);
2486
                                                lptr = inptr;
2487
                                                break;
2488
        case tk_add:  process_rrop(0x04); break;
2489
        case tk_addi: process_riop(0x04); break;
2490
        case tk_align: process_align(); continue; break;
2491
        case tk_and:  process_rrop(0x08); break;
2492
        case tk_andi:  process_riop(0x08); break;
2493
        case tk_asl: process_shift(0x32); break;
2494
        case tk_asr: process_shift(0x33); break;
2495
        case tk_bbc: process_bcci(0x54); break;
2496
        case tk_bbs: process_bcci(0x55); break;
2497
        case tk_begin_expand: expandedBlock = 1; break;
2498
        case tk_beq: process_bcc(0x46); break;
2499
        case tk_beqi: process_bcci(0x56); break;
2500
        case tk_bfext: process_bitfield(5); break;
2501
        case tk_bfextu: process_bitfield(6); break;
2502
        case tk_bge: process_bcc(0x49); break;
2503
        case tk_bgei: process_bcci(0x59); break;
2504
        case tk_bgeu: process_bcc(0x4D); break;
2505
        case tk_bgeui: process_bcci(0x5D); break;
2506
        case tk_bgt: process_bcc(0x4B); break;
2507
        case tk_bgti: process_bcci(0x5B); break;
2508
        case tk_bgtu: process_bcc(0x4F); break;
2509
        case tk_bgtui: process_bcci(0x5F); break;
2510
        case tk_ble: process_bcc(0x4A); break;
2511
        case tk_blei: process_bcci(0x5A); break;
2512
        case tk_bleu: process_bcc(0x4E); break;
2513
        case tk_bleui: process_bcci(0x5E); break;
2514
        case tk_blt: process_bcc(0x48); break;
2515
        case tk_blti: process_bcci(0x58); break;
2516
        case tk_bltu: process_bcc(0x4C); break;
2517
        case tk_bltui: process_bcci(0x5C); break;
2518
        case tk_bne: process_bcc(0x47); break;
2519
        case tk_bnei: process_bcci(0x57); break;
2520
        case tk_bra: process_bra(0x46); break;
2521
        //case tk_bsr: process_bra(0x56); break;
2522
        case tk_bss:
2523
            if (first_bss) {
2524
                while(sections[segment].address & 15)
2525
                    emitByte(0x00);
2526
                sections[3].address = sections[segment].address;
2527
                first_bss = 0;
2528
                binstart = sections[3].index;
2529
                ca = sections[3].address;
2530
            }
2531
            segment = bssseg;
2532
            break;
2533
                case tk_call:   process_call(); break;
2534
                case tk_calltgt:        emit_insn(0x53,0,5); break;
2535
                case tk_chki:  process_chki(0x0E); break;
2536
        case tk_cli: emit_insn(0x00E8,0,5); break;
2537
        case tk_code: process_code(); break;
2538
        case tk_com: process_com(); break;
2539
        case tk_cs:  segprefix = 15; break;
2540
        case tk_csrrc: process_csrrw(0x2); break;
2541
        case tk_csrrs: process_csrrw(0x1); break;
2542
        case tk_csrrw: process_csrrw(0x0); break;
2543
        case tk_data:
2544
            if (first_data) {
2545
                while(sections[segment].address & 15)
2546
                    emitByte(0x00);
2547
                sections[2].address = sections[segment].address;   // set starting address
2548
                first_data = 0;
2549
                binstart = sections[2].index;
2550
                ca = sections[2].address;
2551
            }
2552
            process_data(dataseg);
2553
            break;
2554
                case tk_dec: process_inc(0x25); break;
2555
                case tk_db: process_db(); break;
2556
        case tk_dh_htbl:  process_dh_htbl(); break;
2557
        case tk_div: process_rrop(0x18); break;
2558
        case tk_divu: process_rrop(0x19); break;
2559
                case tk_dd:     process_dd(); break;
2560
        case tk_do:  process_dw(); break;
2561
        case tk_dt:  process_dh(); break;
2562
                case tk_dw:      process_dc(); break;
2563
        case tk_end: goto j1;
2564
        case tk_end_expand: expandedBlock = 0; break;
2565
        case tk_endpublic: break;
2566
        case tk_eor: process_rrop(0x0A); break;
2567
        case tk_eori: process_riop(0x0A); break;
2568
        case tk_extern: process_extern(); break;
2569
        case tk_fabs: process_fprop(0x15); break;
2570
        case tk_fadd: process_fprrop(0x04); break;
2571
        case tk_fbeq: process_fbcc(0x36); break;
2572
        case tk_fbne: process_fbcc(0x37); break;
2573
        case tk_fbun: process_fbcc(0x3D); break;
2574
        case tk_fbor: process_fbcc(0x3C); break;
2575
        case tk_fblt: process_fbcc(0x38); break;
2576
        case tk_fbge: process_fbcc(0x39); break;
2577
        case tk_fble: process_fbcc(0x3A); break;
2578
        case tk_fbgt: process_fbcc(0x3B); break;
2579
        case tk_fcmp: process_fprrop(0x06); break;
2580
        case tk_fdiv: process_fprrop(0x09); break;
2581
        case tk_fill: process_fill(); break;
2582
        case tk_fix2flt: process_fprop(0x13); break;
2583
        case tk_flt2fix: process_fprop(0x12); break;
2584
        case tk_fmov: process_fprop(0x10); break;
2585
        case tk_fmul: process_fprrop(0x08); break;
2586
        case tk_fnabs: process_fprop(0x18); break;
2587
        case tk_fneg: process_fprop(0x14); break;
2588
        case tk_frm: process_fpstat(0x24); break;
2589
//        case tk_fstat: process_fprdstat(0x86); break;
2590
        case tk_fsub: process_fprrop(0x05); break;
2591
                case tk_ftoi: process_fprop(0x12); break;
2592
        case tk_ftx: process_fpstat(0x20); break;
2593
        case tk_gran: process_gran(0x14); break;
2594
                case tk_hint:   process_hint(); break;
2595
                case tk_inc:    process_inc(0x96); break;
2596
                case tk_int:    process_int(); break;
2597
                case tk_iret:   emit_insn(0xE4,0,5); break;
2598
                case tk_itof: process_fprop(0x13); break;
2599
        case tk_jal: process_jal(0x15); break;
2600
        case tk_jmp: process_jal(0x15); break;
2601
                case tk_ld:     process_ld(); break;
2602
        case tk_ldb: process_load(0x80); break;
2603
        case tk_ldbu: process_load(0x81); break;
2604
        case tk_ldw: process_load(0x82); break;
2605
        case tk_ldwu: process_load(0x83); break;
2606
        case tk_ldp: process_load(0x84); break;
2607
        case tk_ldpu: process_load(0x85); break;
2608
        case tk_ldd: process_load(0x86); break;
2609
        case tk_ldi: process_ldi(); break;
2610
                case tk_ldt: process_load(0x87); break;
2611
                case tk_ldtu: process_load(0x88); break;
2612
                case tk_ldvdar: process_load(0x8E); break;
2613
        case tk_lea: process_load(0x26); break;
2614
                case tk_lf:      process_lsfloat(0x26); break;
2615
        case tk_lh:  process_load(0x20); break;
2616
        case tk_lhu: process_load(0x21); break;
2617
//        case tk_lui: process_lui(); break;
2618
        case tk_lw:  process_load(0x22); break;
2619
        case tk_lwar:  process_load(0x23); break;
2620
                case tk_mark1: emit_insn(0xF8,0,5); break;
2621
                case tk_mark2: emit_insn(0xF9,0,5); break;
2622
                case tk_message: process_message(); break;
2623
        case tk_mod: process_rrop(0x1B); break;
2624
        case tk_modu: process_rrop(0x1C); break;
2625
        case tk_mov: process_mov(0x30); break;
2626
        case tk_mul: process_rrop(0x10); break;
2627
        case tk_muli: process_riop(0x10); break;
2628
        case tk_mulu: process_rrop(0x11); break;
2629
        case tk_mului: process_riop(0x11); break;
2630
        case tk_neg: process_neg(); break;
2631
        case tk_nop: emit_insn(0x1A,0,5); break;
2632
//        case tk_not: process_rop(0x07); break;
2633
        case tk_or:  process_rrop(0x09); break;
2634
        case tk_ori: process_riop(0x09); break;
2635
        case tk_org: process_org(); break;
2636
        case tk_plus: expand_flag = 1; break;
2637
                case tk_pea:    process_pea(0x9c); break;
2638
                case tk_pop:    process_push(0x71); break;
2639
        case tk_public: process_public(); break;
2640
                case tk_push:   process_push(0x70); break;
2641
        case tk_rodata:
2642
            if (first_rodata) {
2643
                while(sections[segment].address & 15)
2644
                    emitByte(0x00);
2645
                sections[1].address = sections[segment].address;
2646
                first_rodata = 0;
2647
                binstart = sections[1].index;
2648
                ca = sections[1].address;
2649
            }
2650
            segment = rodataseg;
2651
            break;
2652
                case tk_ret: process_ret(); break;
2653
                case tk_rol: process_shift(0x34); break;
2654
                case tk_roli: process_shifti(0x44); break;
2655
                case tk_ror: process_shift(0x35); break;
2656
                case tk_rori: process_shifti(0x45); break;
2657
        case tk_sei: emit_insn(0xE9,0,5); break;
2658
                case tk_seq:    process_riop(0x76); break;
2659
                case tk_sf:      process_lsfloat(0x27); break;
2660
                case tk_sge:    process_riop(0x79); break;
2661
                case tk_sgeu:   process_riop(0x7D); break;
2662
                case tk_sgt:    process_riop(0x7B); break;
2663
                case tk_sgtu:   process_riop(0x7F); break;
2664
        //case tk_slti:  process_riop(0x13,0x02); break;
2665
        //case tk_sltui:  process_riop(0x13,0x03); break;
2666
        case tk_shl: process_shift(0x30); break;
2667
        case tk_shli: process_shifti(0x40); break;
2668
                case tk_shr: process_shift(0x33); break;
2669
                case tk_shru: process_shift(0x31); break;
2670
                case tk_shrui: process_shifti(0x41); break;
2671
        case tk_slli: process_shifti(0x40); break;
2672
                case tk_sle:    process_riop(0x7A); break;
2673
                case tk_sleu:   process_riop(0x7E); break;
2674
                case tk_slt:    process_riop(0x78); break;
2675
                case tk_sltu:   process_riop(0x7C); break;
2676
                case tk_sne:    process_riop(0x77); break;
2677
        case tk_srai: process_shifti(0x43); break;
2678
        case tk_srli: process_shifti(0x41); break;
2679
        case tk_stb: process_store(0x90); break;
2680
        case tk_stw: process_store(0x91); break;
2681
        case tk_stp: process_store(0x92); break;
2682
        case tk_std: process_store(0x93); break;
2683
        case tk_stdcr: process_store(0x95); break;
2684
        case tk_stt: process_store(0x94); break;
2685
        case tk_sub:  process_rrop(0x07); break;
2686
        case tk_subi:  process_riop(0x07); break;
2687
//        case tk_sub:  process_sub(); break;
2688
        case tk_sxb: process_rop(0x16); break;
2689
        case tk_sxc: process_rop(0x17); break;
2690
        case tk_sxh: process_rop(0x17); break;
2691
        case tk_swap: process_rop(0x03); break;
2692
        case tk_sync: process_sync(0x77); break;
2693
                case tk_tgt:    emit_insn(0x53,0,5); break;
2694
        case tk_xor: process_rrop(0x0A); break;
2695
        case tk_xori: process_riop(0x0A); break;
2696
        case tk_id:  process_label(); break;
2697
        case '-': compress_flag = 1; break;
2698
        }
2699
        NextToken();
2700
    }
2701
j1:
2702
    ;
2703
}
2704
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.