2018-01-21 03:56:50 +00:00
|
|
|
/* Copyright (C) 2016 Jeremiah Orians
|
|
|
|
* This file is part of stage0.
|
|
|
|
*
|
|
|
|
* stage0 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* stage0 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with stage0. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cc.h"
|
2018-07-20 03:33:02 +01:00
|
|
|
#include "gcc_req.h"
|
2018-01-21 03:56:50 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/* Global lists */
|
|
|
|
struct token_list* global_symbol_list;
|
|
|
|
struct token_list* global_function_list;
|
|
|
|
struct token_list* global_constant_list;
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
/* Core lists for this file */
|
|
|
|
struct token_list* function;
|
|
|
|
struct token_list* out;
|
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
/* What we are currently working on */
|
2018-01-25 01:00:22 +00:00
|
|
|
struct type* current_target;
|
2018-06-28 21:40:51 +01:00
|
|
|
char* break_target_head;
|
|
|
|
char* break_target_func;
|
|
|
|
char* break_target_num;
|
2018-08-10 22:30:48 +01:00
|
|
|
struct token_list* break_frame;
|
2018-06-08 20:43:36 +01:00
|
|
|
int current_count;
|
2018-07-20 03:33:02 +01:00
|
|
|
struct type* last_type;
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* Imported functions */
|
|
|
|
char* parse_string(char* string);
|
2018-06-02 01:05:27 +01:00
|
|
|
int escape_lookup(char* c);
|
2018-02-24 02:18:23 +00:00
|
|
|
char* numerate_number(int a);
|
|
|
|
void require_match(char* message, char* required);
|
2018-05-26 21:33:00 +01:00
|
|
|
void line_error();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
struct token_list* emit(char *s, struct token_list* head)
|
|
|
|
{
|
|
|
|
struct token_list* t = calloc(1, sizeof(struct token_list));
|
|
|
|
t->next = head;
|
|
|
|
t->s = s;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void emit_out(char* s)
|
|
|
|
{
|
|
|
|
out = emit(s, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct token_list* uniqueID(char* s, struct token_list* l, char* num)
|
|
|
|
{
|
|
|
|
l = emit(s, l);
|
|
|
|
l = emit("_", l);
|
|
|
|
l = emit(num, l);
|
|
|
|
l = emit("\n", l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uniqueID_out(char* s, char* num)
|
|
|
|
{
|
|
|
|
emit_out(s);
|
|
|
|
emit_out("_");
|
|
|
|
emit_out(num);
|
|
|
|
emit_out("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
struct token_list* sym_declare(char *s, struct type* t, struct token_list* list)
|
|
|
|
{
|
|
|
|
struct token_list* a = calloc(1, sizeof(struct token_list));
|
|
|
|
a->next = list;
|
|
|
|
a->s = s;
|
|
|
|
a->type = t;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct token_list* sym_lookup(char *s, struct token_list* symbol_list)
|
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
struct token_list* i;
|
|
|
|
for(i = symbol_list; NULL != i; i = i->next)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-02-01 04:14:40 +00:00
|
|
|
if(match(i->s, s)) return i;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void expression();
|
|
|
|
void function_call(char* s, int bool)
|
2018-05-03 01:56:44 +01:00
|
|
|
{
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_expression_list\nNo ( was found\n", "(");
|
2018-05-03 01:56:44 +01:00
|
|
|
int passed = 0;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("PUSH_edi\t# Prevent overwriting in recursion\n");
|
|
|
|
emit_out("PUSH_ebp\t# Protect the old base pointer\n");
|
|
|
|
emit_out("COPY_esp_to_edi\t# Copy new base pointer\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
|
|
|
|
if(global_token->s[0] != ')')
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
|
|
|
emit_out("PUSH_eax\t#_process_expression1\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
passed = 1;
|
|
|
|
|
|
|
|
while(global_token->s[0] == ',')
|
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
|
|
|
emit_out("PUSH_eax\t#_process_expression2\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
passed = passed + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_match("ERROR in process_expression_list\nNo ) was found\n", ")");
|
|
|
|
|
2018-07-20 03:33:02 +01:00
|
|
|
if(TRUE == bool)
|
2018-05-03 01:56:44 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_BASE_ADDRESS_eax %");
|
|
|
|
emit_out(s);
|
|
|
|
emit_out("\nLOAD_INTEGER\n");
|
|
|
|
emit_out("COPY_edi_to_ebp\n");
|
|
|
|
emit_out("CALL_eax\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("COPY_edi_to_ebp\n");
|
|
|
|
emit_out("CALL_IMMEDIATE %FUNCTION_");
|
|
|
|
emit_out(s);
|
|
|
|
emit_out("\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(; passed > 0; passed = passed - 1)
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("POP_ebx\t# _process_expression_locals\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("POP_ebp\t# Restore old base pointer\n");
|
|
|
|
emit_out("POP_edi\t# Prevent overwrite\n");
|
2018-05-03 01:56:44 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void constant_load(struct token_list* a)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %");
|
|
|
|
emit_out(a->arguments->s);
|
|
|
|
emit_out("\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void variable_load(struct token_list* a)
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
if(match("FUNCTION", a->type->name) && match("(", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
function_call(numerate_number(a->depth), TRUE);
|
|
|
|
return;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
current_target = a->type;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_BASE_ADDRESS_eax %");
|
|
|
|
emit_out(numerate_number(a->depth));
|
|
|
|
emit_out("\n");
|
|
|
|
if(!match("=", global_token->s) && !match("char**", a->type->name)) emit_out("LOAD_INTEGER\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void function_load(struct token_list* a)
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
if(match("(", global_token->s))
|
|
|
|
{
|
|
|
|
function_call(a->s, FALSE);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-03 01:56:44 +01:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax &FUNCTION_");
|
|
|
|
emit_out(a->s);
|
|
|
|
emit_out("\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void global_load(struct token_list* a)
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
current_target = a->type;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax &GLOBAL_");
|
|
|
|
emit_out(a->s);
|
|
|
|
emit_out("\n");
|
|
|
|
if(!match("=", global_token->s)) emit_out("LOAD_INTEGER\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* primary-expr:
|
2018-07-20 03:33:02 +01:00
|
|
|
* FAILURE
|
|
|
|
* "String"
|
|
|
|
* 'Char'
|
|
|
|
* [0-9]*
|
|
|
|
* [a-z,A-Z]*
|
|
|
|
* ( expression )
|
2018-01-21 03:56:50 +00:00
|
|
|
*/
|
2018-07-20 03:33:02 +01:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr_failure()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-07-20 03:33:02 +01:00
|
|
|
file_print("Recieved ", stderr);
|
|
|
|
file_print(global_token->s, stderr);
|
|
|
|
file_print(" in primary_expr\n", stderr);
|
|
|
|
line_error();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr_string()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
char* number_string = numerate_number(current_count);
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax &STRING_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
/* The target */
|
|
|
|
strings_list = emit(":STRING_", strings_list);
|
|
|
|
strings_list = uniqueID(function->s, strings_list, number_string);
|
|
|
|
|
|
|
|
/* Parse the string */
|
|
|
|
strings_list = emit(parse_string(global_token->s), strings_list);
|
|
|
|
global_token = global_token->next;
|
|
|
|
|
|
|
|
current_count = current_count + 1;
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr_char()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %");
|
2018-07-20 03:33:02 +01:00
|
|
|
if('\\' == global_token->s[1])
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(numerate_number(escape_lookup(global_token->s + 1)));
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(numerate_number(global_token->s[1]));
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
global_token = global_token->next;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr_number()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %");
|
|
|
|
emit_out(global_token->s);
|
|
|
|
emit_out("\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = global_token->next;
|
2018-01-25 01:00:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr_variable()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
char* s = global_token->s;
|
|
|
|
global_token = global_token->next;
|
|
|
|
struct token_list* a = sym_lookup(s, global_constant_list);
|
2018-08-18 20:23:58 +01:00
|
|
|
if(NULL != a)
|
|
|
|
{
|
|
|
|
constant_load(a);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
a= sym_lookup(s, function->locals);
|
2018-08-18 20:23:58 +01:00
|
|
|
if(NULL != a)
|
|
|
|
{
|
|
|
|
variable_load(a);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
a = sym_lookup(s, function->arguments);
|
2018-08-18 20:23:58 +01:00
|
|
|
if(NULL != a)
|
|
|
|
{
|
|
|
|
variable_load(a);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
a= sym_lookup(s, global_function_list);
|
2018-08-18 20:23:58 +01:00
|
|
|
if(NULL != a)
|
|
|
|
{
|
|
|
|
function_load(a);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
a = sym_lookup(s, global_symbol_list);
|
2018-08-18 20:23:58 +01:00
|
|
|
if(NULL != a)
|
|
|
|
{
|
|
|
|
global_load(a);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
file_print(s ,stderr);
|
|
|
|
file_print(" is not a defined symbol\n", stderr);
|
|
|
|
line_error();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr();
|
2018-04-24 02:35:27 +01:00
|
|
|
struct type* promote_type(struct type* a, struct type* b)
|
|
|
|
{
|
|
|
|
if(NULL == a)
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if(NULL == b)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct type* i;
|
|
|
|
for(i = global_types; NULL != i; i = i->next)
|
|
|
|
{
|
|
|
|
if(a->name == i->name)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if(b->name == i->name)
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if(a->name == i->indirect->name)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if(b->name == i->indirect->name)
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void common_recursion(FUNCTION f)
|
2018-01-25 01:00:22 +00:00
|
|
|
{
|
2018-07-20 03:33:02 +01:00
|
|
|
last_type = current_target;
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("PUSH_eax\t#_common_recursion\n");
|
|
|
|
f();
|
2018-04-24 02:35:27 +01:00
|
|
|
current_target = promote_type(current_target, last_type);
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("POP_ebx\t# _common_recursion\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void general_recursion( FUNCTION f, char* s, char* name, FUNCTION iterate)
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
if(match(name, global_token->s))
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
common_recursion(f);
|
|
|
|
emit_out(s);
|
|
|
|
iterate();
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
int ceil_log2(int a)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if((a & (a - 1)) == 0)
|
|
|
|
{
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(a > 0)
|
|
|
|
{
|
|
|
|
result = result + 1;
|
|
|
|
a = a >> 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* postfix-expr:
|
|
|
|
* primary-expr
|
|
|
|
* postfix-expr [ expression ]
|
|
|
|
* postfix-expr ( expression-list-opt )
|
|
|
|
* postfix-expr -> member
|
|
|
|
*/
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void postfix_expr_arrow()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# looking up offset\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
global_token = global_token->next;
|
|
|
|
struct type* i;
|
|
|
|
for(i = current_target->members; NULL != i; i = i->members)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-07-20 03:33:02 +01:00
|
|
|
if(match(i->name, global_token->s)) break;
|
|
|
|
}
|
|
|
|
if(NULL == i)
|
|
|
|
{
|
|
|
|
file_print("ERROR in postfix_expr ", stderr);
|
|
|
|
file_print(current_target->name, stderr);
|
|
|
|
file_print("->", stderr);
|
|
|
|
file_print(global_token->s, stderr);
|
|
|
|
file_print(" does not exist\n", stderr);
|
|
|
|
line_error();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(0 != i->offset)
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# -> offset calculation\n");
|
|
|
|
emit_out("LOAD_IMMEDIATE_ebx %");
|
|
|
|
emit_out(numerate_number(i->offset));
|
|
|
|
emit_out("\nADD_ebx_to_eax\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
|
|
|
if(!match("=", global_token->next->s) && !match("char**",i->type->name))
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_INTEGER\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
|
|
|
current_target = i->type;
|
|
|
|
global_token = global_token->next;
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void postfix_expr_array()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
struct type* array = current_target;
|
2018-08-18 20:23:58 +01:00
|
|
|
common_recursion(expression);
|
2018-07-20 03:33:02 +01:00
|
|
|
current_target = array;
|
|
|
|
char* assign;
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-07-20 03:33:02 +01:00
|
|
|
/* Add support for Ints */
|
|
|
|
if(!match("char*", current_target->name))
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("SAL_eax_Immediate8 !");
|
|
|
|
emit_out(numerate_number(ceil_log2(current_target->indirect->size)));
|
|
|
|
emit_out("\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
assign = "LOAD_INTEGER\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assign = "LOAD_BYTE\n";
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("ADD_ebx_to_eax\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
require_match("ERROR in postfix_expr\nMissing ]\n", "]");
|
|
|
|
|
|
|
|
if(match("=", global_token->s))
|
|
|
|
{
|
|
|
|
assign = "";
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(assign);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:42:09 +00:00
|
|
|
/*
|
|
|
|
* unary-expr:
|
|
|
|
* postfix-expr
|
|
|
|
* - postfix-expr
|
|
|
|
* !postfix-expr
|
2018-01-25 01:00:22 +00:00
|
|
|
* sizeof ( type )
|
2018-01-21 19:42:09 +00:00
|
|
|
*/
|
2018-01-25 01:00:22 +00:00
|
|
|
struct type* type_name();
|
2018-08-18 20:23:58 +01:00
|
|
|
void unary_expr_sizeof()
|
2018-01-21 19:42:09 +00:00
|
|
|
{
|
2018-07-20 03:33:02 +01:00
|
|
|
global_token = global_token->next;
|
|
|
|
require_match("ERROR in unary_expr\nMissing (\n", "(");
|
|
|
|
struct type* a = type_name();
|
|
|
|
require_match("ERROR in unary_expr\nMissing )\n", ")");
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %");
|
|
|
|
emit_out(numerate_number(a->size));
|
|
|
|
emit_out("\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void postfix_expr_stub()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
|
|
|
if(match("[", global_token->s))
|
2018-01-21 19:42:09 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
postfix_expr_array();
|
|
|
|
postfix_expr_stub();
|
2018-01-21 19:42:09 +00:00
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
|
|
|
|
if(match("->", global_token->s))
|
2018-01-21 19:42:09 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
postfix_expr_arrow();
|
|
|
|
postfix_expr_stub();
|
2018-01-21 19:42:09 +00:00
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-01-21 19:42:09 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void postfix_expr()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
primary_expr();
|
|
|
|
postfix_expr_stub();
|
2018-01-21 19:42:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
/*
|
|
|
|
* additive-expr:
|
|
|
|
* postfix-expr
|
2018-01-25 01:00:22 +00:00
|
|
|
* additive-expr * postfix-expr
|
|
|
|
* additive-expr / postfix-expr
|
|
|
|
* additive-expr % postfix-expr
|
2018-01-21 03:56:50 +00:00
|
|
|
* additive-expr + postfix-expr
|
|
|
|
* additive-expr - postfix-expr
|
2018-07-20 03:33:02 +01:00
|
|
|
* additive-expr << postfix-expr
|
|
|
|
* additive-expr >> postfix-expr
|
2018-01-21 03:56:50 +00:00
|
|
|
*/
|
2018-08-18 20:23:58 +01:00
|
|
|
void additive_expr_stub()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
general_recursion(postfix_expr, "ADD_ebx_to_eax\n", "+", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n", "-", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "MULTIPLY_eax_by_ebx_into_eax\n", "*", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nDIVIDE_eax_by_ebx_into_eax\n", "/", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nMODULUS_eax_from_ebx_into_ebx\nMOVE_edx_to_eax\n", "%", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAL_eax_cl\n", "<<", additive_expr_stub);
|
|
|
|
general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAR_eax_cl\n", ">>", additive_expr_stub);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void additive_expr()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
postfix_expr();
|
|
|
|
additive_expr_stub();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 03:33:02 +01:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
/*
|
|
|
|
* relational-expr:
|
2018-07-20 03:33:02 +01:00
|
|
|
* additive_expr
|
|
|
|
* relational-expr < additive_expr
|
|
|
|
* relational-expr <= additive_expr
|
|
|
|
* relational-expr >= additive_expr
|
|
|
|
* relational-expr > additive_expr
|
2018-01-21 03:56:50 +00:00
|
|
|
*/
|
2018-07-20 03:33:02 +01:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void relational_expr_stub()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
general_recursion(additive_expr, "CMP\nSETL\nMOVEZBL\n", "<", relational_expr_stub);
|
|
|
|
general_recursion(additive_expr, "CMP\nSETLE\nMOVEZBL\n", "<=", relational_expr_stub);
|
|
|
|
general_recursion(additive_expr, "CMP\nSETGE\nMOVEZBL\n", ">=", relational_expr_stub);
|
|
|
|
general_recursion(additive_expr, "CMP\nSETG\nMOVEZBL\n", ">", relational_expr_stub);
|
|
|
|
general_recursion(additive_expr, "CMP\nSETE\nMOVEZBL\n", "==", relational_expr_stub);
|
|
|
|
general_recursion(additive_expr, "CMP\nSETNE\nMOVEZBL\n", "!=", relational_expr_stub);
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void relational_expr()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
additive_expr();
|
|
|
|
relational_expr_stub();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-20 03:33:02 +01:00
|
|
|
* bitwise-expr:
|
2018-01-21 03:56:50 +00:00
|
|
|
* relational-expr
|
2018-07-20 03:33:02 +01:00
|
|
|
* bitwise-expr & bitwise-expr
|
|
|
|
* bitwise-expr && bitwise-expr
|
|
|
|
* bitwise-expr | bitwise-expr
|
|
|
|
* bitwise-expr || bitwise-expr
|
|
|
|
* bitwise-expr ^ bitwise-expr
|
2018-01-21 03:56:50 +00:00
|
|
|
*/
|
2018-08-18 20:23:58 +01:00
|
|
|
void bitwise_expr_stub()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
general_recursion(relational_expr, "AND_eax_ebx\n", "&", bitwise_expr_stub);
|
|
|
|
general_recursion(relational_expr, "AND_eax_ebx\n", "&&", bitwise_expr_stub);
|
|
|
|
general_recursion(relational_expr, "OR_eax_ebx\n", "|", bitwise_expr_stub);
|
|
|
|
general_recursion(relational_expr, "OR_eax_ebx\n", "||", bitwise_expr_stub);
|
|
|
|
general_recursion(relational_expr, "XOR_ebx_eax_into_eax\n", "^", bitwise_expr_stub);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void bitwise_expr()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
relational_expr();
|
|
|
|
bitwise_expr_stub();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* expression:
|
|
|
|
* bitwise-or-expr
|
|
|
|
* bitwise-or-expr = expression
|
|
|
|
*/
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void primary_expr()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
if(match("sizeof", global_token->s)) unary_expr_sizeof();
|
2018-08-17 09:55:56 +01:00
|
|
|
else if('-' == global_token->s[0])
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %0\n");
|
|
|
|
common_recursion(primary_expr);
|
|
|
|
emit_out("SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n");
|
2018-08-17 09:55:56 +01:00
|
|
|
}
|
|
|
|
else if('!' == global_token->s[0])
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("LOAD_IMMEDIATE_eax %1\n");
|
|
|
|
common_recursion(postfix_expr);
|
|
|
|
emit_out("XOR_ebx_eax_into_eax\n");
|
2018-08-17 09:55:56 +01:00
|
|
|
}
|
2018-07-20 03:33:02 +01:00
|
|
|
else if(global_token->s[0] == '(')
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-07-20 03:33:02 +01:00
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-07-20 03:33:02 +01:00
|
|
|
require_match("Error in Primary expression\nDidn't get )\n", ")");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
else if(global_token->s[0] == '\'') primary_expr_char();
|
|
|
|
else if(global_token->s[0] == '"') primary_expr_string();
|
|
|
|
else if(in_set(global_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) primary_expr_variable();
|
|
|
|
else if(in_set(global_token->s[0], "0123456789")) primary_expr_number();
|
2018-07-20 03:33:02 +01:00
|
|
|
else primary_expr_failure();
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void expression()
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
bitwise_expr();
|
2018-08-16 17:01:55 +01:00
|
|
|
if(match("=", global_token->s))
|
|
|
|
{
|
|
|
|
char* store;
|
2018-08-17 09:55:56 +01:00
|
|
|
if(!match("]", global_token->prev->s) || !match("char*", current_target->name))
|
2018-08-16 17:01:55 +01:00
|
|
|
{
|
2018-08-17 09:55:56 +01:00
|
|
|
store = "STORE_INTEGER\n";
|
2018-08-16 17:01:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-17 09:55:56 +01:00
|
|
|
store = "STORE_CHAR\n";
|
2018-08-16 17:01:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
common_recursion(expression);
|
|
|
|
emit_out(store);
|
2018-08-17 09:55:56 +01:00
|
|
|
current_target = NULL;
|
2018-08-16 17:01:55 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Process local variable */
|
2018-08-18 20:23:58 +01:00
|
|
|
void collect_local()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
struct type* type_size = type_name();
|
|
|
|
struct token_list* a = sym_declare(global_token->s, type_size, function->locals);
|
2018-07-20 03:33:02 +01:00
|
|
|
if(match("main", function->s) && (NULL == function->locals))
|
|
|
|
{
|
|
|
|
a->depth = -4;
|
|
|
|
}
|
|
|
|
else if((NULL == function->arguments) && (NULL == function->locals))
|
|
|
|
{
|
|
|
|
a->depth = -8;
|
|
|
|
}
|
|
|
|
else if(NULL == function->locals)
|
|
|
|
{
|
|
|
|
a->depth = function->arguments->depth - 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a->depth = function->locals->depth - 4;
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
function->locals = a;
|
2018-08-16 17:01:55 +01:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# Defining local ");
|
|
|
|
emit_out(global_token->s);
|
|
|
|
emit_out("\n");
|
2018-08-16 17:01:55 +01:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = global_token->next;
|
|
|
|
|
2018-07-20 03:33:02 +01:00
|
|
|
if(match("=", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in collect_local\nMissing ;\n", ";");
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("PUSH_eax\t#");
|
|
|
|
emit_out(a->s);
|
|
|
|
emit_out("\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* Evaluate if statements */
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_if()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-06-08 20:43:36 +01:00
|
|
|
char* number_string = numerate_number(current_count);
|
|
|
|
current_count = current_count + 1;
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# IF_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
global_token = global_token->next;
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_if\nMISSING (\n", "(");
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("TEST\nJUMP_EQ %ELSE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_if\nMISSING )\n", ")");
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %_END_IF_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":ELSE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-02-01 04:14:40 +00:00
|
|
|
if(match("else", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(":_END_IF_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_for()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-06-08 20:43:36 +01:00
|
|
|
char* number_string = numerate_number(current_count);
|
|
|
|
current_count = current_count + 1;
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-28 21:40:51 +01:00
|
|
|
char* nested_break_head = break_target_head;
|
|
|
|
char* nested_break_func = break_target_func;
|
|
|
|
char* nested_break_num = break_target_num;
|
2018-08-10 22:30:48 +01:00
|
|
|
struct token_list* nested_locals = break_frame;
|
|
|
|
break_frame = function->locals;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = "FOR_END_";
|
2018-07-20 03:33:02 +01:00
|
|
|
break_target_func = function->s;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_num = number_string;
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# FOR_initialization_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
global_token = global_token->next;
|
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_for\nMISSING (\n", "(");
|
2018-05-03 01:56:44 +01:00
|
|
|
if(!match(";",global_token->s))
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-05-03 01:56:44 +01:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(":FOR_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_for\nMISSING ;1\n", ";");
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("TEST\nJUMP_EQ %FOR_END_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out("JUMP %FOR_THEN_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":FOR_ITER_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_for\nMISSING ;2\n", ";");
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %FOR_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":FOR_THEN_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_for\nMISSING )\n", ")");
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %FOR_ITER_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":FOR_END_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = nested_break_head;
|
|
|
|
break_target_func = nested_break_func;
|
|
|
|
break_target_num = nested_break_num;
|
2018-08-10 22:30:48 +01:00
|
|
|
break_frame = nested_locals;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process Assembly statements */
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_asm()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_asm\nMISSING (\n", "(");
|
2018-01-25 01:00:22 +00:00
|
|
|
while(34 == global_token->s[0])
|
|
|
|
{/* 34 == " */
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out((global_token->s + 1));
|
|
|
|
emit_out("\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = global_token->next;
|
|
|
|
}
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_asm\nMISSING )\n", ")");
|
|
|
|
require_match("ERROR in process_asm\nMISSING ;\n", ";");
|
2018-01-25 01:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process do while loops */
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_do()
|
2018-01-25 01:00:22 +00:00
|
|
|
{
|
2018-06-08 20:43:36 +01:00
|
|
|
char* number_string = numerate_number(current_count);
|
|
|
|
current_count = current_count + 1;
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-06-28 21:40:51 +01:00
|
|
|
char* nested_break_head = break_target_head;
|
|
|
|
char* nested_break_func = break_target_func;
|
|
|
|
char* nested_break_num = break_target_num;
|
2018-08-10 22:30:48 +01:00
|
|
|
struct token_list* nested_locals = break_frame;
|
|
|
|
break_frame = function->locals;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = "DO_END_";
|
2018-07-20 03:33:02 +01:00
|
|
|
break_target_func = function->s;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_num = number_string;
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(":DO_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-25 01:00:22 +00:00
|
|
|
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_do\nMISSING while\n", "while");
|
|
|
|
require_match("ERROR in process_do\nMISSING (\n", "(");
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_do\nMISSING )\n", ")");
|
|
|
|
require_match("ERROR in process_do\nMISSING ;\n", ";");
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("TEST\nJUMP_NE %DO_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":DO_END_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-10 22:30:48 +01:00
|
|
|
break_frame = nested_locals;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = nested_break_head;
|
|
|
|
break_target_func = nested_break_func;
|
|
|
|
break_target_num = nested_break_num;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:00:22 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
/* Process while loops */
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_while()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-06-08 20:43:36 +01:00
|
|
|
char* number_string = numerate_number(current_count);
|
|
|
|
current_count = current_count + 1;
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-28 21:40:51 +01:00
|
|
|
char* nested_break_head = break_target_head;
|
|
|
|
char* nested_break_func = break_target_func;
|
|
|
|
char* nested_break_num = break_target_num;
|
2018-08-10 22:30:48 +01:00
|
|
|
struct token_list* nested_locals = break_frame;
|
|
|
|
break_frame = function->locals;
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = "END_WHILE_";
|
2018-07-20 03:33:02 +01:00
|
|
|
break_target_func = function->s;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_num = number_string;
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(":WHILE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
global_token = global_token->next;
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_while\nMISSING (\n", "(");
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("TEST\nJUMP_EQ %END_WHILE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out("# THEN_while_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in process_while\nMISSING )\n", ")");
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %WHILE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
|
|
|
emit_out(":END_WHILE_");
|
|
|
|
uniqueID_out(function->s, number_string);
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-10 22:30:48 +01:00
|
|
|
break_frame = nested_locals;
|
2018-06-28 21:40:51 +01:00
|
|
|
break_target_head = nested_break_head;
|
|
|
|
break_target_func = nested_break_func;
|
|
|
|
break_target_num = nested_break_num;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that functions return */
|
2018-08-18 20:23:58 +01:00
|
|
|
void return_result()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
if(global_token->s[0] != ';') expression();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in return_result\nMISSING ;\n", ";");
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-01-25 01:00:22 +00:00
|
|
|
struct token_list* i;
|
|
|
|
for(i = function->locals; NULL != i; i = i->next)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("POP_ebx\t# _return_result_locals\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("RETURN\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void process_break()
|
2018-08-16 17:01:55 +01:00
|
|
|
{
|
|
|
|
if(NULL == break_target_head)
|
|
|
|
{
|
|
|
|
file_print("Not inside of a loop or case statement", stderr);
|
|
|
|
line_error();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
struct token_list* i = function->locals;
|
|
|
|
while(i != break_frame)
|
|
|
|
{
|
|
|
|
if(NULL == i) break;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("POP_ebx\t# break_cleanup_locals\n");
|
2018-08-16 17:01:55 +01:00
|
|
|
i = i->next;
|
|
|
|
}
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %");
|
|
|
|
emit_out(break_target_head);
|
|
|
|
emit_out(break_target_func);
|
|
|
|
emit_out("_");
|
|
|
|
emit_out(break_target_num);
|
|
|
|
emit_out("\n");
|
2018-08-16 17:01:55 +01:00
|
|
|
require_match("ERROR in statement\nMissing ;\n", ";");
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void recursive_statement()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
|
|
|
struct token_list* frame = function->locals;
|
|
|
|
|
2018-02-01 04:14:40 +00:00
|
|
|
while(!match("}", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
global_token = global_token->next;
|
|
|
|
|
|
|
|
/* Clean up any locals added */
|
2018-08-16 17:01:55 +01:00
|
|
|
if(!match("RETURN\n", out->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-16 17:01:55 +01:00
|
|
|
struct token_list* i;
|
|
|
|
for(i = function->locals; frame != i; i = i->next)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out( "POP_ebx\t# _recursive_statement_locals\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 17:01:55 +01:00
|
|
|
function->locals = frame;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* statement:
|
|
|
|
* { statement-list-opt }
|
|
|
|
* type-name identifier ;
|
|
|
|
* type-name identifier = expression;
|
|
|
|
* if ( expression ) statement
|
|
|
|
* if ( expression ) statement else statement
|
2018-01-25 01:00:22 +00:00
|
|
|
* do statement while ( expression ) ;
|
2018-01-21 03:56:50 +00:00
|
|
|
* while ( expression ) statement
|
|
|
|
* for ( expression ; expression ; expression ) statement
|
|
|
|
* asm ( "assembly" ... "assembly" ) ;
|
2018-01-25 01:00:22 +00:00
|
|
|
* goto label ;
|
|
|
|
* label:
|
2018-01-21 03:56:50 +00:00
|
|
|
* return ;
|
2018-02-01 04:14:40 +00:00
|
|
|
* break ;
|
2018-01-21 03:56:50 +00:00
|
|
|
* expr ;
|
|
|
|
*/
|
2018-02-01 04:14:40 +00:00
|
|
|
|
2018-08-16 17:01:55 +01:00
|
|
|
struct type* lookup_type(char* s, struct type* start);
|
2018-08-18 20:23:58 +01:00
|
|
|
void statement()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
if(global_token->s[0] == '{')
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
recursive_statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-01-25 01:00:22 +00:00
|
|
|
else if(':' == global_token->s[0])
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out(global_token->s);
|
|
|
|
emit_out("\t#C goto label\n");
|
2018-01-25 01:00:22 +00:00
|
|
|
global_token = global_token->next;
|
|
|
|
}
|
2018-08-16 17:01:55 +01:00
|
|
|
else if((NULL != lookup_type(global_token->s, prim_types)) ||
|
2018-07-20 03:33:02 +01:00
|
|
|
match("struct", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
collect_local();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("if", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_if();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("do", global_token->s))
|
2018-01-25 01:00:22 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_do();
|
2018-01-25 01:00:22 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("while", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_while();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("for", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_for();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("asm", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_asm();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("goto", global_token->s))
|
2018-01-25 01:00:22 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("JUMP %");
|
|
|
|
emit_out(global_token->s);
|
|
|
|
emit_out("\n");
|
2018-01-25 01:00:22 +00:00
|
|
|
global_token = global_token->next;
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in statement\nMissing ;\n", ";");
|
2018-01-25 01:00:22 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("return", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
return_result();
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-02-01 04:14:40 +00:00
|
|
|
else if(match("break", global_token->s))
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
process_break();
|
2018-06-02 01:05:27 +01:00
|
|
|
}
|
|
|
|
else if(match("continue", global_token->s))
|
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("\n#continue statement\n");
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in statement\nMissing ;\n", ";");
|
2018-02-01 04:14:40 +00:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
else
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
expression();
|
2018-06-02 01:05:27 +01:00
|
|
|
require_match("ERROR in statement\nMISSING ;\n", ";");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collect function arguments */
|
2018-08-18 20:23:58 +01:00
|
|
|
void collect_arguments()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
global_token = global_token->next;
|
|
|
|
|
2018-02-01 04:14:40 +00:00
|
|
|
while(!match(")", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
struct type* type_size = type_name();
|
|
|
|
if(global_token->s[0] == ')')
|
|
|
|
{
|
2018-08-16 17:01:55 +01:00
|
|
|
/* foo(int,char,void) doesn't need anything done */
|
|
|
|
continue;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
else if(global_token->s[0] != ',')
|
|
|
|
{
|
|
|
|
/* deal with foo(int a, char b) */
|
|
|
|
struct token_list* a = sym_declare(global_token->s, type_size, function->arguments);
|
2018-07-20 03:33:02 +01:00
|
|
|
if(match("main", function->s))
|
|
|
|
{
|
|
|
|
if(match("argc", a->s)) a->depth = 4;
|
|
|
|
if(match("argv", a->s)) a->depth = 8;
|
|
|
|
}
|
|
|
|
else if(NULL == function->arguments)
|
|
|
|
{
|
|
|
|
a->depth = -4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a->depth = function->arguments->depth - 4;
|
|
|
|
}
|
|
|
|
|
2018-08-16 17:01:55 +01:00
|
|
|
global_token = global_token->next;
|
2018-01-21 03:56:50 +00:00
|
|
|
function->arguments = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore trailing comma (needed for foo(bar(), 1); expressions*/
|
|
|
|
if(global_token->s[0] == ',') global_token = global_token->next;
|
|
|
|
}
|
|
|
|
global_token = global_token->next;
|
|
|
|
}
|
|
|
|
|
2018-08-18 20:23:58 +01:00
|
|
|
void declare_function()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-06-08 20:43:36 +01:00
|
|
|
current_count = 0;
|
2018-08-18 20:23:58 +01:00
|
|
|
function = sym_declare(global_token->prev->s, NULL, global_function_list);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* allow previously defined functions to be looked up */
|
2018-08-18 20:23:58 +01:00
|
|
|
global_function_list = function;
|
|
|
|
collect_arguments();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* If just a prototype don't waste time */
|
|
|
|
if(global_token->s[0] == ';') global_token = global_token->next;
|
|
|
|
else
|
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("# Defining function ");
|
|
|
|
emit_out(function->s);
|
|
|
|
emit_out("\n");
|
|
|
|
emit_out(":FUNCTION_");
|
|
|
|
emit_out(function->s);
|
|
|
|
emit_out("\n");
|
|
|
|
if(match("main", function->s))
|
2018-07-20 03:33:02 +01:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("COPY_esp_to_ebp\t# Deal with special case\n");
|
2018-07-20 03:33:02 +01:00
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
statement();
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* Prevent duplicate RETURNS */
|
2018-06-02 01:05:27 +01:00
|
|
|
if(!match("RETURN\n", out->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
emit_out("RETURN\n");
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* program:
|
|
|
|
* declaration
|
|
|
|
* declaration program
|
|
|
|
*
|
|
|
|
* declaration:
|
|
|
|
* CONSTANT identifer value
|
|
|
|
* type-name identifier ;
|
|
|
|
* type-name identifier ( parameter-list ) ;
|
|
|
|
* type-name identifier ( parameter-list ) statement
|
|
|
|
*
|
|
|
|
* parameter-list:
|
|
|
|
* parameter-declaration
|
|
|
|
* parameter-list, parameter-declaration
|
|
|
|
*
|
|
|
|
* parameter-declaration:
|
|
|
|
* type-name identifier-opt
|
|
|
|
*/
|
2018-08-18 20:23:58 +01:00
|
|
|
struct token_list* program()
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-18 20:23:58 +01:00
|
|
|
out = NULL;
|
|
|
|
function = NULL;
|
2018-02-24 02:18:23 +00:00
|
|
|
struct type* type_size;
|
2018-08-12 18:11:02 +01:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
new_type:
|
2018-08-18 20:23:58 +01:00
|
|
|
if (NULL == global_token) return out;
|
2018-08-12 18:11:02 +01:00
|
|
|
if(match("CONSTANT", global_token->s))
|
|
|
|
{
|
|
|
|
global_constant_list = sym_declare(global_token->next->s, NULL, global_constant_list);
|
|
|
|
global_constant_list->arguments = global_token->next->next;
|
|
|
|
global_token = global_token->next->next->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type_size = type_name();
|
|
|
|
if(NULL == type_size)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
goto new_type;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
/* Add to global symbol table */
|
|
|
|
global_symbol_list = sym_declare(global_token->s, type_size, global_symbol_list);
|
|
|
|
global_token = global_token->next;
|
|
|
|
if(match(";", global_token->s))
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
2018-08-12 18:11:02 +01:00
|
|
|
/* Ensure 4 bytes are allocated for the global */
|
|
|
|
globals_list = emit(":GLOBAL_", globals_list);
|
|
|
|
globals_list = emit(global_token->prev->s, globals_list);
|
|
|
|
globals_list = emit("\nNOP\n", globals_list);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
2018-08-12 18:11:02 +01:00
|
|
|
global_token = global_token->next;
|
|
|
|
}
|
2018-08-18 20:23:58 +01:00
|
|
|
else if(match("(", global_token->s)) declare_function();
|
2018-08-12 18:11:02 +01:00
|
|
|
else if(match("=",global_token->s))
|
|
|
|
{
|
|
|
|
/* Store the global's value*/
|
|
|
|
globals_list = emit(":GLOBAL_", globals_list);
|
|
|
|
globals_list = emit(global_token->prev->s, globals_list);
|
|
|
|
globals_list = emit("\n", globals_list);
|
|
|
|
global_token = global_token->next;
|
|
|
|
if(in_set(global_token->s[0], "0123456789"))
|
|
|
|
{ /* Assume Int */
|
|
|
|
globals_list = emit("%", globals_list);
|
|
|
|
globals_list = emit(global_token->s, globals_list);
|
2018-06-28 21:40:51 +01:00
|
|
|
globals_list = emit("\n", globals_list);
|
2018-06-26 22:03:17 +01:00
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
else if(('"' == global_token->s[0]))
|
|
|
|
{ /* Assume a string*/
|
|
|
|
globals_list = emit(parse_string(global_token->s), globals_list);
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("Recieved ", stderr);
|
|
|
|
file_print(global_token->s, stderr);
|
2018-06-02 01:05:27 +01:00
|
|
|
file_print(" in program\n", stderr);
|
2018-05-26 21:33:00 +01:00
|
|
|
line_error();
|
2018-01-21 03:56:50 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
|
|
|
|
global_token = global_token->next;
|
|
|
|
require_match("ERROR in Program\nMissing ;\n", ";");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_print("Recieved ", stderr);
|
|
|
|
file_print(global_token->s, stderr);
|
|
|
|
file_print(" in program\n", stderr);
|
|
|
|
line_error();
|
|
|
|
exit(EXIT_FAILURE);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 18:11:02 +01:00
|
|
|
goto new_type;
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:00:22 +00:00
|
|
|
void recursive_output(struct token_list* i, FILE* out)
|
2018-01-21 03:56:50 +00:00
|
|
|
{
|
|
|
|
if(NULL == i) return;
|
2018-01-25 01:00:22 +00:00
|
|
|
recursive_output(i->next, out);
|
|
|
|
file_print(i->s, out);
|
2018-01-21 03:56:50 +00:00
|
|
|
}
|