mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 22:02:44 +00:00 
			
		
		
		
	JSSpecCompiler: Store arguments in declaration instead of definition
And create a struct encapsulating argument name in the preparation for argument types and optional arguments.
This commit is contained in:
		
							parent
							
								
									0806ccaeec
								
							
						
					
					
						commit
						483e195e48
					
				
					 8 changed files with 57 additions and 40 deletions
				
			
		|  | @ -14,8 +14,8 @@ namespace JSSpecCompiler { | ||||||
| 
 | 
 | ||||||
| void ReferenceResolvingPass::process_function() | void ReferenceResolvingPass::process_function() | ||||||
| { | { | ||||||
|     for (auto name : m_function->m_argument_names) |     for (auto argument : m_function->m_arguments) | ||||||
|         m_function->m_local_variables.set(name, make_ref_counted<NamedVariableDeclaration>(name)); |         m_function->m_local_variables.set(argument.name, make_ref_counted<NamedVariableDeclaration>(argument.name)); | ||||||
|     GenericASTPass::process_function(); |     GenericASTPass::process_function(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -430,16 +430,16 @@ void SSABuildingPass::rename_variables(Vertex u, Vertex from) | ||||||
| void SSABuildingPass::rename_variables() | void SSABuildingPass::rename_variables() | ||||||
| { | { | ||||||
|     HashMap<StringView, size_t> argument_index_by_name; |     HashMap<StringView, size_t> argument_index_by_name; | ||||||
|     for (size_t i = 0; i < m_function->m_argument_names.size(); ++i) |     for (size_t i = 0; i < m_function->m_arguments.size(); ++i) | ||||||
|         argument_index_by_name.set(m_function->m_argument_names[i], i); |         argument_index_by_name.set(m_function->m_arguments[i].name, i); | ||||||
|     m_function->m_arguments.resize(m_function->m_argument_names.size()); |     m_function->m_ssa_arguments.resize(m_function->m_arguments.size()); | ||||||
| 
 | 
 | ||||||
|     for (auto const& [name, var_decl] : m_function->m_local_variables) { |     for (auto const& [name, var_decl] : m_function->m_local_variables) { | ||||||
|         make_new_ssa_variable_for(var_decl); |         make_new_ssa_variable_for(var_decl); | ||||||
| 
 | 
 | ||||||
|         if (auto maybe_index = argument_index_by_name.get(name); maybe_index.has_value()) { |         if (auto maybe_index = argument_index_by_name.get(name); maybe_index.has_value()) { | ||||||
|             size_t index = maybe_index.value(); |             size_t index = maybe_index.value(); | ||||||
|             m_function->m_arguments[index] = m_def_stack.get(var_decl).value()[0]; |             m_function->m_ssa_arguments[index] = m_def_stack.get(var_decl).value()[0]; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     make_new_ssa_variable_for(m_function->m_named_return_value); |     make_new_ssa_variable_for(m_function->m_named_return_value); | ||||||
|  |  | ||||||
|  | @ -40,15 +40,15 @@ FunctionDeclarationRef TranslationUnit::find_declaration_by_name(StringView name | ||||||
|     return it->value; |     return it->value; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| FunctionDeclaration::FunctionDeclaration(StringView name) | FunctionDeclaration::FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments) | ||||||
|     : m_name(name) |     : m_name(name) | ||||||
|  |     , m_arguments(arguments) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| FunctionDefinition::FunctionDefinition(StringView name, Tree ast, Vector<StringView>&& argument_names) | FunctionDefinition::FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments) | ||||||
|     : FunctionDeclaration(name) |     : FunctionDeclaration(name, move(arguments)) | ||||||
|     , m_ast(move(ast)) |     , m_ast(move(ast)) | ||||||
|     , m_argument_names(move(argument_names)) |  | ||||||
|     , m_named_return_value(make_ref_counted<NamedVariableDeclaration>("$return"sv)) |     , m_named_return_value(make_ref_counted<NamedVariableDeclaration>("$return"sv)) | ||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -38,24 +38,28 @@ private: | ||||||
|     HashMap<StringView, FunctionDeclarationRef> m_function_index; |     HashMap<StringView, FunctionDeclarationRef> m_function_index; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | struct FunctionArgument { | ||||||
|  |     StringView name; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| class FunctionDeclaration : public RefCounted<FunctionDeclaration> { | class FunctionDeclaration : public RefCounted<FunctionDeclaration> { | ||||||
| public: | public: | ||||||
|     FunctionDeclaration(StringView name); |     FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments); | ||||||
| 
 | 
 | ||||||
|     virtual ~FunctionDeclaration() = default; |     virtual ~FunctionDeclaration() = default; | ||||||
| 
 | 
 | ||||||
|     TranslationUnitRef m_translation_unit = nullptr; |     TranslationUnitRef m_translation_unit = nullptr; | ||||||
|     StringView m_name; |     StringView m_name; | ||||||
|  |     Vector<FunctionArgument> m_arguments; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class FunctionDefinition : public FunctionDeclaration { | class FunctionDefinition : public FunctionDeclaration { | ||||||
| public: | public: | ||||||
|     FunctionDefinition(StringView name, Tree ast, Vector<StringView>&& argument_names); |     FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments); | ||||||
| 
 | 
 | ||||||
|     void reindex_ssa_variables(); |     void reindex_ssa_variables(); | ||||||
| 
 | 
 | ||||||
|     Tree m_ast; |     Tree m_ast; | ||||||
|     Vector<StringView> m_argument_names; |  | ||||||
| 
 | 
 | ||||||
|     // Populates during reference resolving
 |     // Populates during reference resolving
 | ||||||
|     // NOTE: The hash map here is ordered since we do not want random hash changes to break our test
 |     // NOTE: The hash map here is ordered since we do not want random hash changes to break our test
 | ||||||
|  | @ -67,7 +71,7 @@ public: | ||||||
|     RefPtr<ControlFlowGraph> m_cfg; |     RefPtr<ControlFlowGraph> m_cfg; | ||||||
| 
 | 
 | ||||||
|     // Fields populate during SSA building
 |     // Fields populate during SSA building
 | ||||||
|     Vector<SSAVariableDeclarationRef> m_arguments; |     Vector<SSAVariableDeclarationRef> m_ssa_arguments; | ||||||
|     SSAVariableDeclarationRef m_return_value; |     SSAVariableDeclarationRef m_return_value; | ||||||
|     Vector<SSAVariableDeclarationRef> m_local_ssa_variables; |     Vector<SSAVariableDeclarationRef> m_local_ssa_variables; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -24,9 +24,9 @@ NonnullRefPtr<FunctionDefinition> CppASTConverter::convert() | ||||||
|     } |     } | ||||||
|     auto tree = make_ref_counted<TreeList>(move(toplevel_statements)); |     auto tree = make_ref_counted<TreeList>(move(toplevel_statements)); | ||||||
| 
 | 
 | ||||||
|     Vector<StringView> arguments; |     Vector<FunctionArgument> arguments; | ||||||
|     for (auto const& parameter : m_function->parameters()) |     for (auto const& parameter : m_function->parameters()) | ||||||
|         arguments.append(parameter->full_name()); |         arguments.append({ .name = parameter->full_name() }); | ||||||
| 
 | 
 | ||||||
|     return make_ref_counted<FunctionDefinition>(name, tree, move(arguments)); |     return make_ref_counted<FunctionDefinition>(name, tree, move(arguments)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -249,12 +249,12 @@ void SpecParsingStep::run(TranslationUnitRef translation_unit) | ||||||
| 
 | 
 | ||||||
|     auto spec_function = SpecFunction::create(&m_document->root()).release_value_but_fixme_should_propagate_errors(); |     auto spec_function = SpecFunction::create(&m_document->root()).release_value_but_fixme_should_propagate_errors(); | ||||||
| 
 | 
 | ||||||
|     Vector<StringView> argument_names; |     Vector<FunctionArgument> arguments; | ||||||
|     for (auto const& argument : spec_function.m_arguments) |     for (auto const& argument : spec_function.m_arguments) | ||||||
|         argument_names.append(argument.name); |         arguments.append({ argument.name }); | ||||||
| 
 | 
 | ||||||
|     translation_unit->adopt_function( |     translation_unit->adopt_function( | ||||||
|         make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree, move(argument_names))); |         make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree, move(arguments))); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| ===== AST after parser ===== | ===== AST after parser ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfBranch |   IfBranch | ||||||
|     UnresolvedReference cond1 |     UnresolvedReference cond1 | ||||||
|  | @ -27,7 +27,7 @@ TreeList | ||||||
|     UnresolvedReference b |     UnresolvedReference b | ||||||
| 
 | 
 | ||||||
| ===== AST after function-call-canonicalization ===== | ===== AST after function-call-canonicalization ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfBranch |   IfBranch | ||||||
|     UnresolvedReference cond1 |     UnresolvedReference cond1 | ||||||
|  | @ -55,7 +55,7 @@ TreeList | ||||||
|     UnresolvedReference b |     UnresolvedReference b | ||||||
| 
 | 
 | ||||||
| ===== AST after if-branch-merging ===== | ===== AST after if-branch-merging ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     UnresolvedReference cond1 |     UnresolvedReference cond1 | ||||||
|  | @ -81,7 +81,7 @@ TreeList | ||||||
|     UnresolvedReference b |     UnresolvedReference b | ||||||
| 
 | 
 | ||||||
| ===== AST after reference-resolving ===== | ===== AST after reference-resolving ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     Var cond1 |     Var cond1 | ||||||
|  | @ -107,7 +107,7 @@ TreeList | ||||||
|     Var b |     Var b | ||||||
| 
 | 
 | ||||||
| ===== AST after cfg-building ===== | ===== AST after cfg-building ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     Var cond1 |     Var cond1 | ||||||
|  | @ -133,7 +133,7 @@ TreeList | ||||||
|     Var b |     Var b | ||||||
| 
 | 
 | ||||||
| ===== CFG after cfg-building ===== | ===== CFG after cfg-building ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| 0: | 0: | ||||||
| ControlFlowBranch true=3 false=7 | ControlFlowBranch true=3 false=7 | ||||||
|   Var cond1 |   Var cond1 | ||||||
|  | @ -183,7 +183,7 @@ BinaryOperation Assignment | ||||||
| ControlFlowJump jump=1 | ControlFlowJump jump=1 | ||||||
| 
 | 
 | ||||||
| ===== AST after cfg-simplification ===== | ===== AST after cfg-simplification ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     Var cond1 |     Var cond1 | ||||||
|  | @ -209,7 +209,7 @@ TreeList | ||||||
|     Var b |     Var b | ||||||
| 
 | 
 | ||||||
| ===== CFG after cfg-simplification ===== | ===== CFG after cfg-simplification ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| 0: | 0: | ||||||
| ControlFlowBranch true=3 false=6 | ControlFlowBranch true=3 false=6 | ||||||
|   Var cond1 |   Var cond1 | ||||||
|  | @ -250,7 +250,7 @@ BinaryOperation Assignment | ||||||
| ControlFlowJump jump=2 | ControlFlowJump jump=2 | ||||||
| 
 | 
 | ||||||
| ===== AST after ssa-building ===== | ===== AST after ssa-building ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     Var cond1@0 |     Var cond1@0 | ||||||
|  | @ -276,7 +276,7 @@ TreeList | ||||||
|     Var b@2 |     Var b@2 | ||||||
| 
 | 
 | ||||||
| ===== CFG after ssa-building ===== | ===== CFG after ssa-building ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| 0: | 0: | ||||||
| ControlFlowBranch true=1 false=6 | ControlFlowBranch true=1 false=6 | ||||||
|   Var cond1@0 |   Var cond1@0 | ||||||
|  | @ -319,7 +319,7 @@ BinaryOperation Assignment | ||||||
| ControlFlowJump jump=3 | ControlFlowJump jump=3 | ||||||
| 
 | 
 | ||||||
| ===== AST after dce ===== | ===== AST after dce ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| TreeList | TreeList | ||||||
|   IfElseIfChain |   IfElseIfChain | ||||||
|     Var cond1@0 |     Var cond1@0 | ||||||
|  | @ -345,7 +345,7 @@ TreeList | ||||||
|     Var b@2 |     Var b@2 | ||||||
| 
 | 
 | ||||||
| ===== CFG after dce ===== | ===== CFG after dce ===== | ||||||
| f([ cond1, cond2 ]): | f(cond1, cond2): | ||||||
| 0: | 0: | ||||||
| ControlFlowBranch true=1 false=6 | ControlFlowBranch true=1 false=6 | ||||||
|   Var cond1@0 |   Var cond1@0 | ||||||
|  |  | ||||||
|  | @ -71,6 +71,19 @@ private: | ||||||
|     Vector<CompilationStepWithDumpOptions> m_pipeline; |     Vector<CompilationStepWithDumpOptions> m_pipeline; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | template<> | ||||||
|  | struct AK::Formatter<Vector<FunctionArgument>> : AK::Formatter<StringView> { | ||||||
|  |     ErrorOr<void> format(FormatBuilder& builder, Vector<FunctionArgument> const& arguments) | ||||||
|  |     { | ||||||
|  |         for (size_t i = 0; i < arguments.size(); ++i) { | ||||||
|  |             TRY(builder.put_string(arguments[i].name)); | ||||||
|  |             if (i + 1 != arguments.size()) | ||||||
|  |                 TRY(builder.put_literal(", "sv)); | ||||||
|  |         } | ||||||
|  |         return {}; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| ErrorOr<int> serenity_main(Main::Arguments arguments) | ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| { | { | ||||||
|     Core::ArgsParser args_parser; |     Core::ArgsParser args_parser; | ||||||
|  | @ -125,13 +138,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|     // Functions referenced in DifferenceISODate
 |     // Functions referenced in DifferenceISODate
 | ||||||
|     // TODO: This is here just for testing. In a long run, we need some place, which is not
 |     // TODO: This is here just for testing. In a long run, we need some place, which is not
 | ||||||
|     //       `serenity_main`, to store built-in functions.
 |     //       `serenity_main`, to store built-in functions.
 | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CompareISODate"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("CreateDateDurationRecord"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("AddISODate"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODaysInMonth"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("ISODateToEpochDays"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("truncate"sv, Vector<FunctionArgument> {})); | ||||||
|     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("remainder"sv)); |     translation_unit.adopt_declaration(make_ref_counted<FunctionDeclaration>("remainder"sv, Vector<FunctionArgument> {})); | ||||||
| 
 | 
 | ||||||
|     for (auto const& step : pipeline.pipeline()) { |     for (auto const& step : pipeline.pipeline()) { | ||||||
|         step.step->run(&translation_unit); |         step.step->run(&translation_unit); | ||||||
|  | @ -144,14 +157,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         if (step.dump_ast) { |         if (step.dump_ast) { | ||||||
|             outln(stderr, "===== AST after {} =====", step.step->name()); |             outln(stderr, "===== AST after {} =====", step.step->name()); | ||||||
|             for (auto const& function : translation_unit.functions_to_compile()) { |             for (auto const& function : translation_unit.functions_to_compile()) { | ||||||
|                 outln(stderr, "{}({}):", function->m_name, function->m_argument_names); |                 outln(stderr, "{}({}):", function->m_name, function->m_arguments); | ||||||
|                 outln(stderr, "{}", function->m_ast); |                 outln(stderr, "{}", function->m_ast); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         if (step.dump_cfg && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) { |         if (step.dump_cfg && translation_unit.functions_to_compile()[0]->m_cfg != nullptr) { | ||||||
|             outln(stderr, "===== CFG after {} =====", step.step->name()); |             outln(stderr, "===== CFG after {} =====", step.step->name()); | ||||||
|             for (auto const& function : translation_unit.functions_to_compile()) { |             for (auto const& function : translation_unit.functions_to_compile()) { | ||||||
|                 outln(stderr, "{}({}):", function->m_name, function->m_argument_names); |                 outln(stderr, "{}({}):", function->m_name, function->m_arguments); | ||||||
|                 outln(stderr, "{}", *function->m_cfg); |                 outln(stderr, "{}", *function->m_cfg); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Klishch
						Dan Klishch