-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathpreproc.c
8070 lines (7061 loc) · 227 KB
/
preproc.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
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2022 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
/*
* preproc.c macro preprocessor for the Netwide Assembler
*/
/* Typical flow of text through preproc
*
* pp_getline gets tokenized lines, either
*
* from a macro expansion
*
* or
* {
* read_line gets raw text from stdmacpos, or predef, or current input file
* tokenize converts to tokens
* }
*
* expand_mmac_params is used to expand %1 etc., unless a macro is being
* defined or a false conditional is being processed
* (%0, %1, %+1, %-1, %%foo
*
* do_directive checks for directives
*
* expand_smacro is used to expand single line macros
*
* expand_mmacro is used to expand multi-line macros
*
* detoken is used to convert the line back to text
*/
#include "compiler.h"
#include "nctype.h"
#include "nasm.h"
#include "nasmlib.h"
#include "error.h"
#include "preproc.h"
#include "hashtbl.h"
#include "quote.h"
#include "stdscan.h"
#include "eval.h"
#include "tokens.h"
#include "tables.h"
#include "listing.h"
#include "dbginfo.h"
/*
* Preprocessor execution options that can be controlled by %pragma or
* other directives. This structure is initialized to zero on each
* pass; this *must* reflect the default initial state.
*/
static struct pp_config {
bool noaliases;
bool sane_empty_expansion;
} ppconf;
/*
* Preprocessor debug-related flags
*/
static enum pp_debug_flags {
PDBG_MMACROS = 1, /* Collect mmacro information */
PDBG_SMACROS = 2, /* Collect smacro information */
PDBG_LIST_SMACROS = 4, /* Smacros to list file (list option 's') */
PDBG_INCLUDE = 8 /* Collect %include information */
} ppdbg;
/*
* Preprocessor options configured on the command line
*/
static enum preproc_opt ppopt;
typedef struct SMacro SMacro;
typedef struct MMacro MMacro;
typedef struct Context Context;
typedef struct Token Token;
typedef struct Line Line;
typedef struct Include Include;
typedef struct Cond Cond;
/*
* Map of preprocessor directives that are also preprocessor functions;
* if they are at the beginning of a line they are a function if and
* only if they are followed by a (
*/
static bool pp_op_may_be_function[PP_count];
/*
* This is the internal form which we break input lines up into.
* Typically stored in linked lists.
*
* Note that `type' serves a double meaning: TOKEN_SMAC_START_PARAMS is
* not necessarily used as-is, but is also used to encode the number
* and expansion type of substituted parameter. So in the definition
*
* %define a(x,=y) ( (x) & ~(y) )
*
* the token representing `x' will have its type changed to
* tok_smac_param(0) but the one representing `y' will be
* tok_smac_param(1); see the accessor functions below.
*
* TOKEN_INTERNAL_STR is a string which has been unquoted, but should
* be treated as if it was a quoted string. The code is free to change
* one into the other at will. TOKEN_NAKED_STR is a text token which
* should be treated as a string, but which MUST NOT be turned into a
* quoted string. TOKEN_INTERNAL_STRs can contain any character,
* including NUL, but TOKEN_NAKED_STR must be a valid C string.
*/
static inline enum token_type tok_smac_param(int param)
{
return TOKEN_SMAC_START_PARAMS + param;
}
static int smac_nparam(enum token_type toktype)
{
return toktype - TOKEN_SMAC_START_PARAMS;
}
static bool is_smac_param(enum token_type toktype)
{
return toktype >= TOKEN_SMAC_START_PARAMS;
}
/*
* This is tuned so struct Token should be 64 bytes on 64-bit
* systems and 32 bytes on 32-bit systems. It enables them
* to be nicely cache aligned, and the text to still be kept
* inline for nearly all tokens.
*
* We prohibit tokens of length > MAX_TEXT even though
* length here is an unsigned int; this avoids problems
* if the length is passed through an interface with type "int",
* and is absurdly large anyway.
*
* Earlier versions of the source code incorrectly stated that
* examining the text string alone can be unconditionally valid. This
* is incorrect, as some token types strip parts of the string,
* e.g. indirect tokens.
*/
#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum token_type)-sizeof(unsigned int)-1)
#define MAX_TEXT (INT_MAX-2)
struct Token {
Token *next;
enum token_type type;
unsigned int len;
union {
char a[INLINE_TEXT+1];
struct {
char pad[INLINE_TEXT+1 - sizeof(char *)];
char *ptr;
} p;
} text;
};
/*
* Note on the storage of both SMacro and MMacros: the hash table
* indexes them case-insensitively, and we then have to go through a
* linked list of potential case aliases (and, for MMacros, parameter
* ranges); this is to preserve the matching semantics of the earlier
* code. If the number of case aliases for a specific macro is a
* performance issue, you may want to reconsider your coding style.
*/
/*
* Function call tp obtain the expansion of an smacro
*/
typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
/*
* Store the definition of a single-line macro.
*
* Note: for user-defined macros, SPARM_VARADIC and SPARM_DEFAULT are
* currently never set, and SPARM_OPTIONAL is set if and only
* if SPARM_GREEDY is set.
*/
enum sparmflags {
SPARM_PLAIN = 0,
SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
SPARM_STR = 2, /* Convert to quoted string ($) */
SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
SPARM_GREEDY = 8, /* Greedy final parameter (+) */
SPARM_VARADIC = 16, /* Any number of separate arguments */
SPARM_OPTIONAL = 32, /* Optional argument */
SPARM_CONDQUOTE = 64 /* With SPARM_STR, don't re-quote a string */
};
struct smac_param {
Token name;
enum sparmflags flags;
const Token *def; /* Default, if any */
};
struct SMacro {
SMacro *next; /* MUST BE FIRST - see free_smacro() */
char *name;
Token *expansion;
ExpandSMacro expand;
intorptr expandpvt;
struct smac_param *params;
int nparam; /* length of the params structure */
int nparam_min; /* allows < nparam arguments */
int in_progress;
bool recursive;
bool varadic; /* greedy or supports > nparam arguments */
bool casesense;
bool alias; /* This is an alias macro */
};
/*
* "No listing" flags. Inside a loop (%rep..%endrep) we may have
* macro listing suppressed with .nolist, but we still need to
* update line numbers for error messages and debug information...
* unless we are nested inside an actual .nolist macro.
*/
enum nolist_flags {
NL_LIST = 1, /* Suppress list output */
NL_LINE = 2 /* Don't update line information */
};
/*
* Store the definition of a multi-line macro. This is also used to
* store the interiors of `%rep...%endrep' blocks, which are
* effectively self-re-invoking multi-line macros which simply
* don't have a name or bother to appear in the hash tables. %rep
* blocks are signified by having a NULL `name' field.
*
* In a MMacro describing a `%rep' block, the `in_progress' field
* isn't merely boolean, but gives the number of repeats left to
* run.
*
* The `next' field is used for storing MMacros in hash tables; the
* `next_active' field is for stacking them on istk entries.
*
* When a MMacro is being expanded, `params', `iline', `nparam',
* `paramlen', `rotate' and `unique' are local to the invocation.
*/
/*
* Expansion stack. Note that .mmac can point back to the macro itself,
* whereas .mstk cannot.
*/
struct mstk {
MMacro *mstk; /* Any expansion, real macro or not */
MMacro *mmac; /* Highest level actual mmacro */
};
struct MMacro {
MMacro *next;
#if 0
MMacroInvocation *prev; /* previous invocation */
#endif
char *name;
int nparam_min, nparam_max;
enum nolist_flags nolist; /* is this macro listing-inhibited? */
bool casesense;
bool plus; /* is the last parameter greedy? */
bool capture_label; /* macro definition has %00; capture label */
int32_t in_progress; /* is this macro currently being expanded? */
int32_t max_depth; /* maximum number of recursive expansions allowed */
Token *dlist; /* All defaults as one list */
Token **defaults; /* Parameter default pointers */
int ndefs; /* number of default parameters */
Line *expansion;
struct mstk mstk; /* Macro expansion stack */
struct mstk dstk; /* Macro definitions stack */
Token **params; /* actual parameters */
Token *iline; /* invocation line */
struct src_location where; /* location of definition */
unsigned int nparam, rotate;
char *iname; /* name invoked as */
int *paramlen;
uint64_t unique;
uint64_t condcnt; /* number of if blocks... */
struct { /* Debug information */
struct debug_macro_def *def; /* Definition */
struct debug_macro_inv *inv; /* Current invocation (if any) */
} dbg;
};
/* Store the definition of a multi-line macro, as defined in a
* previous recursive macro expansion.
*/
#if 0
struct MMacroInvocation {
MMacroInvocation *prev; /* previous invocation */
Token **params; /* actual parameters */
Token *iline; /* invocation line */
unsigned int nparam, rotate;
int *paramlen;
uint64_t unique;
uint64_t condcnt;
};
#endif
/*
* The context stack is composed of a linked list of these.
*/
struct Context {
Context *next;
const char *name;
struct hash_table localmac;
uint64_t number;
unsigned int depth;
};
static inline const char *tok_text(const struct Token *t)
{
return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
}
/*
* Returns a mutable pointer to the text buffer. The text can be changed,
* but the length MUST NOT CHANGE, in either direction; nor is it permitted
* to pad with null characters to create an artificially shorter string.
*/
static inline char *tok_text_buf(struct Token *t)
{
return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
}
static inline unsigned int tok_check_len(size_t len)
{
if (unlikely(len > MAX_TEXT))
nasm_fatal("impossibly large token");
return len;
}
static inline bool tok_text_match(const struct Token *a, const struct Token *b)
{
return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
}
static inline unused_func bool
tok_match(const struct Token *a, const struct Token *b)
{
return a->type == b->type && tok_text_match(a, b);
}
/* strlen() variant useful for set_text() and its variants */
static size_t tok_strlen(const char *str)
{
return strnlen(str, MAX_TEXT+1);
}
/*
* Set the text field to a copy of the given string; the length if
* not given should be obtained with tok_strlen().
*/
static Token *set_text(struct Token *t, const char *text, size_t len)
{
char *textp;
if (t->len > INLINE_TEXT)
nasm_free(t->text.p.ptr);
nasm_zero(t->text);
t->len = len = tok_check_len(len);
textp = (len > INLINE_TEXT)
? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
memcpy(textp, text, len);
textp[len] = '\0';
return t;
}
/*
* Set the text field to the existing pre-allocated string, either
* taking over or freeing the allocation in the process.
*/
static Token *set_text_free(struct Token *t, char *text, unsigned int len)
{
char *textp;
if (t->len > INLINE_TEXT)
nasm_free(t->text.p.ptr);
nasm_zero(t->text);
t->len = len = tok_check_len(len);
if (len > INLINE_TEXT) {
textp = t->text.p.ptr = text;
} else {
textp = memcpy(t->text.a, text, len);
nasm_free(text);
}
textp[len] = '\0';
return t;
}
/*
* Allocate a new buffer containing a copy of the text field
* of the token.
*/
static char *dup_text(const struct Token *t)
{
size_t size = t->len + 1;
char *p = nasm_malloc(size);
return memcpy(p, tok_text(t), size);
}
/*
* Multi-line macro definitions are stored as a linked list of
* these, which is essentially a container to allow several linked
* lists of Tokens.
*
* Note that in this module, linked lists are treated as stacks
* wherever possible. For this reason, Lines are _pushed_ on to the
* `expansion' field in MMacro structures, so that the linked list,
* if walked, would give the macro lines in reverse order; this
* means that we can walk the list when expanding a macro, and thus
* push the lines on to the `expansion' field in _istk_ in reverse
* order (so that when popped back off they are in the right
* order). It may seem cockeyed, and it relies on my design having
* an even number of steps in, but it works...
*
* Some of these structures, rather than being actual lines, are
* markers delimiting the end of the expansion of a given macro.
* This is for use in the cycle-tracking and %rep-handling code.
* Such structures have `finishes' non-NULL, and `first' NULL. All
* others have `finishes' NULL, but `first' may still be NULL if
* the line is blank.
*/
struct Line {
Line *next;
MMacro *finishes;
Token *first;
struct src_location where; /* Where defined */
};
/*
* To handle an arbitrary level of file inclusion, we maintain a
* stack (ie linked list) of these things.
*
* Note: when we issue a message for a continuation line, we want to
* issue it for the actual *start* of the continuation line. This means
* we need to remember how many lines to skip over for the next one.
*/
struct Include {
Include *next;
FILE *fp;
Cond *conds;
Line *expansion;
uint64_t nolist; /* Listing inhibit counter */
uint64_t noline; /* Line number update inhibit counter */
struct mstk mstk;
struct src_location where; /* Filename and current line number */
int32_t lineinc; /* Increment given by %line */
int32_t lineskip; /* Accounting for passed continuation lines */
};
/*
* File real name hash, so we don't have to re-search the include
* path for every pass (and potentially more than that if a file
* is used more than once.)
*/
struct hash_table FileHash;
/*
* Counters to trap on insane macro recursion or processing.
* Note: for smacros these count *down*, for mmacros they count *up*.
*/
struct deadman {
int64_t total; /* Total number of macros/tokens */
int64_t levels; /* Descent depth across all macros */
bool triggered; /* Already triggered, no need for error msg */
};
static struct deadman smacro_deadman, mmacro_deadman;
/*
* Conditional assembly: we maintain a separate stack of these for
* each level of file inclusion. (The only reason we keep the
* stacks separate is to ensure that a stray `%endif' in a file
* included from within the true branch of a `%if' won't terminate
* it and cause confusion: instead, rightly, it'll cause an error.)
*/
enum cond_state {
/*
* These states are for use just after %if or %elif: IF_TRUE
* means the condition has evaluated to truth so we are
* currently emitting, whereas IF_FALSE means we are not
* currently emitting but will start doing so if a %else comes
* up. In these states, all directives are admissible: %elif,
* %else and %endif. (And of course %if.)
*/
COND_IF_TRUE, COND_IF_FALSE,
/*
* These states come up after a %else: ELSE_TRUE means we're
* emitting, and ELSE_FALSE means we're not. In ELSE_* states,
* any %elif or %else will cause an error.
*/
COND_ELSE_TRUE, COND_ELSE_FALSE,
/*
* These states mean that we're not emitting now, and also that
* nothing until %endif will be emitted at all. COND_DONE is
* used when we've had our moment of emission
* and have now started seeing %elifs. COND_NEVER is used when
* the condition construct in question is contained within a
* non-emitting branch of a larger condition construct,
* or if there is an error.
*/
COND_DONE, COND_NEVER
};
struct Cond {
Cond *next;
enum cond_state state;
};
#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
/*
* These defines are used as the possible return values for do_directive
*/
#define NO_DIRECTIVE_FOUND 0
#define DIRECTIVE_FOUND 1
/*
* Condition codes. Note that we use c_ prefix not C_ because C_ is
* used in nasm.h for the "real" condition codes. At _this_ level,
* we treat CXZ and ECXZ as condition codes, albeit non-invertible
* ones, so we need a different enum...
*/
static const char * const conditions[] = {
"a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
"na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
"np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
};
enum pp_conds {
c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
c_none = -1
};
static const enum pp_conds inverse_ccs[] = {
c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
};
/*
* Directive names.
*/
/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
static int is_condition(enum preproc_token arg)
{
return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
}
static int StackSize = 4;
static const char *StackPointer = "ebp";
static int ArgOffset = 8;
static int LocalOffset = 0;
static Context *cstk;
static Include *istk;
static const struct strlist *ipath_list;
static struct strlist *deplist;
static uint64_t unique; /* unique identifier numbers */
static Line *predef = NULL;
static bool do_predef;
static enum preproc_mode pp_mode;
/*
* The current set of multi-line macros we have defined.
*/
static struct hash_table mmacros;
/*
* The current set of single-line macros we have defined.
*/
static struct hash_table smacros;
/*
* The multi-line macro we are currently defining, or the %rep
* block we are currently reading, if any.
*/
static MMacro *defining;
static uint64_t nested_mac_count;
static uint64_t nested_rep_count;
/*
* The number of macro parameters to allocate space for at a time.
*/
#define PARAM_DELTA 16
/*
* The standard macro set: defined in macros.c in a set of arrays.
* This gives our position in any macro set, while we are processing it.
* The stdmacset is an array of such macro sets.
*/
static macros_t *stdmacpos;
static macros_t **stdmacnext;
static macros_t *stdmacros[8];
static macros_t *extrastdmac;
/*
* Map of which %use packages have been loaded
*/
static bool *use_loaded;
/*
* Forward declarations.
*/
static void pp_add_stdmac(macros_t *macros);
static Token *expand_mmac_params(Token * tline);
static Token *expand_smacro(Token * tline);
static Token *expand_id(Token * tline);
static Context *get_ctx(const char *name, const char **namep);
static Token *make_tok_num(Token *next, int64_t val);
static int64_t get_tok_num(const Token *t, bool *err);
static Token *make_tok_qstr(Token *next, const char *str);
static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
static Token *make_tok_char(Token *next, char op);
static Token *new_Token(Token * next, enum token_type type,
const char *text, size_t txtlen);
static Token *new_Token_free(Token * next, enum token_type type,
char *text, size_t txtlen);
static Token *dup_Token(Token *next, const Token *src);
static Token *new_White(Token *next);
static Token *delete_Token(Token *t);
static Token *steal_Token(Token *dst, Token *src);
static const struct use_package *
get_use_pkg(Token *t, const char *dname, const char **name);
static void mark_smac_params(Token *tline, const SMacro *tmpl,
enum token_type type);
/* Safe extraction of token type */
static inline enum token_type tok_type(const Token *x)
{
return x ? x->type : TOKEN_EOS;
}
/* Safe test for token type, false on x == NULL */
static inline bool tok_is(const Token *x, enum token_type t)
{
return tok_type(x) == t;
}
/* True if token is any other kind other that "c", but not NULL */
static inline bool tok_isnt(const Token *x, enum token_type t)
{
return x && x->type != t;
}
/* Whitespace token? */
static inline bool tok_white(const Token *x)
{
return tok_is(x, TOKEN_WHITESPACE);
}
/* Skip past any whitespace */
static inline Token *skip_white(Token *x)
{
while (tok_white(x))
x = x->next;
return x;
}
/* Delete any whitespace */
static Token *zap_white(Token *x)
{
while (tok_white(x))
x = delete_Token(x);
return x;
}
/*
* Unquote a token if it is a string, and set its type to
* TOKEN_INTERNAL_STR.
*/
/*
* Common version for any kind of quoted string; see asm/quote.c for
* information about the arguments.
*/
static const char *unquote_token_anystr(Token *t, uint32_t badctl, char qstart)
{
size_t nlen, olen;
char *p;
if (t->type != TOKEN_STR)
return tok_text(t);
olen = t->len;
p = (olen > INLINE_TEXT) ? t->text.p.ptr : t->text.a;
t->len = nlen = nasm_unquote_anystr(p, NULL, badctl, qstart);
t->type = TOKEN_INTERNAL_STR;
if (olen <= INLINE_TEXT || nlen > INLINE_TEXT)
return p;
nasm_zero(t->text.a);
memcpy(t->text.a, p, nlen);
nasm_free(p);
return t->text.a;
}
/* Unquote any string, can produce any arbitrary binary output */
static const char *unquote_token(Token *t)
{
return unquote_token_anystr(t, 0, STR_NASM);
}
/*
* Same as unquote_token(), but error out if the resulting string
* contains unacceptable control characters.
*/
static const char *unquote_token_cstr(Token *t)
{
return unquote_token_anystr(t, BADCTL, STR_NASM);
}
/*
* Convert a TOKEN_INTERNAL_STR token to a quoted
* TOKEN_STR tokens.
*/
static Token *quote_any_token(Token *t);
static inline unused_func
Token *quote_token(Token *t)
{
if (likely(!tok_is(t, TOKEN_INTERNAL_STR)))
return t;
return quote_any_token(t);
}
/*
* Convert *any* kind of token to a quoted
* TOKEN_STR token.
*/
static Token *quote_any_token(Token *t)
{
size_t len = t->len;
char *p;
p = nasm_quote(tok_text(t), &len);
t->type = TOKEN_STR;
return set_text_free(t, p, len);
}
/*
* In-place reverse a list of tokens.
*/
static Token *reverse_tokens(Token *t)
{
Token *prev = NULL;
Token *next;
while (t) {
next = t->next;
t->next = prev;
prev = t;
t = next;
}
return prev;
}
/*
* getenv() variant operating on an input token
*/
static const char *pp_getenv(const Token *t, bool warn)
{
const char *txt = tok_text(t);
const char *v;
char *buf = NULL;
bool is_string = false;
if (!t)
return NULL;
switch (t->type) {
case TOKEN_ENVIRON:
txt += 2; /* Skip leading %! */
is_string = nasm_isquote(*txt);
break;
case TOKEN_STR:
is_string = true;
break;
case TOKEN_INTERNAL_STR:
case TOKEN_NAKED_STR:
case TOKEN_ID:
is_string = false;
break;
default:
return NULL;
}
if (is_string) {
buf = nasm_strdup(txt);
nasm_unquote_cstr(buf, NULL);
txt = buf;
}
v = getenv(txt);
if (warn && !v) {
/*!
*!pp-environment [on] nonexistent environment variable
*!=environment
*! warns if a nonexistent environment variable
*! is accessed using the \c{%!} preprocessor
*! construct (see \k{getenv}.) Such environment
*! variables are treated as empty (with this
*! warning issued) starting in NASM 2.15;
*! earlier versions of NASM would treat this as
*! an error.
*/
nasm_warn(WARN_PP_ENVIRONMENT,
"nonexistent environment variable `%s'", txt);
v = "";
}
if (buf)
nasm_free(buf);
return v;
}
/*
* Free a linked list of tokens.
*/
static void free_tlist(Token * list)
{
while (list)
list = delete_Token(list);
}
/*
* Free a linked list of lines.
*/
static void free_llist(Line * list)
{
Line *l, *tmp;
list_for_each_safe(l, tmp, list) {
free_tlist(l->first);
nasm_free(l);
}
}
/*
* Free an array of linked lists of tokens
*/
static void free_tlist_array(Token **array, size_t nlists)
{
Token **listp = array;
if (!array)
return;
while (nlists--)
free_tlist(*listp++);
nasm_free(array);
}
/*
* Duplicate a linked list of tokens.
*/
static Token *dup_tlist(const Token *list, Token ***tailp)
{
Token *newlist = NULL;
Token **tailpp = &newlist;
const Token *t;
list_for_each(t, list) {
Token *nt;
*tailpp = nt = dup_Token(NULL, t);
tailpp = &nt->next;
}
if (tailp) {
**tailp = newlist;
*tailp = tailpp;
}
return newlist;
}
/*
* Duplicate a linked list of tokens with a maximum count
*/
static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
{
Token *newlist = NULL;
Token **tailpp = &newlist;
const Token *t;
list_for_each(t, list) {
Token *nt;
if (!cnt--)
break;
*tailpp = nt = dup_Token(NULL, t);
tailpp = &nt->next;
}
if (tailp) {
**tailp = newlist;
if (newlist)
*tailp = tailpp;
}
return newlist;
}
/*
* Duplicate a linked list of tokens in reverse order
*/
static Token *dup_tlist_reverse(const Token *list, Token *tail)
{
const Token *t;
list_for_each(t, list)
tail = dup_Token(tail, t);
return tail;
}
/*
* Append an existing tlist to a tail pointer and returns the
* updated tail pointer.
*/
static Token **steal_tlist(Token *tlist, Token **tailp)
{
*tailp = tlist;
if (!tlist)
return tailp;
list_last(tlist, tlist);
return &tlist->next;
}
/*
* Free an MMacro
*/
static void free_mmacro(MMacro * m)
{
nasm_free(m->name);
free_tlist(m->dlist);
nasm_free(m->defaults);
free_llist(m->expansion);
nasm_free(m);
}
/*
* Clear or free an SMacro
*/
static void free_smacro_members(SMacro *s)
{
if (s->params) {
int i;
for (i = 0; i < s->nparam; i++) {
if (s->params[i].name.len > INLINE_TEXT)