-
Notifications
You must be signed in to change notification settings - Fork 6
/
ccopt.c
1426 lines (1029 loc) · 24.7 KB
/
ccopt.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ccopt.c
Mike's Enhanced Small C Compiler for Z80 & CP/M.
New peephole optimizer with customizable rules, for MESCC v1.10 or higher.
Copyright (c) 2015 Miguel I. Garcia Lopez, FloppySoftware.
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 2, 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, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Revisions:
29 Oct 2015 : Work started from scratch (not based at all on previous ccopt).
02 Nov 2015 : Work on defining rules.
04 Nov 2015 : Work on binary expressions.
05 Nov 2015 : Work on unary expressions.
09 Nov 2015 : Added verbose flag.
01 Dec 2015 : More space for buffers. Cleaned. Removed verbose flag.
02 Dec 2015 : Cleaned.
v2.00
10 Jan 2016 : v2.01 : Cleaned a bit.
28 Jan 2016 : v2.02 : Added 'line too long' error. Solved bug in get_sym().
Improved test_rule() and test_line().
Modified get_num().
Rules:
Symbol fields are specified as @a to @e.
Decimal constant fields are specified as @0 to @4.
The first time a field specificacion is found in the rule, the scanner tries to read
its value from the source - ie:
@b -- symbol name expected
@3 -- decimal constant expected
The following time(s) a field specification is found in the rule, the scanner tries to
match the field value with the next characters in the source.
Output:
Constant operations can be specified as:
@=1+2 To perform a signed add with @1 and @2.
Or, for unary expressions:
@=!1 To perform a logical not of @1
Valir operators are:
+ Add
???????????????
Usage:
ccopt file [-r]
*/
/* MESCC standard libraries
*/
#include <mescc.h>
#include <ctype.h>
#include <string.h>
#include <mem.h>
#include <alloc.h>
#include <fileio.h>
#include <printf.h>
#include <fprintf.h>
#include <sprintf.h>
/* Rules
*/
#include "ccopt.h"
/* Defs.
*/
#define APP_NAME "ccopt"
#define APP_VERSION "v2.02 / 28 Jan 2016"
#define APP_COPYRGT "(c) 2015-2016 FloppySoftware"
#define APP_USAGE "ccopt file [-r]"
#define DEBUG 0 /* != 0 to DEBUG */
#define SPECIAL '@' /* Special character for fields, etc. */
#define SPC_NUMOP '=' /* Subspecial char. for numeric operations */
#define GEN_COMMENT '#' /* Comment in rules */
#define SKIP_ON ";[" /* Skip this line and following */
#define SKIP_OFF ";]" /* Stop skipping lines */
#define LIN_TOT 24 /* Max. # of lines in buffer */
#define LIN_MAX 16 /* Max. # of lines to fill */
#define LIN_LEN 127 /* Max. length of each line */
#define LIN_SIZ 128 /* Max. length of each line + ZERO */
WORD lin[LIN_TOT]; /* char *lin[LIN_TOT] */
int lin_skip[LIN_TOT]; /* True if this line has to be skipped in the rule test */
int lines; /* Lines in array */
int skip; /* NZ if we are in skip mode */
#define SYM_MAX 5 /* Max. # of symbol variables: @a to @e */
#define SYM_LEN 11 /* Variable length in characters */
#define SYM_SIZ 12 /* Variable length in characters + ZERO */
WORD sym[SYM_MAX]; /* char *sym[SYM_MAX] */
#define NUM_MAX 5 /* Max. # of number variables: @0 to @4 */
#define NUM_LEN 6 /* Variable length in characters */
#define NUM_SIZ 7 /* Variable length in characters + ZERO */
WORD num[NUM_MAX]; /* char *num[NUM_MAX] */
FILE *fp_in; /* Input file */
FILE *fp_out; /* Output file */
char fn_in[FILENAME_MAX]; /* Input file */
char fn_out[FILENAME_MAX]; /* Output file */
#define FTYPE_ASM ".ZSM" /* Filetype for ZSM files */
#define FTYPE_TMP ".$$$" /* Filetype for temporary files */
#define FTYPE_RUL ".RUL" /* Filetype for rule files */
#define FTYPE_OUT ".H" /* Filetype for H files */
extern int rules_max; /* # of rules */
extern WORD rules[]; /* char *rules[] */
extern int rules_saved[]; /* # of saved bytes for each rule */
int saved; /* Total # of saved bytes */
int out_lines; /* # of lines written */
#define LAB_MAX 2048 /* Max. # of labels */
#define LAB_LEN 11 /* Label length */
#define LAB_SIZ 12 /* Label length + ZERO */
char *lab_names; /* Label names */
int *lab_lines; /* Line # for each label */
int labels; /* # of stored labels */
#define JP_REL 48 /* Change from JP to JR: -48 / + 48 */
int jumps_saved; /* # of bytes saved in jumps */
/* Error codes
*/
#define ERR_BAD_OPT 10 /* Bad option */
#define ERR_BAD_CMD 11 /* Bad command line */
#define ERR_OPEN 20 /* Can't open */
#define ERR_CLOSE 21 /* Can't close */
#define ERR_READ 22 /* Can't read */
#define ERR_WRITE 23 /* Can't write */
#define ERR_FNAME 24 /* Bad filename */
#define ERR_LN_LONG 25 /* Line too long */
#define ERR_NO_MEM 28 /* Not enough memory */
#define ERR_NO_ROOM 31 /* No room for more lines in buffer */
#define ERR_NO_VAR 32 /* Undefined variable */
#define ERR_NO_SPC 33 /* Bad special format */
#define ERR_NO_FIELD 34 /* Bad field */
#define ERR_TM_LABELS 35 /* Too many labels */
#define ERR_TM_LINES 36 /* Too many lines in rule */
#define ERR_NO_RULE 37 /* Bad rule specification */
/* Entry point
*/
main(argc, argv)
int argc, argv[]; /* char *argv[] */
{
/* Copyright etc. */
printf("%s %s\n\n", APP_NAME, APP_VERSION);
printf("%s\n\n", APP_COPYRGT);
/* Usage */
if(argc == 1) {
printf("Usage: %s\n", APP_USAGE);
exit(0);
}
/* Check options */
if(argc > 2) {
if(argc > 3)
error(ERR_BAD_CMD);
/* Option -R : Generate rules */
if(!strcmp(strupr(argv[2]), "-R")) {
/* Option: Generate rules */
set_fnames(argv[1], FTYPE_RUL, FTYPE_OUT);
gen_rules();
}
else
error(ERR_BAD_OPT);
}
else {
/* Optimize */
set_fnames(argv[1], FTYPE_ASM, FTYPE_TMP);
optimize();
}
/* Success */
return 0;
}
/* Set input and output filenames
*/
set_fnames(fn, tpin, tpout)
char *fn, *tpin, *tpout;
{
if(strchr(fn, '.') != NULL
|| strlen(fn) + strlen(tpin) >= FILENAME_MAX
|| strlen(fn) + strlen(tpout) >= FILENAME_MAX)
error_ext(ERR_FNAME, fn);
strcpy(fn_in, fn); strcat(fn_in, tpin);
strcpy(fn_out, fn); strcat(fn_out, tpout);
}
/* Open input file
*/
open_in()
{
if((fp_in = fopen(fn_in, "r")) == NULL)
error_ext(ERR_OPEN, fn_in);
}
/* Close input file
*/
close_in()
{
fclose(fp_in);
}
/* Open input and output files
*/
open_all()
{
/* Open input file */
open_in();
/* Open output file */
if((fp_out = fopen(fn_out, "w")) == NULL)
error_ext(ERR_OPEN, fn_out);
}
/* Close input and output files
*/
close_all()
{
close_in();
if(fclose(fp_out))
error_ext(ERR_CLOSE, fn_out);
}
/* Generate rules
*/
gen_rules()
{
int i, no;
char *pch;
printf("Generating rules:\n\n");
/* Alloc memory for buffer lines */
lin_alloc();
/* Reset lines */
lin_reset();
/* Setup */
no = 0;
/* Open input and output files */
open_all();
/* Write header */
if(fprintf(fp_out, "/* Include file for CCOPT, the MESCC optimizer */\n\n#asm\n") < 0)
error_ext(ERR_WRITE, fn_out);
/* Write rules */
while(1) {
/* Read rule lines */
if(!gen_fill())
break;
if(lines > LIN_MAX)
error(ERR_TM_LINES);
printf("Rule #%d.\n", no);
/* Write # of lines in rule */
if(fprintf(fp_out, "rule%d\n DEFB %d\n", no++, lines) < 0)
error_ext(ERR_WRITE, fn_out);
/* Write rule lines */
for(i = 0; i < lines; ++i) {
if(fprintf(fp_out, " DEFB '%s', 0\n", lin[i]) < 0)
error_ext(ERR_WRITE, fn_out);
}
/* Read output lines */
gen_fill();
if(lines > LIN_MAX)
error(ERR_TM_LINES);
/* Write # of output lines */
if(fprintf(fp_out, " DEFB %d\n", lines) < 0)
error_ext(ERR_WRITE, fn_out);
/* Write output lines */
for(i = 0; i < lines; ++i) {
if(fprintf(fp_out, " DEFB '%s', 0\n", lin[i]) < 0)
error_ext(ERR_WRITE, fn_out);
}
/* Go to # of bytes saved, skip comments */
while((pch = read_line(fp_in, lin[0])) != NULL) {
if(*pch != GEN_COMMENT)
break;
}
if(pch == NULL)
error(ERR_NO_RULE);
/* Write # of bytes saved */
if(fprintf(fp_out, " DEFB %s\n\n", lin[0]) < 0)
error_ext(ERR_WRITE, fn_out);
}
/* Write array of rule pointers */
if(!no)
error(ERR_NO_RULE);
if(fprintf(fp_out, "rules\n") < 0)
error_ext(ERR_WRITE, fn_out);
for(i = 0; i < no; ++i) {
if(fprintf(fp_out, " DEFW rule%d\n", i) < 0)
error_ext(ERR_WRITE, fn_out);
}
/* Write # of rules */
if(fprintf(fp_out, "\nrules_max\n DEFW %d\n", no) < 0)
error_ext(ERR_WRITE, fn_out);
/* Write array of # of bytes saved in each rule */
if(fprintf(fp_out, "\nrules_saved\n DEFS %d\n", no + no) < 0)
error_ext(ERR_WRITE, fn_out);
/* Write footer */
if(fprintf(fp_out, "#endasm\n") < 0)
error_ext(ERR_WRITE, fn_out);
/* Close input and output files */
close_all();
printf("\nDone.\n");
}
/* Optimize (test rules and jumps)
*/
optimize()
{
int i, t_saved, pass;
printf("Optimizing (%d rules):\n\n", rules_max);
/* Alloc memory for buffer lines and variables */
lin_alloc();
var_alloc();
/* Setup */
// jumps_saved = saved = 0;
pass = 1;
/* Do a pass */
do {
printf("Rules - pass #%d: ", pass++);
t_saved = saved;
/* Reset lines */
lin_reset();
/* Reset values */
out_lines = skip = 0;
/* Test all rules */
test_rules();
/* Swap filenames */
swap_fnames();
printf("%4d bytes saved.\n", saved - t_saved);
} while(t_saved != saved);
/* Test jumps */
printf("\nJumps: ");
/* Alloc memory for labels */
lab_alloc();
/* Setup */
//labels = 0;
/* Read all labels */
read_labels();
/* Test jumps */
test_jumps();
printf("%4d bytes saved (%d labels found).\n", jumps_saved, labels);
/* Remove input file */
remove(fn_in);
/* Rename output file? */
if(pass % 2)
rename(fn_out, fn_in); // TMP to ZSM
/* Final report */
printf("\nTotal saved: %4d bytes (Rules: %4d, Jumps: %4d)\n\nDone.\n",
t_saved + jumps_saved, t_saved, jumps_saved);
//printf("%d lines in file\n", out_lines);
/*****************
for(i = 0; i < rules_max; ++i) {
if(!rules_saved[i])
printf("Rule #%d not used?\n", i);
}
********************/
/****************
if(labels) {
printf("%d labels:\n", labels);
for(i = 0; i < labels; ++i)
printf("%5d = %5d : %s\n", i, lab_lines[i], lab_names + i * LAB_SIZ);
}
***************/
}
/* Read all labels in input file
*/
read_labels()
{
int line, skip;
char *buf;
/* Open input file */
open_in();
/* Setup */
buf = lin[0];
line = skip = 0;
/* Read all input lines */
while(read_line(fp_in, buf) != NULL) {
/* Skip lines between #asm and #endasm */
if(skip) {
if(!strcmp(buf, SKIP_OFF))
skip = 0;
}
else if(!strcmp(buf, SKIP_ON)) {
skip = 1;
}
else if(isalpha(*buf)) {
/* Get label */
if(labels >= LAB_MAX)
error(ERR_TM_LABELS);
if(get_field(lab_names + labels * LAB_SIZ, buf, LAB_LEN, 0) != NULL)
lab_lines[labels++] = line;
}
++line;
}
/* Close input file */
close_in();
}
/* Test all jumps, changing from JP to JR if possible
*/
test_jumps()
{
int i, line, skip, rel;
char *buf, *pch, *plb;
// FIXME: Do a sort and find labels by binary search?
/* Open input and output files */
open_all();
/* Setup */
buf = lin[0];
line = skip = 0;
/* Read all input lines */
while(read_line(fp_in, buf) != NULL) {
/* Skip lines between #asm and #endasm */
if(skip) {
if(!strcmp(buf, SKIP_OFF))
skip = 0;
}
else if(!strcmp(buf, SKIP_ON)) {
skip = 1;
}
else if(!memcmp(buf, " JP ", 4)) {
pch = buf + 4;
if(!memcmp(pch, "Z,", 2))
pch += 2;
else if(!memcmp(pch, "NZ,", 3))
pch += 3;
else if(!memcmp(pch, "C,", 2))
pch += 2;
else if(!memcmp(pch, "NC,", 3))
pch += 3;
else if(!memcmp(pch, "M,", 2))
pch = NULL;
else if(!memcmp(pch, "P,", 2))
pch = NULL;
else if(!memcmp(pch, "PE,", 3))
pch = NULL;
else if(!memcmp(pch, "PO,", 3))
pch = NULL;
/* Search this label */
if(pch != NULL) {
plb = lab_names;
for(i = 0; i < labels; ++i) {
/* Match? */
if(!strcmp(pch, plb)) {
/* Match */
rel = line - lab_lines[i];
if(rel < 0)
rel = -rel;
/* Change from JP to JR */
if(rel <= JP_REL) {
buf[2] = 'R';
++jumps_saved;
break;
}
}
/* Next label */
plb += LAB_SIZ;
}
}
}
/* Write the line */
if(fprintf(fp_out, "%s\n", buf) < 0)
error_ext(ERR_WRITE, fn_out);
++line;
}
/* Close input and output files */
close_all();
}
/* Test all rules
*/
test_rules()
{
int i;
char *pch;
/* Open input and output files */
open_all();
/* Test all input lines */
while(fill_lines()) {
/* Skip this line? */
if(!lin_skip[0]) {
/* Test all rules against lines in buffer */
for(i = 0; i < rules_max; ++i) {
#if DEBUG
printf("DEBUG: Testing rule %3d, line %4d", i, out_lines);
#endif
/* Match? */
if((pch = test_rule(rules[i])) != NULL) {
#if DEBUG
printf(" : MATCH");
#endif
/* Match */
rules_saved[i] += *pch;
saved += *pch;
/* Exit on EOF */
if(!fill_lines())
break;
/* Skip this line? */
if(lin_skip[0])
break;
/* Loop again from the beginning */
i = -1;
}
#if DEBUG
printf("\n");
#endif
}
/* Exit on EOF */
if(!lines)
break;
}
/* Out 1st line in buffer */
if(fprintf(fp_out, "%s\n", lin[0]) < 0)
error_ext(ERR_WRITE, fn_out);
++out_lines;
make_room(-1);
}
/* Close input and output files */
close_all();
}
/* Test and output a rule
----------------------
Returns pointer to bytes saved field on success,
or NULL on failure.
*/
test_rule(rul_ptr)
char *rul_ptr;
{
int i, rul_lines;
char *pch;
/* Number of lines in the rule */
rul_lines = *rul_ptr++;
/* Abort if there are not enough lines in buffer */
if(lines < rul_lines)
return NULL;
/* Reset variables */
//var_reset();
for(i = 0; i < SYM_MAX; ++i) {
*(pch = sym[i]) = '\0';
}
for(i = 0; i < NUM_MAX; ++i) {
*(pch = num[i]) = '\0';
}
/* Test all lines in rule */
for(i = 0; i < rul_lines; ++i) {
if(lin_skip[i])
return NULL;
if((rul_ptr = test_line(rul_ptr, lin[i])) == NULL)
return NULL;
}
/* Success */
return out_rule(rul_ptr, rul_lines);
}
/* Test a rule line
----------------
Returns pointer to following char. after the line + ZERO (next rule line or ZERO) on success,
or NULL on failure.
*/
test_line(rul_line, src_line)
char *rul_line, *src_line;
{
char *pch;
int len;
/* Process the whole rule line */
while(*rul_line) {
/* Check EOL in source line */
//if(!(*src_line))
// return NULL;
/* Check normal character in rule line */
if(*rul_line != SPECIAL) {
//if(*rul_line != *src_line)
// return NULL;
//++rul_line;
//++src_line;
//continue;
if(*rul_line++ == *src_line++)
continue;
return NULL;
}
/* It's special */
++rul_line;
if((pch = get_sym(rul_line)) != NULL) {
if(*pch) {
if(memcmp(src_line, pch, (len = strlen(pch))))
return NULL;
src_line += len;
}
else if((src_line = get_field(pch, src_line, SYM_LEN, 0)) == NULL)
return NULL;
}
else if((pch = get_num(rul_line)) != NULL) {
if(*pch) {
if(memcmp(src_line, pch, (len = strlen(pch))))
return NULL;
src_line += len;
}
else if((src_line = get_field(pch, src_line, NUM_LEN, 1)) == NULL)
return NULL;
}
else if(*rul_line == SPECIAL) {
if(*src_line++ != SPECIAL)
return NULL;
}
else
error(ERR_NO_SPC);
++rul_line;
}
/* rul_line is exhausted */
return *src_line ? NULL : ++rul_line;
}
/* Get field value
---------------
Fills dst with value + ZERO from src, and returns updated src pointer,
or NULL on failure.
*/
get_field(dst, src, max, dec)
char *dst, *src; int max, dec;
{
int ch, len;
/* Setup counter */
len = max;
/* Numeric or alphanumeric? */
if(dec) {
if(*src == '-') {
*dst++ = *src++; --len;
}
}
else if(!isalpha(*src))
return NULL;
/* Read value */
while((ch = *src)) {
/* Read char. */
if(dec ? isdigit(ch) : isalpha(ch) || isdigit(ch) || ch == '_') {
if(len--)
*dst++ = *src++;
else
return NULL;
}
else
break;
}
/* Set ZERO */
*dst = '\0';
/* Return success or failure */
return len < max ? src : NULL;
}
/* Transform and output a rule
---------------------------
Return pointer to the bytes saved.
*/
out_rule(out_ptr, rul_lines)
char *out_ptr; int rul_lines;
{
int i, prt_lines;
prt_lines = *out_ptr++;
make_room(prt_lines - rul_lines);
for(i = 0; i < prt_lines; ++i)
out_ptr = out_line(out_ptr, lin[i]);
return out_ptr;
}
/* Transform and output a rule line
--------------------------------
Returns pointer to following char. after the ZERO.
*/
out_line(out_ptr, dst_ptr)
char *out_ptr, *dst_ptr;
{
int s1, s2, r;
char *pch;
int ch1, ch2;
while(*out_ptr) {
if(*out_ptr != SPECIAL) {
*dst_ptr++ = *out_ptr++;
continue;
}
/* It's special */
++out_ptr;
if(*out_ptr == SPC_NUMOP) {
if(!isdigit(*(++out_ptr))) {
/* Unary expression? */
ch1 = *out_ptr++;
if((pch = get_num(out_ptr++)) == NULL)
error(ERR_NO_SPC);
s1 = atoi(pch);
switch(ch1) {
case '!' : r = !s1; break;
case '-' : r = -s1; break;
case '~' : r = ~s1; break;
default : error(ERR_NO_SPC); break;
}
sprintf(dst_ptr, "%d", r);
dst_ptr += strlen(dst_ptr);
continue;
}
/* Binary expression? */
if((pch = get_num(out_ptr++)) == NULL)
error(ERR_NO_SPC);
s1 = atoi(pch);
ch1 = *out_ptr++;
ch2 = isdigit(*out_ptr) ? 0 : *out_ptr++;
if((pch = get_num(out_ptr)) == NULL)
error(ERR_NO_SPC);