forked from uli/huc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.c
648 lines (582 loc) · 10.1 KB
/
io.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* File io.c: 2.1 (83/03/20,16:02:07) */
/*% cc -O -c %
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "data.h"
#include "io.h"
#include "optimize.h"
#include "preproc.h"
#include "main.h"
#include "code.h"
#include "sym.h"
/*
* open input file
* Input : char* p
* Output : int error code
*
* Try to open the file whose filename is p, return YES if opened, else NO
* Updates fname with the actual opened name
* input is the handle of the opened file
*
*/
int openin (char *p)
{
strcpy(fname, p);
strcpy(fname_copy, fname);
fixname(fname);
if (!checkname(fname)) {
fprintf(stderr, "%s: unknown file type\n", fname);
return (NO);
}
if ((input = fopen(fname, "r")) == NULL) {
perror(fname);
return (NO);
}
kill();
return (YES);
}
/*
* open output file
* Input : nothing but uses global fname
* Output : nothing but fname contain the name of the out file now
*
* Guess the name of the outfile thanks to the input one and try to open it
* In case of succes returns YES and output is the handle of the opened file
* else returns NO
*
*/
int openout (void)
{
if (user_outfile[0])
output = fopen(user_outfile, "w");
else {
outfname(fname);
output = fopen(fname, "w");
}
if (output == NULL) {
pl("Open failure : ");
if (user_outfile[0])
pl(user_outfile);
else
pl(fname);
pl("\n");
return (NO);
}
kill();
return (YES);
}
/*
* change input filename to output filename
* Input : char* s
* Output : char* s is updated
*
* Simply replace the last letter of s by 's'
* Used to return "file.s" from "file.c"
*
*/
void outfname (char *s)
{
while (*s)
s++;
*--s = 's';
}
/*
* remove NL from filenames
* Input : char* s
* Output : char* s is updated
*
* if any, remove the trailing newline char from the s string
*
*/
void fixname (char *s)
{
while (*s && *s++ != EOL) ;
if (!*s) return;
*(--s) = 0;
}
/*
* check that filename is "*.c"
* Input : char* s
* Output : int
*
* verify that the 2 last letter of s are ".c", returns YES in this case,
* else NO
*
*/
int checkname (char *s)
{
while (*s)
s++;
if (*--s != 'c' && *s != 'C')
return (NO);
if (*--s != '.')
return (NO);
return (YES);
}
/*
* kill
* Input : nothing
* Output : nothing but updates lptr and line[lptr]
*
* lptr and line[lptr] are set to zero what appears to clear the current
* input line
*
*/
void kill (void)
{
lptr = 0;
line[lptr] = 0;
}
/*
* unget_line
* Input : # of characters to kill preceding what we see in line[lptr]
* Output : nothing
*
* This function looks at line and lptr variables with a line of text
* and moves the file pointer back as though the line was never read
* then kills line/lptr
*
* Only use this function for operations at the top level (ie. the
* file pointer 'fp')
*
*/
void unget_line (void)
{
int i;
i = (int)strlen(line);
if (i > 0) {
fseek(input, 0 - i - CR_LEN, SEEK_CUR);
line_number--;
}
kill();
}
/*
* readline
* Input : nothing
* Output : nothing
*
* This function seems to fill line and lptr variables with a line of text
* coming either form an included file or the main one
*
*/
void readline (void)
{
int k;
FILE *unit;
FOREVER {
if (!input || feof(input))
return;
if ((unit = input2) == NULL)
unit = input;
kill();
while ((k = fgetc(unit)) != EOF) {
if ((k == '\r') | (k == EOL) | (lptr >= LINEMAX))
break;
line[lptr++] = k;
}
line_number++;
line[lptr] = 0;
if (k <= 0)
if (input2 != NULL) {
if (globals_h_in_process) {
/* Add special treatment to ensure globals.h stuff appears at the beginning */
gdata();
outstr("huc_globals:\n");
dumpglbs();
outstr("huc_globals_end:\n");
gtext();
globals_h_in_process = 0;
}
input2 = inclstk[--inclsp];
line_number = inclstk_line[inclsp];
fclose(unit);
}
if (lptr) {
if ((ctext) & (cmode)) {
flush_ins();
comment();
tab();
tab();
tab();
tab();
tab();
tab();
outstr(line);
nl();
}
lptr = 0;
return;
}
}
}
/*
* inbyte
* Input : nothing
* Output : int, (actualy char)
*
* Uses the preprocessor as much as possible to get readable data
* then read the next char and make lptr points to the next one
*
*/
int inbyte (void)
{
while (ch() == 0) {
if (feof(input))
return (0);
preprocess();
}
return (gch());
}
/*
* inchar
* Input : nothing
* Output : int, (actualy char)
*
* Returns the current char, making lptr points to the next one
* If the buffer if empty, fill it with next line from input
*
*/
int inchar (void)
{
if (ch() == 0)
readline();
if (feof(input))
return (0);
return (gch());
}
/*
* gch
* Input : nothing
* Output : int, (actualy char)
*
* If the pointed char (by line and lptr) is 0, return this value
* else return the current pointed char and advance the lptr to point
* on the following char
*
*/
int gch (void)
{
if (ch() == 0)
return (0);
else
return (line[lptr++] & 127);
}
/*
* nch
* Input : nothing
* Output : int, (actualy char)
*
* If called when the pointed char is at the end of the line, return 0
* else return the following char
* Doesn't change line nor lptr variable
*
*/
int nch (void)
{
if (ch() == 0)
return (0);
else
return (line[lptr + 1] & 127);
}
/*
* ch
*
* Input : nothing but use global line and lptr variables
* Output : int, (actually char), corresponding to the current pointed char
* during the parsing
*
* Appears to be the major function used during the parsing.
* The global variables line and lptr aren't changed
*
*/
int ch (void)
{
return (line[lptr] & 127);
}
/*
* print a carriage return and a string only to console
*
*/
void pl (char *str)
/*char *str; */
{
int k;
k = 0;
putchar(EOL);
while (str[k])
putchar(str[k++]);
}
/*
* glabel - generate label
*/
void glabel (char *lab)
/*char *lab;*/
{
flush_ins(); /* David - optimize.c related */
prefix();
outstr(lab);
col();
nl();
}
/*
* gnlabel - generate numeric label
*/
void gnlabel (int nlab)
{
out_ins(I_LABEL, T_VALUE, nlab);
}
/*
* Output internal generated label prefix
*/
void olprfix (void)
{
outstr("LL");
}
/*
* Output a label definition terminator
*/
void col (void)
{
outstr(":\n");
}
/*
* begin a comment line for the assembler
*
*/
void comment (void)
{
outbyte(';');
}
/*
* Output a prefix in front of user labels
*/
void prefix (void)
{
outbyte('_');
}
/*
* tab
* Input : nothing
* Output : nothing
*
* Write a tab charater in the assembler file
*
*/
void tab (void)
{
outbyte(9);
}
/*
* ol
* Input : char* ptr
* Output : nothing
*
* Writes the string ptr to the assembler file, preceded by a tab, ended by
* a newline
*
*/
void ol (char *ptr)
/*char ptr[]; */
{
ot(ptr);
nl();
}
/*
* ot
* Input : char* ptr
* Output : nothing
*
* Writes the string ptr to the assembler file, preceded by a tab
*
*/
void ot (char *ptr)
/*char ptr[]; */
{
tab();
outstr(ptr);
}
/*
* nl
* Input : nothing
* Output : nothing
*
* Display a newline in the assembler file
*
*/
void nl (void)
{
outbyte(EOL);
}
/*
* outsymbol
* Input : char* ptr
* Output : nothing
*
* Writes the string ptr preceded with the result of the function prefix
*
*/
void outsymbol (SYMBOL *ptr)
{
if ((ptr->storage & STORAGE) == LSTATIC) {
outstr(ptr->name);
} else {
prefix();
outstr(ptr->name);
}
if (ptr->linked) {
outstr(" /* ");
outstr(ptr->linked->name);
outstr(" */");
}
}
/*
* print specified number as label
*/
void outconst (int label)
{
outstr("__const");
outdec(label);
}
/*
* print specified number as label
*/
void outlabel (int label)
{
olprfix();
outdec(label);
}
/*
* Output a decimal number to the assembler file
*/
/*
void outdec (int number)
{
int k, zs;
char c;
if (number == -32768) {
outstr ("-32768");
return;
}
zs = 0;
k = 10000;
if (number < 0) {
number = (-number);
outbyte ('-');
}
while (k >= 1) {
c = number / k + '0';
if ((c != '0' | (k == 1) | zs)) {
zs = 1;
outbyte (c);
}
number = number % k;
k = k / 10;
}
}
*/
/* Newer version, shorter and certainly faster */
void outdec (int number)
{
char s[16];
int i = 0;
sprintf(s, "%d", number);
while (s[i])
outbyte(s[i++]);
}
/*
* Output an hexadecimal unsigned number to the assembler file
*/
/*
void outhex (int number)
{
int k, zs;
char c;
zs = 0;
k = 0x10000000;
outbyte('$');
while (k >= 1) {
c = number / k;
if (c <= 9)
c += '0';
else
c += 'A' - 10;
outbyte (c);
number = number % k;
k = k / 16;
}
}
*/
/* Newer version, shorter and certainly faster */
void outhex (int number)
{
int i = 0;
char s[10];
outbyte('$');
sprintf(s, "%0X", (int)number);
while (s[i])
outbyte(s[i++]);
}
/*
* Output an hexadecimal number with a certain number of digits
*/
void outhexfix (int number, int length)
{
int i = 0;
char s[10];
char format[10];
outbyte('$');
sprintf(format, "%%0%dX", (int)length);
sprintf(s, format, number);
while (s[i])
outbyte(s[i++]);
}
/*
* outbyte
* Input : char c
* Output : same as input, c
*
* if c is different of zero, write it to the output file
*
*/
char outbyte (char c)
{
if (c == 0)
return (0);
fputc(c, output);
return (c);
}
/*
* outstr
* Input : char*, ptr
* Output : nothing
*
* Send the input char* to the assembler file
*
*/
void outstr (const char *ptr)
{
int k;
k = 0;
while (outbyte(ptr[k++])) ;
}
/*
* outlocal
* Input : char* ptr
* Output : nothing
*
* Writes the variable name as a comment
*
*/
void outlocal (SYMBOL *ptr)
{
if (ptr) {
outstr(" /* ");
outstr(ptr->name);
outstr(" */");
}
}