🤯🤯🤯🤯🤯🤯🤯🤯🤯

This commit is contained in:
Scarlett
2025-05-04 23:54:42 -04:00
parent 8b0d191409
commit 0fc796aa25
32 changed files with 867 additions and 1021 deletions

View File

@ -16,20 +16,20 @@ typedef struct Stack Stack;
typedef struct __Node __Node;
typedef struct __Node {
void * v;
__Node * next;
void *v;
__Node *next;
} __Node;
typedef struct Stack {
__Node * n;
int w;
int size;
__Node *n;
int w;
int size;
} Stack;
Stack * S_Init();
Stack *S_Init();
void S_Free(Stack *s);
void S_Push(Stack * s, void *v, int i);
void * S_Pop(Stack *s);
void * S_Peek(Stack *s);
void S_Push(Stack *s, void *v, int i);
void *S_Pop(Stack *s);
void *S_Peek(Stack *s);
bool S_IsEmpty(Stack *s);
int S_Size(Stack *s);
void S_Merge(Stack *list);
@ -39,137 +39,130 @@ typedef union TNConstUnion TNConstUnion;
typedef struct Instruction Instruction;
typedef struct TNodeOrConst TNodeOrConst;
// these are from page 364
typedef enum { // these are from page 364
E_LABEL = 10000, // this is not in the book
E_FUNC_START,
E_ADD, // 1 from the list
E_SUB, // 1
E_MUL, // 1
E_DIV, // 1
E_MOD, // 1 TODO: Please change to REM
E_OR, // 1
E_AND, // 1
E_NEG, // 2
E_NOT, // 2
E_ASSIGN, // 3
E_GOTO, // 4
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
E_IF_X_TRUE, // 5
E_IF_X_FALSE, // 5
E_LESS_THAN, // 6 rule 1 + 5
E_EQUAL_TO, // 6 rule 1 + 5
E_CALL, // 7
E_PARAM, // 7
E_RETURN, // 7
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
E_INDEX_COPY_LEFT, // 8 x[i] = y
E_ADDRESS_OF, // 9 x = &y
E_DEREF_RIGHT, // 9 x = *y
E_DEREF_LEFT // 9 x* = y
typedef enum { // these are from page 364
E_LABEL = 10000, // this is not in the book
E_FUNC_START,
E_ADD, // 1 from the list
E_SUB, // 1
E_MUL, // 1
E_DIV, // 1
E_MOD, // 1 TODO: Please change to REM
E_OR, // 1
E_AND, // 1
E_NEG, // 2
E_NOT, // 2
E_ASSIGN, // 3
E_GOTO, // 4
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
E_IF_X_TRUE, // 5
E_IF_X_FALSE, // 5
E_LESS_THAN, // 6 rule 1 + 5
E_EQUAL_TO, // 6 rule 1 + 5
E_CALL, // 7
E_PARAM, // 7
E_RETURN, // 7
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
E_INDEX_COPY_LEFT, // 8 x[i] = y
E_ADDRESS_OF, // 9 x = &y
E_DEREF_RIGHT, // 9 x = *y
E_DEREF_LEFT // 9 x* = y
} Op;
typedef enum {
NODE = 11000, // TableNode
INTEGER, // int
STRING, // char *
CHARACTER, // char
ADDRESS, // void *
BOOLEAN // bool
NODE = 11000, // TableNode
INTEGER, // int
STRING, // char *
CHARACTER, // char
ADDRESS, // void *
BOOLEAN // bool
} Discriminant;
typedef union TNConstUnion {
TableNode * node;
int integer;
char * string;
char character;
void * address;
bool Boolean;
TableNode *node;
int integer;
char *string;
char character;
void *address;
bool Boolean;
} TNConstUnion;
typedef struct TNodeOrConst {
Discriminant d;
TNConstUnion * tnc_union;
Discriminant d;
TNConstUnion *tnc_union;
} TNodeOrConst;
typedef struct Instruction {
Op opcode;
TableNode * result;
TNodeOrConst * operand1;
TNodeOrConst * operand2;
int label;
int index;
Op opcode;
TableNode *result;
TNodeOrConst *operand1;
TNodeOrConst *operand2;
int label;
int index;
Instruction * prev;
Instruction * next;
Instruction *prev;
Instruction *next;
} Instruction;
// NOTE We are not using this We are using the Stack api
typedef struct TFList {
Instruction * i;
TFList * next;
Instruction *i;
TFList *next;
} TFList;
// TFList * make_list(Instruction * i);
// - makelist(i) function to create instruction lists
// - makelist(i) function to create instruction lists
// void merge(TFList * l1, TFList * l2);
// - merge(p1,p2) function to concatenate lists
// - merge(p1,p2) function to concatenate lists
// void backpatch(TFList * l, int label);
// - backpatch(p,i) function to fill in jump targets
// - backpatch(p,i) function to fill in jump targets
// void bp_temp(int n);
extern Instruction * begin;
extern Instruction * current;
extern Instruction *begin;
extern Instruction *current;
extern int label_count;
extern bool code_gen;
extern FILE * ir_flag;
extern FILE *ir_flag;
TNodeOrConst * tn_or_const(Discriminant , void * );
void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2);
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg);
void emit_assignment(TableNode * target, TNodeOrConst * source);
void emit_as_file(FILE * out_file, Instruction * instr_arr);
TNodeOrConst *tn_or_const(Discriminant, void *);
void emit_binary_op(Op op, TableNode *result, TNodeOrConst *arg1, TNodeOrConst *arg2);
void emit_unary_op(Op op, TableNode *result, TNodeOrConst *arg);
void emit_assignment(TableNode *target, TNodeOrConst *source);
void emit_as_file(FILE *out_file, Instruction *instr_arr);
void emit_label(int label);
void emit_jump(int label);
void emit_conditional_jump(Op condition, int label, ...);
void emit_function_start(TableNode* name);
void emit_parameter(TNodeOrConst * param);
void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name);
void emit_return(TNodeOrConst * value);
void emit_reserve(TableNode * result, TNodeOrConst * size);
void emit_release(TableNode * pointer);
void emit_field_access(char* result, char* record, char* field);
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index);
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr);
void emit_function_start(TableNode *name);
void emit_parameter(TNodeOrConst *param);
void emit_function_call(TableNode *result, int param_count, TNodeOrConst *name);
void emit_return(TNodeOrConst *value);
void emit_reserve(TableNode *result, TNodeOrConst *size);
void emit_release(TableNode *pointer);
void emit_field_access(char *result, char *record, char *field);
void emit_array_access(Op op, TableNode *result, TNodeOrConst *array, TNodeOrConst *index);
void emit_bounds_check(TNodeOrConst *index, TNodeOrConst *arr);
void emit_goto(int i);
void emit_detach();
void emit_push_all(Stack * s);
void emit_push_all(Stack *s);
int getLabel(Instruction *i);
TableNode *getTN(TNodeOrConst *tnc);
int getConst(TNodeOrConst *tnc);
int getLabel(Instruction * i);
TableNode* getTN(TNodeOrConst* tnc);
int getConst(TNodeOrConst* tnc);
TNodeOrConst * getOperand1(Instruction * i);
TNodeOrConst * getOperand2(Instruction * i);
TableNode * getResult(Instruction * i);
Op getOp(Instruction * i);
int getLabel(Instruction * i);
int get_index(Instruction * i);
void set_label(Instruction * i, int label);
bool isConst(TNodeOrConst * tnc);
TNodeOrConst *getOperand1(Instruction *i);
TNodeOrConst *getOperand2(Instruction *i);
TableNode *getResult(Instruction *i);
Op getOp(Instruction *i);
int getLabel(Instruction *i);
int get_index(Instruction *i);
void set_label(Instruction *i, int label);
bool isConst(TNodeOrConst *tnc);
int label_gen();
void backpatch(Stack *s, int l);
void emit_backpatch(Stack *s, int l);
extern int offset;
extern int currentsp;
extern CGNode* cgList;
extern CGNode *cgList;