Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
Optimize multiwrite concat
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Dec 6, 2017
1 parent 43fdc31 commit ff91b04
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
77 changes: 76 additions & 1 deletion src/code_optimizer_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void code_optimizer_optimize_jumps(CodeOptimizer* optimizer) {
}
}

void code_optimizer_optimize_comparements(CodeOptimizer* optimizer) {
void code_optimizer_optimize_comparisons(CodeOptimizer* optimizer) {
NULL_POINTER_CHECK(optimizer,);
CodeGenerator* generator = optimizer->generator;
CodeInstruction* start = generator->first;
Expand Down Expand Up @@ -626,3 +626,78 @@ void code_optimizer_optimize_comparements(CodeOptimizer* optimizer) {
actual = next;
}
}


void code_optimizer_multi_write(CodeOptimizer* optimizer) {
NULL_POINTER_CHECK(optimizer,);
CodeGenerator* generator = optimizer->generator;
CodeInstruction* start = generator->first;
NULL_POINTER_CHECK(start,);

CodeInstruction* actual = start;
CodeInstruction* next;
while(actual != NULL) {
next = actual->next;

if(actual->type == I_WRITE && actual->next != NULL && actual->next->type == I_WRITE) {
if(actual->op0->type == TYPE_INSTRUCTION_OPERAND_CONSTANT &&
actual->next->op0->type == TYPE_INSTRUCTION_OPERAND_CONSTANT) {
String* together = string_init();
size_t max_buffer = 512;
char* tmp = memory_alloc(sizeof(char) * max_buffer);

if(actual->op0->data.constant.data_type == DATA_TYPE_STRING) {
string_append(together, actual->op0->data.constant.data.string);
} else if(actual->op0->data.constant.data_type == DATA_TYPE_INTEGER) {
snprintf(tmp, max_buffer, "% d", actual->op0->data.constant.data.integer);
string_append_s(together, tmp);
} else if(actual->op0->data.constant.data_type == DATA_TYPE_DOUBLE) {
snprintf(tmp, max_buffer, "% g", actual->op0->data.constant.data.double_);
string_append_s(together, tmp);
} else if(actual->op0->data.constant.data_type == DATA_TYPE_BOOLEAN) {
if(actual->op0->data.constant.data.boolean) {
snprintf(tmp, max_buffer, "%s", "true");
} else {
snprintf(tmp, max_buffer, "%s", "false");
}
string_append_s(together, tmp);
} else {
memory_free(tmp);
actual = next;
continue;
}
if(actual->next->op0->data.constant.data_type == DATA_TYPE_STRING) {
string_append(together, actual->next->op0->data.constant.data.string);
} else if(actual->next->op0->data.constant.data_type == DATA_TYPE_INTEGER) {
snprintf(tmp, max_buffer, "% d", actual->next->op0->data.constant.data.integer);
string_append_s(together, tmp);
} else if(actual->next->op0->data.constant.data_type == DATA_TYPE_DOUBLE) {
snprintf(tmp, max_buffer, "% g", actual->next->op0->data.constant.data.double_);
string_append_s(together, tmp);
} else if(actual->next->op0->data.constant.data_type == DATA_TYPE_BOOLEAN) {
if(actual->next->op0->data.constant.data.boolean) {
snprintf(tmp, max_buffer, "%s", "true");
} else {
snprintf(tmp, max_buffer, "%s", "false");
}
string_append_s(together, tmp);
} else {
memory_free(tmp);
actual = next;
continue;
}

memory_free(tmp);

code_instruction_operand_free(&actual->op0);
actual->op0 = code_instruction_operand_init_string(together);
string_free(&together);

code_generator_remove_instruction(optimizer->generator, actual->next);
next = actual;
}
}

actual = next;
}
}
4 changes: 3 additions & 1 deletion src/code_optimizer_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void code_optimizer_optimize_partial_expression_eval(CodeOptimizer* optimizer);

void code_optimizer_optimize_jumps(CodeOptimizer* optimizer);

void code_optimizer_optimize_comparements(CodeOptimizer* optimizer);
void code_optimizer_optimize_comparisons(CodeOptimizer* optimizer);

void code_optimizer_multi_write(CodeOptimizer* optimizer);

#endif //_CODE_OPTIMIZER_EXPR_H
4 changes: 2 additions & 2 deletions src/ifj2017.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main(int argc, char** argv) {


while(code_optimizer_peep_hole_optimization(parser->optimizer));
code_optimizer_optimize_comparements(parser->optimizer);
code_optimizer_optimize_comparisons(parser->optimizer);


code_optimizer_update_meta_data(parser->optimizer);
Expand All @@ -91,7 +91,7 @@ int main(int argc, char** argv) {
code_optimizer_update_meta_data(parser->optimizer);
code_optimizer_remove_unused_variables(parser->optimizer, false, true);


code_optimizer_multi_write(parser->optimizer);
code_generator_render(parser->code_constructor->generator, stdout);
fflush(stdout);

Expand Down

1 comment on commit ff91b04

@thejoeejoee
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SonyPony pamatuješ tento bájný commit? 😄

Please sign in to comment.