symbol.h

来自「用C++编写的一个编译器」· C头文件 代码 · 共 194 行

H
194
字号
#ifndef SYMBOL_H
#define SYMBOL_H

#include "dccdef.h"
#include "syntax.h"
#include "utility.h"
/* storage classes */
enum en_sc {
    sc_static, sc_auto, sc_global, sc_external, 
    sc_register, //here, my support is just to ignore
	sc_member, sc_label, sc_arg, //sc_const????
	sc_typedef }; //just the format of the usage of typedef like storage class
//note after define, there are only follows in use
//sc_static, sc_global, sc_external, sc_label, sc_auto,

/* basic types */
//!!!remember a single bt_pionter means char*
//
enum en_bt {//can not change the order
	bt_void, bt_bool, bt_enum, 
    bt_char, bt_short, bt_long, bt_int, 
	bt_unsignedchar, bt_unsignedshort, bt_unsignedlong, bt_unsignedint, 
	bt_float, bt_double, 
	bt_struct, bt_union, 
	bt_func, bt_funcptr, bt_typedef, bt_pointer, 
	bt_ellipsis //used in variable argument list
};
//after derfination there is no 
//bt_enum, bt_typedef, bt_ellipsis

#define is_integer(bt)	((bt)>=bt_bool&&(bt)<=bt_unsignedint)
#define is_unsigned(bt) ((bt)>=bt_unsignedchar&&(bt)<=bt_unsignedint)
#define is_float(bt)	((bt)==bt_float||(bt)==bt_double)
#define is_real(bt)		((bt)>=bt_bool&&(bt)<=bt_double)
//#define is_simple(bt)	((bt)>=bt_bool&&(bt)<=bt)
/* Type declarations */

typedef struct _datatype{
	char*			tname;		//for enum, or struct, or union, or typedef
	enum en_bt		basic_type;
	unsigned		array_vol;	//zero for not a array, 
								//init is exp*, you must calc it when do defination, !!! remember
	int				size;		//if array size is the size of array, init is unknown
	struct _datatype	*subtype;//if bt_typedef subtype means the original type, otherowise   means the  type pointer points to 
	
	struct _node	**memlist;	//for struct or union 
								//this member will be stmt* before defination complete, !!! remember.	
	struct _sym		*arglist;	//func arglist
}Type;

/* symbols */
typedef struct _sym {
	struct _sym		*next;			/* next symbol (just used in symbol list , such as in struct or arglist)*/
    char            *name;			/* symbol name */

    enum en_sc		storage_class;	/* storage class */
	Type			*datatype;
	bool			const_flag; //if true means const
	/* these fields depend on storage_class */
    union {
	/*	no support to named const for this moment */
	//for const , but enumerator need it
	
        long            i;		// int val
    /*    unsigned long   u;		// unsigned val 
        double          f;		// float val 
        char            *s;		// string val 
	*/
	//for function
		long			localdata;
	//for label
		long			labelno; //init is UNKNOWN
	//for simple auto, or member, or argument
		long			offset;
	//for static , or global, or external
		char*			linkname;	//for static or global variable 
    } value;  //it is the address for array 
	int 			refcount;		//for opt
	struct _expnode	*init;			//used in declaration
	
}Symbol;

typedef struct _supertype{
	enum en_sc 		sc;
	struct _datatype 	*datatype;
}SuperType;

/********************************************************************/
//symbol c
#define	SYMTABSIZE 4057
extern struct _node *symtab[SYMTABSIZE];

#define	TYPETABSIZE 229
extern struct _node *typetab[TYPETABSIZE];

#define MEMTABSIZE	71

extern struct _sym *current_func;

extern List const_list; //const string, or like that
extern List data_list;
extern List bss_list;
extern List extern_list; //extern var and all function decl

void *value_init(struct _expnode *exp);
struct _datatype *newType(
	char 			*name, 
	int		 		bt,
	struct	_expnode *array_vol
);

int check_basictype(
	int prefix,
	int origin
);
struct _sym *newEnumerator(
	char		*name,
	struct _expnode * val);

struct _sym *newSymbol(
	char		*name,
	int			sc,
	Type		*datatype);

struct _datatype *add_newType(
	struct _datatype *newtype
);

struct _datatype *new_sameType(
	struct _datatype *type,
	bool	auto_flag
);
struct _supertype *newSuperType(
	int sc,
	struct _datatype *type
);

struct _node **member_declare(
	int bt,
	struct _stmtnode *declaration_list,
	int *size	//out
);
int add_const(char *string);

int global_define(
	struct _supertype *prefix,
	struct _sym *var
);

struct _stmtnode *local_define(
	struct _supertype *prefix,
	struct _sym *var
);

struct _datatype *compoundType(
	struct _datatype *origin,
	struct _datatype *extra
);
char *add_GlobalData(
	char *name,//if null create one
	int sc,
	int size,
	char *val,
	bool copy_flag
);
//return the size of the type, remember
int checkTypeSize(
	struct _datatype* type
);
int checkTypeSame(
	struct _datatype	*t1,
	struct _datatype 	*t2
);
//the parameter and return value to ready 
//call  asm_func_begin

void prepare_local(
	struct _sym *func
);

void exit_local();

//get out of a function, remember to clean this list
void clean_named_label();

//return status, add label list
struct _sym *add_named_label(
	char *labelname, 
	int labelno
); 
extern long newlabelno;
void enter_block();
void leave_block();
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?