📄 globals.h
字号:
/****************************************************/
/* File: globals.h */
/* Global types and vars for TINY compiler */
/* must come before other include files */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
#ifndef _GLOBALS_H_
#define _GLOBALS_H_
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <map>
#include "y.tab.h"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define BYTE unsigned char
#define WORD unsigned int
#define DWORD unsigned long
#define sizeofint 4
#define sizeofchar 1
#define sizeofdouble 8
#define typestring 2
/* MAXRESERVED = the number of reserved words */
#define MAXRESERVED 16
#define LABEL_SIZE 10
#define MAXLEVEL 10
typedef int TokenType;
typedef enum {
opclRR, /* reg operands r,s,t */
opclRM, /* reg r, mem d+s */
opclRA /* reg r, int d+s */
} OPCLASS;
#define IADDR_SIZE 1024
#define DADDR_SIZE 1024 /* increase for large programs */
#define NO_REGS 8
#define PC_REG 7
#define LABLE_SIZE 128
typedef enum {
/* RR instructions */
opSTART,
opHALT, /* RR halt, operands are ignored */
opIN, /* RR read into reg(r); s and t are ignored */
opOUT, /* RR write from reg(r), s and t are ignored */
opADD, /* RR reg(r) = reg(s)+reg(t) */
opSUB, /* RR reg(r) = reg(s)-reg(t) */
opMUL, /* RR reg(r) = reg(s)*reg(t) */
opDIV, /* RR reg(r) = reg(s)/reg(t) */
opLT, /* RA if reg(r)<0 then reg(7) = d+reg(s) */
opLE, /* RA if reg(r)<=0 then reg(7) = d+reg(s) */
opGT, /* RA if reg(r)>0 then reg(7) = d+reg(s) */
opGE, /* RA if reg(r)>=0 then reg(7) = d+reg(s) */
opEQ, /* RA if reg(r)==0 then reg(7) = d+reg(s) */
opNEQ, /* RA if reg(r)!=0 then reg(7) = d+reg(s) */
opAND,
opOR,
opNOT,
opUJP,
opFJP,
opLAB,
opPUSH,
opPOP,
opCOPY,
opSYSCALL,
opRRLim, /* limit of RR opcodes */
/* RM instructions */
opLD, /* RM reg(r) = mem(d+reg(s)) */
opST, /* RM mem(d+reg(s)) = reg(r) */
opRMLim, /* Limit of RM opcodes */
/* RA instructions */
opLDA, /* RA reg(r) = d+reg(s) */
opLDC, /* RA reg(r) = d ; reg(s) is ignored */
opENT, //函数开始
opRET, //退出函数
opMST, //标志栈
opCUP, //调用函数
opCSP, //调用标准函数
opRALim /* Limit of RA opcodes */
} OPCODE;
typedef struct _INSTRUCTION{
int iop ;
int iarg1 ;
int iarg2 ;
int iarg3 ;
int isize; //字符4,整数4,浮点数8,记录为实际大小
int itrans; //1:32位转换成64位,2:64位转换成32位
double darg2;
void SetInstruction(int op, int arg1, int arg2, int arg3, int size = 4, int trans = 0){
iop = op; iarg1 = arg1; iarg2 = arg2; iarg3 = arg3;
isize = size; itrans = trans;
}
void SetInstruction2(int op, int arg1, double arg2, int arg3, int size = 4, int trans = 0){
iop = op; iarg1 = arg1; darg2 = arg2; iarg3 = arg3;
isize = size; itrans = trans;
}
} INSTRUCTION;
extern INSTRUCTION iMem [IADDR_SIZE];
extern FILE* source; /* source code text file */
extern FILE* listing; /* listing output text file */
extern FILE* code; /* code text file for TM simulator */
extern int lineno; /* source line number for listing */
extern INSTRUCTION instruction;
/**************************************************/
/*********** Syntax tree for parsing ************/
/**************************************************/
typedef enum {ReadC,WriteC,AbsC,FloorC,CeilC,FAbsC,FModC,StrCmpC,StrCatC,StrCpyC} CallType;
typedef enum {DecK,StmtK,ExpK} NodeKind;
typedef enum {IfK,AssignK,WhileK,CallK,ReturnK,BreakK,ContinueK} StmtKind;
typedef enum {OpK,NumK,DNumK,CharK,IdK,StringK,FieldIdK,StructIdK} ExpKind;
typedef enum {FunDecK,FunDefK,VarK,CompK,ParamK,StructDecK,FieldDecK,StructEndK} DecKind;
/* ExpType is used for type checking */
typedef enum {Void,Integer,Boolean,Double,Char,String,Struct} ExpType;
typedef struct _VExpType{
ExpType type;
char *name;
}VExpType;
/* the size of the symble table */
#define SIZE 13
/* the record in the variable table for
each variable */
typedef struct valEntry {
char * name;
VExpType type;
int offset; //used for activation record
int refFlag;
int size;
int count;
struct valEntry * next;
} ValEntry;
/* the record in the function table for
each function */
typedef struct funEntry {
char * name;
VExpType type;
int ret_val;
ValEntry * para; //parameters of the function
struct funEntry * next;
} FunEntry;
typedef struct structEntry {
char * name;
int ret_val;
ValEntry * field; //field of struct
struct structEntry * next;
} StructEntry;
/* the symble table structure for variables */
typedef struct symtab {
struct symtab * parent;
int nestlevel;
int memloc; //memory location for variables in the symble table
ValEntry * valTable[SIZE];
FunEntry * funEntry;
StructEntry * structEntry;
//char lab1[LABEL_SIZE];
//char lab2[LABEL_SIZE];
int lab1;
int lab2;
} Symtab;
#define MAXCHILDREN 3
typedef struct treeNode
{
struct treeNode * child[MAXCHILDREN];
struct treeNode * sibling;
int level;
int lineno;
NodeKind nodekind;
union { StmtKind stmt; ExpKind exp;DecKind dec;} kind;
union {
TokenType op;
union{
int i;
double f;
char *str;
} val;
char * name;
Symtab * table;
} attr;
int refFlag;
int call_stmt;
VExpType type; /* for type checking of exps */
char *type_name;
} TreeNode;
/**************************************************/
/*********** Flags for tracing ************/
/**************************************************/
/* EchoSource = TRUE causes the source program to
* be echoed to the listing file with line numbers
* during parsing
*/
extern int EchoSource;
/* TraceScan = TRUE causes token information to be
* printed to the listing file as each token is
* recognized by the scanner
*/
extern int TraceScan;
/* TraceParse = TRUE causes the syntax tree to be
* printed to the listing file in linearized form
* (using indents for children)
*/
extern int TraceParse;
/* TraceAnalyze = TRUE causes symbol table inserts
* and lookups to be reported to the listing file
*/
extern int TraceAnalyze;
/* TraceCode = TRUE causes comments to be written
* to the TM code file as code is generated
*/
extern int TraceCode;
/* Error = TRUE prevents further passes if an error occurs */
extern int Error;
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -