-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokc.cpp
11443 lines (11217 loc) · 300 KB
/
tokc.cpp
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
#define _TOKC_
#include <fcntl.h> /* O_ constant definitions */
#include <sys/stat.h>
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#endif
#include "misc.h"
#include "tok.h"
#if !defined(__WIN32__)
void GetFileTime(int fd, struct ftime *buf);
#else
#include <time.h>
#endif
#define MAXIF 32
// idrec *crec=NULL;
char invaliddecrem[] = "invalid token to be decremented";
char mesLOOPNZ[] = "LOOPNZ";
char mesIF[] = "IF";
char mesELSE[] = "ELSE";
char mesWHILE[] = "WHILE";
char mesFOR[] = "FOR";
char mesRETURN[] = "RETURN";
int tok, tok2; /* token holders current, next */
unsigned char string[STRLEN], string2[STRLEN + 20],
string3[STRLEN]; //содержит не преобразованную строку
unsigned int posts = 0; /* number of post entrys */
postinfo *postbuf;
unsigned int maxposts = MAXPOSTS;
unsigned int secondcallnum = 1; /* # of different second calls and labels */
unsigned int externnum = 0;
unsigned long postsize = 0; /* total size of all post vars */
unsigned int poststrptr = 0; /* post string index in output */
unsigned int outptrsize = MAXDATA; //размер выходного буфера для кода
unsigned int outdatasize = MAXDATA; //размер выходного буфера для данных
long runfilesize;
int error = 0; /* current error number holder */
unsigned char dos1 = 0,
dos2 = 0; /* DOS version required for program execution */
unsigned char cpu = 0; /* CPU required, 2 for 80286, 3 for 80386, ... */
unsigned int paramsize = 0; /* byte size of all procedure parameters */
unsigned int localsize = 0; /* byte size of all procedure local variables */
unsigned int procedure_start = 0; /* address of start of procedure */
unsigned int current_proc_type; /* current procedure type */
int returntype; /* return type, (void, byte, word, ...) */
unsigned char warning;
/*+++++++++++++++++++++++ Установки по умолчанию +++++++++++++++++++++++*/
unsigned char am32 = FALSE; // режим 32 битной адресации
unsigned char comfile = file_com; // output file format
unsigned char optimizespeed = 1; // optimize for size or speed flag
unsigned char alignword = 1; // flag whether or not to align words
unsigned char aligner = 0; // value used to align words
unsigned char header = 1; // output SPHINX C-- Ver1 Ver2 header
unsigned char chip = 0; // CPU optimization (286,386,486,...)
unsigned char killctrlc = 0; // add disable CTRL-C code in header
unsigned int stacksize = 2048; // stack size (2048 default)
unsigned char splitdata = FALSE; //отделить данные от кода
unsigned char AlignCycle = FALSE; //выравнивать начала циклов
/*+++++++++++++++++++ end of flexable compiler options ++++++++++++++++++++*/
unsigned char notdoneprestuff = TRUE; // flag if initial stuff has been entered
unsigned int datasize = 0, alignersize = 0; /* size of data and other */
unsigned int outptr = 0x100, outptrdata = 0x100; /* ptr to output */
unsigned char *output;
unsigned char *outputdata = NULL;
unsigned int linenumber = 0;
unsigned char dynamic_flag = 0; //флаг обработки динамических элементов
unsigned char *input; /* dynamic input buffer */
unsigned int endinptr; /* end index of input array */
unsigned int inptr; /* index in input buffer */
unsigned char cha; /* pipe byte for token production */
char endoffile; /* end of input file flag */
unsigned char insertmode = FALSE;
unsigned int numblocks = 0; //номер вложенного блока
treelocalrec *tlr = NULL; //цепочка локальных блоков
treelocalrec *btlr = NULL; //цепочка использованых локальных блоков
RETLIST *listreturn = NULL;
unsigned int numreturn = 0;
idrec *staticlist;
unsigned char stat_reg[8]; //таблица занятости регистров
int sizestack = 0; //размер не компенсированных параметров функций
unsigned char addstack = TRUE;
extern char shorterr[];
extern unsigned long long li[];
/*-----------------01.05.98 19:22-------------------
дополнительные переменные для реализации BREAK CONTINUE
--------------------------------------------------*/
#define MAXIN 100 //максимальная вложеность циклов
unsigned int numbr = 0; //счетчик общего числа циклов LOOP DO-WHILE...
unsigned int listbr[MAXIN]; //таблица номеров циклов
unsigned int usebr[MAXIN]; //использовано break
unsigned int useco[MAXIN]; //использовано continue
unsigned int curbr = 0, curco = 0; //текущаявложеность циклов
unsigned int startStartup = 0x100;
unsigned int endStartup = 0;
unsigned char useStartup = FALSE;
unsigned char notpost = FALSE;
int retproc;
int lastcommand; //последний оператор в блоке
unsigned char FastCallApi = TRUE; //разрешить быстрый вызов API процедур
unsigned char FixUp = FALSE; //Делать ли таблицу перемещений
unsigned char AlignProc = FALSE;
//------- работа с union ---------------------------------------------
char param[256]; //буфер для параметров процедуры
char *BackTextBlock; //буфер для перенесенного текста
int SizeBackBuf = 0, MaxSizeBackBuf;
FILEINFO *startfileinfo = NULL;
unsigned int totalmodule = 0;
unsigned int currentfileinfo;
unsigned char setzeroflag; //операция меняет zero flag
unsigned char notunreach = FALSE;
unsigned int initBP = 0;
int inlineflag = 0; // flag for disabling entry and exit codes production
unsigned char fstatic = FALSE;
unsigned long addESP = 0; //добавка стека
unsigned char blockproc = FALSE; //идетразборка блока функции
unsigned int updatelocalvar(char *str, int tok, unsigned int num);
void setuprm();
void doloop(unsigned int typeb); /* both short and long loops */
void doBREAK(unsigned char typeb);
void doCONTINUE(unsigned char typeb);
void dowhile(unsigned int typeb);
void MakeContinue(unsigned char typeb);
void dofor(unsigned int typeb);
void dodo();
void globalvar(); /* both initialized and unitialized combined */
void doswitch();
void CalcRegPar(int reg, int def, char *&ofsstr);
void JXorJMP();
int loadinputfile(char *inpfile);
int SaveStartUp(int size, char *var_name);
void LoadData(unsigned int size, int filehandle);
void SetNewTok(int type, int typev);
void doreturn(int type = tokens); /* do return(...); */
void notnegit(int notneg);
void insertcode(); // force code procedure at specified location
void interruptproc();
void dobigif();
void doif(void);
void doasmblock();
void declareextern();
unsigned long dounion(int, int);
void RunBackText();
int FindDublString(int segm, unsigned int len, int term);
void *liststring = NULL; //цепочка информационных блоков о строках
void GetNameLabel(int type, int num);
void CheckPosts();
SAVEPAR *SRparam(int save,
SAVEPAR *par); // save or restore global param compiler
void AddRetList(int pos, int line, int type);
void CheckRealizable();
void declare_procedure(int oflag, int orm, int npointr);
void labelindata();
void AddRegistr(int razr, int reg);
void ClearRegister();
int GetRegister(int mode = 0);
void RegAddNum(int reg);
void dowhilefast(unsigned int typeb);
int getrazr(int type);
void RestoreSaveReg();
void killlocals(/*int endp=TRUE*/);
void leaveproc();
int IsSaveReg();
void CorrectParamVar();
extern void ManyLogicCompare();
extern void maxdataerror();
extern void CompareOr();
extern void dynamiclabelerror();
extern void retvoid();
extern int numberbreak;
SAVEREG savereg;
SAVEREG *psavereg = &savereg;
int loadfile(char *filename, int firstflag) {
int hold;
for (int i = 0; i <= numfindpath; i++) {
sprintf((char *)string2, "%s%s",
findpath[(firstflag == 0 ? i : numfindpath - i)], filename);
if ((hold = loadinputfile((char *)string2)) != -2)
break;
if (firstflag == 2 || (firstflag == 0 && (i + 1) == numfindpath))
break;
}
if (hold == -2) {
unableopenfile(filename); //сообщение о ошибке
exit(e_cannotopeninput); //завершить работу если не смогли загрузить файл
}
return hold;
}
void compilefile(char *filename, int firstflag) {
int hold;
hold = loadfile(filename, firstflag);
if (hold == 1 || hold == -1)
return;
if (strcmp(filename, "startup.h--") == 0)
startupfile = currentfileinfo;
inptr = 0;
endoffile = 0;
startline = (char *)input;
endinput = startline + endinptr;
warning = gwarning;
nextchar();
cha2 = cha; //символ из буфера
inptr2 = inptr; //запомн указатель на след символ
linenum2 = 1; //номер строки
{ //проверка на файл ресурсов и его обработка
char *a;
if ((a = strrchr(filename, '.')) != NULL) {
if (strcasecmp(a, ".rc") == 0) {
input_res();
free(input);
return;
}
}
}
nexttok(); //опр тип первого и второго токена
while (tok != tk_eof) { //цикл пока не кончится файл
while (tok == tk_question) {
directive(); //обработка директив
if (tok == tk_semicolon)
nexttok();
}
usedirectiv = FALSE;
if (notdoneprestuff == TRUE)
doprestuff(); // startup
switch (tok) {
case tk_ID:
case tk_id:
if (FindTeg(TRUE) != NULL) {
InitStruct();
break;
}
if (tok2 == tk_colon) {
labelindata();
break;
}
case tk_far:
case tk_cdecl:
case tk_pascal:
case tk_stdcall:
case tk_fastcall:
case tk_declare:
case tk_undefproc:
case tk_float:
case tk_long:
case tk_dword:
case tk_word:
case tk_byte:
case tk_char:
case tk_int:
case tk_void:
case tk_export:
case tk_qword:
case tk_double:
case tk_fpust:
if ((hold = testInitVar()) == FALSE)
define_procedure();
else if (hold == TRUE)
globalvar();
break;
case tk_struct:
InitStruct();
break;
case tk_interrupt:
interruptproc();
break;
case tk_at:
insertcode();
break; //вставка регистровой процедуры
case tk_colon:
nexttok();
dynamic_flag = 2;
break; // опр динамической процедуры
case tk_inline:
if (testInitVar()) {
preerror("Bad header dynamic function");
nexttok();
}
dynamic_proc();
break;
case tk_static:
fstatic = 2;
nexttok();
break;
case tk_enum:
doenum();
break;
case tk_from:
nexttok();
dofrom();
nextseminext();
break;
case tk_extract:
nexttok();
doextract();
seminext();
break;
case tk_loop:
case tk_while:
case tk_do:
case tk_else:
case tk_ELSE:
case tk_if:
case tk_IF:
case tk_interruptproc:
case tk_proc:
case tk_charvar:
case tk_intvar:
case tk_bytevar:
case tk_longvar:
case tk_dwordvar:
case tk_floatvar:
case tk_qwordvar:
case tk_doublevar:
case tk_wordvar:
idalreadydefined();
break;
case tk_reg32:
case tk_debugreg:
case tk_controlreg:
case tk_testreg:
case tk_reg:
case tk_seg:
case tk_beg:
case tk_reg64:
preerror("register name cannot be used as an identifier");
nexttok();
case tk_eof:
break;
case tk_locallabel:
internalerror("local label token found outside function block.");
break;
case tk_extern:
declareextern();
break;
case tk_union:
dynamic_flag = 0;
dounion(TRUE, fstatic == 0 ? 0 : f_static);
break;
case tk_semicolon:
nexttok();
break;
case tk_asm:
if (tok2 == tk_openbrace)
doasmblock();
else
doasm();
break;
case tk_idasm:
doasm(TRUE);
break;
case tk_dollar:
doasm(FALSE);
break;
default:
unuseableinput();
/* while(itok.type==tp_stopper&&tok!=tk_eof)*/
nexttok();
break;
}
if (fstatic)
fstatic--;
else if (dynamic_flag)
dynamic_flag--;
}
(startfileinfo + currentfileinfo)->stlist = staticlist;
free(input);
}
/* ------------------- output procedures start ------------------- */
int CheckCodeSize()
//проверка размера буфера для кода
{
if (!am32) {
maxoutputerror();
return FALSE;
}
outptrsize += MAXDATA;
output = (unsigned char *)REALLOC(output, outptrsize);
if (splitdata == FALSE)
outputdata = output;
return TRUE;
}
int CheckDataSize()
//проверка размера буфера для кода
{
if (!am32) {
maxoutputerror();
return FALSE;
}
outdatasize += MAXDATA;
outputdata = (unsigned char *)REALLOC(outputdata, outdatasize);
return TRUE;
}
void op(int byte) {
if (outptr >= outptrsize && CheckCodeSize() == FALSE)
return;
output[outptr++] = (unsigned char)byte;
if (splitdata == FALSE)
outptrdata = outptr;
retproc = FALSE;
}
void opd(int byte) {
if (splitdata == FALSE) {
if (outptr >= outptrsize && CheckCodeSize() == FALSE)
return;
output[outptr++] = (unsigned char)byte;
outptrdata = outptr;
} else {
if (outptrdata >= outdatasize && CheckDataSize() == FALSE)
return;
outputdata[outptrdata++] = (unsigned char)byte;
}
}
void CorrectOfsBit(int bitofs) {
bitofs = (bitofs + 7) / 8;
if (splitdata)
outptrdata += bitofs;
else {
outptr += bitofs;
outptrdata = outptr;
}
}
long GetBitMask(int ofs, int size) { return (~((li[size] - 1) << ofs)); }
void opb(unsigned long num, unsigned int ofs, unsigned int size) {
int s;
//проверить выход за границы блока памяти
s = (ofs + size + 7) / 8;
if (splitdata == FALSE) {
if ((outptr + s + 8) >= outptrsize && CheckCodeSize() == FALSE)
return;
} else {
if ((outptrdata + s + 8) >= outdatasize && CheckDataSize() == FALSE)
return;
}
if (size != 32)
num = num & (li[size] - 1);
s = outptrdata + ofs / 8;
ofs = ofs % 8;
*(long *)&outputdata[s] &= GetBitMask(ofs, size);
*(long *)&outputdata[s] |= (num << ofs);
// printf("ofs=%Xh mask=%X value=%X\n",s,GetBitMask(ofs,size),(num<<ofs));
if ((ofs + size) > 32) {
*(long *)&outputdata[s + 4] &= GetBitMask(0, ofs + size - 32);
*(long *)&outputdata[s + 4] |= (num >> (64 - ofs - size));
// printf("continue ofs=%Xh mask=%X
// value=%X\n",s+4,GetBitMask(0,ofs+size-32),num>>(64-ofs-size));
}
}
void outword(unsigned int num) {
op(num);
op(num / 256);
}
void outwordd(unsigned int num) {
opd(num);
opd(num / 256);
}
void outdword(unsigned long num) {
outword((unsigned int)(num & 0xFFFFL));
outword((unsigned int)(num / 0x10000L));
}
void outdwordd(unsigned long num) {
outwordd((unsigned int)(num & 0xFFFFL));
outwordd((unsigned int)(num / 0x10000L));
}
void outqword(unsigned long long num) {
outdword((unsigned long)(num & 0xFFFFFFFFL));
outdword((unsigned long)(num / 0x100000000L));
}
void outqwordd(unsigned long long num) {
outdwordd((unsigned long)(num & 0xFFFFFFFFL));
outdwordd((unsigned long)(num / 0x100000000L));
}
void doasmblock() {
nexttok();
useasm = TRUE;
expecting(tk_openbrace);
for (;;) {
if (tok == tk_closebrace)
break;
if (tok == tk_eof) {
unexpectedeof();
break;
}
lastcommand = tok;
if (dbg)
AddLine();
doasm(TRUE);
}
useasm = FALSE;
nexttok();
}
void doblock() {
expecting(tk_openbrace);
doblock2();
/* for(;;){
if(tok==tk_closebrace)break;
if(tok==tk_eof){
unexpectedeof();
break;
}
docommand();
}
RestoreStack();*/
}
void doblock2() {
for (;;) {
if (tok == tk_closebrace)
break;
if (tok == tk_eof) {
unexpectedeof();
break;
}
docommand();
}
if (numblocks == 1 && addstack && sizestack && localsize && am32 && ESPloc &&
IsSaveReg() == FALSE) {
localsize += sizestack;
sizestack = 0;
} else
RestoreStack();
}
void gotodo() {
nexttok();
if (gotol(0))
nexttok();
seminext();
}
void GOTOdo() {
nexttok();
if (GOTO())
nexttok();
seminext();
}
void docommand() /* do a single command */
{
unsigned int useflag;
useflag = 0;
if (dbg)
AddLine();
// loops:
lastcommand = tok;
// printf("tok=%d %s\n",tok,itok.name);
switch (tok) {
case tk_ID:
useflag++;
case tk_id:
if ((useflag = doid((char)useflag, tk_void)) != tokens) {
nextseminext();
if (useflag == tk_fpust)
preerror("function returned parametr in FPU stack");
} else if (tok != tk_closebrace)
docommand();
break;
case tk_apiproc:
case tk_undefproc:
case tk_declare:
if (doanyundefproc() != tokens)
nextseminext();
else if (tok != tk_closebrace)
docommand();
break;
case tk_proc:
doanyproc();
nextseminext();
break;
case tk_interruptproc:
outword(0x0E9C); // pushf //push cs
useflag = itok.post;
callloc(itok.number);
nexttok();
expecting(tk_openbracket);
expecting(tk_closebracket);
#ifdef OPTVARCONST
FreeGlobalConst();
#endif
seminext();
clearregstat(useflag);
break;
case tk_bits:
dobits();
break;
case tk_charvar:
useflag = 1;
case tk_bytevar:
dobytevar(useflag);
break;
case tk_intvar:
useflag = 1;
case tk_wordvar:
do_d_wordvar(useflag, r16);
break;
case tk_longvar:
useflag = 1;
case tk_dwordvar:
do_d_wordvar(useflag, r32);
break;
case tk_doublevar:
useflag = 4;
case tk_floatvar:
dofloatvar(useflag, tk_floatvar, tk_semicolon);
break;
case tk_qwordvar:
doqwordvar();
break;
case tk_fpust:
dofloatstack(itok.number);
break;
case tk_structvar:
dostruct();
break;
case tk_pointer:
dopointer();
break;
case tk_mult:
dovalpointer();
break;
case tk_RETURN:
case tk_return:
RestoreStack();
#ifdef OPTVARCONST
ClearLVIC();
#endif
doreturn(tok);
CheckRealizable();
break;
case tk_at:
nexttok();
if (tok2 == tk_colon) {
LLabel();
if (tok != tk_closebrace)
docommand();
} else if (macros(tk_void) != 0)
nextseminext();
break;
case tk_if:
RestoreStack();
doif();
break;
case tk_IF:
RestoreStack();
dobigif();
break;
case tk_loopnz:
case tk_LOOPNZ:
case tk_loop:
RestoreStack();
doloop(tok);
break;
case tk_while:
case tk_WHILE:
RestoreStack();
dowhilefast(tok);
break;
case tk_do:
RestoreStack();
dodo();
break;
case tk_for:
case tk_FOR:
RestoreStack();
dofor(tok);
break;
case tk_reg32:
doreg_32((unsigned int)itok.number, r32);
break;
case tk_reg:
doreg_32((unsigned int)itok.number, r16);
break;
case tk_beg:
dobeg((unsigned int)itok.number);
break;
case tk_reg64:
doreg64(itok.number);
break;
case tk_seg:
doseg((unsigned int)itok.number);
break;
case tk_openbrace:
startblock();
doblock();
nexttok();
endblock();
break;
case tk_from:
nexttok();
dofrom();
nextseminext();
break;
case tk_extract:
nexttok();
doextract();
seminext();
break;
case tk_minus:
useflag = 8;
case tk_not:
notnegit(useflag);
nextseminext();
break;
case tk_locallabel:
RestoreStack();
define_locallabel();
break;
case tk_camma:
case tk_semicolon:
nexttok();
break;
case tk_else:
preerror("else without preceeding if or IF");
nexttok();
break;
case tk_ELSE:
preerror("ELSE without preceeding IF or if");
nexttok();
break;
case tk_eof:
unexpectedeof();
break;
case tk_void:
case tk_long:
case tk_dword:
case tk_word:
case tk_byte:
case tk_int:
case tk_char:
preerror("cannot declare variables within function { } block");
nexttok();
break;
case tk_GOTO:
RestoreStack();
GOTOdo();
CheckRealizable();
break;
case tk_goto:
RestoreStack();
gotodo();
CheckRealizable();
break;
case tk_BREAK:
RestoreStack();
doBREAK(BREAK_SHORT);
CheckRealizable();
break;
case tk_break:
RestoreStack();
doBREAK((unsigned char)(am32 == FALSE ? BREAK_NEAR : BREAK_32));
CheckRealizable();
break;
case tk_CONTINUE:
RestoreStack();
doCONTINUE(CONTINUE_SHORT);
CheckRealizable();
break;
case tk_continue:
RestoreStack();
doCONTINUE((unsigned char)(am32 == FALSE ? CONTINUE_NEAR : CONTINUE_32));
CheckRealizable();
break;
case tk_asm:
if (tok2 == tk_openbrace)
doasmblock();
else
doasm();
break;
case tk_idasm:
useflag = TRUE;
case tk_dollar:
doasm(useflag);
break;
case tk_SWITCH:
case tk_switch:
RestoreStack();
doswitch();
break;
case tk_delete:
dodelete();
break;
case tk_new:
donew();
seminext();
break;
case tk_question:
// calcnumber=FALSE;
while (tok == tk_question)
directive();
break;
/* case tk_openbracket:
nexttok();
nexttok();
expectingoperand(tk_closebracket);
goto loops;*/
default:
unuseableinput();
break;
}
notunreach = FALSE;
}
void doBREAK(unsigned char typeb) {
if (curbr == 0)
preerror("'BREAK' or 'break' use only in loop, do-while..");
else
MakeBreak(typeb);
nextseminext();
}
void doCONTINUE(unsigned char typeb) {
if (curco == 0)
preerror("'CONTINUE' or 'continue' use only in loop, do-while..");
else
MakeContinue(typeb);
nextseminext();
}
void MakeBreak(unsigned char typeb) {
unsigned int nbr = 0;
if (tok2 == tk_number) {
nexttok();
nbr = itok.number;
if (nbr >= curbr)
preerror("'BREAK' or 'break' on incorrect number skip cycle");
}
numberbreak = nbr;
nbr = curbr - 1 - nbr;
if (usebr[nbr] == 0) {
GetNameLabel(tk_break, nbr);
addlocalvar((char *)string2, tk_locallabel, secondcallnum, TRUE);
usebr[nbr] = secondcallnum;
secondcallnum++;
}
addacall(usebr[nbr], typeb);
if (typeb == BREAK_SHORT)
outword(0x00EB); // JMP SHORT
else
jumploc0();
}
void MakeContinue(unsigned char typeb) {
unsigned int nbr = 0;
if (tok2 == tk_number) {
nexttok();
nbr = itok.number;
if (nbr >= curco)
preerror("'CONTINUE' or 'continue' on incorrect number skip cycle");
}
nbr = curco - 1 - nbr;
if (useco[nbr] == 0) {
GetNameLabel(tk_continue, nbr);
// printf("%s nbr=%d\n",(char *)string2,nbr);
addlocalvar((char *)string2, tk_locallabel, secondcallnum, TRUE);
useco[nbr] = secondcallnum;
secondcallnum++;
}
addacall(useco[nbr], typeb);
if (typeb == CONTINUE_SHORT)
outword(0x00EB); // JMP SHORT
else
jumploc0();
}
int CheckExitProc() {
if (strcmp(itok.name, "EXIT") == 0 || strcmp(itok.name, "ABORT") == 0)
return TRUE;
return FALSE;
}
void LLabel() {
localrec *ptr;
#ifdef OPTVARCONST
ClearLVIC();
#endif
RestoreStack();
clearregstat();
switch (tok) {
case tk_id:
case tk_ID:
FindOff((unsigned char *)itok.name, CS);
ptr = addlocalvar(itok.name, tk_number, outptr, TRUE);
if (FixUp)
ptr->rec.flag = f_reloc;
break;
case tk_undefproc:
ptr = addlocalvar(itok.name, tk_number, outptr,
TRUE); //добавить в локальный список
if (FixUp)
ptr->rec.flag = f_reloc;
updatecall((unsigned int)itok.number, outptr,
procedure_start); //обработать ранние обращения
break;
default:
preerror("error declaretion local label");
break;
}
nexttok();
nexttok();
}
void AddApiToPost(unsigned int num) {
CheckPosts();
(postbuf + posts)->type = CALL_32I;
(postbuf + posts)->loc = outptr;
(postbuf + posts)->num = num;
posts++;
outdword(0);
}
/* ---------------------- Procedure Calling Starts -------------------- */
int doanyundefproc(int jumpsend) {
unsigned int cnum, snum;
int returnvalue;
int regs;
char fname[IDLENGTH];
if (tok2 == tk_colon) { // if a label
#ifdef OPTVARCONST
ClearLVIC();
#endif
RestoreStack();
clearregstat();
if (CidOrID() == tk_ID) { // local label that has been used, but not placed
localrec *ptr = addlocalvar(itok.name, tk_number, outptr, TRUE);
if (FixUp)
ptr->rec.flag = f_reloc;
updatecall((unsigned int)itok.number, outptr,
procedure_start); //обработать ранние обращения
} else { //глобальная метка
tok = tk_proc;
itok.number = outptr;
string[0] = 0;
updatecall((unsigned int)updatetree(), (unsigned int)itok.number, 0);
}
nexttok(); // move past id
nexttok(); // move past :
return (tokens);
}
if (tok2 == tk_openbracket) {
strcpy(fname, itok.name);
if (tok ==
tk_declare) { //сменить статус процедуры с объявленой на неизвестную
tok = tk_undefproc;
updatetree();
if (itok.flag & f_classproc)
AddUndefClassProc();
}
cnum = (unsigned int)itok.number;
regs = itok.post;
returnvalue = itok.rm;
unsigned int tproc = itok.flag;
unsigned char apiproc = FALSE;
unsigned int oaddESP = addESP;
int sizestack = -1;
if (tok == tk_apiproc) {
apiproc = TRUE;
sizestack = itok.size; //размер стека под параметры
}
#ifdef OPTVARCONST
if (tproc & f_useidx)
ClearLVIC();
else
FreeGlobalConst();
#endif
int exitproc = CheckExitProc();
snum = initparamproc();
if (sizestack != -1) {
if (snum > (unsigned int)sizestack)