Skip to content

Commit

Permalink
do 文の実装
Browse files Browse the repository at this point in the history
  • Loading branch information
tenpoku1000 committed Jan 13, 2022
1 parent ebed756 commit 18014ab
Show file tree
Hide file tree
Showing 42 changed files with 1,145 additions and 327 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ C:\>echo.>PE_COFF-master.zip:Zone.Identifier

## 開発環境

* Visual Studio Community 2019
* Visual Studio Community 2022

Windows SDK and developer tools - Windows app development
https://developer.microsoft.com/en-us/windows/downloads/

Visual Studio Older Downloads
https://visualstudio.microsoft.com/vs/older-downloads/

Download the Windows Driver Kit (WDK) - Windows drivers | Microsoft Docs
https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk
* 64 ビット版 Windows 10

* 64 ビット版 Windows 10 / 11

## ビルド方法

Expand Down
26 changes: 22 additions & 4 deletions src/lib/tp_compiler/tp_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ bool tp_print_parse_tree_grammer(
);

TP_PARSE_TREE* tp_parse_translation_unit(TP_SYMBOL_TABLE* symbol_table);
TP_PARSE_TREE* tp_parse_statement(TP_SYMBOL_TABLE* symbol_table);
TP_PARSE_TREE* tp_parse_statement(
TP_SYMBOL_TABLE* symbol_table, TP_PARSE_WRAP_CONTEXT parse_wrap_context
);
TP_PARSE_TREE* tp_parse_compound_statement(TP_SYMBOL_TABLE* symbol_table);
TP_PARSE_TREE* tp_parse_expression(TP_SYMBOL_TABLE* symbol_table);
TP_PARSE_TREE* tp_parse_constant_expression(TP_SYMBOL_TABLE* symbol_table);
Expand Down Expand Up @@ -506,6 +508,11 @@ bool tp_make_C_IR_expression_statement(
TP_PARSE_TREE* parse_tree, TP_GRAMMER_CONTEXT grammer_context,
TP_C_OBJECT* c_object
);
bool tp_make_C_IR_iteration_statement(
TP_SYMBOL_TABLE* symbol_table,
TP_PARSE_TREE* parse_tree, TP_GRAMMER_CONTEXT grammer_context,
TP_C_OBJECT* c_object
);
bool tp_make_C_IR_jump_statement(
TP_SYMBOL_TABLE* symbol_table,
TP_PARSE_TREE* parse_tree, TP_GRAMMER_CONTEXT grammer_context,
Expand Down Expand Up @@ -692,7 +699,8 @@ bool tp_make_wasm_C_expression(
bool tp_make_wasm_C_iteration_statement_do(
TP_SYMBOL_TABLE* symbol_table, uint8_t* entry_point_symbol,
TP_WASM_MODULE_SECTION* section, TP_C_OBJECT* c_object,
TP_C_TYPE_ITERATION_STATEMENT_DO* type_iteration_statement_do
TP_C_TYPE_ITERATION_STATEMENT_DO* type_iteration_statement_do,
TP_C_TYPE* type_return, uint32_t parameter_num, TP_C_TYPE_FUNCTION_F_PARAM* parameter
);
bool tp_make_wasm_C_jump_statement_return(
TP_SYMBOL_TABLE* symbol_table, uint8_t* entry_point_symbol,
Expand Down Expand Up @@ -833,7 +841,10 @@ uint32_t tp_make_i64_store_code(
uint32_t tp_make_i32_const_code(uint8_t* buffer, size_t offset, int32_t value);
uint32_t tp_make_i64_const_code(uint8_t* buffer, size_t offset, int64_t value);

// Comparison operators
// Comparison operators(i32)
uint32_t tp_make_i32_ne_code(uint8_t* buffer, size_t offset);

// Comparison operators(i64)
uint32_t tp_make_i64_ne_code(uint8_t* buffer, size_t offset);

// Numeric operators(i32)
Expand Down Expand Up @@ -963,7 +974,14 @@ uint32_t tp_encode_i64_const_code(
TP_WASM_RELOCATION* wasm_relocation, int64_t value, bool* status
);

// Comparison operators
// Comparison operators(i32)

uint32_t tp_encode_i32_ne_code(
TP_SYMBOL_TABLE* symbol_table, uint8_t* x64_code_buffer, uint32_t x64_code_offset,
TP_WASM_RELOCATION* wasm_relocation, TP_WASM_STACK_ELEMENT* op1, TP_WASM_STACK_ELEMENT* op2
);

// Comparison operators(i64)

uint32_t tp_encode_i64_ne_code(
TP_SYMBOL_TABLE* symbol_table, uint8_t* x64_code_buffer, uint32_t x64_code_offset,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/tp_compiler/tp_compiler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_initializer.c" />
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_statements.c" />
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_statements_expr.c" />
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_statements_iteration.c" />
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_statements_jump.c" />
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_translation_unit.c" />
<ClCompile Include="tp_semantic_analysis\tp_semantic_analysis.c" />
Expand Down Expand Up @@ -121,7 +122,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<EnableASAN>false</EnableASAN>
</PropertyGroup>
Expand Down Expand Up @@ -151,6 +152,8 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/tp_compiler/tp_compiler.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@
<ClCompile Include="tp_make_parse_tree\tp_parser_declaration_type_name.c">
<Filter>ソース ファイル\tp_make_parse_tree</Filter>
</ClCompile>
<ClCompile Include="tp_semantic_analysis\tp_make_C_IR_statements_iteration.c">
<Filter>ソース ファイル\tp_semantic_analysis</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="tp_compiler.h">
Expand Down
48 changes: 38 additions & 10 deletions src/lib/tp_compiler/tp_compiler/tp_init_symbol_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void tp_free_memory_and_file(TP_SYMBOL_TABLE** symbol_table)

rsize_t token_pos = (*symbol_table)->member_tp_token_pos;

for (rsize_t i = 0; token_pos > i; ++i){
for (rsize_t i = (((*symbol_table)->member_is_int_calc_compiler) ? 0 : 2); token_pos > i; ++i){

if (token[i].member_string){

Expand Down Expand Up @@ -798,12 +798,16 @@ static void free_c_type(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)
return;
}

// C compiler: type
TP_C_TYPE_COMPOUND_STATEMENT* compound_statement = NULL;
TP_C_TYPE_COMPOUND_STATEMENT* type_compound_statement = NULL;

switch ((*c_type)->member_type){
case TP_C_TYPE_TYPE_NONE:
break;
case TP_C_TYPE_TYPE_BASIC:
break;
case TP_C_TYPE_TYPE_STRUCT:
TP_PUT_LOG_MSG_ICE(symbol_table);
break;
case TP_C_TYPE_TYPE_FUNCTION:{

TP_C_TYPE_FUNCTION* function = &((*c_type)->member_body.member_type_function);
Expand Down Expand Up @@ -835,7 +839,7 @@ static void free_c_type(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)

TP_FREE(symbol_table, &(function->member_c_return_type), sizeof(TP_C_TYPE));

compound_statement = &(function->member_function_body);
type_compound_statement = &(function->member_function_body);
goto compound_statement;
}
case TP_C_TYPE_TYPE_FUNCTION_PARAM:{
Expand All @@ -846,17 +850,20 @@ static void free_c_type(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)
TP_FREE(symbol_table, type_param, sizeof(TP_C_TYPE));
break;
}
case TP_C_TYPE_TYPE_POINTER:
TP_PUT_LOG_MSG_ICE(symbol_table);
break;
case TP_C_TYPE_TYPE_NULL_STATEMENT:
break;
case TP_C_TYPE_TYPE_COMPOUND_STATEMENT:{
compound_statement = &((*c_type)->member_body.member_type_compound_statement);
type_compound_statement = &((*c_type)->member_body.member_type_compound_statement);
compound_statement:
;
TP_C_OBJECT** c_object_local = compound_statement->member_statement_body;
TP_C_OBJECT** c_object_local = type_compound_statement->member_statement_body;

if (c_object_local){

rsize_t c_object_local_pos = compound_statement->member_statement_body_pos;
rsize_t c_object_local_pos = type_compound_statement->member_statement_body_pos;

for (rsize_t i = 0; c_object_local_pos > i; ++i){

Expand All @@ -866,7 +873,7 @@ static void free_c_type(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)
}
}

TP_FREE2(symbol_table, &c_object_local, compound_statement->member_statement_body_size);
TP_FREE2(symbol_table, &c_object_local, type_compound_statement->member_statement_body_size);
}
break;
}
Expand All @@ -876,10 +883,20 @@ static void free_c_type(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)
&((*c_type)->member_body.member_type_declaration_statement.member_declaration);
free_c_type(symbol_table, type_declaration);
TP_FREE(symbol_table, type_declaration, sizeof(TP_C_TYPE));
// break;
free_c_expr(symbol_table, c_type);
break;
}
case TP_C_TYPE_TYPE_EXPRESSION_STATEMENT:
// break;
free_c_expr(symbol_table, c_type);
break;
case TP_C_TYPE_TYPE_ITERATION_STATEMENT_DO:{

TP_C_OBJECT** statement =
&((*c_type)->member_body.member_type_iteration_statement_do.member_statement);
tp_free_c_object(symbol_table, statement);
free_c_expr(symbol_table, c_type);
break;
}
case TP_C_TYPE_TYPE_JUMP_STATEMENT_RETURN:
free_c_expr(symbol_table, c_type);
break;
Expand Down Expand Up @@ -917,6 +934,17 @@ static void free_c_expr(TP_SYMBOL_TABLE* symbol_table, TP_C_TYPE** c_type)
);
break;
}
case TP_C_TYPE_TYPE_ITERATION_STATEMENT_DO:{

TP_C_TYPE_ITERATION_STATEMENT_DO* type_iteration_statement_do =
&(c_type_body->member_type_iteration_statement_do);

TP_FREE(
symbol_table, &(type_iteration_statement_do->member_c_expr),
type_iteration_statement_do->member_c_expr_size
);
break;
}
case TP_C_TYPE_TYPE_JUMP_STATEMENT_RETURN:{

TP_C_TYPE_JUMP_STATEMENT_RETURN* type_jump_statement_return =
Expand Down
28 changes: 28 additions & 0 deletions src/lib/tp_compiler/tp_compiler/tp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ TEST_CASE_TABLE tp_C_test_case_table[] = {
" return calc_body(g_value1 = (1 + 2) * 3, value2, g_value3, 51);\n"
"}\n", 202 },

// No.29
{ "int calc(void)\n"
"{\n"
" int v = 3;\n"
" int w = 3;\n"
"\n"
" do{\n"
" v = v - 1;\n"
" do w = w - 1; while (w);\n"
" }while (v);\n"
"\n"
" return v;\n"
"}\n", 0 },

// No.30
{ "int calc(void)\n"
"{\n"
" int v = 0;\n"
"\n"
" do{\n"
" v = v + 1;\n"
" int w = 0;\n"
" do w = w + 1; while (w != 3);\n"
" }while (v != 3);\n"
"\n"
" return v;\n"
"}\n", 3 },

{ NULL, 0 }
};

Expand Down
21 changes: 13 additions & 8 deletions src/lib/tp_compiler/tp_compiler_parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@
#define IS_END_OF_TOKEN(token) ((NULL == (token)) || ((token) && (TP_SYMBOL_NULL == (token)->member_symbol)))

#define MAKE_PARSE_SUBTREE(symbol_table, grammer, ...) \
tp_make_parse_subtree( \
(symbol_table), (grammer), \
(TP_PARSE_TREE_ELEMENT[]){ __VA_ARGS__ }, \
sizeof((TP_PARSE_TREE_ELEMENT[]){ __VA_ARGS__ }) / sizeof(TP_PARSE_TREE_ELEMENT) \
)
tp_make_parse_subtree( \
(symbol_table), (grammer), \
(TP_PARSE_TREE_ELEMENT[]){ __VA_ARGS__ }, \
sizeof((TP_PARSE_TREE_ELEMENT[]){ __VA_ARGS__ }) / sizeof(TP_PARSE_TREE_ELEMENT) \
)
#define TP_TREE_TOKEN(token) (TP_PARSE_TREE_ELEMENT){ \
.member_type = TP_PARSE_TREE_TYPE_TOKEN, \
.member_body.member_tp_token = (token) \
Expand All @@ -246,16 +246,23 @@ typedef enum TP_PARSE_TREE_TYPE_
TP_PARSE_TREE_TYPE_NODE
}TP_PARSE_TREE_TYPE;

typedef struct tp_parse_tree_ TP_PARSE_TREE;

typedef union tp_parse_tree_element_union_{
TP_TOKEN* member_tp_token; // NOTE: member_token must not free memory.
struct tp_parse_tree_* member_child;
TP_PARSE_TREE* member_child;
}TP_PARSE_TREE_ELEMENT_UNION;

typedef struct tp_parse_tree_element_{
TP_PARSE_TREE_TYPE member_type;
TP_PARSE_TREE_ELEMENT_UNION member_body;
}TP_PARSE_TREE_ELEMENT;

typedef enum TP_PARSE_WRAP_CONTEXT_{
TP_PARSE_NONE_WRAP_COMPOUND_STATEMENT = 0,
TP_PARSE_WRAP_COMPOUND_STATEMENT
}TP_PARSE_WRAP_CONTEXT;

typedef enum TP_PARSE_TREE_GRAMMER_
{
TP_PARSE_TREE_GRAMMER_NULL = 0,
Expand Down Expand Up @@ -836,8 +843,6 @@ typedef enum TP_PARSE_TREE_GRAMMER_
TP_PARSE_TREE_GRAMMER_NUM
}TP_PARSE_TREE_GRAMMER;

typedef struct tp_c_object_ TP_C_OBJECT;

typedef struct tp_parse_tree_{
TP_PARSE_TREE_GRAMMER member_grammer;
size_t member_element_num;
Expand Down
Loading

0 comments on commit 18014ab

Please sign in to comment.