-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdefs.h
108 lines (92 loc) · 2.97 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Structure and enum definitions
// Copyright (c) 2019 Warren Toomey, GPL3
#define TEXTLEN 512 // Length of symbols in input
#define NSYMBOLS 1024 // Number of symbol table entries
// Token types
enum {
T_EOF,
// Binary operators
T_ASSIGN, 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,
// Structural tokens
T_INTLIT, T_STRLIT, T_SEMI, T_IDENT,
T_LBRACE, T_RBRACE, T_LPAREN, T_RPAREN,
T_LBRACKET, T_RBRACKET, T_COMMA
};
// Token structure
struct token {
int token; // Token type, from the enum list above
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_LOGOR, A_LOGAND, A_OR, A_XOR, A_AND,
A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE, A_LSHIFT, A_RSHIFT,
A_ADD, A_SUBTRACT, A_MULTIPLY, A_DIVIDE,
A_INTLIT, A_STRLIT, A_IDENT, A_GLUE,
A_IF, A_WHILE, A_FUNCTION, A_WIDEN, A_RETURN,
A_FUNCCALL, A_DEREF, A_ADDR, A_SCALE,
A_PREINC, A_PREDEC, A_POSTINC, A_POSTDEC,
A_NEGATE, A_INVERT, A_LOGNOT, A_TOBOOL
};
// Primitive types
enum {
P_NONE, P_VOID, P_CHAR, P_INT, P_LONG,
P_VOIDPTR, P_CHARPTR, P_INTPTR, P_LONGPTR
};
// Abstract Syntax Tree structure
struct ASTnode {
int op; // "Operation" to be performed on this tree
int type; // Type of any expression this tree generates
int rvalue; // True if the node is an rvalue
struct ASTnode *left; // Left, middle and right child trees
struct ASTnode *mid;
struct ASTnode *right;
union { // For A_INTLIT, the integer value
int intvalue; // For A_IDENT, the symbol slot number
int id; // For A_FUNCTION, the symbol slot number
int size; // For A_SCALE, the size to scale by
} v; // For A_FUNCCALL, the symbol slot number
};
#define NOREG -1 // Use NOREG when the AST generation
// functions have no register to return
#define NOLABEL 0 // Use NOLABEL when we have no label to
// pass to genAST()
// 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
};
// Symbol table structure
struct symtable {
char *name; // Name of a symbol
int type; // Primitive type for the symbol
int stype; // Structural type for the symbol
int class; // Storage class for the symbol
int endlabel; // For S_FUNCTIONs, the end label
int size; // Number of elements in the symbol
int posn; // For locals, either the negative offset
// from stack base pointer, or register id
#define nelems posn // For functions, # of params
// For structs, # of fields
};