📄 symtab.c
字号:
/****************************************************/
/* File: symtab.c */
/* Symbol table implementation */
/* for the C_Minus compiler */
/****************************************************/
#include <stdio.h>
#include "Globals.h"
#include "Util.h"
#include "SymTab.h"
Symtab * GlobalTable; //global symble table for variables
Symtab * pTable;
FunEntry * FunTable[SIZE]; //symble table for functions
FunEntry * SysFunTable[SIZE]; //symble table for functions
StructEntry * StructTable[SIZE]; //symble table for functions
/* the hash function */
static int hash(char * key)
{
//printf("key: %s\n", key);
int temp = 0;
int i;
for (i = 0; key[i] != '\0'; i++)
temp = ((temp << SHIFT) + key[i]) % SIZE;
return temp;
}
/* create a new variable table
* and link it to its parent
*/
Symtab * Createtab(Symtab * pTable, FunEntry *pEntry, StructEntry *pStruct)
{
Symtab * newtab = (Symtab *)malloc(sizeof(Symtab));
if (newtab == NULL)
return NULL; //out of memory
newtab->parent = pTable;
newtab->nestlevel = (pTable == NULL)? 0 : pTable->nestlevel+1;
newtab->memloc = 0;
memset(newtab->valTable, 0, SIZE * sizeof(ValEntry *));
newtab->funEntry = pEntry;
newtab->structEntry = pStruct;
return newtab;
}
/* insert a new entry to the variable table */
ValEntry * Insert_Var(Symtab * pTable, char * name, VExpType type, int count)
{
int hashnumber = hash(name), size = 0;
ValEntry * pnew = (ValEntry *)malloc(sizeof(ValEntry));
//Struct
if(type.type == Struct){
StructEntry * pfind = (StructEntry *)malloc(sizeof(StructEntry));
pfind = Lookup_Struct(type.name);
if(pfind == NULL){
printf("Struct %s not find\n", type.name);
} else
size = pfind->ret_val;
printf("Struct %s size: %d\n", type.name, size);
}
switch(type.type) {
case Integer: size = sizeofint; break;
case Double: size = sizeofdouble; break;
case Char: size = sizeofchar; break;
case Struct: break;
default: size = sizeofint;
}
pTable->memloc += size * count;
pnew->size = size;
pnew->count = count;
pnew->name = copyString(name);
pnew->type.type = type.type;
pnew->type.name = copyString(type.name);
pnew->offset = - pTable->memloc;
pnew->refFlag = 0;
pnew->next = pTable->valTable[hashnumber];
pTable->valTable[hashnumber] = pnew;
return pnew;
}
/* lookup a variable in the symbol table */
int Lookup_Var(Symtab * pTable, FunEntry * pFun, char * name, ValEntry * pEntry)
{
int hashnumber = hash(name);
ValEntry * ptemp;
for ( ; pTable != NULL; pTable = pTable->parent) {
for (ptemp = pTable->valTable[hashnumber]; ptemp != NULL; ptemp = ptemp->next)
if (strcmp(ptemp->name, name) == 0) {
pEntry->name = copyString(ptemp->name);
pEntry->type.type = ptemp->type.type;
pEntry->type.name = copyString(ptemp->type.name);
pEntry->offset = ptemp->offset;
pEntry->next = NULL;
pEntry->refFlag = ptemp->refFlag;
pEntry->size = ptemp->size;
pEntry->count = ptemp->count;
return pTable->nestlevel;
}
}
//在参数中查找
if (pFun != NULL) {
for (ptemp = pFun->para; ptemp != NULL; ptemp = ptemp->next)
if (strcmp(name, ptemp->name) == 0) {
//printf("Lookup_Var name: %s\n", name);
pEntry->name = copyString(ptemp->name);
pEntry->type.type = ptemp->type.type;
pEntry->type.name = copyString(ptemp->type.name);
//printf("Lookup_Var type name: %s %s\n", ptemp->type.name, pEntry->type.name);
//参数要加4
pEntry->offset = ptemp->offset+4;
pEntry->refFlag = ptemp->refFlag;
pEntry->next = NULL;
pEntry->size = ptemp->size;
pEntry->count = 1;
return 1;
}
}
return -1;
}
int Lookup_Param(FunEntry * pFun, int index, ValEntry * pEntry)
{
ValEntry * ptemp;
int i;
i = 0;
for (ptemp = pFun->para; ptemp != NULL; ptemp = ptemp->next){
if (i == index) {
pEntry->name = copyString(ptemp->name);
pEntry->type.type = ptemp->type.type;
pEntry->type.name = copyString(ptemp->type.name);
pEntry->offset = ptemp->offset;
pEntry->next = NULL;
pEntry->refFlag = ptemp->refFlag;
pEntry->size = ptemp->size;
pEntry->count = ptemp->count;
return 1;
}
i++;
}
return -1;
}
/* insert a new entry to the function table */
FunEntry * Insert_Fun(char * name, VExpType type, TreeNode * pTreeNode)
{
int hashnumber = hash(name), nSize, size = 16;
FunEntry * pnew = (FunEntry *)malloc(sizeof(FunEntry));
ValEntry * para;
TreeNode * ptemp;
pnew->name = copyString(name);
pnew->type.type = type.type;
pnew->type.name = copyString(type.name);
pnew->para = NULL;
//if (strcmp(name, "main") == 0)
// size += 4;
if (pTreeNode != NULL) {
//第一个参数
pnew->para = (ValEntry *)malloc(sizeof(ValEntry));
pnew->para->name = copyString(pTreeNode->attr.name);
pnew->para->type.type = pTreeNode->type.type;
pnew->para->type.name = copyString(pTreeNode->type.name);
pnew->para->offset = size;
pnew->para->refFlag = pTreeNode->refFlag;
if(pnew->para->type.type == Struct){
StructEntry * pfind = (StructEntry *)malloc(sizeof(StructEntry));
pfind = Lookup_Struct(pnew->para->type.name);
if(pfind == NULL){
printf("Struct %s not find\n", pnew->para->type.name);
} else
nSize = pfind->ret_val;
printf("Struct %s size: %d\n", pnew->para->type.name, nSize);
}
if (pTreeNode->child[0] == NULL)
switch (pnew->para->type.type) {
case Integer: nSize = sizeofint; break;
case Double: nSize = sizeofdouble; break;
case Char: nSize = sizeofchar; break;
case Struct: break;
default: nSize = sizeofint;
}
else
nSize = sizeofint;
pnew->para->size = nSize;
size += nSize;
//后面的参数
for (ptemp = pTreeNode->sibling, para = pnew->para;
ptemp != NULL; ptemp = ptemp->sibling, para = para->next) {
para->next = (ValEntry *)malloc(sizeof(ValEntry));
para->next->name = copyString(ptemp->attr.name);
para->next->type.type = ptemp->type.type;
para->next->type.name = copyString(ptemp->type.name);
para->next->offset = size;
para->next->refFlag = ptemp->refFlag;
if(para->next->type.type == Struct){
StructEntry * pfind = (StructEntry *)malloc(sizeof(StructEntry));
pfind = Lookup_Struct(pnew->para->type.name);
if(pfind == NULL){
printf("Struct %s not find\n", para->next->type.name);
} else
nSize = pfind->ret_val;
printf("Struct %s size: %d\n", para->next->type.name, nSize);
}
if (ptemp->child[0] == NULL){
switch (para->next->type.type) {
case Integer: nSize = sizeofint; break;
case Double: nSize = sizeofdouble; break;
case Char: nSize = sizeofchar; break;
case Struct: break;
default: nSize = sizeofint;
}
}
else
nSize = sizeofint;
para->next->size = nSize;
size += nSize;
}
para->next = NULL;
}
pnew->ret_val = size;
pnew->next = FunTable[hashnumber];
FunTable[hashnumber] = pnew;
return pnew;
}
/* lookup a function in the symbol table */
FunEntry * Lookup_Fun(char * name)
{
int hashnumber = hash(name);
FunEntry * pEntry;
for (pEntry = FunTable[hashnumber]; pEntry != NULL; pEntry = pEntry->next)
if (strcmp(pEntry->name, name) == 0)
return pEntry;
return NULL;
}
/* insert a new entry to the function table */
void Insert_SysFun()
{
char * name = NULL;
int hashnumber;
FunEntry * pnew;
ValEntry * para;
printf("Insert_SysFun begin\n");
name = (char*)malloc(20);
strcpy(name, "read");
hashnumber = hash(name);
pnew = (FunEntry *)malloc(sizeof(FunEntry));
pnew->name = copyString(name);
pnew->type.type = Void;
pnew->para = NULL;
pnew->ret_val = ReadC;
pnew->next = SysFunTable[hashnumber];
SysFunTable[hashnumber] = pnew;
strcpy(name, "write");
hashnumber = hash(name);
pnew = (FunEntry *)malloc(sizeof(FunEntry));
pnew->name = copyString(name);
pnew->type.type = Void;
pnew->para = NULL;
pnew->ret_val = WriteC;
pnew->next = SysFunTable[hashnumber];
SysFunTable[hashnumber] = pnew;
strcpy(name, "abs");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -