Skip to content

Commit 3e81540

Browse files
static verif sur les fonctions, change archi pour symbol table
1 parent 3fc8995 commit 3e81540

8 files changed

+165
-157
lines changed

.vscode/settings.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@
8282
"semaphore": "cpp",
8383
"stop_token": "cpp",
8484
"thread": "cpp",
85-
"cinttypes": "cpp"
85+
"cinttypes": "cpp",
86+
"complex": "cpp",
87+
"forward_list": "cpp",
88+
"optional": "cpp",
89+
"hash_map": "cpp",
90+
"hash_set": "cpp",
91+
"slist": "cpp",
92+
"shared_mutex": "cpp",
93+
"typeindex": "cpp",
94+
"valarray": "cpp"
8695
}
8796
}

compiler/IR.cpp

+5-14
Original file line numberDiff line numberDiff line change
@@ -272,34 +272,25 @@ string CFG::create_new_tempvar(Type t) {
272272
// Récupérer l'offset:
273273
int offset = 0;
274274
if (t == Type::INT) {
275-
stv.offsetTable[functionName] -= 4;
276-
offset = stv.offsetTable[functionName];
275+
stv.funcTable[functionName].offset -= 4;
276+
offset = stv.funcTable[functionName].offset;
277277
}
278278

279279
// Créer le vrai nom avec l'offset dans la pile
280280
string temp_name("!tmp"+offset);
281281

282282
// Mettre à jour la table des symboles avec le nouveau nom
283-
stv.symbolTable[temp_name].offset = offset;
284-
stv.symbolTable[temp_name].type = t;
285-
stv.symbolTable[temp_name].declared = true;
286-
stv.symbolTable[temp_name].used = false;
283+
stv.varTable[temp_name] = {.type = t, .name = temp_name, .offset = offset, .declared = true, .used = false};
287284

288285
return temp_name;
289286
}
290287

291288
int CFG::get_var_offset(string name) {
292-
if (stv.symbolTable.find(name) != stv.symbolTable.end()) {
293-
return stv.symbolTable[name].offset;
294-
}
295-
return 0; // Erreur, la variable n'existe pas
289+
return stv.varTable[name].offset;
296290
}
297291

298292
Type CFG::get_var_type(string name) {
299-
if (stv.symbolTable.find(name) != stv.symbolTable.end()) {
300-
return stv.symbolTable[name].type;
301-
}
302-
return Type::INT; // Par défaut
293+
return stv.varTable[name].type;
303294
}
304295

305296
string CFG::new_BB_name() {

compiler/IRGenVisitor.cpp

+33-66
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ antlrcpp::Any IRGenVisitor::visitDecl_stmt(ifccParser::Decl_stmtContext *ctx)
6565
{
6666
std::string nomVar = scope + "_" + ctx->decl_element(i)->ID()->getText();
6767
// std::cout << "# nomVar " << i << " : " << nomVar << "\n";
68-
std::string address = "RBP" + std::to_string(stv.symbolTable[nomVar].offset);
68+
std::string address = "RBP" + std::to_string(stv.varTable[nomVar].offset);
6969
// std::cout << "# address " << address << "\n";
7070

7171
// Évaluation de l'expression qu'on place dans le registre universel !reg
@@ -85,7 +85,7 @@ antlrcpp::Any IRGenVisitor::visitAssign_stmt(ifccParser::Assign_stmtContext *ctx
8585
{
8686
// On récupère le nom et l'adresse stack de la variable en question
8787
std::string tried_scope = scope;
88-
while (tried_scope != "" && stv.symbolTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.symbolTable.end()) {
88+
while (tried_scope != "" && stv.varTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.varTable.end()) {
8989
while (tried_scope.size() != 0 && tried_scope.back() != '_') {
9090
tried_scope.pop_back();
9191
}
@@ -96,28 +96,15 @@ antlrcpp::Any IRGenVisitor::visitAssign_stmt(ifccParser::Assign_stmtContext *ctx
9696
std::string nomVar = tried_scope + "_" + ctx->ID()->getText();
9797
std::pair<bool, int> res(visit(ctx->expr()));
9898

99-
// on regarde si c'est un argument de la fonction
100-
if (stv.symbolTable[nomVar].index_arg >= 0) {
101-
// Évaluation de l'expression qu'on place dans le registre universel !reg
102-
if (res.first) {
103-
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
104-
cfgs.back()->current_bb->add_IRInstr(instruction_const);
105-
}
106-
107-
IRInstr *instruction_copy_arg = new Copy(cfgs.back()->current_bb, "!arg" + std::to_string(stv.symbolTable[nomVar].index_arg), "!reg");
108-
cfgs.back()->current_bb->add_IRInstr(instruction_copy_arg);
109-
}
110-
else {
111-
std::string address = "RBP" + std::to_string(stv.symbolTable[nomVar].offset);
99+
std::string address = "RBP" + std::to_string(stv.varTable[nomVar].offset);
112100

113-
// Évaluation de l'expression qu'on place dans le registre universel !reg
114-
if (res.first) {
115-
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
116-
cfgs.back()->current_bb->add_IRInstr(instruction_const);
117-
}
118-
IRInstr *instruction = new Wmem(cfgs.back()->current_bb, address, "!reg"); // block, dst, src
119-
cfgs.back()->current_bb->add_IRInstr(instruction);
101+
// Évaluation de l'expression qu'on place dans le registre universel !reg
102+
if (res.first) {
103+
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
104+
cfgs.back()->current_bb->add_IRInstr(instruction_const);
120105
}
106+
IRInstr *instruction = new Wmem(cfgs.back()->current_bb, address, "!reg"); // block, dst, src
107+
cfgs.back()->current_bb->add_IRInstr(instruction);
121108

122109
return 0;
123110
}
@@ -267,7 +254,7 @@ antlrcpp::Any IRGenVisitor::visitIdUse(ifccParser::IdUseContext *ctx)
267254
{
268255
// On récupère le nom et l'adresse stack de la variable en question
269256
std::string tried_scope = scope;
270-
while (tried_scope != "" && stv.symbolTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.symbolTable.end()) {
257+
while (tried_scope != "" && stv.varTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.varTable.end()) {
271258
while (tried_scope.size() != 0 && tried_scope.back() != '_') {
272259
tried_scope.pop_back();
273260
}
@@ -277,25 +264,18 @@ antlrcpp::Any IRGenVisitor::visitIdUse(ifccParser::IdUseContext *ctx)
277264
}
278265
std::string nomVar = tried_scope + "_" + ctx->ID()->getText();
279266

280-
// on regarde si c'est un argument de la fonction
281-
if (stv.symbolTable[nomVar].index_arg >= 0) {
282-
IRInstr *instruction_copy_arg = new Copy(cfgs.back()->current_bb, "!reg", "!arg" + std::to_string(stv.symbolTable[nomVar].index_arg));
283-
cfgs.back()->current_bb->add_IRInstr(instruction_copy_arg);
284-
}
285-
else {
286-
std::string address = "RBP" + std::to_string(stv.symbolTable[nomVar].offset);
267+
std::string address = "RBP" + std::to_string(stv.varTable[nomVar].offset);
287268

288-
IRInstr *instruction = new Rmem(cfgs.back()->current_bb, "!reg", address);
289-
cfgs.back()->current_bb->add_IRInstr(instruction);
290-
}
269+
IRInstr *instruction = new Rmem(cfgs.back()->current_bb, "!reg", address);
270+
cfgs.back()->current_bb->add_IRInstr(instruction);
291271

292272
return std::pair<bool, int>(false, 0);
293273
}
294274

295275
antlrcpp::Any IRGenVisitor::visitAssignExpr(ifccParser::AssignExprContext *ctx) {
296276
// On récupère le nom et l'adresse stack de la variable en question
297277
std::string tried_scope = scope;
298-
while (tried_scope != "" && stv.symbolTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.symbolTable.end()) {
278+
while (tried_scope != "" && stv.varTable.find(tried_scope + '_' + ctx->ID()->getText()) == stv.varTable.end()) {
299279
while (tried_scope.size() != 0 && tried_scope.back() != '_') {
300280
tried_scope.pop_back();
301281
}
@@ -307,28 +287,15 @@ antlrcpp::Any IRGenVisitor::visitAssignExpr(ifccParser::AssignExprContext *ctx)
307287
std::string nomVar = tried_scope + "_" + ctx->ID()->getText();
308288
std::pair<bool, int> res(visit(ctx->expr()));
309289

310-
// on regarde si c'est un argument de la fonction
311-
if (stv.symbolTable[nomVar].index_arg >= 0) {
312-
// Évaluation de l'expression qu'on place dans le registre universel !reg
313-
if (res.first) {
314-
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
315-
cfgs.back()->current_bb->add_IRInstr(instruction_const);
316-
}
317-
318-
IRInstr *instruction_copy_arg = new Copy(cfgs.back()->current_bb, "!arg" + std::to_string(stv.symbolTable[nomVar].index_arg), "!reg");
319-
cfgs.back()->current_bb->add_IRInstr(instruction_copy_arg);
320-
}
321-
else {
322-
std::string address = "RBP" + std::to_string(stv.symbolTable[nomVar].offset);
323-
324-
// Évaluation de l'expression qu'on place dans le registre universel !reg
325-
if (res.first) {
326-
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
327-
cfgs.back()->current_bb->add_IRInstr(instruction_const);
328-
}
329-
IRInstr *instruction = new Wmem(cfgs.back()->current_bb, address, "!reg"); // block, dst, src
330-
cfgs.back()->current_bb->add_IRInstr(instruction);
290+
std::string address = "RBP" + std::to_string(stv.varTable[nomVar].offset);
291+
292+
// Évaluation de l'expression qu'on place dans le registre universel !reg
293+
if (res.first) {
294+
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
295+
cfgs.back()->current_bb->add_IRInstr(instruction_const);
331296
}
297+
IRInstr *instruction = new Wmem(cfgs.back()->current_bb, address, "!reg"); // block, dst, src
298+
cfgs.back()->current_bb->add_IRInstr(instruction);
332299

333300
return res;
334301
}
@@ -408,7 +375,7 @@ antlrcpp::Any IRGenVisitor::visitMulDivExpr(ifccParser::MulDivExprContext *ctx)
408375

409376
else {
410377
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
411-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
378+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
412379
// Copier le résultat de l'expression dans la variable temporaire
413380
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
414381
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -487,7 +454,7 @@ antlrcpp::Any IRGenVisitor::visitAddSubExpr(ifccParser::AddSubExprContext *ctx)
487454

488455
else {
489456
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
490-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
457+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
491458
// Copier le résultat de l'expression dans la variable temporaire
492459
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
493460
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -565,7 +532,7 @@ antlrcpp::Any IRGenVisitor::visitCompExpr(ifccParser::CompExprContext *ctx) {
565532

566533
else {
567534
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
568-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
535+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
569536
// Copier le résultat de l'expression dans la variable temporaire
570537
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
571538
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -633,7 +600,7 @@ antlrcpp::Any IRGenVisitor::visitEqExpr(ifccParser::EqExprContext *ctx) {
633600

634601
else {
635602
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
636-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
603+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
637604
// Copier le résultat de l'expression dans la variable temporaire
638605
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
639606
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -679,7 +646,7 @@ antlrcpp::Any IRGenVisitor::visitAndExpr(ifccParser::AndExprContext *ctx) {
679646

680647
else {
681648
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
682-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
649+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
683650
// Copier le résultat de l'expression dans la variable temporaire
684651
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
685652
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -715,7 +682,7 @@ antlrcpp::Any IRGenVisitor::visitXorExpr(ifccParser::XorExprContext *ctx) {
715682

716683
else {
717684
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
718-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
685+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
719686
// Copier le résultat de l'expression dans la variable temporaire
720687
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
721688
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -752,7 +719,7 @@ antlrcpp::Any IRGenVisitor::visitOrExpr(ifccParser::OrExprContext *ctx) {
752719

753720
else {
754721
std::string temp_left = cfgs.back()->create_new_tempvar(Type::INT);
755-
std::string address_left = "RBP" + std::to_string(stv.symbolTable[temp_left].offset);
722+
std::string address_left = "RBP" + std::to_string(stv.varTable[temp_left].offset);
756723
// Copier le résultat de l'expression dans la variable temporaire
757724
IRInstr *instruction_left = new Wmem(cfgs.back()->current_bb, address_left, "!reg");
758725
cfgs.back()->current_bb->add_IRInstr(instruction_left);
@@ -784,11 +751,11 @@ antlrcpp::Any IRGenVisitor::visitFuncCall(ifccParser::FuncCallContext *ctx) {
784751
std::vector<std::string> args_temp_addr;
785752

786753
for (int i = 0; i < ctx->expr().size(); i++) {
787-
std::string temp(cfgs.back()->create_new_tempvar(Type::INT));
788-
args_temp_addr.push_back("RBP" + std::to_string(stv.symbolTable[temp].offset));
754+
std::string temp(cfgs.back()->create_new_tempvar(stv.funcTable[nomFonction].args[i]->type));
755+
args_temp_addr.push_back("RBP" + std::to_string(stv.varTable[temp].offset));
789756
std::pair<bool, int> res(visit(ctx->expr(i)));
790757
if (res.first) {
791-
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second); // block, dst, src
758+
IRInstr *instruction_const = new LdConst(cfgs.back()->current_bb, "!reg", res.second);
792759
cfgs.back()->current_bb->add_IRInstr(instruction_const);
793760
}
794761
IRInstr *instruction_temp = new Wmem(cfgs.back()->current_bb, args_temp_addr.back(), "!reg");
@@ -797,7 +764,7 @@ antlrcpp::Any IRGenVisitor::visitFuncCall(ifccParser::FuncCallContext *ctx) {
797764
}
798765

799766
// On appelle la fonction
800-
IRInstr *instruction_call = new Call(cfgs.back()->current_bb, nomFonction, args_temp_addr); // block, dst, src
767+
IRInstr *instruction_call = new Call(cfgs.back()->current_bb, nomFonction, args_temp_addr);
801768
cfgs.back()->current_bb->add_IRInstr(instruction_call);
802769

803770
return std::pair<bool, int>(false, 0);

compiler/IRInstr.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ void Prologue::gen_x86(std::ostream& o) {
1717
o << " movq %rsp, %rbp" << "\n";
1818

1919
// Allouer de l'espace pour les variables locales
20-
int frameSize = ((-bb->cfg->stv.offsetTable[bb->cfg->functionName] + 15) & ~15); // Alignement sur 16 octets uniquement
20+
int frameSize = ((-bb->cfg->stv.funcTable[bb->cfg->functionName].offset + 15) & ~15); // Alignement sur 16 octets uniquement
2121
if (frameSize > 0) {
2222
o << " subq $" << frameSize << ", %rsp" << "\n";
2323
}
24+
25+
// on sauvegarde les registres
26+
for (int i = 0; i < std::min<int>(6, bb->cfg->stv.funcTable[bb->cfg->functionName].args.size()); i++) {
27+
o << " movl " << bb->cfg->IR_reg_to_x86("!arg" + std::to_string(i) + "32") << ", " << bb->cfg->IR_addr_to_x86("RBP" + std::to_string(bb->cfg->stv.varTable[bb->cfg->stv.funcTable[bb->cfg->functionName].args[i]->name].offset)) << "\n";
28+
}
29+
2430
// o << " jmp " << bb->cfg->functionName << "_0\n";
2531
// on peut l'enlever car arrive tout le temps juste après
2632
}

0 commit comments

Comments
 (0)