⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 一个用C++实现的C的Compiler。 代码风格良好。 原作者自己写了这个编译器
💻 CPP
字号:
#ifndef __COMMON_CPP__
#define __COMMON_CPP__

const		int			Limit_ID				=		100000;
const		int			Limit_Level				=		100;

const		int			Limit_TempVar			=		100000;

const		int			REG_OP_READ				=		0;
const		int			REG_OP_WRITE			=		1;

const		int			SRC_relational_LES		=		0;
const		int			SRC_relational_LEQ		=		1;
const		int			SRC_relational_GTR		=		2;
const		int			SRC_relational_GEQ		=		3;
const		int			SRC_relational_EQL		=		4;
const		int			SRC_relational_NEQ		=		5;
const		int			SRC_logical_NOT			=		6;
const		int			SRC_logical_AND			=		7;
const		int			SRC_logical_OR			=		8;
const		int			SRC_calculational_ADD	=		9;
const		int			SRC_calculational_MINUS	=		10;
const		int			SRC_calculational_MUL	=		11;
const		int			SRC_calculational_DIV	=		12;
const		int			SRC_calculational_MOD	=		13;
const		int			SRC_calculational_AND	=		14;
const		int			SRC_calculational_OR	=		15;
const		int			SRC_calculational_XOR	=		16;
const		int			SRC_calculational_SHL	=		17;
const		int			SRC_calculational_SHR	=		18;
const		int			SRC_calculational_NOT	=		19;

const		char*		SRC_OP_name[20]			=		{		"<",		"<=",		">",		">=",		"==",
																"!=",		"!",		"&&",		"||",		"+",
																"-",		"*",		"/",		"%",		"&",
																"|",		"^",		"<<",		">>",		"~"
														};

struct TASMop {
	char* op_name;
	char* code;
};

const		TASMop		ASMops[]				=		{	{"ADDSP3",			"00000xxxYYYYYYYY"},
															{"B",				"00010XXXXXXXXXXX"},
															{"BEQZ",			"00100xxxYYYYYYYY"},
															{"BNEZ",			"00101xxxYYYYYYYY"},
															{"SLL",				"00110xxxyyyZZZ00"},
															{"SRL",				"00110xxxyyyZZZ10"},
															{"SRA",				"00110xxxyyyZZZ11"},
															{"ADDIU3",			"01000xxxyyy0ZZZZ"},
															{"ADDIU",			"01001xxxYYYYYYYY"},
															{"SLTI",			"01010xxxYYYYYYYY"},
															{"SLTUI",			"01011xxxYYYYYYYY"},
															{"BTEQZ",			"01100000XXXXXXXX"},
															{"BTNEZ",			"01100001XXXXXXXX"},
															{"SW-RS",			"01100010XXXXXXXX"},
															{"ADDSP",			"01100011XXXXXXXX"},
															{"MTSP",			"01100100xxx00000"},
															{"MOVE",			"01100111xxx00yyy"},
															{"LI",				"01101xxxYYYYYYYY"},
															{"CMPI",			"01110xxxYYYYYYYY"},
															{"LW-SP",			"10010xxxYYYYYYYY"},
															{"LW",				"10011xxxyyyZZZZZ"},
															{"SW-SP",			"11010xxxYYYYYYYY"},
															{"SW",				"11011xxxyyyZZZZZ"},
															{"ADDU",			"11100xxxyyyzzz01"},
															{"SUBU",			"11100xxxyyyzzz11"},
															{"JR",				"11101xxx00000000"},
															{"MFHI",			"11101xxx00010000"},
															{"MFLO",			"11101xxx00010010"},
															{"MFPC",			"11101xxx01000000"},
															{"SLT",				"11101xxxyyy00010"},
															{"SLTU",			"11101xxxyyy00011"},
															{"SLLV",			"11101xxxyyy00100"},
															{"SRLV",			"11101xxxyyy00110"},
															{"SRAV",			"11101xxxyyy00111"},
															{"CMP",				"11101xxxyyy01010"},
															{"NEG",				"11101xxxyyy01011"},
															{"AND",				"11101xxxyyy01100"},
															{"OR",				"11101xxxyyy01101"},
															{"XOR",				"11101xxxyyy01110"},
															{"NOT",				"11101xxxyyy01111"},
															{"MULT",			"11101xxxyyy11000"},
															{"MULTU",			"11101xxxyyy11001"},
															{"DIV",				"11101xxxyyy11010"},
															{"DIVU",			"11101xxxyyy11011"},
															{"NOP",				"0000100000000000"},
															{"MFIH",			"11110xxx00000000"},
															{"MTIH",			"11110xxx00000001"},
															{"INT",				"111110000000XXXX"}
														};

const		char*		error_message[]			=		{		
																"0: unknown error",
																"1: syntax error",
																"2: declaration of '%1' here conflicts\nLine %2:                        ...with earlier declaration here",
																"3: identifier '%1' already used in this parameter list",
																"4: struct '%1' not found",
																"5: undeclared identifier '%1'",
																"6: cannot declare identifier '%1' as void type",
																"7: incompatible operands: %1 %2 %3",
																"8: incompatible operand: %1 %2",
																"9: test expression must have boolean type",
																"10: [] can only be applied to arrays and pointers",
																"11: array base type must be non-void type",
																"12: only 'length' field is allowed for arrays",
																"13: function 'length' of array expects 0 argument(s) but %1 given",
																"14: array subscript must be an integer/ordinal type",
																"15: second argument(array size) to 'new' operator must be an integer/ordinal type",
																"16: calling a non-function variable is not allowed",
																"17: undeclared function '%1'",
																"18: overridden method '%1' must match inherited type signature",
																"19: function type %1 expects %2 argument(s) but %3 given",
																"20: incompatible argument %1, %2 given, %3 expected",
																"21: incompatible return: %1 given, %2 expected",
																"22: static field access is not supported",
																"23: '%1' is not a struct",
																"24: '%1' is not a method but a variable in class '%2'",
																"25: field '%1' not found in '%2'",
																"26: variable or field '%1' declared as void",
																"27: variable or field '%1' declared as array of void",
																"28: 'this' is only allowed inside a class",
																"29: incompatible argument %1 for 'Print', %2 given, int/double/bool/string expected",
																"30: 'break' is only allowed inside a loop",
																"31: function '%1' redefined",
																"32: non-lvalue in %1",
																"33: '%1' is not a variable but a method in class '%2'",
																"34: '%1' is not a variable",
																"35: undefined reference to '%1'",
																"36: diveded by const 0",
																"37: invalid type argument of 'unary *'",
																"38: non-lvalue in 'unary &'",
																"39: constant value required",
																"40: cannot convert type %1 to %2, maybe they are of different size or other reasons",
																"41: need a positive constant for array subscript bound",
																"42: return type of function must be basic type or pointer",
																"43: need a register name here",
																"44: only variables are allowed to take part in register operations",
																"45: unknown asm operator %1",
																"46: asm operator %1 requires %2 operands, but %3 given",
																"47: asm operator %1 needs %2 as its argument %3",
																"48: '%1' is not a point-to-object type",
														};

#endif				// __COMMON_CPP__

⌨️ 快捷键说明

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