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

📄 codegenerator.h

📁 decafc的源代码
💻 H
字号:
/**************************************************************************** * File name:	codeGenerator.h						    * * Description:	header file for codeGenerator.c				    * * Input:	none							    * * Output:	none							    * * Author:	Luojian Chen                                                * * Date:	April 26, 1997						    * ****************************************************************************/#ifndef _CODE_GENERATOR_H_#define _CODE_GENERATOR_H_#include "decafc.h"#include "ASTree.h"#define MAX_ADDRESS_VAR_NUMBER 32#define STRING_NEW_LINE	"\n"#define STRING_INCLUDE_STDIO_H		"#include <stdio.h>\n"#define STRING_INCLUDE_MALLOC_H		"#include <malloc.h>\n"#define STRING_INCLUDE_H_FILE_FMT	"#include \"%s\"\n"#define STRING_STACK_DEC	"int	**stack = NULL;\n"#define STRING_STACK_SIZE_DEC	"int	stackSize = 0;\n"#define STRING_HEAP_DEC		"int	*heap = NULL;\n"#define STRING_HEAP_SIZE_DEC	"int	heapSize = 0;\n"#define STRING_HEAP_TOP_DEC	"int	*heapTop = NULL;\n"#define STRING_ENV_DEC		"int	*env = NULL;\n"#define STRING_ENV_SIZE_DEC	"int	envSize = 0;\n"// #define STRING_ENV_1_DEC	"int	*env1 = NULL;\n"// #define STRING_ENV_1_SIZE_DEC	"int	env1Size = 0;\n"#define STRING_ADDRESS_DEC_FMT	"int	*address%d = NULL;\n"#define STRING_TMP_DEC		"int	tmp = 0;\n"#define STRING_THIS_OFFSET_DEC	"int	thisOffset = 1;\n"#define STRING_RETURN_VALUE_DEC	"int	returnValue = 0;\n"#define STRING_ARG_LIST_DEC	"int	*argList = NULL;\n"#define STRING_LEFT_CURLY_BRACKET	"{\n"#define STRING_RIGHT_CURLY_BRACKET	"}\n"#define STRING_MAIN_FUNCTION	"main()\n"#define STRING_MALLOC_STACK	"stack = (int **)malloc(sizeof(int *));\n"#define STRING_INIT_STACK_SIZE	"stackSize = 1;\n"#define STRING_MALLOC_HEAP		"heap = (int *)malloc(sizeof(int));\n"#define STRING_INIT_HEAP_SIZE		"heapSize = 1;\n"#define STRING_INIT_ZERO_ADDRESS	"*heap = 1;\n"#define STRING_INIT_HEAP_TOP		"heapTop = heap + 1;\n"#define STRING_UPDATE_HEAP_SIZE_1_FMT	"heapSize = heapSize + %d;\n"#define STRING_UPDATE_HEAP_SIZE_2_FMT	"heapSize = heapSize + *address%d;\n"#define STRING_REALLOC_HEAP		"heap = (int *)realloc(heap, heapSize * sizeof(int));\n"// #define STRING_SAVE_HEAP_TOP		"*heap = heapTop - heap;\n"#define STRING_SAVE_HEAP_SIZE	"*heap = heapSize;\n"#define STRING_UPDATE_HEAP_TOP		"heapTop = heap + *heap;\n"#define STRING_INIT_OBJ_SIZE_FMT	"*heapTop = %d;\n"#define STRING_INIT_OBJ_SIZE_2_FMT	"*heapTop = *address%d;\n"#define STRING_INIT_OBJ_REF_CNT		"*(heapTop + 1) = 1;\n"#define STRING_INIT_CLASS_VAR_FMT	"*(heapTop + %d) = 0;\n"#define STRING_INIT_ARRAY_FMT		"for (tmp = 2; tmp < *address%d; tmp++) {\n*(heapTop  + tmp) = 0;\n}\n"// #define STRING_FUNCTION_DEC_FMT	"void\tDecaf_%s_%s(int\t**env)\n"#define STRING_FUNCTION_HEADER_FMT	"void\tDecaf_%s_%s()\n"#define STRING_FUNCTION_PROTOTYPE_FMT	"void\tDecaf_%s_%s();\n"// #define STRING_INIT_ENV_SIZE_FMT	"envSize = %d;\n"#define STRING_UPDATE_ENV_SIZE_FMT	"envSize = envSize + %d;\n"// #define STRING_MALLOC_VAR_FMT 		"*env = (int *)realloc(*env, envSize * sizeof(int));\n"#define STRING_MALLOC_VAR_FMT 		"env = (int *)realloc(env, envSize * sizeof(int));\n"#define STRING_INIT_LOCAL_VAR_FMT	"*(env + %d) = 0;\n"#define STRING_PRINT_CONST_FMT		"printf(\"%%d \", %d);\n"#define STRING_PRINT_VAR_FMT		"printf(\"%%d \", *address%d);\n"#define STRING_PRINTLN_CONST_FMT	"printf(\"%%d \\n\", %d);\n"#define STRING_PRINTLN_VAR_FMT		"printf(\"%%d \\n\", *address%d);\n"// #define STRING_THIS_OBJ_ADDR_FMT  "address%d = *env + 0;\n"#define STRING_THIS_OBJ_ADDR_FMT  "address%d = env + 0;\n"// #define STRING_MAIN_THIS_OBJ_ADDR_FMT  "address%d = env + 0;\n"#define STRING_CLS_VAR_ADDR_FMT_P1 "address%d = heap + *address%d;\n"#define STRING_CLS_VAR_ADDR_FMT_P2 "address%d = address%d + %d;\n"// #define STRING_LCL_VAR_FMT	  "address%d = *env + %d;\n"#define STRING_LOCAL_VAR_FMT	  "address%d = env + %d;\n"// #define STRING_LCL_VAR_1_FMT	  "address%d = env1 + %d;\n"#define STRING_ASSIGN_VAR_FMT		"*address%d = *address%d;\n"#define STRING_ASSIGN_VAR_1_FMT		"*address%d = *address%d + %d;\n"#define STRING_ASSIGN_CONST_FMT		"*address%d = %d;\n"// #define STRING_FUNCTION_CALL_FMT	"Decaf_%s_%s(&env1);\n"// #define STRING_FUNCTION_CALL_FMT	"Decaf_%s_%s(&env);\n"#define STRING_FUNCTION_CALL_FMT	"Decaf_%s_%s();\n"// #define STRING_MAIN_FUNCTION_CALL_FMT	"Decaf_%s_main(&env);\n"#define STRING_MAIN_FUNCTION_CALL_FMT	"Decaf_%s_main();\n"#define STRING_EXPAND_STACK_SIZE	"stackSize = stackSize + 1;\n"#define STRING_SHRINK_STACK_SIZE	"stackSize = stackSize - 1;\n"#define STRING_REALLOC_STACK	"stack = (int **)realloc(stack, stackSize * sizeof(int *));\n"// #define STRING_PUSH_ENV			"*(stack + stackSize - 1) = *env;\n"#define STRING_PUSH_ENV			"*(stack + stackSize - 1) = env;\n"// #define STRING_POP_ENV			"*env = *(stack + stackSize - 1);\n"#define STRING_POP_ENV			"env = *(stack + stackSize - 1);\n"// #define STRING_FREE_ENV_1		"free(env1);\n"#define STRING_FREE_ENV		"free(env);\n"#define STRING_SAVE_ENV_SIZE	"*(env + 1) = envSize;\n"#define STRING_RESTORE_ENV_SIZE	"envSize = *(env + 1);\n"// #define STRING_MALLOC_ENV_FMT	"env = (int *)malloc(%d * sizeof(int));\n"#define STRING_MALLOC_ENV		"env = (int *)malloc(envSize * sizeof(int));\n"// #define STRING_MALLOC_NEW_ENV_FMT	"env1 = (int *)malloc(%d * sizeof(int));\n"// #define STRING_NEW_THIS_OBJ_ADDR_FMT  "address%d = env1 + 0;\n"// #define STRING_NEW_THIS_OBJ_ADDR_FMT  "address%d = *env + 0;\n"// #define STRING_NEW_THIS_OBJ_ADDR_FMT  "address%d = env + 0;\n"#define STRING_ASSIGN_THIS_OBJ_OFFSET	"*env = thisOffset;\n"#define STRING_ASSIGN_NEW_OBJ_OFFSET	"*address%d = thisOffset;\n"#define STRING_SAVE_THIS_OBJ_OFFSET_FMT	"thisOffset = *address%d;\n"#define STRING_PASS_PARAMETER_FMT	"*address%d = *address%d;\n"// #define STRING_SUBSTITUTE_ENV	"env = env1;\n"#define STRING_CHECK_NULL		"if (*address%d == 0) {\nfflush(stdout);\nfprintf(stderr, \"Error: NULL reference\\n\");\nexit(1);\n}\n"// #define STRING_TMP_VAR_FMT 	"address%d = *env + %d;\n"#define STRING_TMP_VAR_FMT 	"address%d = env + %d;\n"#define STRING_READ			"scanf(\"%%d\", address%d);\n"#define STRING_RETURN		"return;\n"// #define STRING_RETURN_ADDR_FMT	"address%d = *env + 1;\n"#define STRING_RETURN_ADDR_FMT	"address%d = env + 1;\n"#define STRING_UNARY_EXP_1_FMT	"*address%d = %s%d;\n"#define STRING_UNARY_EXP_2_FMT	"*address%d = %s(*address%d);\n"// #define STRING_TMP_ADDR_FMT	"address%d = &tmp;\n"#define STRING_BINARY_EXP_1_FMT	"*address%d = %d %s %d;\n"#define STRING_BINARY_EXP_2_FMT	"*address%d = %d %s *address%d;\n"#define STRING_BINARY_EXP_3_FMT	"*address%d = *address%d %s %d;\n"#define STRING_BINARY_EXP_4_FMT	"*address%d = *address%d %s *address%d;\n"#define STRING_IF_TRUE_FMT	"if (*address%d == 1)\n"#define STRING_IF_FALSE_FMT	"if (*address%d == 0)\n"#define STRING_ELSE		"else"#define STRING_EMPTY_STATEMENT	";\n"#define STRING_DO		"do {\n"#define STRING_BREAK		"break;\n"#define STRING_WHILE_1		"} while(1);\n"#define STRING_SAVE_RETURN_VALUE	"returnValue = *(env + 1);\n"#define STRING_GET_RETURN_VALUE_FMT	"*address%d = returnValue;\n"#define STRING_MALLOC_ARG_LIST_FMT	"argList = (int *)malloc(%d * sizeof(int));\n"#define STRING_SAVE_ARGUMENT_1_FMT	"*(argList + %d) = %d;\n"#define STRING_SAVE_ARGUMENT_2_FMT	"*(argList + %d) = *address%d;\n"#define STRING_PASS_ARGUMENT_FMT	"*(env + %d) = *(argList + %d);\n"#define STRING_FREE_ARG_LIST		"free(argList);\n"#define STRING_ASSIGN_OBJ_OFFSET_FMT	"*address%d = heapTop - heap;\n"// #define STRING_ARRAY_FMT		"*address%d = *address%d + *address%d;\n"#define STRING_ARRAY_INDEX_FMT		"*address%d = *address%d + *address%d;\n"#define STRING_ARRAY_ADDRESS_FMT	"address%d = (heap  + *address%d);\n"#define STRING_ARRAY_LENGTH_P1_FMT	"*address%d = *(heap  + *address%d);\n"#define STRING_ARRAY_LENGTH_P2_FMT	"*address%d = *address%d - %d;\n"#define SRTRING_CHECK_ARRAY_LEN_1_FMT	"if (*address%d < 2) {\nfprintf(stderr, \"Error: array index out of bound\\n\");\nexit(1);\n}\n"#define SRTRING_CHECK_ARRAY_LEN_2_FMT	"if (*address%d >= *(heap + *address%d)) {\nfprintf(stderr, \"Error: array index out of bound\\n\");\nexit(1);\n}\n"#endif

⌨️ 快捷键说明

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