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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [Open8 Tools/] [open8_src/] [open8_as/] [parse.c] - Blame information for rev 329

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 178 jshamlet
 
2
#include <ctype.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
 
7
#include "defines.h"
8
 
9
#include "parse.h"
10
#include "pass_1.h"
11
#include "stack.h"
12
 
13
 
14
int input_number_error_msg = YES, ss, string_size, input_float_mode = OFF, parse_floats = YES;
15
int newline_beginning = ON;
16
char label[MAX_NAME_LENGTH], xyz[256];
17
char unevaluated_expression[256];
18
char expanded_macro_string[256];
19
double flo;
20
 
21
extern int i, size, d, macro_active;
22
extern char *buffer, tmp[4096], cp[256];
23
extern struct active_file_info *active_file_info_first, *active_file_info_last, *active_file_info_tmp;
24
extern struct definition *defines, *tmp_def, *next_def;
25
extern struct macro_runtime *macro_runtime_current;
26
extern int latest_stack;
27
 
28
int operand_hint;
29
 
30
 
31
int compare_next_token(char *in, int s) {
32
 
33
  int a, t, d, k;
34
  char e;
35
 
36
 
37
  a = i;
38
  for (e = buffer[a]; e == ' ' || e == ',' || e == 0x0A; e = buffer[++a]);
39
 
40
  /* MACRO mode? */
41
  if (macro_active != 0 && e == '\\') {
42
    for (d = 0, k = 0; k < 16; k++) {
43
      e = buffer[++a];
44
      if (e >= '0' && e <= '9')
45
        d = (d * 10) + e - '0';
46
      else
47
        break;
48
    }
49
 
50
    if (d > macro_runtime_current->supplied_arguments) {
51
      if (input_number_error_msg == YES) {
52
        sprintf(xyz, "COMPARE_NEXT_SYMBOL: Macro \"%s\" wasn't called with enough arguments.\n", macro_runtime_current->macro->name);
53
        print_error(xyz, ERROR_NONE);
54
      }
55
      return FAILED;
56
    }
57
 
58
    a = macro_runtime_current->argument_data[d - 1]->start;
59
 
60
    e = buffer[a];
61
    for (t = 0; t < s && e != ' ' && e != ',' && e != 0x0A; ) {
62
      if (toupper(in[t]) != toupper(e))
63
        return FAILED;
64
      t++;
65
      e = buffer[++a];
66
    }
67
  }
68
  /* not in MACRO mode */
69
  else {
70
    for (t = 0; t < s && e != ' ' && e != ',' && e != 0x0A; ) {
71
      if (toupper(in[t]) != toupper(e))
72
        return FAILED;
73
      t++;
74
      e = buffer[++a];
75
    }
76
  }
77
 
78
  if (t == s)
79
    return SUCCEEDED;
80
  else
81
    return FAILED;
82
}
83
 
84
 
85
int input_next_string(void) {
86
 
87
  char e;
88
  int k, d;
89
 
90
 
91
  for (e = buffer[i++]; e == ' ' || e == ','; e = buffer[i++]);
92
 
93
  if (e == 0x0A)
94
    return INPUT_NUMBER_EOL;
95
 
96
  /* last choice is a label */
97
  tmp[0] = e;
98
  for (k = 1; k < MAX_NAME_LENGTH - 1; k++) {
99
    e = buffer[i++];
100
    if (e == 0x0A || e == ',') {
101
      i--;
102
      break;
103
    }
104
    else if (e == ' ')
105
      break;
106
    tmp[k] = e;
107
  }
108
 
109
  if (k == MAX_NAME_LENGTH - 1) {
110
    if (input_number_error_msg == YES) {
111
      sprintf(xyz, "The string is too long (max %d bytes).\n", MAX_NAME_LENGTH - 1);
112
      print_error(xyz, ERROR_NUM);
113
    }
114
    return FAILED;
115
  }
116
 
117
  tmp[k] = 0;
118
 
119
  /* expand eg. \1 and \@ */
120
  if (macro_active != 0) {
121
    d = 0;
122
    if (expand_macro_arguments(tmp, &d) == FAILED)
123
      return FAILED;
124
    if (d != 0)
125
      strcpy(tmp, expanded_macro_string);
126
  }
127
 
128
  return SUCCEEDED;
129
}
130
 
131
 
132
int input_number(void) {
133
 
134
  unsigned char e, ee;
135
  int k, p, q;
136
  double dn;
137
 
138
 
139
  operand_hint = HINT_NONE;
140
 
141
  for (e = buffer[i++]; e == ' ' || e == ','; e = buffer[i++]);
142
 
143
  if (e == 0x0A)
144
    return INPUT_NUMBER_EOL;
145
 
146
  /* check the type of the expression */
147
  p = i;
148
  ee = e;
149
  while (ee != 0x0A) {
150
    /* string / symbol -> no calculating */
151
    if (ee == '"' || ee == ',' || (ee == '=' && buffer[p] == '=') ||
152
        (ee == '!' && buffer[p] == '='))
153
      break;
154
    if (ee == '-' || ee == '+' || ee == '*' || ee == '/' || ee == '&' || ee == '|' || ee == '^' ||
155
        ee == '<' || ee == '>' || ee == '#' || ee == '~') {
156
      /* launch stack calculator */
157
      p = stack_calculate(&buffer[i - 1], &d);
158
      if (p == STACK_CALCULATE_DELAY)
159
        break;
160
      else if (p == STACK_RETURN_LABEL)
161
        return INPUT_NUMBER_ADDRESS_LABEL;
162
      else
163
        return p;
164
    }
165
    ee = buffer[p];
166
    p++;
167
  }
168
 
169
  /* MACRO */
170
  if (macro_active != 0 && e == '\\') {
171
 
172
    struct macro_argument *ma;
173
 
174
 
175
    if (buffer[i] == '@') {
176
      i++;
177
      d = macro_runtime_current->macro->calls - 1;
178
      return SUCCEEDED;
179
    }
180
 
181
    for (d = 0, k = 0; k < 4; k++) {
182
      e = buffer[i++];
183
      if (e >= '0' && e <= '9')
184
        d = (d * 10) + (e - '0');
185
      else {
186
        i--;
187
        break;
188
      }
189
    }
190
 
191
    if (d > macro_runtime_current->supplied_arguments) {
192
      sprintf(xyz, "Referencing argument number %d inside macro \"%s\". The macro has only %d arguments.\n", d, macro_runtime_current->macro->name, macro_runtime_current->supplied_arguments);
193
      print_error(xyz, ERROR_NUM);
194
      return FAILED;
195
    }
196
 
197
    /* return the macro argument */
198
    ma = macro_runtime_current->argument_data[d - 1];
199
    k = ma->type;
200
 
201
    if (k == INPUT_NUMBER_ADDRESS_LABEL)
202
      strcpy(label, ma->string);
203
    else if (k == INPUT_NUMBER_STRING) {
204
      strcpy(label, ma->string);
205
      string_size = strlen(ma->string);
206
    }
207
    else if (k == INPUT_NUMBER_STACK)
208
      latest_stack = ma->value;
209
    else if (k == SUCCEEDED)
210
      d = ma->value;
211
    else {
212
      print_error("Macro argument list has been corrupted! Please send a bug report!\n", ERROR_ERR);
213
      return FAILED;
214
    }
215
 
216
    return k;
217
  }
218
 
219
  /* is it a hexadecimal value? */
220
  d = 0;
221
  if (e >= '0' && e <= '9') {
222
    for (k = 0; 1; k++) {
223
      if (buffer[i+k] >= '0' && buffer[i+k] <= '9')
224
        continue;
225
      if (buffer[i+k] >= 'a' && buffer[i+k] <= 'f')
226
        continue;
227
      if (buffer[i+k] >= 'A' && buffer[i+k] <= 'F')
228
        continue;
229
      if (buffer[i+k] == 'h' || buffer[i+k] == 'H') {
230
        d = 1;
231
        break;
232
      }
233
      break;
234
    }
235
  }
236
 
237
  if (e == '$' || d == 1) {
238
    if (d == 1)
239
      i--;
240
    for (d = 0, k = 0; k < 8; k++, i++) {
241
      e = buffer[i];
242
      if (e >= '0' && e <= '9')
243
        d = (d << 4) + e - '0';
244
      else if (e >= 'A' && e <= 'F')
245
        d = (d << 4) + e - 'A' + 10;
246
      else if (e >= 'a' && e <= 'f')
247
        d = (d << 4) + e - 'a' + 10;
248
      else if (e == 'h' || e == 'H') {
249
        i++;
250
        e = buffer[i];
251
        break;
252
      }
253
      else
254
        break;
255
    }
256
 
257
    if (e == '.') {
258
      e = buffer[i+1];
259
      if (e == 'b' || e == 'B') {
260
        operand_hint = HINT_8BIT;
261
        i += 2;
262
      }
263
      else if (e == 'w' || e == 'W') {
264
        operand_hint = HINT_16BIT;
265
        i += 2;
266
      }
267
    }
268
 
269
    return SUCCEEDED;
270
  }
271
 
272
  if (e >= '0' && e <= '9') {
273
    q = 0;
274
    flo = e-'0';
275
    dn = 0.1;
276
    for (k = 0; k < 9; k++, i++) {
277
      e = buffer[i];
278
      if (e >= '0' && e <= '9') {
279
        if (q == 0) {
280
          /* still parsing an integer */
281
          flo = flo*10 + e-'0';
282
        }
283
        else {
284
          flo = flo + dn*(e-'0');
285
          dn /= 10.0;
286
        }
287
      }
288
      else if (e == '.') {
289
        if (q == 1) {
290
          print_error("Syntax error.\n", ERROR_NUM);
291
          return FAILED;
292
        }
293
        e = buffer[i+1];
294
        if (e >= '0' && e <= '9') {
295
          /* float mode, read decimals */
296
          if (parse_floats == NO)
297
            break;
298
          q = 1;
299
        }
300
        else if (e == 'b' || e == 'B') {
301
          operand_hint = HINT_8BIT;
302
          i += 2;
303
          break;
304
        }
305
        else if (e == 'w' || e == 'W') {
306
          operand_hint = HINT_16BIT;
307
          i += 2;
308
          break;
309
        }
310
      }
311
      else
312
        break;
313
    }
314
 
315
    /* drop the decimals */
316
    d = flo;
317
 
318
    if (q == 1 && input_float_mode == ON)
319
      return INPUT_NUMBER_FLOAT;
320
 
321
    return SUCCEEDED;
322
  }
323
 
324
  if (e == '%') {
325
    for (d = 0, k = 0; k < 32; k++, i++) {
326
      e = buffer[i];
327
      if (e == '0' || e == '1')
328
        d = (d << 1) + e - '0';
329
      else
330
        break;
331
    }
332
 
333
    if (e == '.') {
334
      e = buffer[i+1];
335
      if (e == 'b' || e == 'B') {
336
        operand_hint = HINT_8BIT;
337
        i += 2;
338
      }
339
      else if (e == 'w' || e == 'W') {
340
        operand_hint = HINT_16BIT;
341
        i += 2;
342
      }
343
    }
344
 
345
    return SUCCEEDED;
346
  }
347
 
348
  if (e == '\'') {
349
    d = buffer[i++];
350
    e = buffer[i];
351
    if (e != '\'') {
352
      if (input_number_error_msg == YES) {
353
        sprintf(xyz, "Got '%c' (%d) when expected \"'\".\n", e, e);
354
        print_error(xyz, ERROR_NUM);
355
      }
356
      return FAILED;
357
    }
358
    i++;
359
 
360
    return SUCCEEDED;
361
  }
362
 
363
  if (e == '"') {
364
 
365
    for (k = 0; k < MAX_NAME_LENGTH - 1; ) {
366
      e = buffer[i++];
367
 
368
      if (e == '\\' && buffer[i] == '"') {
369
        label[k++] = '"';
370
        i++;
371
        continue;
372
      }
373
 
374
      if (e == '"')
375
        break;
376
 
377
      if (e == 0 || e == 0x0A) {
378
        print_error("String wasn't terminated properly.\n", ERROR_NUM);
379
        return FAILED;
380
      }
381
 
382
      label[k++] = e;
383
    }
384
 
385
    label[k] = 0;
386
 
387
    /* expand eg. \1 and \@ */
388
    if (macro_active != 0) {
389
      d = 0;
390
      if (expand_macro_arguments(label, &d) == FAILED)
391
        return FAILED;
392
      if (d != 0) {
393
        strcpy(label, expanded_macro_string);
394
        k = strlen(label);
395
      }
396
    }
397
 
398
    if (k == MAX_NAME_LENGTH - 1) {
399
      if (input_number_error_msg == YES) {
400
        sprintf(xyz, "The string is too long (max %d bytes).\n", MAX_NAME_LENGTH - 1);
401
        print_error(xyz, ERROR_NUM);
402
      }
403
      return FAILED;
404
    }
405
 
406
    label[k] = 0;
407
    string_size = k;
408
    return INPUT_NUMBER_STRING;
409
  }
410
 
411
  /* last choice is a label */
412
  label[0] = e;
413
  for (k = 1; k < MAX_NAME_LENGTH - 1; k++) {
414
    e = buffer[i++];
415
    if (e == 0x0A || e == ')' || e == ',' || e == ']') {
416
      i--;
417
      break;
418
    }
419
    else if (e == ' ')
420
      break;
421
    label[k] = e;
422
  }
423
 
424
  if (k == MAX_NAME_LENGTH - 1) {
425
    if (input_number_error_msg == YES) {
426
      sprintf(xyz, "The label is too long (max %d bytes).\n", MAX_NAME_LENGTH - 1);
427
      print_error(xyz, ERROR_NUM);
428
    }
429
    return FAILED;
430
  }
431
 
432
  /* size hint? */
433
  if (label[k-2] == '.') {
434
    if (label[k-1] == 'b' || label[k-1] == 'B') {
435
      operand_hint = HINT_8BIT;
436
      k -= 2;
437
    }
438
    else if (label[k-1] == 'w' || label[k-1] == 'W') {
439
      operand_hint = HINT_16BIT;
440
      k -= 2;
441
    }
442
  }
443
 
444
  label[k] = 0;
445
 
446
  /* expand eg. \1 and \@ */
447
  if (macro_active != 0) {
448
    d = 0;
449
    if (expand_macro_arguments(label, &d) == FAILED)
450
      return FAILED;
451
    if (d != 0)
452
      strcpy(label, expanded_macro_string);
453
  }
454
 
455
  /* check if the label is actually a definition */
456
  tmp_def = defines;
457
  while (tmp_def != NULL) {
458
    if (strcmp(label, tmp_def->alias) == 0) {
459
      if (tmp_def->type == DEFINITION_TYPE_VALUE) {
460
        d = tmp_def->value;
461
        return SUCCEEDED;
462
      }
463
      else if (tmp_def->type == DEFINITION_TYPE_STACK) {
464
        /* skip stack definitions -> use its name instead */
465
      }
466
      else {
467
        string_size = tmp_def->size;
468
        memcpy(label, tmp_def->string, string_size);
469
        label[string_size] = 0;
470
        return INPUT_NUMBER_STRING;
471
      }
472
    }
473
    tmp_def = tmp_def->next;
474
  }
475
 
476
  return INPUT_NUMBER_ADDRESS_LABEL;
477
}
478
 
479
 
480
int get_next_token(void) {
481
 
482
  int q;
483
 
484
 
485
  while (1) {
486
    if (i == size)
487
      break;
488
    if (buffer[i] == ' ') {
489
      i++;
490
      newline_beginning = OFF;
491
      continue;
492
    }
493
    if (buffer[i] == 0x0A) {
494
      i++;
495
      next_line();
496
      continue;
497
    }
498
    break;
499
  }
500
 
501
  if (buffer[i] == '"') {
502
    for (ss = 0, i++; buffer[i] != 0x0A && buffer[i] != '"'; ) {
503
      if (buffer[i] == '\\' && buffer[i + 1] == '"') {
504
        tmp[ss++] = '"';
505
        i += 2;
506
      }
507
      else
508
        tmp[ss++] = buffer[i++];
509
    }
510
 
511
    if (buffer[i] == 0x0A) {
512
      print_error("GET_NEXT_TOKEN: String wasn't terminated properly.\n", ERROR_NONE);
513
      return FAILED;
514
    }
515
    tmp[ss] = 0;
516
    i++;
517
 
518
    /* expand eg. \1 and \@ */
519
    if (macro_active != 0) {
520
      q = 0;
521
      if (expand_macro_arguments(tmp, &q) == FAILED)
522
        return FAILED;
523
      if (q != 0) {
524
        strcpy(tmp, expanded_macro_string);
525
        ss = strlen(tmp);
526
      }
527
    }
528
 
529
    return GET_NEXT_TOKEN_STRING;
530
  }
531
 
532
  if (buffer[i] == '.') {
533
    tmp[0] = '.';
534
    i++;
535
    for (ss = 1; buffer[i] != 0x0A && buffer[i] != ' ' && ss <= MAX_NAME_LENGTH; ) {
536
      tmp[ss] = buffer[i];
537
      cp[ss - 1] = toupper((int)buffer[i]);
538
      i++;
539
      ss++;
540
    }
541
    cp[ss - 1] = 0;
542
  }
543
  else if (buffer[i] == '=' || buffer[i] == '>' || buffer[i] == '<' || buffer[i] == '!') {
544
    for (ss = 0; buffer[i] != 0x0A && (buffer[i] == '=' || buffer[i] == '!' || buffer[i] == '<' || buffer[i] == '>')
545
           && ss <= MAX_NAME_LENGTH; tmp[ss++] = buffer[i++]);
546
  }
547
  else {
548
    for (ss = 0; buffer[i] != 0x0A && buffer[i] != ',' && buffer[i] != ' ' && ss <= MAX_NAME_LENGTH; ) {
549
      tmp[ss] = buffer[i];
550
      ss++;
551
      i++;
552
    }
553
    if (buffer[i] == ',')
554
      i++;
555
  }
556
 
557
  if (ss > MAX_NAME_LENGTH) {
558
    print_error("GET_NEXT_TOKEN: Too long for a token.\n", ERROR_NONE);
559
    return FAILED;
560
  }
561
 
562
  tmp[ss] = 0;
563
 
564
  /* expand eg. \1 and \@ */
565
  if (macro_active != 0) {
566
    q = 0;
567
    if (expand_macro_arguments(tmp, &q) == FAILED)
568
      return FAILED;
569
    if (q != 0) {
570
      strcpy(tmp, expanded_macro_string);
571
      ss = strlen(tmp);
572
    }
573
  }
574
 
575
  return SUCCEEDED;
576
}
577
 
578
 
579
int skip_next_token(void) {
580
 
581
  for (; buffer[i] == ' ' || buffer[i] == ','; i++);
582
 
583
  if (buffer[i] == 0x0A)
584
    return FAILED;
585
 
586
  if (buffer[i] == '"') {
587
    for (i++; buffer[i] != 0x0A && buffer[i] != '"'; i++);
588
    if (buffer[i] == 0x0A) {
589
      print_error("SKIP_NEXT_TOKEN: String wasn't terminated properly.\n", ERROR_NONE);
590
      return FAILED;
591
    }
592
    i++;
593
 
594
    return SUCCEEDED;
595
  }
596
 
597
  for (; buffer[i] != 0x0A && buffer[i] != ' ' && buffer[i] != ','; i++);
598
 
599
  return SUCCEEDED;
600
}
601
 
602
 
603
int expand_macro_arguments(char *in, int *s) {
604
 
605
  char t[256];
606
  int i, k, d;
607
 
608
 
609
  for (i = 0; i < MAX_NAME_LENGTH; i++) {
610
    if (in[i] == '\\') {
611
      if (in[i + 1] == '"' || in[i + 1] == 'n' || in[i + 1] == '\\') {
612
        expanded_macro_string[i] = in[i];
613
        i++;
614
        expanded_macro_string[i] = in[i];
615
        continue;
616
      }
617
      break;
618
    }
619
    expanded_macro_string[i] = in[i];
620
    if (in[i] == 0)
621
      return SUCCEEDED;
622
  }
623
 
624
  k = i;
625
  i++;
626
 
627
  (*s)++;
628
 
629
  if (in[i] == '@') {
630
    i++;
631
    sprintf(&expanded_macro_string[k], "%d%c", macro_runtime_current->macro->calls - 1, 0);
632
    k = strlen(expanded_macro_string);
633
 
634
    for (; i < MAX_NAME_LENGTH; i++, k++) {
635
      expanded_macro_string[k] = in[i];
636
      if (in[i] == 0) {
637
        break;
638
      }
639
    }
640
 
641
    strcpy(t, expanded_macro_string);
642
    expand_macro_arguments(t, &d);
643
    return SUCCEEDED;
644
  }
645
 
646
  if (in[i] <= '0' || in[i] >= '9') {
647
    if (input_number_error_msg == YES) {
648
      sprintf(xyz, "EXPAND_MACRO_ARGUMENTS: Unsupported special character '%c'.\n", in[i]);
649
      print_error(xyz, ERROR_NUM);
650
    }
651
    return FAILED;
652
  }
653
 
654
  for (d = 0; in[i] != 0; i++) {
655
    if (in[i] >= '0' && in[i] <= '9')
656
      d = (d * 10) + in[i] - '0';
657
    else
658
      break;
659
  }
660
 
661
  if (d > macro_runtime_current->supplied_arguments) {
662
    if (input_number_error_msg == YES) {
663
      sprintf(xyz, "Macro \"%s\" wasn't called with enough arguments.\n", macro_runtime_current->macro->name);
664
      print_error(xyz, ERROR_NUM);
665
    }
666
    return FAILED;
667
  }
668
 
669
  d = macro_runtime_current->argument_data[d - 1]->start;
670
 
671
  for (; k < MAX_NAME_LENGTH; d++, k++) {
672
    if (buffer[d] == 0 || buffer[d] == ' ' || buffer[d] == 0x0A || buffer[d] == ',')
673
      break;
674
    expanded_macro_string[k] = buffer[d];
675
  }
676
 
677
  for (; k < MAX_NAME_LENGTH; i++, k++) {
678
    expanded_macro_string[k] = in[i];
679
    if (in[i] == 0) {
680
      break;
681
    }
682
  }
683
 
684
  strcpy(t, expanded_macro_string);
685
  expand_macro_arguments(t, &d);
686
 
687
  return SUCCEEDED;
688
}

powered by: WebSVN 2.1.0

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