1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| int array_exp(struct node *T,struct node **Tr,int *sum,int key){ struct node *T0; char arr[32]; char name[32]; int i=0,num,use_struct=0; T0 = T->ptr[key]; while (T0->kind == EXP_ARRAY) { arr[i++] = T0->ptr[1]->ptr[0]->type_int; T0 = T0->ptr[0]; } T0->offset = T->offset; *Tr = T0;
Exp(T0); if(T0->kind == EXP_ELE){ T0 = T0->ptr[1]; use_struct = 1; }
if(i!=symbolTable.symbols[T0->place].paramnum-1){ semantic_error(T->pos, "", "多级数组的阶数不匹配"); } T->type = T0->type; T->width = T0->width; T->code = T0->code;
*sum = 0; for(int x=0;x<i;x++){ num = arr[x]; for(int y=0;y<x;y++){ num *= symbolTable.symbols[T0->place].array[symbolTable.symbols[T0->place].paramnum-y-1]; } *sum += num; } T->ptr[1-key]->offset = T->offset; Exp(T->ptr[1-key]);
if(symbolTable.symbols[T->ptr[1-key]->place].flag == 'a'){ semantic_error(T->pos, T->ptr[1-key]->type_id, "是数组名,不是普通变量,语义错误"); }
T->code = merge(2, T->code, T->ptr[1-key]->code);
return use_struct; }
void array_exp_right(struct node *T){ struct opn opn1, opn2, result; struct node *T0; char name[32]; char stname[32]; int sum=-1;
if(array_exp(T,&T0,&sum,1)){ opn1.kind = ID; sprintf(name,"%s(%s[%d])<+%d>",symbolTable.symbols[T0->ptr[0]->place].alias,symbolTable.symbols[T0->ptr[1]->place].name, sum,symbolTable.symbols[T0->ptr[1]->place].offset+sum*get_typelen(symbolTable.symbols[T0->ptr[1]->place].type)); strcpy(opn1.id, name); } else{ opn1.kind = ID; sprintf(name,"%s[%d]",symbolTable.symbols[T0->place].alias,sum); strcpy(opn1.id, name); }
if(T->ptr[0]->kind == EXP_ELE){ sprintf(stname,symbolTable.symbols[T->ptr[0]->ptr[1]->place].name); sprintf(name,"%s(%s)<+%d>",symbolTable.symbols[T->ptr[0]->ptr[0]->place].alias,stname,symbolTable.symbols[T->ptr[0]->ptr[1]->place].offset);
result.kind = ID; strcpy(result.id, name); result.offset = symbolTable.symbols[T->ptr[0]->ptr[0]->place].offset; } else{ result.kind = ID; strcpy(result.id, symbolTable.symbols[T->ptr[0]->place].alias); result.offset = symbolTable.symbols[T->ptr[0]->place].offset; }
T->code = merge(2, T->code, genIR(ASSIGNOP, opn1, opn2, result)); }
void array_exp_left(struct node *T){ struct opn opn1, opn2, result; struct node *T0; char name[32]; char stname[32]; int sum=-1;
if(array_exp(T,&T0,&sum,0)){ result.kind = ID; sprintf(name,"%s(%s[%d])<+%d>",symbolTable.symbols[T0->ptr[0]->place].alias,symbolTable.symbols[T0->ptr[1]->place].name, sum,symbolTable.symbols[T0->ptr[1]->place].offset+sum*get_typelen(symbolTable.symbols[T0->ptr[1]->place].type)); strcpy(result.id, name); } else{ result.kind = ID; sprintf(name,"%s[%d]",symbolTable.symbols[T0->place].alias,sum); strcpy(result.id, name); result.offset = symbolTable.symbols[T0->place].offset; }
if(T->ptr[1]->kind == EXP_ELE){ sprintf(stname,symbolTable.symbols[T->ptr[1]->ptr[1]->place].name); sprintf(name,"%s(%s)<+%d>",symbolTable.symbols[T->ptr[1]->ptr[0]->place].alias,stname,symbolTable.symbols[T->ptr[1]->ptr[1]->place].offset);
result.kind = ID; strcpy(result.id, name); result.offset = symbolTable.symbols[T->ptr[0]->ptr[0]->place].offset; } else{ opn1.kind = ID; strcpy(opn1.id, symbolTable.symbols[T->ptr[1]->place].alias); opn1.offset = symbolTable.symbols[T->ptr[1]->place].offset; }
T->code = merge(2, T->code, genIR(ASSIGNOP, opn1, opn2, result)); }
void array_exp_all(struct node *T){ struct opn opn1, opn2, result; struct node *T0; char name[32]; int sum=-1;
if(array_exp(T,&T0,&sum,1)){ opn1.kind = ID; sprintf(name,"%s(%s[%d])<+%d>",symbolTable.symbols[T0->ptr[0]->place].alias,symbolTable.symbols[T0->ptr[1]->place].name, sum,symbolTable.symbols[T0->ptr[1]->place].offset+sum*get_typelen(symbolTable.symbols[T0->ptr[1]->place].type)); strcpy(opn1.id, name); }else{ opn1.kind = ID; sprintf(name,"%s[%d]",symbolTable.symbols[T0->place].alias,sum); strcpy(opn1.id, name); }
if(array_exp(T,&T0,&sum,0)){ result.kind = ID; sprintf(name,"%s(%s[%d])<+%d>",symbolTable.symbols[T0->ptr[0]->place].alias,symbolTable.symbols[T0->ptr[1]->place].name, sum,symbolTable.symbols[T0->ptr[1]->place].offset+sum*get_typelen(symbolTable.symbols[T0->ptr[1]->place].type)); strcpy(result.id, name); } else{ result.kind = ID; sprintf(name,"%s[%d]",symbolTable.symbols[T0->place].alias,sum); strcpy(result.id, name); }
result.offset = symbolTable.symbols[T0->place].offset; T->code = merge(2, T->code, genIR(ASSIGNOP, opn1, opn2, result)); }
|