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

Subversion Repositories open8_urisc

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

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

Line No. Rev Author Line
1 178 jshamlet
#include <ctype.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
 
6
#include "defines.h"
7
 
8
#include "parse.h"
9
#include "pass_1.h"
10
#include "stack.h"
11
#include "include_file.h"
12
 
13
 
14
extern int input_number_error_msg, bankheader_status, input_float_mode;
15
extern int i, size, d, macro_active, string_size, section_status, parse_floats;
16
extern char xyz[256], *buffer, tmp[4096], expanded_macro_string[256], label[MAX_NAME_LENGTH];
17
extern struct definition *defines, *tmp_def, *next_def;
18
extern struct active_file_info *active_file_info_first, *active_file_info_last, *active_file_info_tmp;
19
extern struct macro_runtime *macro_runtime_current;
20
extern struct section_def *sec_tmp;
21
extern double flo;
22
 
23
int latest_stack = 0, stacks_inside = 0, stacks_outside = 0, stack_id = 0;
24
struct stack *stacks_first = NULL, *stacks_tmp = NULL, *stacks_last = NULL;
25
struct stack *stacks_header_first = NULL, *stacks_header_last = NULL;
26
 
27
extern int stack_inserted;
28
 
29
extern int operand_hint;
30
 
31
static int _stack_insert(void) {
32
 
33
  /* outside bankheader sections */
34
  if (bankheader_status == OFF) {
35
    if (stacks_first == NULL) {
36
      stacks_first = stacks_tmp;
37
      stacks_last = stacks_tmp;
38
    }
39
    else {
40
      stacks_last->next = stacks_tmp;
41
      stacks_last = stacks_tmp;
42
    }
43
    stacks_outside++;
44
 
45
    stack_inserted = STACK_OUTSIDE;
46
  }
47
  /* inside a bankheader section */
48
  else {
49
    if (stacks_header_first == NULL) {
50
      stacks_header_first = stacks_tmp;
51
      stacks_header_last = stacks_tmp;
52
    }
53
    else {
54
      stacks_header_last->next = stacks_tmp;
55
      stacks_header_last = stacks_tmp;
56
    }
57
    stacks_inside++;
58
 
59
    stack_inserted = STACK_INSIDE;
60
  }
61
 
62
  stacks_tmp->id = stack_id;
63
  stacks_tmp->section_status = section_status;
64
  if (section_status == ON)
65
    stacks_tmp->section_id = sec_tmp->id;
66
  else
67
    stacks_tmp->section_id = 0;
68
 
69
  latest_stack = stack_id;
70
  stack_id++;
71
 
72
  return SUCCEEDED;
73
}
74
 
75
 
76
int stack_calculate(char *in, int *value) {
77
 
78
  int q = 0, b = 0, d, k, op[256], n, o, l;
79
  struct stack_item si[256], ta[256];
80
  struct stack s;
81
  unsigned char e;
82
  double dou, dom;
83
 
84
 
85
  /* slice the data into infix format */
86
  while (*in != 0x0A) {
87
    /* init the stack item */
88
    si[q].type = 0x123456;
89
    si[q].sign = 0x123456;
90
    si[q].value = 0x123456;
91
    si[q].string[0] = 0;
92
 
93
    if (*in == ' ') {
94
      in++;
95
      continue;
96
    }
97
    else if (*in == '-') {
98
      if (*(in + 1) == '-') {
99
        si[q].type = STACK_ITEM_TYPE_STRING;
100
        si[q].sign = SI_SIGN_POSITIVE;
101
        for (k = 0; *in == '-' && k < 32; k++, in++) {
102
          si[q].string[k] = '-';
103
        }
104
        si[q].string[k] = 0;
105
      }
106
      else {
107
        si[q].type = STACK_ITEM_TYPE_OPERATOR;
108
        si[q].value = SI_OP_MINUS;
109
        in++;
110
      }
111
      q++;
112
    }
113
    else if (*in == '+') {
114
      if (*(in + 1) == '+') {
115
        si[q].type = STACK_ITEM_TYPE_STRING;
116
        si[q].sign = SI_SIGN_POSITIVE;
117
        for (k = 0; *in == '+' && k < 32; k++, in++)
118
          si[q].string[k] = '+';
119
        si[q].string[k] = 0;
120
      }
121
      else {
122
        si[q].type = STACK_ITEM_TYPE_OPERATOR;
123
        si[q].value = SI_OP_PLUS;
124
        in++;
125
      }
126
      q++;
127
    }
128
    else if (*in == '*') {
129
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
130
      si[q].value = SI_OP_MULTIPLY;
131
      q++;
132
      in++;
133
    }
134
    else if (*in == '/') {
135
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
136
      si[q].value = SI_OP_DIVIDE;
137
      q++;
138
      in++;
139
    }
140
    else if (*in == '|') {
141
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
142
      si[q].value = SI_OP_OR;
143
      q++;
144
      in++;
145
    }
146
    else if (*in == '&') {
147
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
148
      si[q].value = SI_OP_AND;
149
      q++;
150
      in++;
151
    }
152
    else if (*in == '^') {
153
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
154
      si[q].value = SI_OP_POWER;
155
      q++;
156
      in++;
157
    }
158
    else if (*in == '#') {
159
      if (q == 0) {
160
        if (input_number_error_msg == YES)
161
          print_error("Syntax error. Invalid use of modulo.\n", ERROR_STC);
162
        return FAILED;
163
      }
164
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
165
      si[q].value = SI_OP_MODULO;
166
      q++;
167
      in++;
168
    }
169
    else if (*in == '<' && *(in + 1) == '<') {
170
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
171
      si[q].value = SI_OP_SHIFT_LEFT;
172
      q++;
173
      in += 2;
174
    }
175
    else if (*in == '<') {
176
      /* should we end parsing here? */
177
      if (b == 0 && q > 0) {
178
        if ((si[q-1].type == STACK_ITEM_TYPE_OPERATOR && si[q-1].value == SI_OP_RIGHT) ||
179
            si[q-1].type == STACK_ITEM_TYPE_VALUE || si[q-1].type == STACK_ITEM_TYPE_STRING)
180
          break;
181
      }
182
 
183
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
184
      si[q].value = SI_OP_LOW_BYTE;
185
      q++;
186
      in++;
187
    }
188
    else if (*in == '>' && *(in + 1) == '>') {
189
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
190
      si[q].value = SI_OP_SHIFT_RIGHT;
191
      q++;
192
      in += 2;
193
    }
194
    else if (*in == '>') {
195
      /* should we end parsing here? */
196
      if (b == 0 && q > 0) {
197
        if ((si[q-1].type == STACK_ITEM_TYPE_OPERATOR && si[q-1].value == SI_OP_RIGHT) ||
198
            si[q-1].type == STACK_ITEM_TYPE_VALUE || si[q-1].type == STACK_ITEM_TYPE_STRING)
199
          break;
200
      }
201
 
202
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
203
      si[q].value = SI_OP_HIGH_BYTE;
204
      q++;
205
      in++;
206
    }
207
    else if (*in == '~') {
208
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
209
      si[q].value = SI_OP_XOR;
210
      q++;
211
      in++;
212
    }
213
    else if (*in == '=' && *(in + 1) == '=')
214
      break;
215
    else if (*in == '!' && *(in + 1) == '=')
216
      break;
217
    else if (*in == '(') {
218
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
219
      si[q].value = SI_OP_LEFT;
220
      q++;
221
      b++;
222
      in++;
223
    }
224
    else if (*in == '.') {
225
      in++;
226
      d = operand_hint;
227
      if (*in == 'b' || *in == 'B') {
228
        operand_hint = HINT_8BIT;
229
        in++;
230
      }
231
      else if (*in == 'w' || *in == 'W') {
232
        operand_hint = HINT_16BIT;
233
        in++;
234
      }
235
      else
236
        break;
237
 
238
      if (d != HINT_NONE && d != operand_hint) {
239
        print_error("Confusing operand hint!\n", ERROR_STC);
240
        in++;
241
      }
242
    }
243
    else if (*in == ')') {
244
      si[q].type = STACK_ITEM_TYPE_OPERATOR;
245
      si[q].value = SI_OP_RIGHT;
246
      /* end of expression? */
247
      if (b == 0)
248
        break;
249
      b--;
250
      q++;
251
      in++;
252
    }
253
    else if (*in == ',')
254
      break;
255
    else if (*in == ']')
256
      break;
257
    else if (*in == '%') {
258
      d = 0;
259
      for (k = 0; k < 31; k++, d = d<<1) {
260
        in++;
261
        e = *in;
262
        if (e == '0' || e == '1')
263
          d += e - '0';
264
        else if (e == ' ' || e == ')' || e == '|' || e == '&' || e == '+' || e == '-' || e == '*' ||
265
                 e == '/' || e == ',' || e == '^' || e == '<' || e == '>' || e == '#' || e == '~' ||
266
                 e == ']' || e == '.' || e == 0x0A)
267
          break;
268
        else {
269
          if (input_number_error_msg == YES) {
270
            sprintf(xyz, "Got '%c' (%d) when expected (0/1).\n", e, e);
271
            print_error(xyz, ERROR_NUM);
272
          }
273
          return FAILED;
274
        }
275
      }
276
 
277
      d = d>>1;
278
 
279
      si[q].type = STACK_ITEM_TYPE_VALUE;
280
      si[q].value = d;
281
      si[q].sign = SI_SIGN_POSITIVE;
282
      q++;
283
    }
284
    else if (*in == '\'') {
285
      in++;
286
      d = *in;
287
      in++;
288
      if (*in != '\'') {
289
        sprintf(xyz, "Got '%c' (%d) when expected \"'\".\n", *in, *in);
290
        print_error(xyz, ERROR_NUM);
291
        return FAILED;
292
      }
293
      in++;
294
 
295
      si[q].type = STACK_ITEM_TYPE_VALUE;
296
      si[q].value = d;
297
      si[q].sign = SI_SIGN_POSITIVE;
298
      q++;
299
    }
300
    else if (*in == '$') {
301
      d = 0;
302
      for (k = 0; k < 8; k++, d = d << 4) {
303
        in++;
304
        e = *in;
305
        if (e >= '0' && e <= '9')
306
          d += e - '0';
307
        else if (e >= 'A' && e <= 'F')
308
          d += e - 'A' + 10;
309
        else if (e >= 'a' && e <= 'f')
310
          d += e - 'a' + 10;
311
        else if (e == ' ' || e == ')' || e == '|' || e == '&' || e == '+' || e == '-' ||
312
                 e == '*' || e == '/' || e == ',' || e == '^' || e == '<' || e == '>' ||
313
                 e == '#' || e == '~' || e == ']' || e == '.' || e == 0x0A)
314
          break;
315
        else {
316
          if (input_number_error_msg == YES) {
317
            sprintf(xyz, "Got '%c' (%d) when expected (0-F).\n", e, e);
318
            print_error(xyz, ERROR_NUM);
319
          }
320
          return FAILED;
321
        }
322
      }
323
 
324
      d = d >> 4;
325
 
326
      si[q].type = STACK_ITEM_TYPE_VALUE;
327
      si[q].value = d;
328
      si[q].sign = SI_SIGN_POSITIVE;
329
      q++;
330
    }
331
    else if (*in >= '0' && *in <= '9') {
332
      /* is it a hexadecimal value after all? */
333
      n = 0;
334
      for (k = 0; k < 9; k++) {
335
        if (in[k] >= '0' && in[k] <= '9')
336
          continue;
337
        if (in[k] >= 'a' && in[k] <= 'f') {
338
          n = 1;
339
          break;
340
        }
341
        if (in[k] >= 'A' && in[k] <= 'F') {
342
          n = 1;
343
          break;
344
        }
345
        if (in[k] == 'h' || in[k] == 'H') {
346
          n = 1;
347
          break;
348
        }
349
        break;
350
      }
351
 
352
      if (n == 1) {
353
        /* it's hex */
354
        d = 0;
355
        for (k = 0; k < 8; k++, d = d << 4) {
356
          e = *(in++);
357
          if (e >= '0' && e <= '9')
358
            d += e - '0';
359
          else if (e >= 'A' && e <= 'F')
360
            d += e - 'A' + 10;
361
          else if (e >= 'a' && e <= 'f')
362
            d += e - 'a' + 10;
363
          else if (e == ' ' || e == ')' || e == '|' || e == '&' || e == '+' || e == '-' ||
364
                   e == '*' || e == '/' || e == ',' || e == '^' || e == '<' || e == '>' ||
365
                   e == '#' || e == '~' || e == ']' || e == '.' || e == 'h' || e == 'H' || e == 0x0A)
366
            break;
367
          else {
368
            if (input_number_error_msg == YES) {
369
              sprintf(xyz, "Got '%c' (%d) when expected (0-F).\n", e, e);
370
              print_error(xyz, ERROR_NUM);
371
            }
372
            return FAILED;
373
          }
374
        }
375
 
376
        d = d >> 4;
377
 
378
        si[q].type = STACK_ITEM_TYPE_VALUE;
379
        si[q].value = d;
380
        si[q].sign = SI_SIGN_POSITIVE;
381
        q++;
382
      }
383
      else {
384
        /* it's decimal */
385
        dou = (*in - '0')*10.0;
386
        dom = 1.0;
387
        n = 0;
388
        for (k = 0; k < 9; k++) {
389
          in++;
390
          e = *in;
391
          if (e >= '0' && e <= '9') {
392
            if (n == 0) {
393
              dou += e - '0';
394
              dou *= 10.0;
395
            }
396
            else if (n == 1) {
397
              dou += dom*(e - '0');
398
              dom /= 10.0;
399
            }
400
          }
401
          else if (e == ' ' || e == ')' || e == '|' || e == '&' || e == '+' || e == '-' || e == '*' ||
402
                   e == '/' || e == ',' || e == '^' || e == '<' || e == '>' || e == '#' || e == '~' ||
403
                   e == ']' || e == 0x0A)
404
            break;
405
          else if (e == '.') {
406
            if (*(in+1) == 'b' || *(in+1) == 'B' || *(in+1) == 'w' || *(in+1) == 'W')
407
              break;
408
            if (parse_floats == NO)
409
              break;
410
            if (n == 1) {
411
              if (input_number_error_msg == YES)
412
                print_error("Syntax error.\n", ERROR_NUM);
413
              return FAILED;
414
            }
415
            n = 1;
416
          }
417
          else {
418
            if (input_number_error_msg == YES) {
419
              sprintf(xyz, "Got '%c' (%d) when expected (0-9).\n", e, e);
420
              print_error(xyz, ERROR_NUM);
421
            }
422
            return FAILED;
423
          }
424
        }
425
 
426
        dou /= 10;
427
 
428
        si[q].type = STACK_ITEM_TYPE_VALUE;
429
        si[q].value = dou;
430
        si[q].sign = SI_SIGN_POSITIVE;
431
        q++;
432
      }
433
    }
434
    else {
435
      /* it must be a string! */
436
      si[q].sign = SI_SIGN_POSITIVE;
437
      for (k = 0; k < 63; k++) {
438
        e = *in;
439
        if (e == ' ' || e == ')' || e == '|' || e == '&' || e == '+' || e == '-' || e == '*' ||
440
            e == '/' || e == ',' || e == '^' || e == '<' || e == '>' || e == '#' || e == ']' ||
441
            e == '~' || e == 0xA)
442
          break;
443
        if (e == '.' && (*(in+1) == 'b' || *(in+1) == 'B' || *(in+1) == 'w' || *(in+1) == 'W' || *(in+1) == 'l' || *(in+1) == 'L') &&
444
            (*(in+2) == ' ' || *(in+2) == ')' || *(in+2) == '|' || *(in+2) == '&' || *(in+2) == '+' || *(in+2) == '-' || *(in+2) == '*' ||
445
             *(in+2) == '/' || *(in+2) == ',' || *(in+2) == '^' || *(in+2) == '<' || *(in+2) == '>' || *(in+2) == '#' || *(in+2) == ']' ||
446
             *(in+2) == '~' || *(in+2) == 0xA))
447
          break;
448
        si[q].string[k] = e;
449
        in++;
450
      }
451
      si[q].string[k] = 0;
452
 
453
      si[q].type = STACK_ITEM_TYPE_STRING;
454
      q++;
455
    }
456
 
457
    if (q == 255) {
458
      print_error("Out of stack space.\n", ERROR_STC);
459
      return FAILED;
460
    }
461
  }
462
 
463
  if (b != 0) {
464
    print_error("Unbalanced parentheses.\n", ERROR_STC);
465
    return FAILED;
466
  }
467
 
468
  /* only one item found -> let the item parser handle it */
469
  if (q == 1)
470
    return STACK_CALCULATE_DELAY;
471
 
472
  /* check if there was data before the computation */
473
  if (q > 1 && (si[0].type == STACK_ITEM_TYPE_STRING || si[0].type == STACK_ITEM_TYPE_VALUE)) {
474
    if (si[1].type == STACK_ITEM_TYPE_STRING || si[1].type == STACK_ITEM_TYPE_VALUE)
475
      return STACK_CALCULATE_DELAY;
476
    if (si[1].type == STACK_ITEM_TYPE_OPERATOR) {
477
      if (si[1].value == SI_OP_LEFT)
478
        return STACK_CALCULATE_DELAY;
479
    }
480
  }
481
 
482
  /* check if the computation is of the form "+-..." and remove that leading "+" */
483
  if (q > 2 && si[0].type == STACK_ITEM_TYPE_OPERATOR && si[0].value == SI_OP_PLUS &&
484
      si[1].type == STACK_ITEM_TYPE_OPERATOR && si[1].value == SI_OP_MINUS) {
485
    si[0].type = STACK_ITEM_TYPE_DELETED;
486
  }
487
 
488
  /* update the source pointer */
489
  i = in - buffer;
490
 
491
  /* fix the sign in every operand */
492
  for (b = 1, k = 0; k < q; k++) {
493
    if ((q - k) != 1 && si[k].type == STACK_ITEM_TYPE_OPERATOR && si[k + 1].type == STACK_ITEM_TYPE_OPERATOR) {
494
      if (si[k].value != SI_OP_LEFT && si[k].value != SI_OP_RIGHT && si[k + 1].value != SI_OP_LEFT && si[k + 1].value != SI_OP_RIGHT) {
495
        print_error("Error in computation syntax.\n", ERROR_STC);
496
        return FAILED;
497
      }
498
    }
499
    if (si[k].type == STACK_ITEM_TYPE_OPERATOR && si[k].value == SI_OP_MINUS && b == 1) {
500
      if (si[k + 1].type == STACK_ITEM_TYPE_VALUE || si[k + 1].type == STACK_ITEM_TYPE_STRING) {
501
        if (si[k + 1].sign == SI_SIGN_POSITIVE)
502
          si[k + 1].sign = SI_SIGN_NEGATIVE;
503
        else
504
          si[k + 1].sign = SI_SIGN_POSITIVE;
505
        /* it wasn't a minus operator, it was a sign */
506
        si[k].type = STACK_ITEM_TYPE_DELETED;
507
      }
508
      else if (si[k + 1].type == STACK_ITEM_TYPE_OPERATOR && si[k + 1].value == SI_OP_LEFT) {
509
        o = 1;
510
        l = k + 2;
511
        while (o > 0 && l < q) {
512
          if (si[l].type == STACK_ITEM_TYPE_VALUE || si[l].type == STACK_ITEM_TYPE_STRING) {
513
            if (si[l].sign == SI_SIGN_POSITIVE)
514
              si[l].sign = SI_SIGN_NEGATIVE;
515
            else
516
              si[l].sign = SI_SIGN_POSITIVE;
517
          }
518
          else if (si[l].type == STACK_ITEM_TYPE_OPERATOR) {
519
            if (si[l].value == SI_OP_LEFT)
520
              o++;
521
            else if (si[l].value == SI_OP_RIGHT)
522
              o--;
523
          }
524
          l++;
525
        }
526
 
527
        if (o != 0) {
528
          print_error("Unbalanced parentheses.\n", ERROR_STC);
529
          return FAILED;
530
        }
531
 
532
        si[k].type = STACK_ITEM_TYPE_DELETED;
533
      }
534
    }
535
    /* remove unnecessary + */
536
    if (si[k].type == STACK_ITEM_TYPE_OPERATOR && si[k].value == SI_OP_PLUS && b == 1) {
537
      if (si[k + 1].type == STACK_ITEM_TYPE_VALUE || si[k + 1].type == STACK_ITEM_TYPE_STRING)
538
        si[k].type = STACK_ITEM_TYPE_DELETED;
539
      else if (si[k + 1].type == STACK_ITEM_TYPE_OPERATOR && si[k + 1].value == SI_OP_LEFT)
540
        si[k].type = STACK_ITEM_TYPE_DELETED;
541
    }
542
    else if (si[k].type == STACK_ITEM_TYPE_VALUE || si[k].type == STACK_ITEM_TYPE_STRING)
543
      b = 0;
544
    else if (si[k].type == STACK_ITEM_TYPE_OPERATOR && si[k].value == SI_OP_LEFT)
545
      b = 1;
546
  }
547
 
548
  /* convert infix stack into postfix stack */
549
  for (b = 0, k = 0, d = 0; k < q; k++) {
550
    /* operands pass through */
551
    if (si[k].type == STACK_ITEM_TYPE_VALUE) {
552
      ta[d].type = si[k].type;
553
      ta[d].value = si[k].value;
554
      ta[d].sign = si[k].sign;
555
      d++;
556
    }
557
    else if (si[k].type == STACK_ITEM_TYPE_STRING) {
558
      ta[d].type = si[k].type;
559
      strcpy(ta[d].string, si[k].string);
560
      ta[d].sign = si[k].sign;
561
      d++;
562
    }
563
    /* operators get inspected */
564
    else if (si[k].type == STACK_ITEM_TYPE_OPERATOR) {
565
      if (b == 0) {
566
        op[0] = si[k].value;
567
        b++;
568
      }
569
      else {
570
        if (si[k].value == SI_OP_PLUS) {
571
          b--;
572
          while (b != -1 && op[b] != SI_OP_LEFT) {
573
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
574
            ta[d].value = op[b];
575
            b--;
576
            d++;
577
          }
578
          b++;
579
          op[b] = SI_OP_PLUS;
580
          b++;
581
        }
582
        else if (si[k].value == SI_OP_MINUS) {
583
          b--;
584
          while (b != -1 && op[b] != SI_OP_LEFT) {
585
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
586
            ta[d].value = op[b];
587
            b--;
588
            d++;
589
          }
590
          b++;
591
          op[b] = SI_OP_MINUS;
592
          b++;
593
        }
594
        else if (si[k].value == SI_OP_LOW_BYTE) {
595
          b--;
596
          while (b != -1 && op[b] != SI_OP_LEFT) {
597
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
598
            ta[d].value = op[b];
599
            b--;
600
            d++;
601
          }
602
          b++;
603
          op[b] = SI_OP_LOW_BYTE;
604
          b++;
605
        }
606
        else if (si[k].value == SI_OP_HIGH_BYTE) {
607
          b--;
608
          while (b != -1 && op[b] != SI_OP_LEFT) {
609
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
610
            ta[d].value = op[b];
611
            b--;
612
            d++;
613
          }
614
          b++;
615
          op[b] = SI_OP_HIGH_BYTE;
616
          b++;
617
        }
618
        else if (si[k].value == SI_OP_XOR) {
619
          b--;
620
          while (b != -1 && op[b] != SI_OP_LEFT) {
621
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
622
            ta[d].value = op[b];
623
            b--;
624
            d++;
625
          }
626
          b++;
627
          op[b] = SI_OP_XOR;
628
          b++;
629
        }
630
        else if (si[k].value == SI_OP_MULTIPLY) {
631
          b--;
632
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
633
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
634
            ta[d].value = op[b];
635
            b--;
636
            d++;
637
          }
638
          b++;
639
          op[b] = SI_OP_MULTIPLY;
640
          b++;
641
        }
642
        else if (si[k].value == SI_OP_DIVIDE) {
643
          b--;
644
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
645
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
646
            ta[d].value = op[b];
647
            b--;
648
            d++;
649
          }
650
          b++;
651
          op[b] = SI_OP_DIVIDE;
652
          b++;
653
        }
654
        else if (si[k].value == SI_OP_MODULO) {
655
          b--;
656
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
657
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
658
            ta[d].value = op[b];
659
            b--;
660
            d++;
661
          }
662
          b++;
663
          op[b] = SI_OP_MODULO;
664
          b++;
665
        }
666
        else if (si[k].value == SI_OP_POWER) {
667
          b--;
668
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
669
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
670
            ta[d].value = op[b];
671
            b--;
672
            d++;
673
          }
674
          b++;
675
          op[b] = SI_OP_POWER;
676
          b++;
677
        }
678
        else if (si[k].value == SI_OP_SHIFT_LEFT) {
679
          b--;
680
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
681
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
682
            ta[d].value = op[b];
683
            b--;
684
            d++;
685
          }
686
          b++;
687
          op[b] = SI_OP_SHIFT_LEFT;
688
          b++;
689
        }
690
        else if (si[k].value == SI_OP_SHIFT_RIGHT) {
691
          b--;
692
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
693
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
694
            ta[d].value = op[b];
695
            b--;
696
            d++;
697
          }
698
          b++;
699
          op[b] = SI_OP_SHIFT_RIGHT;
700
          b++;
701
        }
702
        else if (si[k].value == SI_OP_AND) {
703
          b--;
704
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
705
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
706
            ta[d].value = op[b];
707
            b--;
708
            d++;
709
          }
710
          b++;
711
          op[b] = SI_OP_AND;
712
          b++;
713
        }
714
        else if (si[k].value == SI_OP_OR) {
715
          b--;
716
          while (b != -1 && op[b] != SI_OP_LEFT && op[b] != SI_OP_PLUS && op[b] != SI_OP_MINUS) {
717
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
718
            ta[d].value = op[b];
719
            b--;
720
            d++;
721
          }
722
          b++;
723
          op[b] = SI_OP_OR;
724
          b++;
725
        }
726
        else if (si[k].value == SI_OP_LEFT) {
727
          op[b] = SI_OP_LEFT;
728
          b++;
729
        }
730
        else if (si[k].value == SI_OP_RIGHT) {
731
          b--;
732
          while (op[b] != SI_OP_LEFT) {
733
            ta[d].type = STACK_ITEM_TYPE_OPERATOR;
734
            ta[d].value = op[b];
735
            b--;
736
            d++;
737
          }
738
        }
739
      }
740
    }
741
  }
742
 
743
  /* empty the operator stack */
744
  while (b > 0) {
745
    b--;
746
    ta[d].type = STACK_ITEM_TYPE_OPERATOR;
747
    ta[d].value = op[b];
748
    d++;
749
  }
750
 
751
  /* DEBUG output
752
  printf("STACK ID %d LINE %d\n", stack_id, active_file_info_last->line_current);
753
  for (k = 0; k < d; k++) {
754
    char ar[] = "+-*()|&/^«»%~<>";
755
 
756
    if (ta[k].type == STACK_ITEM_TYPE_OPERATOR)
757
      printf("%c ", ar[((int)ta[k].value)]);
758
    else if (ta[k].type == STACK_ITEM_TYPE_VALUE)
759
      printf("V(%f) ", ta[k].value);
760
    else
761
      printf("S(%s) ", ta[k].string);
762
  }
763
  printf("\n");
764
  */
765
 
766
  /* are all the symbols known? */
767
  if (resolve_stack(ta, d) == SUCCEEDED) {
768
    s.stack = ta;
769
    s.linenumber = active_file_info_last->line_current;
770
    s.filename_id = active_file_info_last->filename_id;
771
 
772
    if (compute_stack(&s, d, &dou) == FAILED)
773
      return FAILED;
774
 
775
    if (input_float_mode == ON) {
776
      flo = dou;
777
      return INPUT_NUMBER_FLOAT;
778
    }
779
 
780
    *value = (int)dou;
781
 
782
    return SUCCEEDED;
783
  }
784
 
785
  /* only one string? */
786
  if (d == 1 && ta[0].type == STACK_ITEM_TYPE_STRING) {
787
    strcpy(label, ta[0].string);
788
    return STACK_RETURN_LABEL;
789
  }
790
 
791
  /* we have a stack full of computation and we save it for wlalink */
792
  stacks_tmp = malloc(sizeof(struct stack));
793
  if (stacks_tmp == NULL) {
794
    print_error("Out of memory error while allocating room for a new stack.\n", ERROR_STC);
795
    return FAILED;
796
  }
797
  stacks_tmp->next = NULL;
798
  stacks_tmp->type = STACKS_TYPE_UNKNOWN;
799
  stacks_tmp->bank = -123456;
800
  stacks_tmp->stacksize = d;
801
  stacks_tmp->stack = malloc(sizeof(struct stack_item) * d);
802
  if (stacks_tmp->stack == NULL) {
803
    free(stacks_tmp);
804
    print_error("Out of memory error while allocating room for a new stack.\n", ERROR_STC);
805
    return FAILED;
806
  }
807
 
808
  stacks_tmp->linenumber = active_file_info_last->line_current;
809
  stacks_tmp->filename_id = active_file_info_last->filename_id;
810
 
811
  /* all stacks will be definition stacks by default. pass_4 will mark
812
     those that are referenced to be STACK_POSITION_CODE stacks */
813
  stacks_tmp->position = STACK_POSITION_DEFINITION;
814
 
815
  for (q = 0; q < d; q++) {
816
    if (ta[q].type == STACK_ITEM_TYPE_OPERATOR) {
817
      stacks_tmp->stack[q].type = STACK_ITEM_TYPE_OPERATOR;
818
      stacks_tmp->stack[q].value = ta[q].value;
819
    }
820
    else if (ta[q].type == STACK_ITEM_TYPE_VALUE) {
821
      stacks_tmp->stack[q].type = STACK_ITEM_TYPE_VALUE;
822
      stacks_tmp->stack[q].value = ta[q].value;
823
      stacks_tmp->stack[q].sign = ta[q].sign;
824
    }
825
    else if (ta[q].type == STACK_ITEM_TYPE_STACK) {
826
      stacks_tmp->stack[q].type = STACK_ITEM_TYPE_STACK;
827
      stacks_tmp->stack[q].value = ta[q].value;
828
      stacks_tmp->stack[q].sign = ta[q].sign;
829
    }
830
    else {
831
      stacks_tmp->stack[q].type = STACK_ITEM_TYPE_STRING;
832
      stacks_tmp->stack[q].sign = ta[q].sign;
833
      strcpy(stacks_tmp->stack[q].string, ta[q].string);
834
    }
835
  }
836
 
837
  _stack_insert();
838
 
839
  return INPUT_NUMBER_STACK;
840
}
841
 
842
 
843
int resolve_stack(struct stack_item s[], int x) {
844
 
845
  struct macro_argument *ma;
846
  struct stack_item *st;
847
  int a, b, k, q = x;
848
  char c;
849
 
850
 
851
  st = s;
852
  while (x > 0) {
853
    if (s->type == STACK_ITEM_TYPE_STRING) {
854
      if (macro_active != 0 && s->string[0] == '\\') {
855
        if (s->string[1] == '@' && s->string[2] == 0) {
856
          s->type = STACK_ITEM_TYPE_VALUE;
857
          s->value = macro_runtime_current->macro->calls - 1;
858
        }
859
        else {
860
          for (a = 0, b = 0; s->string[a + 1] != 0 && a < 10; a++) {
861
            c = s->string[a + 1];
862
            if (c < '0' && c > '9') {
863
              print_error("Error in MACRO argument number definition.\n", ERROR_DIR);
864
              return FAILED;
865
            }
866
            b = (b * 10) + (c - '0');
867
          }
868
 
869
          if (b > macro_runtime_current->supplied_arguments) {
870
            sprintf(xyz, "Reference to MACRO argument number %d is out of range.\n", b);
871
            print_error(xyz, ERROR_STC);
872
            return FAILED;
873
          }
874
 
875
          /* return the macro argument */
876
          ma = macro_runtime_current->argument_data[b - 1];
877
          k = ma->type;
878
 
879
          if (k == INPUT_NUMBER_ADDRESS_LABEL)
880
            strcpy(label, ma->string);
881
          else if (k == INPUT_NUMBER_STRING) {
882
            strcpy(label, ma->string);
883
            string_size = strlen(ma->string);
884
          }
885
          else if (k == INPUT_NUMBER_STACK)
886
            latest_stack = ma->value;
887
          else if (k == SUCCEEDED)
888
            d = ma->value;
889
 
890
          if (!(k == SUCCEEDED || k == INPUT_NUMBER_ADDRESS_LABEL || k == INPUT_NUMBER_STACK))
891
            return FAILED;
892
 
893
          if (k == SUCCEEDED) {
894
            s->type = STACK_ITEM_TYPE_VALUE;
895
            s->value = d;
896
          }
897
          else if (k == INPUT_NUMBER_STACK) {
898
            s->type = STACK_ITEM_TYPE_STACK;
899
            s->value = latest_stack;
900
          }
901
          else
902
            strcpy(s->string, label);
903
        }
904
      }
905
      else {
906
        if (macro_active != 0) {
907
          /* expand e.g., \1 and \@ */
908
          d = 0;
909
          if (expand_macro_arguments(s->string, &d) == FAILED)
910
            return FAILED;
911
          strcpy(s->string, expanded_macro_string);
912
        }
913
 
914
        tmp_def = defines;
915
        while (tmp_def != NULL) {
916
          if (strcmp(tmp_def->alias, s->string) == 0) {
917
            if (tmp_def->type == DEFINITION_TYPE_STRING) {
918
              sprintf(xyz, "Definition \"%s\" is a string definition.\n", tmp_def->alias);
919
              print_error(xyz, ERROR_STC);
920
              return FAILED;
921
            }
922
            else if (tmp_def->type == DEFINITION_TYPE_STACK) {
923
              /* skip stack definitions -> use its name instead */
924
            }
925
            else {
926
              s->type = STACK_ITEM_TYPE_VALUE;
927
              s->value = tmp_def->value;
928
              break;
929
            }
930
          }
931
          tmp_def = tmp_def->next;
932
        }
933
      }
934
    }
935
    s++;
936
    x--;
937
  }
938
 
939
  /* find a string or a stack and fail */
940
  while (q > 0) {
941
    if (st->type == STACK_ITEM_TYPE_STRING || st->type == STACK_ITEM_TYPE_STACK)
942
      return FAILED;
943
    q--;
944
    st++;
945
  }
946
 
947
  return SUCCEEDED;
948
}
949
 
950
 
951
int compute_stack(struct stack *sta, int x, double *result) {
952
 
953
  struct stack_item *s;
954
  double v[256], q;
955
  int r, t, z;
956
 
957
 
958
  s = sta->stack;
959
  for (r = 0, t = 0; r < x; r++, s++) {
960
    if (s->type == STACK_ITEM_TYPE_VALUE) {
961
      if (s->sign == SI_SIGN_NEGATIVE)
962
        v[t] = -s->value;
963
      else
964
        v[t] = s->value;
965
      t++;
966
    }
967
    else {
968
      switch ((int)s->value) {
969
      case SI_OP_PLUS:
970
        v[t - 2] += v[t - 1];
971
        t--;
972
        break;
973
      case SI_OP_MINUS:
974
        v[t - 2] -= v[t - 1];
975
        t--;
976
        break;
977
      case SI_OP_MULTIPLY:
978
        v[t - 2] *= v[t - 1];
979
        t--;
980
        break;
981
      case SI_OP_LOW_BYTE:
982
        z = (int)v[t - 1];
983
        v[t - 1] = z & 0xFF;
984
        break;
985
      case SI_OP_HIGH_BYTE:
986
        z = (int)v[t - 1];
987
        v[t - 1] = (z>>8) & 0xFF;
988
        break;
989
      case SI_OP_XOR:
990
        /* 16bit XOR? */
991
        if (v[t - 2] > 0xFF || v[t - 2] < -128 || v[t - 1] > 0xFF || v[t - 1] < -128)
992
          v[t - 2] = ((int)v[t - 1] ^ (int)v[t - 2]) & 0xFFFF;
993
        /* 8bit XOR */
994
        else
995
          v[t - 2] = ((int)v[t - 1] ^ (int)v[t - 2]) & 0xFF;
996
        t--;
997
        break;
998
      case SI_OP_OR:
999
        v[t - 2] = (int)v[t - 1] | (int)v[t - 2];
1000
        t--;
1001
        break;
1002
      case SI_OP_AND:
1003
        v[t - 2] = (int)v[t - 1] & (int)v[t - 2];
1004
        t--;
1005
        break;
1006
      case SI_OP_MODULO:
1007
        if (((int)v[t - 1]) == 0) {
1008
          fprintf(stderr, "%s:%d: COMPUTE_STACK: Modulo by zero.\n", get_file_name(sta->filename_id), sta->linenumber);
1009
          return FAILED;
1010
        }
1011
        v[t - 2] = (int)v[t - 2] % (int)v[t - 1];
1012
        t--;
1013
        break;
1014
      case SI_OP_DIVIDE:
1015
        if (((int)v[t - 1]) == 0) {
1016
          fprintf(stderr, "%s:%d: COMPUTE_STACK: Division by zero.\n", get_file_name(sta->filename_id), sta->linenumber);
1017
          return FAILED;
1018
        }
1019
        v[t - 2] /= v[t - 1];
1020
        t--;
1021
        break;
1022
      case SI_OP_POWER:
1023
        q = 1;
1024
        for (z = 0; z < v[t - 1]; z++)
1025
          q *= v[t - 2];
1026
        v[t - 2] = q;
1027
        t--;
1028
        break;
1029
      case SI_OP_SHIFT_LEFT:
1030
        v[t - 2] = (int)v[t - 2] << (int)v[t - 1];
1031
        t--;
1032
        break;
1033
      case SI_OP_SHIFT_RIGHT:
1034
        v[t - 2] = (int)v[t - 2] >> (int)v[t - 1];
1035
        t--;
1036
        break;
1037
      }
1038
    }
1039
  }
1040
 
1041
  if (v[0] < -32768 || v[0] > 65536) {
1042
    print_error("Out of 16bit range.\n", ERROR_STC);
1043
    return FAILED;
1044
  }
1045
 
1046
  *result = v[0];
1047
 
1048
  return SUCCEEDED;
1049
}
1050
 
1051
 
1052
int stack_create_label_stack(char *label) {
1053
 
1054
  if (label == NULL)
1055
    return FAILED;
1056
 
1057
  /* we need to create a stack that holds just one label */
1058
  stacks_tmp = malloc(sizeof(struct stack));
1059
  if (stacks_tmp == NULL) {
1060
    print_error("Out of memory error while allocating room for a new stack.\n", ERROR_STC);
1061
    return FAILED;
1062
  }
1063
  stacks_tmp->next = NULL;
1064
  stacks_tmp->type = STACKS_TYPE_UNKNOWN;
1065
  stacks_tmp->bank = -123456;
1066
  stacks_tmp->stacksize = 1;
1067
  stacks_tmp->stack = malloc(sizeof(struct stack_item) * 1);
1068
  if (stacks_tmp->stack == NULL) {
1069
    free(stacks_tmp);
1070
    print_error("Out of memory error while allocating room for a new stack.\n", ERROR_STC);
1071
    return FAILED;
1072
  }
1073
 
1074
  stacks_tmp->linenumber = active_file_info_last->line_current;
1075
  stacks_tmp->filename_id = active_file_info_last->filename_id;
1076
 
1077
  /* all stacks will be definition stacks by default. pass_4 will mark
1078
     those that are referenced to be STACK_POSITION_CODE stacks */
1079
  stacks_tmp->position = STACK_POSITION_DEFINITION;
1080
 
1081
  stacks_tmp->stack[0].type = STACK_ITEM_TYPE_STRING;
1082
  stacks_tmp->stack[0].sign = SI_SIGN_POSITIVE;
1083
  strcpy(stacks_tmp->stack[0].string, label);
1084
 
1085
  _stack_insert();
1086
 
1087
  return SUCCEEDED;
1088
}

powered by: WebSVN 2.1.0

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