WONKY



LOG | FILES | OVERVIEW


F diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt
set(TEST_DIR ${CMAKE_SOURCE_DIR}/tests)
set(WOBLER_DUMP_LOG ${CMAKE_BINARY_DIR}/log.wobler)
- message("BUILDING ${CMAKE_BUILD_TYPE}")
include_directories(src/)
set(SOURCES
- src/backend/text/print/print.c
- src/backend/text/lines.c
src/backend/asm/intel/intel_asm.c
+ src/backend/asm/intel/intel_compile.c
src/backend/asm/intel/intel_instruction.c
src/backend/asm/intel/intel_location.c
- src/backend/asm/intel/intel_compile.c
src/backend/compile.c
+ src/backend/text/lines.c
+ src/backend/text/print/print.c
+ src/debug/debug_ast.c
src/debug/debug_ast.c
src/debug/debug_denoted.c
src/debug/debug_initialiser.c
src/debug/debug_scope.c
src/debug/debug_type.c
src/debug/debug_value.c
- src/debug/debug_ast.c
+ src/environment/command_arguments/gcc_arguments.c
+ src/environment/error/gcc_error.c
src/frontend/lex/automatas/chonky.c
src/frontend/lex/automatas/chonky_jr.c
src/frontend/lex/lexer.c
src/misc/map.c
src/misc/queue.c
src/misc/stack.c
- src/environment/command_arguments/gcc_arguments.c
- src/environment/error/gcc_error.c
- src/semantics/program/program.c
+ src/misc/wonky_malloc.c
src/semantics/ast.c
src/semantics/constraints/expression_constraints.c
src/semantics/constraints/initialiser_constraints.c
src/semantics/identifiers/scope.c
src/semantics/memory/location.c
src/semantics/memory/object.c
+ src/semantics/program/program.c
src/semantics/value/constant.c
src/semantics/value/evaluation.c
src/semantics/value/initialiser.c
file(COPY ${CMAKE_SOURCE_DIR}/tools/wsh DESTINATION ${CMAKE_BINARY_DIR})
+ message("BUILD TYPE: ${CMAKE_BUILD_TYPE}")
+
+ message("make test runs tests and copies test .c files to build directory")
+ message("./wsh does the compile ritual for a linux machine. Tested only on debian 11")
F diff --git a/doc/todo.txt b/doc/todo.txt --- a/doc/todo.txt +++ b/doc/todo.txt
Finish implementing initialiser parsing
Make multiple definitions check
Make variadic functions and macros
+ Implement declarations in for cycle - for(int i=0;i<10;++i)
F diff --git a/src/backend/asm/intel/intel_compile.c b/src/backend/asm/intel/intel_compile.c --- a/src/backend/asm/intel/intel_compile.c +++ b/src/backend/asm/intel/intel_compile.c
void compile_translation_unit_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Translation_Unit *unit)
{
struct Queue_Node *it;
- Map_Map_Extended(&((struct Normal_Scope*)unit->file_scope)->ordinary,(void (*)(void*,void*))intel_asm_anotate_denoted,compile_data);
+ Map_Map_Extended(((struct Normal_Scope*)unit->file_scope)->ordinary,(void (*)(void*,void*))intel_asm_anotate_denoted,compile_data);
for(it=unit->function_definitions->first;it!=NULL;it=it->prev)
compile_ast_to_intel_asm(compile_data,it->data);
}
F diff --git a/src/backend/text/print/print.c b/src/backend/text/print/print.c --- a/src/backend/text/print/print.c +++ b/src/backend/text/print/print.c
{
struct Source_File *base_file;
+ struct Memory *memory;
struct Translation_Data *hold_translation_data;
char *this_directory[]={"./",NULL};
char ret;
}
ret=0;
- hold_translation_data=get_translation_data(NULL,get_linkage(),get_linkage());
+ memory=get_memory();
+
+ hold_translation_data=get_translation_data(NULL,get_linkage(),get_linkage(),memory);
do
{
base_file=get_source_file(*base_source_names,this_directory);
F diff --git a/src/environment/error/gcc_error.h b/src/environment/error/gcc_error.h --- a/src/environment/error/gcc_error.h +++ b/src/environment/error/gcc_error.h
#include <gcc_string.h>
#include <common.h>
#include <compile.h>
-
+ #include <type.h>
struct Translation_Message
{
F diff --git a/src/frontend/lex/lexer.h b/src/frontend/lex/lexer.h --- a/src/frontend/lex/lexer.h +++ b/src/frontend/lex/lexer.h
#ifndef LEXER_H
#define LEXER_H LEXER_H
#include <lexer.hh>
+
#include <stdio.h>
#include <chonky.h>
#include <chonky_jr.h>
F diff --git a/src/frontend/lex/preprocessing.c b/src/frontend/lex/preprocessing.c --- a/src/frontend/lex/preprocessing.c +++ b/src/frontend/lex/preprocessing.c
hold_index=malloc(sizeof(int));
*hold_index=number_of_arguments;
++number_of_arguments;
- Map_Push(new_macro->arguments,hold_token->data,hold_token->data_size,hold_index);
+ Map_Push(new_macro->arguments,hold_token->data,hold_token->data_size,hold_index,translation_data->memory);
free(hold_token);
hold_token=get_next_token(src,&chonky[0],0);
if(hold_token->type!=KW_COMMA)
translation_data->tokens=hold_tokens;
/*push the directive into the macro map*/
- Map_Push(translation_data->macros,macro_name->data,macro_name->data_size,new_macro);
+ Map_Push(translation_data->macros,macro_name->data,macro_name->data_size,new_macro,translation_data->memory);
//free(macro_name);
chase_new_line(src,translation_data);
F diff --git a/src/frontend/parse/parse_declaration.c b/src/frontend/parse/parse_declaration.c --- a/src/frontend/parse/parse_declaration.c +++ b/src/frontend/parse/parse_declaration.c
exit:
if(ret->specifier==TS_ENUM)
{
- ret->type=(struct Type*)get_enum_type(ret);
+ ret->type=(struct Type*)get_enum_type(ret,translation_data);
}else if(ret->specifier==TS_STRUCT || ret->specifier==TS_UNION)
{
- ret->type=(struct Type*)get_struct_union_type(ret);
+ ret->type=(struct Type*)get_struct_union_type(ret,translation_data);
}else if(ret->type==NULL)
{
- ret->type=(struct Type*)get_basic_type(ret);
+ ret->type=(struct Type*)get_basic_type(ret,translation_data);
}
return ret;
}
break;
}
}
- base->type=(struct Type*)get_pointer_type(base->type,is_const,is_volatile);
+ base->type=(struct Type*)get_pointer_type(base->type,is_const,is_volatile,translation_data);
}
parse_direct_declarator(translation_data,scope,base);
{
/*TODO error*/
push_translation_error("']' expected",translation_data);
- base->type=(struct Type*)get_type_error(base->type);
+ base->type=(struct Type*)get_type_error(base->type,translation_data);
delete_ast(hold_expression);
return;
}
F diff --git a/src/misc/map.c b/src/misc/map.c --- a/src/misc/map.c +++ b/src/misc/map.c
/*
* tree must not be null
* */
- void Map_Push(Map *tree,void *str,size_t size,void *id)
+ void Map_Push(Map *tree,void *str,size_t size,void *id,struct Memory *mem)
{
size_t temp;
Map_Scour(tree,str,size,&temp,&tree);
}
for(temp;temp<size;++temp)
{
- tree=tree->delta[((unsigned char*)str)[temp]]=calloc(1,sizeof(Map));
+ tree=tree->delta[((unsigned char*)str)[temp]]=wonky_malloc_map_node(mem);
/*
Map_Init(
tree=tree->delta[((unsigned char*)str)[temp]]=malloc(sizeof(Map))
}
}
- struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id)
+ struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id,struct Memory *mem)
{
size_t temp;
Map_Scour(tree,str,size,&temp,&tree);
return tree;
}
- void* Map_Check_And_Push(struct Map *tree,void *str,size_t size,void *id)
+ void* Map_Check_And_Push(struct Map *tree,void *str,size_t size,void *id,struct Memory *mem)
{
size_t temp;
Map_Scour(tree,str,size,&temp,&tree);
F diff --git a/src/misc/map.h b/src/misc/map.h --- a/src/misc/map.h +++ b/src/misc/map.h
#ifndef GMAP_H
#define GMAP_H GMAP_H
+ #include <map.hh>
+
+
#include <stdlib.h>
#include <stdio.h>
#include <stack.h>
#include <queue.h>
#include <common.h>
+ #include <wonky_malloc.h>
- typedef struct Map Map;
void Map_Init(Map *tree);
void Map_Scour(Map *tree,void *str,size_t size,size_t *where,Map **final_node);
- void Map_Push(Map *tree,void *str,size_t size,void *id);
+ void Map_Push(Map *tree,void *str,size_t size,void *id,struct Memory *mem);
void* Map_Check(Map *tree, void *str,size_t size);
struct Map* Map_Check_And_Get(Map *tree, void *str,size_t size);
void Map_Remove(Map *tree, void *str,size_t size);
void Map_Delete_Map(struct Map *tree);
struct Condensed_Map* Map_Condense(Map* tree);
- struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id);
+ struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id,struct Memory *mem);
/*returns NULL if id is not taken , returns pointer to taken id otherwise*/
- void* Map_Check_And_Push(struct Map *tree,void *str,size_t size,void *id);
+ void* Map_Check_And_Push(struct Map *tree,void *str,size_t size,void *id,struct Memory *mem);
#endif
F diff --git a/src/misc/map.hh b/src/misc/map.hh new file mode 100644 --- /dev/null +++ b/src/misc/map.hh
+ #ifndef GMAP_HH
+ #define GMAP_HH GMAP_HH
+
+ struct Map;
+ typedef struct Map Map;
+
+ #endif
F diff --git a/src/misc/queue.h b/src/misc/queue.h --- a/src/misc/queue.h +++ b/src/misc/queue.h
#ifndef GQUEUE_H
#define GQUEUE_H GQUEUE_H
- #include<stdlib.h>//malloc free
- #include<wonky_assert.h>
+ #include <queue.hh>
+
+ #include <stdlib.h>
+ #include <wonky_assert.h>
#include <common.h>
- typedef struct Queue Queue;
struct Queue
{
F diff --git a/src/misc/queue.hh b/src/misc/queue.hh new file mode 100644 --- /dev/null +++ b/src/misc/queue.hh
+ #ifndef GQUEUE_HH
+ #define GQUEUE_HH GQUEUE_HH
+
+ struct Queue;
+ typedef struct Queue Queue;
+
+ #endif
F diff --git a/src/misc/stack.h b/src/misc/stack.h --- a/src/misc/stack.h +++ b/src/misc/stack.h
#ifndef GSTACK_H
#define GSTACK_H GSTACK_H
- #include<stdlib.h>
+ #include <stack.hh>
+
+ #include <stdlib.h>
#include <common.h>
- typedef struct Stack Stack;
struct Stack_Node
{
F diff --git a/src/misc/stack.hh b/src/misc/stack.hh new file mode 100644 --- /dev/null +++ b/src/misc/stack.hh
+ #ifndef GSTACK_HH
+ #define GSTACK_HH GSTACK_HH
+
+
+ struct Stack_Node;
+ struct Stack;
+
+ typedef struct Stack Stack;
+
+ #endif
F diff --git a/src/misc/wonky_malloc.c b/src/misc/wonky_malloc.c new file mode 100644 --- /dev/null +++ b/src/misc/wonky_malloc.c
+ #ifndef WONKY_MALLOC_C
+ #define WONKY_MALLOC_C WONKY_MALLOC_C
+ #include <wonky_malloc.h>
+
+ struct Memory* get_memory()
+ {
+ struct Memory *ret;
+ ret=malloc(sizeof(struct Memory));
+ if(ret==NULL)
+ return NULL;
+
+ ret->map_nodes=calloc_memory_segment(NULL,NUMBER_OF_MAP_NODES_IN_INCREMENTATION_OF_MAP_MEMORY_SEGMENT,sizeof(struct Map));
+ if(ret->map_nodes==NULL)
+ {
+ free(ret);
+ return NULL;
+ }
+
+
+ ret->tokens=get_memory_segment(NULL);
+ if(ret->tokens==NULL)
+ {
+ delete_memory_segment(ret->map_nodes);
+ free(ret);
+ return NULL;
+ }
+
+ ret->generic=calloc_memory_segment(NULL,NUMBER_OF_TOKENS_IN_INCREMENTATION_OF_TOKEN_MEMORY_SEGMENT,sizeof(struct token));
+ if(ret->generic==NULL)
+ {
+ delete_memory_segment(ret->map_nodes);
+ delete_memory_segment(ret->tokens);
+ free(ret);
+ return NULL;
+ }
+
+ return ret;
+ }
+ void delete_memory(struct Memory *mem)
+ {
+ struct Memory_Segment *current_segment;
+ struct Memory_Segment *hold_previous_segment;
+
+ current_segment=mem->map_nodes;
+
+ while(current_segment!=NULL)
+ {
+ hold_previous_segment=current_segment->previous;
+ delete_memory_segment(current_segment);
+ current_segment=hold_previous_segment;
+ }
+ free(mem);
+ }
+ void delete_memory_segment(struct Memory_Segment *segment)
+ {
+ free(segment->data);
+ free(segment);
+ }
+ void* wonky_malloc(size_t size,struct Memory *mem)
+ {
+ if(mem->generic->capacity - mem->generic->size < size)
+ {
+ if(mem->generic->capacity/REALLOC_TRIGGER>=size)
+ mem->generic=resize_memory_segment(mem->generic->size+size,mem->generic);
+ else
+ mem->generic=get_memory_segment(mem->generic);
+ }
+ mem->generic->size+=size;
+ return mem->generic->data + mem->generic->size - size;
+ }
+ struct Map* wonky_malloc_map_node(struct Memory *mem)
+ {
+ if(mem->map_nodes->size==mem->map_nodes->capacity)
+ mem->map_nodes=calloc_memory_segment(mem->map_nodes,NUMBER_OF_MAP_NODES_IN_INCREMENTATION_OF_MAP_MEMORY_SEGMENT,sizeof(struct Map));
+
+ ++mem->map_nodes->size;
+ return (struct Map*) (mem->map_nodes->data + mem->map_nodes->size*sizeof(struct Map) - sizeof(struct Map));
+ }
+ struct token* wonky_malloc_token(struct Memory *mem)
+ {
+ if(mem->tokens->size==mem->tokens->capacity)
+ mem->tokens=calloc_memory_segment(mem->tokens,NUMBER_OF_TOKENS_IN_INCREMENTATION_OF_TOKEN_MEMORY_SEGMENT,sizeof(struct token));
+
+ ++mem->tokens->size;
+ return (struct token*) (mem->tokens->data + mem->tokens->size*sizeof(struct token) - sizeof(struct token));
+ }
+
+
+ struct Memory_Segment* get_memory_segment(struct Memory_Segment *previous)
+ {
+ struct Memory_Segment *ret;
+ if(previous==NULL)
+ {
+ ret=malloc(sizeof(struct Memory_Segment));
+ ret->data=malloc(NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT);
+
+ ret->capacity=NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT;
+ ret->size=0;
+ ret->previous=NULL;
+ }else
+ {
+ ret=malloc(sizeof(struct Memory_Segment));
+ ret->data=malloc(NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT+previous->capacity);
+
+ ret->capacity=NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT+previous->capacity;
+ ret->size=0;
+ ret->previous=previous;
+ }
+
+ return ret;
+ }
+ struct Memory_Segment* resize_memory_segment(size_t new_capacity,struct Memory_Segment *to_be_resized)
+ {
+ struct Memory_Segment *ret;
+
+ wonky_assert(to_be_resized!=NULL && to_be_resized->capacity<=new_capacity);
+
+ ret=realloc(to_be_resized->data,new_capacity);
+ ret->capacity=new_capacity;
+
+ return ret;
+ }
+ struct Memory_Segment* calloc_memory_segment(struct Memory_Segment *previous,size_t increment_size,size_t element_size)
+ {
+ struct Memory_Segment *ret;
+ if(previous==NULL)
+ {
+ ret=malloc(sizeof(struct Memory_Segment));
+ ret->data=calloc(element_size,increment_size);
+
+ ret->capacity=increment_size;
+ ret->size=0;
+ ret->previous=NULL;
+ }else
+ {
+ ret=malloc(sizeof(struct Memory_Segment));
+ ret->data=calloc(element_size,increment_size+previous->capacity);
+
+ ret->capacity=increment_size+previous->capacity;
+ ret->size=0;
+ ret->previous=previous;
+ }
+
+ return ret;
+ }
+ #endif
F diff --git a/src/misc/wonky_malloc.h b/src/misc/wonky_malloc.h new file mode 100644 --- /dev/null +++ b/src/misc/wonky_malloc.h
+ #ifndef WONKY_MALLOC_H
+ #define WONKY_MALLOC_H WONKY_MALLOC_H
+ #include <wonky_malloc.hh>
+
+ #include <program.h>
+ #include <map.h>
+ #include <lexer.h>
+
+ #include <stdlib.h>
+
+ #define NUMBER_OF_MAP_NODES_IN_INCREMENTATION_OF_MAP_MEMORY_SEGMENT 1024
+ #define NUMBER_OF_TOKENS_IN_INCREMENTATION_OF_TOKEN_MEMORY_SEGMENT 1024
+ #define NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT 1024
+
+ /*
+ if the memory requested in a wonky_malloc can't fit in the current segment
+ then if it overflows by 1/REALLOC_TRIGGER of the capacity of the segment,
+ then the segment is realloced
+ */
+ #define REALLOC_TRIGGER 2
+
+ struct Memory_Segment
+ {
+ size_t capacity;
+ size_t size;
+ struct Memory_Segment *previous;
+ unsigned char *data;
+ };
+ struct Memory
+ {
+ struct Memory_Segment *map_nodes;
+ struct Memory_Segment *tokens;
+ struct Memory_Segment *generic;
+ };
+
+ struct Memory* get_memory();
+ void delete_memory(struct Memory *mem);
+ void delete_memory_segment(struct Memory_Segment *segment);
+
+ void* wonky_malloc(size_t size,struct Memory *mem);
+ struct Map* wonky_malloc_map_node(struct Memory *mem);
+ struct token* wonky_malloc_token(struct Memory *mem);
+
+ struct Memory_Segment* get_memory_segment(struct Memory_Segment *previous);
+ struct Memory_Segment* resize_memory_segment(size_t new_capacity,struct Memory_Segment *to_be_resized);
+ struct Memory_Segment* calloc_memory_segment(struct Memory_Segment *previous,size_t increment_size,size_t element_size);
+
+ #define wonky_free(x)
+ #define wonky_free_token(x)
+ #define wonky_free_map_node(x)
+
+ #endif
F diff --git a/src/misc/wonky_malloc.hh b/src/misc/wonky_malloc.hh new file mode 100644 --- /dev/null +++ b/src/misc/wonky_malloc.hh
+ #ifndef WONKY_MALLOC_HH
+ #define WONKY_MALLOC_HH WONKY_MALLOC_HH
+
+ struct Memory_Segment;
+ struct Memory;
+
+ #endif
F diff --git a/src/semantics/ast.c b/src/semantics/ast.c --- a/src/semantics/ast.c +++ b/src/semantics/ast.c
if(expression_type->specifier==TS_ARRAY)
{
array_type=(struct Type_Array*)expression_type;
- expression_type=get_pointer_type(array_type->is_array_of,1,0);
+ expression_type=get_pointer_type(array_type->is_array_of,1,0,translation_data);
wonky_assert(expression_type!=NULL);
ret->operand=operand;
operand_type=extract_expresion_value_type(operand->value,translation_data);
- ret->value=get_expression_value_rvalue(get_temp_object(get_pointer_type(operand_type,0,0)));
+ ret->value=get_expression_value_rvalue(get_temp_object(get_pointer_type(operand_type,0,0,translation_data)));
return ret;
}else
F diff --git a/src/semantics/constraints/linkage_constraints.c b/src/semantics/constraints/linkage_constraints.c --- a/src/semantics/constraints/linkage_constraints.c +++ b/src/semantics/constraints/linkage_constraints.c
{
struct AST_Object_Declaration *hold_object;
linkage=(denoted_object->linkage==LINKAGE_EXTERNAL?translation_data->external_linkage:translation_data->internal_linkage);
- hold_object=check_and_push_id(denoted_object,linkage->ids,denoted_object->id);
+ hold_object=check_and_push_id(denoted_object,linkage->ids,denoted_object->id,translation_data);
if(hold_object!=NULL)
}else if(denoted_function->linkage==LINKAGE_EXTERNAL || denoted_function->linkage==LINKAGE_INTERNAL)
{
linkage=(denoted_function->linkage==LINKAGE_EXTERNAL?translation_data->external_linkage:translation_data->internal_linkage);
- function_tree=check_and_push_id(denoted_function,linkage->ids,denoted_function->id);
+ function_tree=check_and_push_id(denoted_function,linkage->ids,denoted_function->id,translation_data);
if(function_tree!=NULL)
{
F diff --git a/src/semantics/identifiers/denoted.h b/src/semantics/identifiers/denoted.h --- a/src/semantics/identifiers/denoted.h +++ b/src/semantics/identifiers/denoted.h
#ifndef WONKY_DENOTED_H
#define WONKY_DENOTED_H WONKY_DENOTED_H
#include <denoted.hh>
+
+ #include <map.h>
+ #include <lexer.h>
#include <type.h>
#include <scope.h>
#include <evaluation.h>
#include <linkage.h>
#include <object.h>
#include <common.h>
+ #include <location.h>
struct Denoted
{
F diff --git a/src/semantics/identifiers/linkage.c b/src/semantics/identifiers/linkage.c --- a/src/semantics/identifiers/linkage.c +++ b/src/semantics/identifiers/linkage.c
if(possible_declaration==NULL)
{
if(object->object->linkage==LINKAGE_EXTERNAL)
- Map_Push(translation_data->external_linkage->ids,object->object->id->data,object->object->id->data_size,object);
+ Map_Push(translation_data->external_linkage->ids,object->object->id->data,object->object->id->data_size,object,translation_data->memory);
else if(object->object->linkage==LINKAGE_INTERNAL)
- Map_Push(translation_data->internal_linkage->ids,object->object->id->data,object->object->id->data_size,object);
+ Map_Push(translation_data->internal_linkage->ids,object->object->id->data,object->object->id->data_size,object,translation_data->memory);
}else
{
if(object->initializer!=NULL && possible_declaration->initializer!=NULL)
possible_declaration=Map_Check(translation_data->external_linkage->ids,function->function->id->data,function->function->id->data_size);
if(function->function->linkage==LINKAGE_EXTERNAL)
- Map_Push(translation_data->external_linkage->ids,function->function->id->data,function->function->id->data_size,function);
+ Map_Push(translation_data->external_linkage->ids,function->function->id->data,function->function->id->data_size,function,translation_data->memory);
else
- Map_Push(translation_data->internal_linkage->ids,function->function->id->data,function->function->id->data_size,function);
+ Map_Push(translation_data->internal_linkage->ids,function->function->id->data,function->function->id->data_size,function,translation_data->memory);
if(possible_declaration!=NULL)
{
if(possible_definition==NULL)
{
if(function->function->linkage==LINKAGE_EXTERNAL)
- Map_Push(translation_data->external_linkage->ids,function->function->id->data,function->function->id->data_size,function);
+ Map_Push(translation_data->external_linkage->ids,function->function->id->data,function->function->id->data_size,function,translation_data->memory);
else
- Map_Push(translation_data->internal_linkage->ids,function->function->id->data,function->function->id->data_size,function);
+ Map_Push(translation_data->internal_linkage->ids,function->function->id->data,function->function->id->data_size,function,translation_data->memory);
}
}
#endif
F diff --git a/src/semantics/identifiers/linkage.h b/src/semantics/identifiers/linkage.h --- a/src/semantics/identifiers/linkage.h +++ b/src/semantics/identifiers/linkage.h
#include <map.h>
#include <denoted.h>
#include <common.h>
+ #include <scope.h>
enum Linkage_Type;
F diff --git a/src/semantics/identifiers/scope.c b/src/semantics/identifiers/scope.c --- a/src/semantics/identifiers/scope.c +++ b/src/semantics/identifiers/scope.c
ret=malloc(sizeof(struct Normal_Scope));
ret->type=type;
+ ret->tags=malloc(sizeof(struct Map));
+ ret->ordinary=malloc(sizeof(struct Map));
wonky_assert((type!=FILE_SCOPE) || parent==NULL);
ret->parent=parent;
- Map_Init(&ret->tags);
-
-
- Map_Init(&ret->ordinary);
+ Map_Init(ret->tags);
+ Map_Init(ret->ordinary);
ret->type=FUNCTION_SCOPE;
ret->parent=parent;
ret->function=function;
+ ret->labels=malloc(sizeof(struct Map));
- Map_Init(&ret->labels);
+ Map_Init(ret->labels);
ret->label_order=malloc(sizeof(struct Queue));
Queue_Init(ret->label_order);
void delete_normal_scope(struct Normal_Scope *scope)
{
- Map_Map(&scope->tags,delete_denoted_wrapper);
- Map_Destroy(&scope->tags);
- Map_Map(&scope->ordinary,delete_denoted_with_no_linkage_wrapper);
- Map_Destroy(&scope->ordinary);
+ Map_Map(scope->tags,delete_denoted_wrapper);
+ Map_Destroy(scope->tags);
+ Map_Map(scope->ordinary,delete_denoted_with_no_linkage_wrapper);
+ Map_Destroy(scope->ordinary);
while(scope->object_order->size>0)
Queue_Pop(scope->object_order);
free(scope->object_order);
void delete_function_scope(struct Function_Scope *scope)
{
- Map_Map(&scope->labels,delete_denoted_wrapper);
- Map_Destroy(&scope->labels);
+ Map_Map(scope->labels,delete_denoted_wrapper);
+ Map_Destroy(scope->labels);
while(scope->label_order->size>0)
Queue_Pop(scope->label_order);
free(scope->label_order);
function_scope=get_enclosing_function_scope(current);
if(function_scope!=NULL)
- hold=Map_Check(&function_scope->labels,id->data,id->data_size);
+ hold=Map_Check(function_scope->labels,id->data,id->data_size);
return hold;
}
case FILE_SCOPE:
case BLOCK_SCOPE:
case FUNCTION_PROTOTYPE_SCOPE:
- hold=Map_Check(&((struct Normal_Scope*)current)->tags,id->data,id->data_size);
+ hold=Map_Check(((struct Normal_Scope*)current)->tags,id->data,id->data_size);
}
current=current->parent;
case FILE_SCOPE:
case BLOCK_SCOPE:
case FUNCTION_PROTOTYPE_SCOPE:
- hold=Map_Check(&((struct Normal_Scope*)current)->ordinary,id->data,id->data_size);
+ hold=Map_Check(((struct Normal_Scope*)current)->ordinary,id->data,id->data_size);
}
current=current->parent;
void* check_struct_union_member(struct Normal_Scope *inner,struct token *id)
{
- return Map_Check(&inner->ordinary,id->data,id->data_size);
+ return Map_Check(inner->ordinary,id->data,id->data_size);
}
void Scope_Push(struct Scope *scope,struct Denoted *declarator,struct Translation_Data *translation_data)
{
current_function_scope=get_enclosing_function_scope(current);
- hold_statement=(struct Denoted_Statement*)Map_Check(&current_function_scope->labels,label->data,label->data_size);
+ hold_statement=(struct Denoted_Statement*)Map_Check(current_function_scope->labels,label->data,label->data_size);
if(hold_statement!=NULL)
{
{
hold_statement=(struct Denoted_Statement*)get_denoted_statement(label,statement,get_last_known_denoted_object((struct Normal_Scope*)current));
- Map_Push(&current_function_scope->labels,label->data,label->data_size,hold_statement);
+ Map_Push(current_function_scope->labels,label->data,label->data_size,hold_statement,translation_data->memory);
Queue_Push(current_function_scope->label_order,hold_statement);
- hold_previous=check_and_push_id(denoted_object,&AS_NORMAL_SCOPE(current)->ordinary,denoted_object->id);
+ hold_previous=check_and_push_id(denoted_object,AS_NORMAL_SCOPE(current)->ordinary,denoted_object->id,translation_data);
if(has_new_errors(translation_data) || !constraint_check_object_linkage(denoted_object,hold_previous,current,translation_data) )
{
- hold_previous=check_and_push_id(denoted_function,&AS_NORMAL_SCOPE(current)->ordinary,denoted_function->id);
+ hold_previous=check_and_push_id(denoted_function,AS_NORMAL_SCOPE(current)->ordinary,denoted_function->id,translation_data);
if(has_new_errors(translation_data) || !constraint_check_function_linkage(denoted_function,hold_previous,current,translation_data))
{
translation_data->number_of_errors_when_last_checked--;
}
}
- #define CHECK_AND_PUSH(thing,scope) Map_Check_And_Push(scope,thing->id->data,thing->id->data_size,thing)
+ #define CHECK_AND_PUSH(thing,scope) Map_Check_And_Push(scope,thing->id->data,thing->id->data_size,thing,translation_data->memory)
void push_typedef(struct Scope *current,struct Translation_Data *translation_data,struct Denoted_Type *denoted_typedef)
{
struct Denoted *hold_denotated;
- hold_denotated=CHECK_AND_PUSH(denoted_typedef,&AS_NORMAL_SCOPE(current)->ordinary);
+ hold_denotated=CHECK_AND_PUSH(denoted_typedef,AS_NORMAL_SCOPE(current)->ordinary);
if(hold_denotated)
{
delete_denoted_typedef(denoted_typedef);
void push_denoted_enum_tag(struct Scope *current,struct Translation_Data *translation_data,struct Denoted_Enum *denoted_enum)
{
struct Denoted *hold_denotated;
- hold_denotated=Map_Check_And_Push(&AS_NORMAL_SCOPE(current)->tags,denoted_enum->enumeration->id->data,denoted_enum->enumeration->id->data_size,denoted_enum);
+ hold_denotated=Map_Check_And_Push(AS_NORMAL_SCOPE(current)->tags,denoted_enum->enumeration->id->data,denoted_enum->enumeration->id->data_size,denoted_enum,translation_data->memory);
if(hold_denotated)
{
delete_denoted_enum(denoted_enum);
void push_denoted_enum_constant(struct Scope *current,struct Translation_Data *translation_data,struct Denoted_Enum_Const *denoted_enum_constant)
{
struct Denoted *hold_denotated;
- hold_denotated=CHECK_AND_PUSH(denoted_enum_constant,&AS_NORMAL_SCOPE(current)->ordinary);
+ hold_denotated=CHECK_AND_PUSH(denoted_enum_constant,AS_NORMAL_SCOPE(current)->ordinary);
if(hold_denotated)
{
delete_denoted_enum_constant(denoted_enum_constant);
void push_denoted_struct_union_tag(struct Scope *current,struct Translation_Data *translation_data,struct Denoted_Struct_Union *denoted_struct_union)
{
struct Denoted *hold_denotated;
- hold_denotated=Map_Check_And_Push(&AS_NORMAL_SCOPE(current)->tags,denoted_struct_union->struct_union->id->data,denoted_struct_union->struct_union->id->data_size,denoted_struct_union);
+ hold_denotated=Map_Check_And_Push(AS_NORMAL_SCOPE(current)->tags,denoted_struct_union->struct_union->id->data,denoted_struct_union->struct_union->id->data_size,denoted_struct_union,translation_data->memory);
if(hold_denotated)
{
delete_denoted_struct_union(denoted_struct_union);
push_translation_error("redefinition of tag",translation_data);
}
}
- void* check_and_push_id(void *data,struct Map *scope_map,struct token *id)
+ void* check_and_push_id(void *data,struct Map *scope_map,struct token *id,struct Translation_Data *translation_data)
{
- return Map_Check_And_Push(scope_map,id->data,id->data_size,data);
+ return Map_Check_And_Push(scope_map,id->data,id->data_size,data,translation_data->memory);
}
#endif
F diff --git a/src/semantics/identifiers/scope.h b/src/semantics/identifiers/scope.h --- a/src/semantics/identifiers/scope.h +++ b/src/semantics/identifiers/scope.h
#ifndef WONKY_SCOPE_H
#define WONKY_SCOPE_H WONKY_SCOPE_H
#include <scope.hh>
+
#include <map.h>
#include <denoted.h>
#include <location.h>
enum Scope_Type type;
struct Scope *parent;
- Map tags;
- Map ordinary;
+ struct Map *tags;
+ struct Map *ordinary;
/*we need the object order for the unconditional jumps*/
struct Queue *object_order; /*queue of denoted objects*/
struct Scope *parent;
struct Denoted_Function *function;
- Map labels;
+ struct Map *labels;
/*here to ease checking of undeclared but used labels*/
struct Queue *label_order; /*queue of denoted statements*/
- void* check_and_push_id(void *data,struct Map *scope_map,struct token *id);
+ void* check_and_push_id(void *data,struct Map *scope_map,struct token *id,struct Translation_Data *translation_data);
_Bool check_if_typedefed(struct Scope* scope,struct token *id);
#endif
F diff --git a/src/semantics/program/program.c b/src/semantics/program/program.c --- a/src/semantics/program/program.c +++ b/src/semantics/program/program.c
ret->types=malloc(sizeof(struct Map));
ret->functions_without_a_definition=malloc(sizeof(struct Queue));
ret->external_objects_without_an_initialiser=malloc(sizeof(struct Queue));
+ ret->memory=get_memory();
ret->external_linkage=get_linkage();
fclose(in);
return src;
}
- struct Translation_Data* get_translation_data(struct Map *types,struct Linkage *internal_linkage,struct Linkage *external_linkage)
+ struct Translation_Data* get_translation_data(struct Map *types,struct Linkage *internal_linkage,struct Linkage *external_linkage,struct Memory *program_memory)
{
struct Translation_Data *ret;
ret=malloc(sizeof(struct Translation_Data));
ret->external_linkage=external_linkage;
ret->internal_linkage=internal_linkage;
+ ret->memory=program_memory;
return ret;
}
}
program=get_program();
- hold_translation_data=get_translation_data(program->types,get_linkage(),program->external_linkage);
+ hold_translation_data=get_translation_data(program->types,get_linkage(),program->external_linkage,program->memory);
do
{
base_file=get_source_file(*base_source_names,this_directory);
F diff --git a/src/semantics/program/program.h b/src/semantics/program/program.h --- a/src/semantics/program/program.h +++ b/src/semantics/program/program.h
#ifndef WONKY_PROGRAM_H
#define WONKY_PROGRAM_H WONKY_PROGRAM_H
-
-
#include <program.hh>
+
#include <queue.h>
+ #include <map.h>
+ #include <linkage.h>
+
#include <scope.h>
#include <lexer.h>
#include <gcc_string.h>
#include <parse_translation_unit.h>
#include <common.h>
#include <ast.h>
+ #include <wonky_malloc.h>
struct Program
/*ASTs*/
struct Queue *functions_without_a_definition;
struct Queue *external_objects_without_an_initialiser;
+
+ struct Memory *memory;
};
struct Translation_Data
{
struct Map *types;
struct Linkage *external_linkage;
struct Linkage *internal_linkage;
+ struct Memory *memory;
/*end of passed from program struct*/
+
};
struct Program* get_program();
struct Source_File* get_source_file(char *filename,char **where_to_search);
void normalise_source_name(struct Source_Name *name);
- struct Translation_Data* get_translation_data(struct Map *types,struct Linkage *internal_linkage,struct Linkage *external_linkage);
+ struct Translation_Data* get_translation_data(struct Map *types,struct Linkage *internal_linkage,struct Linkage *external_linkage,struct Memory *program_memory);
struct Program* parse_program(char **base_source_names);
F diff --git a/src/semantics/value/type.c b/src/semantics/value/type.c --- a/src/semantics/value/type.c +++ b/src/semantics/value/type.c
- struct Type* type_check_and_push(struct Type *type,struct Map *base,size_t struct_size)
+ struct Type* type_check_and_push(struct Type *type,struct Map *base,size_t struct_size,struct Translation_Data *translation_data)
{
struct Map *hold_node;
hold_node=Map_Check_And_Get(base,type,struct_size);
if(hold_node==NULL)
{
- type->node=Map_Push_And_Get(base,type,struct_size,type);
+ type->node=Map_Push_And_Get(base,type,struct_size,type,translation_data->memory);
return type;
}else
{
}
}
- struct Type* get_type_error(struct Type *type)
+ struct Type* get_type_error(struct Type *type,struct Translation_Data *translation_data)
{
struct Type_Error *ret;
ret->specifier=TS_ERROR;
ret->error=type;
if(type!=NULL)
- ret=(struct Type_Error*)type_check_and_push((struct Type*)ret,type->node,sizeof(struct Type_Error));
+ ret=(struct Type_Error*)type_check_and_push((struct Type*)ret,type->node,sizeof(struct Type_Error),translation_data);
wonky_assert(is_valid_type_error(ret));
return (struct Type*)ret;
}
- struct Type* get_struct_union_type(struct Denotation_Prototype *prototype)
+ struct Type* get_struct_union_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data)
{
struct Type_Struct_Union *ret;
ret->is_const=prototype->is_const;
ret->is_volatile=prototype->is_volatile;
- ret=(struct Type_Struct_Union*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Struct_Union));
+ ret=(struct Type_Struct_Union*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Struct_Union),translation_data);
if(prototype->constraint!=TC_NONE || prototype->sign!=TSIGN_NONE || (prototype->specifier!=TS_UNION && prototype->specifier!=TS_STRUCT))
{
- return get_type_error((struct Type*)ret);
+ return get_type_error((struct Type*)ret,translation_data);
}else
{
wonky_assert(is_valid_type_struct(ret) || is_valid_type_union(ret));
return ret;
}
- struct Type* get_basic_type(struct Denotation_Prototype *prototype)
+ struct Type* get_basic_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data)
{
struct Type_Basic *ret;
ret=calloc(1,sizeof(struct Type_Basic));
}
- ret=(struct Type_Basic*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Basic));
+ ret=(struct Type_Basic*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Basic),translation_data);
switch(ret->specifier)
{
|| prototype->constraint==TC_SHORT
|| prototype->sign!=TSIGN_NONE)
{
- return get_type_error((struct Type*)ret);
+ return get_type_error((struct Type*)ret,translation_data);
}
break;
case TS_CHAR:
if(prototype->constraint!=TC_NONE)
{
- return get_type_error((struct Type*)ret);
+ return get_type_error((struct Type*)ret,translation_data);
}
break;
case TS_INT:
default:
if(prototype->constraint!=TC_NONE || prototype->sign!=TSIGN_NONE)
{
- return get_type_error((struct Type*)ret);
+ return get_type_error((struct Type*)ret,translation_data);
}
}
return (struct Type*)ret;
}
- struct Type* get_pointer_type(struct Type *points_to,_Bool is_const,_Bool is_volatile)
+ struct Type* get_pointer_type(struct Type *points_to,_Bool is_const,_Bool is_volatile,struct Translation_Data *translation_data)
{
struct Type_Pointer *ret;
ret=calloc(1,sizeof(struct Type_Pointer));
ret->is_const=is_const;
ret->is_volatile=is_volatile;
- ret=(struct Type_Pointer*)type_check_and_push((struct Type*)ret,points_to->node,sizeof(struct Type_Pointer));
+ ret=(struct Type_Pointer*)type_check_and_push((struct Type*)ret,points_to->node,sizeof(struct Type_Pointer),translation_data);
wonky_assert(is_valid_type_pointer(ret));
return (struct Type*)ret;
ret->number_of_elements=0;
}
ret->is_array_of=array_of;
- ret=(struct Type_Array*)type_check_and_push((struct Type*)ret,array_of->node,sizeof(struct Type_Array));
+ ret=(struct Type_Array*)type_check_and_push((struct Type*)ret,array_of->node,sizeof(struct Type_Array),translation_data);
wonky_assert(is_valid_type_array(ret));
return (struct Type*)ret;
ret->specifier=TS_ARRAY;
ret->size=size;
ret->is_array_of=array_of;
- ret=(struct Type_Array*)type_check_and_push((struct Type*)ret,array_of->node,sizeof(struct Type_Array));
+ ret=(struct Type_Array*)type_check_and_push((struct Type*)ret,array_of->node,sizeof(struct Type_Array),translation_data);
wonky_assert(is_valid_type_array(ret));
return (struct Type*)ret;
}
- struct Type* get_enum_type(struct Denotation_Prototype *prototype)
+ struct Type* get_enum_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data)
{
struct Type_Enum *ret;
ret->is_const=prototype->is_const;
ret->is_volatile=prototype->is_volatile;
- ret=(struct Type_Enum*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Enum));
+ ret=(struct Type_Enum*)type_check_and_push((struct Type*)ret,prototype->node,sizeof(struct Type_Enum),translation_data);
if(prototype->sign!=TSIGN_NONE || prototype->constraint!=TC_NONE)
{
- return get_type_error((struct Type*)ret);
+ return get_type_error((struct Type*)ret,translation_data);
}
wonky_assert(is_valid_type_enum(ret));
ret->arguments[i]=(struct Denoted_Object*)Queue_Pop(parameters);
}
free(parameters);
- ret=(struct Type_Function*)type_check_and_push((struct Type*)ret,return_type->node,sizeof(struct Type_Function));
+ ret=(struct Type_Function*)type_check_and_push((struct Type*)ret,return_type->node,sizeof(struct Type_Function),translation_data);
if(is_variadic && ret->number_of_arguments==0)
{
push_translation_error("variadic function needs atleast one named argument",translation_data);
- return (struct Type*)get_type_error((struct Type*)ret);
+ return (struct Type*)get_type_error((struct Type*)ret,translation_data);
}
wonky_assert(is_valid_type_function(ret));
ret->constraint=constraint;
ret->sign=sign;
- ret=(struct Type_Basic*)type_check_and_push((struct Type*)ret,translation_data->types,sizeof(struct Type_Basic));
+ ret=(struct Type_Basic*)type_check_and_push((struct Type*)ret,translation_data->types,sizeof(struct Type_Basic),translation_data);
wonky_assert(is_valid_type_basic(ret));
return ret;
case TS_DOUBLE:
prototype->specifier=type->specifier;
- hold_unqualified_type=get_basic_type(prototype);
+ hold_unqualified_type=get_basic_type(prototype,translation_data);
break;
case TS_STRUCT:
case TS_UNION:
prototype->specifier=type->specifier;
prototype->struct_union=((struct Type_Struct_Union*)type)->struct_union;
- hold_unqualified_type=get_struct_union_type(prototype);
+ hold_unqualified_type=get_struct_union_type(prototype,translation_data);
break;
case TS_ENUM:
prototype->specifier=TS_ENUM;
prototype->enumerator=((struct Type_Enum*)type)->enumeration;
- hold_unqualified_type=get_enum_type(prototype);
+ hold_unqualified_type=get_enum_type(prototype,translation_data);
break;
case TS_POINTER:
- hold_unqualified_type=get_pointer_type(((struct Type_Pointer*)type)->points_to,0,0);
+ hold_unqualified_type=get_pointer_type(((struct Type_Pointer*)type)->points_to,0,0,translation_data);
break;
case TS_ARRAY:
hold_unqualified_type=type;
F diff --git a/src/semantics/value/type.h b/src/semantics/value/type.h --- a/src/semantics/value/type.h +++ b/src/semantics/value/type.h
#ifndef WONKY_TYPE_H
#define WONKY_TYPE_H WONKY_TYPE_H
#include <type.hh>
+
#include <denoted.h>
#include <scope.h>
#include <limits.h>
- struct Type* type_check_and_push(struct Type *type,struct Map *base,size_t struct_size);
+ struct Type* type_check_and_push(struct Type *type,struct Map *base,size_t struct_size,struct Translation_Data *translation_data);
- struct Type* get_type_error(struct Type *type);
- struct Type* get_struct_union_type(struct Denotation_Prototype *prototype);
+ struct Type* get_type_error(struct Type *type,struct Translation_Data *translation_data);
+ struct Type* get_struct_union_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data);
struct Struct_Union* get_struct_union_base(struct Scope *scope ,enum Type_Specifier struct_or_union,struct token *id);
struct Enum *get_enum_base(struct token *id);
- struct Type* get_basic_type(struct Denotation_Prototype *prototype);
- struct Type* get_pointer_type(struct Type *points_to,_Bool is_const,_Bool is_volatile);
+ struct Type* get_basic_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data);
+ struct Type* get_pointer_type(struct Type *points_to,_Bool is_const,_Bool is_volatile,struct Translation_Data *translation_data);
struct Type* get_array_type(struct Type *array_of,struct AST* number_of_elements,struct Translation_Data *translation_data);
struct Type* get_array_type_raw(struct Type *array_of,size_t size,struct Translation_Data *translation_data);
- struct Type* get_enum_type(struct Denotation_Prototype *prototype);
+ struct Type* get_enum_type(struct Denotation_Prototype *prototype,struct Translation_Data *translation_data);
struct Type* get_function_type(struct Type *return_type,struct Queue *parameters,struct Normal_Scope* function_prototype_scope,_Bool is_variadic,struct Translation_Data *translation_data);