Skip to content

Commit e48760c

Browse files
committed
Merge branch 'development'
2 parents 23a73fb + 48788a5 commit e48760c

23 files changed

Lines changed: 162 additions & 527 deletions

AST/ASTNodes/AST.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../ASTVisitor/ASTCollectLowerings.h"
3131
#include "../ASTVisitor/ASTVariableChecking.h"
3232
#include "../ASTVisitor/TypeInference.h"
33+
#include "../ASTVisitor/ASTLowerTypes.h"
3334
#include "../ASTVisitor/FunctionHandling/BuiltinRestrictionValidator.h"
3435
#include "../ASTVisitor/FunctionHandling/FunctionExitCheck.h"
3536
#include "../ASTVisitor/FunctionHandling/ReturnPathValidator.h"
@@ -208,6 +209,11 @@ void NodeAST::do_type_inference(NodeProgram *program) {
208209
accept(inference);
209210
}
210211

212+
NodeAST * NodeAST::do_type_lowering(NodeProgram *program) {
213+
static ASTLowerTypes lowering_types(program);
214+
return accept(lowering_types);
215+
}
216+
211217
NodeAST * NodeAST::do_lowering(NodeProgram *program) {
212218
static ASTCollectLowerings collect_lowerings(program);
213219
return accept(collect_lowerings);

AST/ASTNodes/AST.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ struct NodeAST {
109109
[[nodiscard]] struct NodeCallback* get_current_callback() const;
110110
[[nodiscard]] struct NodeFunctionDefinition* get_current_function() const;
111111
NodeAST *do_constant_folding();
112+
/// calls TypeInference pass
112113
void do_type_inference(NodeProgram *program);
114+
/// calls ASTLowerTypes pass
115+
NodeAST* do_type_lowering(NodeProgram *program);
113116
NodeAST* do_lowering(NodeProgram* program);
114117
NodeAST* collect_declarations(NodeProgram* program);
115118
NodeAST* collect_call_sites(NodeProgram* program);

AST/ASTNodes/ASTInstructions.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,12 @@ void NodeFunctionCall::determine_function_strategy(NodeProgram *program, NodeCal
276276
}
277277

278278
bool NodeFunctionCall::is_in_access_chain() const {
279-
return parent and parent->cast<NodeAccessChain>();
279+
if (!parent) return false;
280+
if (auto chain = parent->cast<NodeAccessChain>()) {
281+
if (chain->member(0).get() == this) return false;
282+
return true;
283+
}
284+
return false;
280285
}
281286

282287
bool NodeFunctionCall::has_side_effects(const std::unordered_set<std::string> &free_vars) {

AST/ASTNodes/ASTInstructions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ struct NodeFunctionCall final : NodeInstruction {
111111
bool is_destructive_builtin_func() const;
112112
bool check_restricted_environment(NodeCallback *current_callback) const;
113113
void determine_function_strategy(NodeProgram* program, NodeCallback* current_callback);
114+
/// returns true if the function call is inside an access chain. NOT if it is the first member of
115+
/// the chain
114116
bool is_in_access_chain() const;
115117
/// Checks if the function call or any of its arguments has side effects
116118
/// this gets checked by giving a set of free variables that are being modified inside

AST/ASTVisitor/ASTLifeTimeAnalysis.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
#include "ASTVisitor.h"
88

9+
/**
10+
* This pass analysis lifetimes of variables by using Life structs with the first statement
11+
* and the last statement they are used in.
12+
* It parses stmt by stmt, assigns a lifetime object with the current stmt as start and end
13+
* upon visiting a declaration and assignes a new end stmt every time a reference to this var is
14+
* visited
15+
* - variables declared in a while loop (since all other loop forms are transformed at this point)
16+
* are always assigned the full lifetime of the while loop body
17+
*/
918
class ASTLifeTimeAnalysis : public ASTVisitor {
1019
DefinitionProvider* m_def_provider = nullptr;
1120
NodeStatement* m_current_statement = nullptr;
@@ -61,6 +70,18 @@ class ASTLifeTimeAnalysis : public ASTVisitor {
6170
return m_life_times;
6271
}
6372

73+
// this is not used in variable reuse because all relevant function parameters are already
74+
// transformed into assignment statements by parameter transform pass
75+
std::unordered_map<NodeDataStructure*, Life>& run(NodeFunctionDefinition& def) {
76+
m_variables_in_while.clear();
77+
is_in_while_loop = false;
78+
m_life_times.clear();
79+
m_visited_blocks.clear();
80+
m_current_statement = def.body->front();
81+
def.accept(*this);
82+
return m_life_times;
83+
}
84+
6485
/// can be called after run() -> deletes all declaration nodes with variables where
6586
/// start and end member in Life are the same
6687
int remove_unused_local_variables() {
@@ -101,7 +122,7 @@ class ASTLifeTimeAnalysis : public ASTVisitor {
101122

102123
NodeAST *visit(NodeBlock &node) override {
103124
m_visited_blocks.insert(&node);
104-
m_end_of_current_block.push(node.statements.empty() ? nullptr : node.statements.back().get());
125+
m_end_of_current_block.push(node.back());
105126
ASTVisitor::visit(node);
106127
m_end_of_current_block.pop();
107128
return &node;
@@ -132,6 +153,17 @@ class ASTLifeTimeAnalysis : public ASTVisitor {
132153
return &node;
133154
}
134155

156+
NodeAST* visit(NodeFunctionParam & node) override {
157+
node.variable->accept(*this);
158+
// have to do this extra if-check because function param nodes are also used for
159+
// foreach key, value
160+
if (node.variable->is_function_param()) {
161+
add_lifetime_start(node.variable.get(), m_current_statement);
162+
}
163+
if (node.value) node.value->accept(*this);
164+
return &node;
165+
}
166+
135167
NodeAST *visit(NodeArrayRef &node) override {
136168
add_lifetime_end(node, m_current_statement);
137169
return ASTVisitor::visit(node);

AST/ASTVisitor/ASTLowerTypes.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class ASTLowerTypes final : public ASTVisitor {
1919
// most func defs will be visited when called, keeping local scopes in mind
2020
m_program->global_declarations->accept(*this);
2121
m_program->init_callback->accept(*this);
22-
// for(const auto & s : node.struct_definitions) {
23-
// s->accept(*this);
24-
// }
2522
for(const auto & callback : node.callbacks) {
2623
if(callback.get() != m_program->init_callback) callback->accept(*this);
2724
}
@@ -95,9 +92,11 @@ class ASTLowerTypes final : public ASTVisitor {
9592
node.function->accept(*this);
9693
if (node.kind == NodeFunctionCall::Kind::Builtin) return &node;
9794
// go first into definition to lower the header type first und give that to the header ref
98-
if(node.bind_definition(m_program)) {
99-
if(!node.get_definition()->visited) node.get_definition()->accept(*this);
100-
node.get_definition()->visited = true;
95+
node.bind_definition(m_program);
96+
auto decl = node.get_definition();
97+
if(decl) {
98+
if(!decl->visited) decl->accept(*this);
99+
decl->visited = true;
101100
}
102101
if (node.ty->get_element_type() == TypeRegistry::Boolean) {
103102
node.set_element_type(TypeRegistry::Integer);

AST/ASTVisitor/ASTParameterQualifier.h

Lines changed: 0 additions & 207 deletions
This file was deleted.

AST/ASTVisitor/ASTReturnFunctionRewriting.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ class ASTReturnFunctionRewriting final : public ASTVisitor {
5252
void do_rewriting(NodeProgram& node) {
5353
// do hoisting and return parameter promotion
5454
node.accept(*this);
55-
static ReturnFunctionCallHoisting hoisting;
55+
ReturnFunctionCallHoisting hoisting;
5656
hoisting.visit(node);
5757
node.reset_function_visited_flag();
5858
node.debug_print();
5959

60-
static ReturnFunctionIsolation isolation(m_program);
60+
ReturnFunctionIsolation isolation(m_program);
6161
isolation.do_return_function_isolation(node);
6262
node.debug_print();
6363

64-
static ASTTemporaryPointerScope temp_scope(m_program);
64+
ASTTemporaryPointerScope temp_scope(m_program);
6565
temp_scope.visit(node);
6666

6767
node.update_function_lookup();

0 commit comments

Comments
 (0)