-
Notifications
You must be signed in to change notification settings - Fork 1
/
pass2.c
565 lines (554 loc) · 18.6 KB
/
pass2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
pass2.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "outx.h"
#include "memmgt.h"
long xfer_addr;
static int noout_flag;
#if 0
extern char *outexp();
extern char *outxfer();
extern int endlin(),rewind_tmp(),read_from_tmp();
extern int flushobj();
extern int outorg(),outtstexp();
extern SEG_struct *get_absseg();
extern void outboffexp(char *, int, EXP_stk *);
extern void outoorexp(char *, int, EXP_stk *);
#endif
#define LONG_LBF 'l'
#define LONG_HBF 'L'
#define WORD_LBF 'w'
#define WORD_HBF 'W'
#define SIGN_LBF 'i'
#define SIGN_HBF 'I'
#define USIGN_LBF 'u'
#define USIGN_HBF 'U'
#define VAR_LBF 'x'
#define VAR_HBF 'X'
#if 0
#define FLIP_SENSE 0
#define LONG_LBF 'L'
#define LONG_HBF 'l'
#define WORD_LBF 'W'
#define WORD_HBF 'w'
#define SIGN_LBF 'I'
#define SIGN_HBF 'i'
#define USIGN_LBF 'U'
#define USIGN_HBF 'u'
#define VAR_LBF 'X'
#define VAR_HBF 'x'
#endif
#define LONG_LWF 'j'
#define LONG_HWF 'J'
int out_compexp(unsigned char *value, long tv, int tag, int taglen) {
int flip = 0;
switch (tag)
{
case VAR_LBF:
case VAR_HBF: {
long mask;
mask = (1<<taglen) -1; /* compute mask */
if (tv&~mask)
{
if ( (edmask&ED_TRUNC) )
trunc_err(mask,tv);
tv &= mask;
}
if (tag == VAR_HBF)
{
int i;
for (i=taglen-macxx_mau;i>0;i -= macxx_mau) *value = tv>>i;
}
else
{
int i;
for (i=0; i < (taglen (macxx_mau-1))/macxx_mau; i, tv = tv >> 8) *value = tv;
}
return(taglen (macxx_mau-1))/macxx_mau;
}
case LONG_LWF: { /* long, high byte of low word first */
*value = tv>>8; /* bits 15-8 */
*value = tv;
*value = tv>>24;
*value = tv>>16;
#if 0
unsigned char b0,b1,b2,b3; /* bytes in long */
b0 = tv&0xFF; /* pickup individual bytes */
b1 = (tv>>8)&0xFF;
b2 = (tv>>16)&0xFF;
b3 = (tv>>24)&0xFF;
tv = (b2<<24) | (b3<<16) | (b0<<8) | (b1);
*value = tv;
#endif
return sizeof(long);
}
case LONG_HWF: { /* long, low byte of high word first */
*value = tv>>16;
*value = tv>>24;
*value = tv;
*value = tv>>8;
#if 0
b0 = tv&0xFF; /* pickup individual bytes */
b1 = (tv>>8)&0xFF;
b2 = (tv>>16)&0xFF;
b3 = (tv>>24)&0xFF;
tv = (b1<<24) | (b0<<16) | (b3<<8) | (b2);
*value = tv;
#endif
return sizeof(long);
}
case LONG_HBF: { /* long, high byte first */
*value = tv>>24;
*value = tv>>16;
*value = tv>>8;
*value = tv;
#if 0
char *vp;
char b0,b1,b2,b3; /* bytes in long */
vp = (char *)value; /* point to area */
b0 = *vp ; /* pickup individual bytes */
b1 = *vp ;
b2 = *vp ;
b3 = *vp;
*vp = b0; /* and swap them */
*--vp = b1;
*--vp = b2;
*--vp = b3;
#endif
} /* fall thru to 'l' */
case LONG_LBF: { /* long, low byte first */
*value = tv;
*value = tv>>8;
*value = tv>>16;
*value = tv>>24;
return sizeof(long);
}
case WORD_HBF: { /* word, high byte first */
flip = 1; /* signal to flip it */
} /* fall thru to 'w' */
case WORD_LBF: {
if ( (edmask&ED_TRUNC) && (tv > 65535 || tv < -65536) )
trunc_err(65535L,tv);
goto common_word; /* jump to common word processing */
}
case SIGN_HBF: {
flip = 1;
}
case SIGN_LBF: {
if ( (edmask&ED_TRUNC) && (tv > 32767 || tv < -32768))
trunc_err(65535L,tv);
goto common_word;
}
case USIGN_HBF: {
flip = 1;
}
case USIGN_LBF: {
if ( (edmask&ED_TRUNC) && (tv &0xFFFF0000L))
trunc_err(65535L,tv);
}
common_word:
#if 0
{
char *vp;
char b0,b1; /* bytes in long */
#ifndef BIG_ENDIAN
if (flip == 1)
{ /* flip the bytes */
vp = (char *)value; /* point to area */
b0 = *vp ; /* pickup individual bytes */
b1 = *vp;
*vp = b0; /* and swap them */
*--vp = b1;
}
#else
vp = ((char *)value) 3; /* point to area */
b0 = *vp; /* pickup individual bytes */
b1 = *--vp;
if (flip == 1)
{
*--vp = b1; /* and swap them */
*--vp = b0;
}
else
{
*--vp = b0;
*--vp = b1;
}
#endif
return sizeof(short);
}
#else
if (flip)
{
*value = tv >> 8;
*value = tv;
}
else
{
*value = tv;
*value = tv >> 8;
}
return sizeof(short);
#endif
case 'b': {
if ( (edmask&ED_TRUNC) && (tv > 255 || tv < -128))
trunc_err(255L,tv);
*value = tv;
return sizeof(char);
}
case 's': {
if ( (edmask&ED_TRUNC) && (tv > 127 || tv < -128))
trunc_err(255L,tv);
*value = tv;
return sizeof(char);
}
case 'c': {
if ( (edmask&ED_TRUNC) && (tv & 0xFFFFFF00))
trunc_err(255L,tv);
*value = tv;
return sizeof(char);
}
case 'y': {
if ( tv > 32767L || tv < -32768L )
{
if ((edmask&ED_TRUNC))
trunc_err(65534L,tv);
tv = -3;
}
*value = tv;
*value = tv>>8;
return sizeof(short);
}
case 'Y': {
if ( tv > 32767L || tv < -32768L )
{
if ( (edmask&ED_TRUNC) )
trunc_err(65534L,tv);
tv = -3;
}
*value = tv>>8;
*value = tv;
return sizeof(short);
}
case 'z': {
/*
if ((tv == 0 || tv == -1) && macxx_name[3] == '6' && macxx_name[4] == '8') {
sprintf(emsg,"Branch offset of 0 or -1 is illegal on line %d in file %s",
current_fnd->fn_line,current_fnd->fn_name_only);
err_msg(MSG_ERROR,emsg);
}
*/
if (tv > 127 || tv < -128)
{
if ( (edmask&ED_TRUNC) )
trunc_err(254L, tv);
tv = -2;
}
*value = tv;
return sizeof(char);
}
}
return 0;
}
/**********************************************************************
* Pass2 - generate output
*/
int pass2( void )
/*
* At entry:
* no requirements, called from mainline
* At exit:
* output file written.
*/
{
int i,r_flg=1;
long lc;
if ((outxabs_fp = obj_fp) == 0) return(1); /* no output required */
endlin(); /* flush the opcode buffer */
if (options[QUAL_DEBUG]) dbg_flush(1); /* flush the debug buffers */
write_to_tmp(TMP_EOF,0,0,0); /* make sure that there's an EOF in tmp */
rewind_tmp(); /* rewind to beginning */
while (1)
{
if (r_flg) read_from_tmp(); /* now read the whole thing in */
r_flg = 1; /* gotta read next time */
switch (tmp_ptr->tf_type)
{ /* see what we gotta do */
case TMP_EOF: {
if (options[QUAL_DEBUG]) dbg_output(); /* add debug stuff to object file */
termobj(xfer_addr); /* terminate and flush the output buffer */
if (!tmp_fp)
{
int ferr;
ferr = MEM_free((char *)tmp_top);
#if !defined(SUN)
if (ferr)
{ /* give back the memory */
sprintf(emsg,"Error (X) free'ing %d bytes at lX from tmp_pool",
ferr, max_token*8, (unsigned long)tmp_top);
err_msg(MSG_WARN,emsg);
}
#endif
tmp_top = 0;
}
return(1); /* done with pass2 */
}
case TMP_EXPR: {
if (noout_flag != 0)
{
break;
}
else
{
EXP0.ptr = compress_expr(&EXP0);
if (EXP0.ptr <= 0)
{
char *s;
if (options[QUAL_OCTAL])
{
s = "\t(lo bytes offset from segment {%s})\n";
}
else
{
s = "\t(lX bytes offset from segment {%s})\n";
}
snprintf(emsg,ERRMSG_SIZE,s,current_offset,current_section->seg_string);
err_msg(MSG_ERROR|MSG_CTRL,emsg);
}
#ifdef PC_DEBUG
fprintf(stderr,"%s:%d: EXPR pc = lX, sec = {%s}, tag=%c:%d\n",
current_fnd->fn_buff,
current_fnd->fn_line,current_offset,
current_section->seg_string,
EXP0.tag,EXP0.tag_len);
#endif
}
if (EXP0.tag != 0)
{
if ((lc = EXP0.tag_len) == 0)
{
lc = EXP0.tag_len = 1; /* get the count */
}
if ((EXP0.ptr == 1 && EXP0SP->expr_code == EXPR_VALUE) &&
(EXP0.tag_len == 1 || _toupper(EXP0.tag) == 'X'))
{
unsigned char tmpv[sizeof(long)];
i = out_compexp(tmpv, EXP0SP->expr_value, (int)EXP0.tag,EXP0.tag_len);
outbstr((char *)tmpv,i);
}
else
{
register char tag;
flushobj();
tag = EXP0.tag;
if (tag == 'y')
EXP0.tag = tag = 'w';
if (tag == 'Y')
EXP0.tag = tag = 'W';
if (tag == 'z')
EXP0.tag = tag = 's';
if (options[QUAL_BINARY])
{
union vexp ve;
ve.vexp_chp = eline;
*ve.vexp_type = VLDA_EXPR;
outexp(&EXP0,ve.vexp_chp,eline,obj_fp);
}
else
{
outexp(&EXP0,eline,eline 1,obj_fp);
}
tag = _toupper(tag);
if (tag == 'B' || tag == 'C' || tag == 'S')
{
i = macxx_mau_byte;
}
else
{
if (tag == 'L')
{
i = macxx_mau_long;
}
else if (tag == 'X')
{
i = EXP0.tag_len (macxx_mau-1)/macxx_mau;
}
else
{
i = macxx_mau_word;
}
}
}
current_offset = lc*i;
break; /* exit the TMP_xxx switch */
}
else
{
if (options[QUAL_RELATIVE])
{
flushobj();
outexp(&EXP0,eline,eline,obj_fp);
}
}
break;
}
case TMP_BSTNG:
case TMP_ASTNG: {
if (noout_flag == 0) outbstr((char *)tmp_pool,(int)tmp_ptr->tf_length);
#ifdef PC_DEBUG
fprintf(stderr,"%s:%d: TXT, %ld bytes, pc = lX, sec = {%s}\n",
current_fnd->fn_buff,
current_fnd->fn_line,
tmp_ptr->tf_length,
current_offset,
current_section->seg_string);
#endif
current_offset = tmp_ptr->tf_length*macxx_mau_byte;
break;
}
case TMP_ORG: {
SEG_struct *seg_ptr;
/* EXP0.ptr = compress_expr(&EXP0); */
if (EXP0.ptr <= 0)
{
if (options[QUAL_OCTAL])
{
sprintf(emsg,"ORG may be incorrectly set to 0lo\n",
EXP0SP->expr_value);
}
else
{
sprintf(emsg,"ORG may be incorrectly set to lX\n",
EXP0SP->expr_value);
}
err_msg(MSG_CONT,emsg);
}
if ((seg_ptr = EXP0SP->expr_seg) == 0)
{
EXP0SP->expr_code = EXPR_SEG;
EXP0SP->expr_seg = seg_ptr = get_absseg();
}
current_section = seg_ptr;
current_offset = EXP0SP->expr_value seg_ptr->rel_offset;
noout_flag = seg_ptr->flg_noout;
if (noout_flag == 0) outorg(&EXP0);
#ifdef PC_DEBUG
fprintf(stderr,"%s:%d: ORG at newpc = lX, sec = {%s}\n",
current_fnd->fn_buff,
current_fnd->fn_line,
current_offset,
current_section->seg_string);
#endif
break;
} /* -- case */
case TMP_START: {
EXP0.ptr = compress_expr(&EXP0);
if (EXP0.ptr <= 0)
{
if (options[QUAL_OCTAL])
{
sprintf(emsg,"XFER addr may be incorrectly set to 0lo\n",
EXP0SP->expr_value);
}
else
{
sprintf(emsg,"XFER addr may be incorrectly set to lX\n",
EXP0SP->expr_value);
}
err_msg(MSG_CONT,emsg);
}
if (options[QUAL_RELATIVE])
{
outxfer(&EXP0,obj_fp);
}
else
{
if (xfer_addr == 0 || xfer_addr == 1)
{
xfer_addr = EXP0SP->expr_value;
}
}
break;
} /* -- case */
case TMP_OOR:
case TMP_BOFF:
case TMP_TEST: {
EXP_stk *eps;
EXPR_struct *expr;
int otyp = tmp_ptr->tf_type; /* save our type */
eps = exprs_stack;
expr = eps->stack;
eps->ptr = compress_expr(eps); /* evaluate the expression */
read_from_tmp(); /* get the ascii message */
if (tmp_ptr->tf_type != TMP_ASTNG)
{
err_msg(MSG_ERROR,"Internal error processing .TEST, .BOFF ot .OOR directives");
}
else if (eps->ptr <= 0)
{
sprintf(emsg,"Expression error while processing .TEST, .BOFF or .OOR (eps->ptr=%d): %s\n",
eps->ptr, (char *)tmp_pool);
err_msg(MSG_CONT,emsg);
}
else
{
if (eps->ptr == 1 && expr->expr_code == EXPR_VALUE)
{
if (expr->expr_value != 0)
{
if (otyp == TMP_BOFF)
{
sprintf(emsg, "Branch offset out of range: %s", (char *)tmp_pool);
}
else if (otyp == TMP_OOR)
{
sprintf(emsg, "Operand out of range: %s", (char *)tmp_pool);
}
else
{
sprintf(emsg, ".TEST expression evaluated true: %s", (char *)tmp_pool);
}
err_msg(MSG_ERROR, emsg);
}
break;
}
if (otyp == TMP_TEST)
{
outtstexp((char *)tmp_pool,(int)tmp_ptr->tf_length,&EXP0);
}
else if (otyp == TMP_BOFF)
{
outboffexp((char *)tmp_pool, (int)tmp_ptr->tf_length, &EXP0);
}
else
{
outoorexp((char *)tmp_pool, (int)tmp_ptr->tf_length, &EXP0);
}
}
break;
} /* -- case */
case TMP_FILE: {
continue; /* FILE type is easy */
}
case TMP_SCOPE: {
continue; /* SCOPE type is easy */
}
default: {
printf("%%MACXX-F-Undefined code byte found in tmp file: %d\n",
tmp_ptr->tf_type);
EXIT_FALSE;
} /* -- case */
} /* -- switch */
} /* -- while */
} /* -- pass2 */