forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.h
140 lines (123 loc) · 4.19 KB
/
defs.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "incdir.h"
// Structure and enum definitions
// Copyright (c) 2019 Warren Toomey, GPL3
enum {
TEXTLEN = 512 // Length of identifiers in input
};
// Commands and default filenames
#define AOUT "a.out"
#define ASCMD "as -o "
#define LDCMD "cc -o "
#define CPPCMD "cpp -nostdinc -isystem "
// Token types
enum {
T_EOF,
// Binary operators
T_ASSIGN, T_ASPLUS, T_ASMINUS,
T_ASSTAR, T_ASSLASH,
T_QUESTION, T_LOGOR, T_LOGAND,
T_OR, T_XOR, T_AMPER,
T_EQ, T_NE,
T_LT, T_GT, T_LE, T_GE,
T_LSHIFT, T_RSHIFT,
T_PLUS, T_MINUS, T_STAR, T_SLASH,
// Other operators
T_INC, T_DEC, T_INVERT, T_LOGNOT,
// Type keywords
T_VOID, T_CHAR, T_INT, T_LONG,
// Other keywords
T_IF, T_ELSE, T_WHILE, T_FOR, T_RETURN,
T_STRUCT, T_UNION, T_ENUM, T_TYPEDEF,
T_EXTERN, T_BREAK, T_CONTINUE, T_SWITCH,
T_CASE, T_DEFAULT, T_SIZEOF, T_STATIC,
// Structural tokens
T_INTLIT, T_STRLIT, T_SEMI, T_IDENT,
T_LBRACE, T_RBRACE, T_LPAREN, T_RPAREN,
T_LBRACKET, T_RBRACKET, T_COMMA, T_DOT,
T_ARROW, T_COLON
};
// Token structure
struct token {
int token; // Token type, from the enum list above
char *tokstr; // String version of the token
int intvalue; // For T_INTLIT, the integer value
};
// AST node types. The first few line up
// with the related tokens
enum {
A_ASSIGN = 1, A_ASPLUS, A_ASMINUS, A_ASSTAR, A_ASSLASH, // 1
A_TERNARY, A_LOGOR, A_LOGAND, A_OR, A_XOR, A_AND, // 6
A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE, A_LSHIFT, A_RSHIFT, // 12
A_ADD, A_SUBTRACT, A_MULTIPLY, A_DIVIDE, // 20
A_INTLIT, A_STRLIT, A_IDENT, A_GLUE, // 24
A_IF, A_WHILE, A_FUNCTION, A_WIDEN, A_RETURN, // 28
A_FUNCCALL, A_DEREF, A_ADDR, A_SCALE, // 33
A_PREINC, A_PREDEC, A_POSTINC, A_POSTDEC, // 37
A_NEGATE, A_INVERT, A_LOGNOT, A_TOBOOL, A_BREAK, // 41
A_CONTINUE, A_SWITCH, A_CASE, A_DEFAULT, A_CAST // 46
};
// Primitive types. The bottom 4 bits is an integer
// value that represents the level of indirection,
// e.g. 0= no pointer, 1= pointer, 2= pointer pointer etc.
enum {
P_NONE, P_VOID = 16, P_CHAR = 32, P_INT = 48, P_LONG = 64,
P_STRUCT=80, P_UNION=96
};
// Structural types
enum {
S_VARIABLE, S_FUNCTION, S_ARRAY
};
// Storage classes
enum {
C_GLOBAL = 1, // Globally visible symbol
C_LOCAL, // Locally visible symbol
C_PARAM, // Locally visible function parameter
C_EXTERN, // External globally visible symbol
C_STATIC, // Static symbol, visible in one file
C_STRUCT, // A struct
C_UNION, // A union
C_MEMBER, // Member of a struct or union
C_ENUMTYPE, // A named enumeration type
C_ENUMVAL, // A named enumeration value
C_TYPEDEF // A named typedef
};
// Symbol table structure
struct symtable {
char *name; // Name of a symbol
int type; // Primitive type for the symbol
struct symtable *ctype; // If struct/union, ptr to that type
int stype; // Structural type for the symbol
int class; // Storage class for the symbol
int size; // Total size in bytes of this symbol
int nelems; // Functions: # params. Arrays: # elements
#define st_endlabel st_posn // For functions, the end label
int st_posn; // For locals, the negative offset
// from the stack base pointer
int *initlist; // List of initial values
struct symtable *next; // Next symbol in one list
struct symtable *member; // First member of a function, struct,
}; // union or enum
// Abstract Syntax Tree structure
struct ASTnode {
int op; // "Operation" to be performed on this tree
int type; // Type of any expression this tree generates
struct symtable *ctype; // If struct/union, ptr to that type
int rvalue; // True if the node is an rvalue
struct ASTnode *left; // Left, middle and right child trees
struct ASTnode *mid;
struct ASTnode *right;
struct symtable *sym; // For many AST nodes, the pointer to
// the symbol in the symbol table
#define a_intvalue a_size // For A_INTLIT, the integer value
int a_size; // For A_SCALE, the size to scale by
};
enum {
NOREG = -1, // Use NOREG when the AST generation
// functions have no register to return
NOLABEL = 0 // Use NOLABEL when we have no label to
// pass to genAST()
};