-
Notifications
You must be signed in to change notification settings - Fork 4
/
b.h
184 lines (170 loc) · 3.59 KB
/
b.h
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#define nil NULL
/* machine independent configuration */
enum
{
NCPS = 8, /* number of characters per symbol */
NSYMS = 400, /* Size of symbol table */
NNODES = 200, /* Size of node storage */
NNSTACK = 40, /* Size of node stack */
NSWITCH = 200, /* Size of switch table */
};
enum
{
/* types and keywords */
KeyType = 1,
Intern,
Extern,
Auto,
Param,
Goto,
Return,
If,
While,
Else,
Switch,
Case,
Break,
Default,
};
enum {
Bin = 1,
R = 2,
Lval = 4,
};
/* The string is just a debugging aid */
#define OPS\
X(None, "", 0, 0)\
/* token types */\
X(Space, " ", 0, 0)\
X(DQuote, "\"", 0, 0)\
X(SQuote, "'", 0, 0)\
X(LParen, "(", 0, 0)\
X(RParen, ")", 0, 0)\
X(LBrack, "[", 0, 0)\
X(RBrack, "]", 0, 0)\
X(LBrace, "{", 0, 0)\
X(RBrace, "}", 0, 0)\
X(Semi, ";", 0, 0)\
X(Digit, "digit", 0, 0)\
X(Letter, "letter", 0, 0)\
X(Const, "const", 0, 0)\
X(String, "string", 0, 0)\
X(Name, "name", 0, 0)\
X(Keyword, "key", 0, 0)\
/* misc */\
X(Comma, ",", 6, Bin|R)\
X(Colon, ":", 12, Bin|R)\
X(Quest, "?", 12, Bin|R)\
X(Call, "$", 0, Bin)\
X(Vector, "@", 0, Bin)\
/* binary operators */\
X(Assign, "=", 10, Bin|R|Lval)\
X(Or, "|", 14, Bin)\
X(And, "&", 16, Bin)\
X(Eq, "==", 18, Bin)\
X(Neq, "!=", 18, Bin)\
X(Leq, "<=", 20, Bin)\
X(Less, "<", 20, Bin)\
X(Geq, ">=", 20, Bin)\
X(Greater, ">", 20, Bin)\
X(RShift, ">>", 22, Bin)\
X(LShift, "<<", 22, Bin)\
X(Plus, "+", 24, Bin)\
X(Minus, "-", 24, Bin)\
X(Mod, "%", 26, Bin)\
X(Times, "*", 26, Bin)\
X(Div, "/", 26, Bin)\
X(AOr, "=|", 10, Bin|R|Lval)\
X(AAnd, "=&", 10, Bin|R|Lval)\
X(AEq, "===", 10, Bin|R|Lval)\
X(ANeq, "=!=", 10, Bin|R|Lval)\
X(ALeq, "=<=", 10, Bin|R|Lval)\
X(ALess, "=<", 10, Bin|R|Lval)\
X(AGeq, "=>=", 10, Bin|R|Lval)\
X(AGreater, "=>", 10, Bin|R|Lval)\
X(ARShift, "=>>", 10, Bin|R|Lval)\
X(ALShift, "=<<", 10, Bin|R|Lval)\
X(APlus, "=+", 10, Bin|R|Lval)\
X(AMinus, "=-", 10, Bin|R|Lval)\
X(AMod, "=%", 10, Bin|R|Lval)\
X(ATimes, "=*", 10, Bin|R|Lval)\
X(ADiv, "=/", 10, Bin|R|Lval)\
/* unary operators */\
X(Amp, "&u", 28, R|Lval)\
X(Neg, "-u", 28, R)\
X(Star, "*u", 28, R)\
X(Not, "!u", 28, R)\
X(Preinc, "++u", 28, R|Lval)\
X(Predec, "--u", 28, R|Lval)\
X(Postinc, "u++", 28, R|Lval)\
X(Postdec, "u--", 28, R|Lval)
enum
{
#define X(def, str, prec, flag) def,
OPS
#undef X
Invalid = 255,
};
typedef struct Sym Sym;
struct Sym
{
int type; /* keyword, extern, auto, label */
int val; /* keyword id, label number, stack offset */
char name[NCPS];
};
typedef struct Node Node;
struct Node
{
int op;
union {
Sym *s;
word w; /* const */
int l; /* string label */
Node *n1;
};
Node *n2;
};
typedef struct SwCase SwCase;
struct SwCase
{
word val;
int lab;
};
#define prf(...) fprf(stdout, __VA_ARGS__)
void fprf(FILE *f, char *fmt, ...);
void prologue(void);
void epilogue(void);
void label(int l);
void clab(int cl);
void etcop(int n);
void binop(int n);
void unaop(int n);
void opsym(char *op, char *s);
void oplab(char *op, int l);
void opint(char *op, int i);
void opconst(char *op, word w);
void wdsym(char *s);
void wdlab(int i);
void wdint(int i);
void wdconst(word w);
void global(char *s);
void textsym(void);
void datasym(char *s);
void bsssym(char *s);
void skip(int n);
void setlab(int lab, int val);
void fixup(char *s, int n);
void startstr(int n);
void strchar(int c);
void endstr(void);
extern int loc;
extern int prevfix;
extern FILE *tmpfil1, *tmpfil2;