[关闭]
@weixin 2015-04-16T16:26:12.000000Z 字数 3065 阅读 1152

compiler study 1 - crowbar

compile


CRB_create_interpreter

main.c

Created with Raphaël 2.1.2Startcheck argcyes or nocheck fileyes or noCRB_create_interpreter()CRB_compile()CRB_interpret()CRB_dispose_interpreter()Endyesnoyesno

CRB_create_interpreter()

datatype : CRB_Interpreter
- CRB.h - typedef struct CRB_Interpreter_tag CRB_Interpreter
- crowbar.h

  1. struct CRB_Interpreter_tag {
  2. Mem_Storage interpreter_storage;
  3. Mem_Storage execute_storage;
  4. Variable *variable;
  5. FunctionDefinition *function_list;
  6. StatementList *statement_list;
  7. int current_line_number;
  8. }

Mem_Storage
- mem.h - typedef struct Mem_Storage_tag *Mem_Storage
- storage.c

  1. struct MEM_Storage_tag {
  2. MemoryPageList pageList;
  3. int currentPageSize;
  4. }

MemoryPageList
- storage.c - typedef MemoryPage *MemoryPageList
MemoryPage
- storage.c - typedef struct MemoryPage_tag MemoryPage
MemoryPage_tag
- storage.c

  1. struct MemoryPage_tag {
  2. int cell_num;
  3. int use_cell_num;
  4. MemoryPageList next;
  5. Cell cell[1]
  6. }

Cell
- storge.c

  1. typedef union {
  2. long l_dummy;
  3. double d_dummy;
  4. void *p_dummy;
  5. } Cell;

sequential graph 1

Created with Raphaël 2.1.2CRB_create_interpreter 1main.cmain.cinterface.cinterface.cMEM.hMEM.hstorage.cstorage.cCRB_create_interpreterMEM_open_storageMEM_open_storage_funcMEM_malloc_func

MEM_open_storage
- MEM.h

  1. #define MEM_open_storage(page_size)\
  2. (MEM_open_storage_func(MEM_CURRENT_CONTROLLER,\
  3. __FILE__, __LINE__, page_size))

MEM_CURRENT_CONTROLLER
- MEM.h

  1. #ifdef MEM_CONTROLLER
  2. #define MEM_CURRENT_CONTROLLER MEM_CONTROLLER
  3. #else
  4. #define MEM_CURRENT_CONTROLLER mem_default_controller
  5. #endif

if MEM_CONTROLLER is not defined, then define MEM_CURRENT_CONTROLLER to be mem_default_controller

mem_default_controller
- memory.c

  1. MEM_Controller mem_default_controller = &st_default_controller;

MEM_Controller
- MEM.h - typedef struct MEM_Controller_tag *MEM_Controller

MEM_Controller_tag
- memory.h

  1. struct MEM_Controller_tag {
  2. FILE *error_fp;
  3. MEM_ErrorHandler error_handler;
  4. MEM_FailMode fail_mode;
  5. Header *block_header;
  6. };

Header
- memory.h - typedef union Header_tag header
- memory.c

  1. union Header_tag {
  2. HeaderStruct s;
  3. Align u[HEAD_ALIGN_SIZE];
  4. }

HeaderStruct
- memory.c

  1. typedef struct {
  2. int size;
  3. char *filename;
  4. int line;
  5. Header *prev;
  6. Header *next;
  7. unsigned char mark[MARK_SIZE];
  8. } HeaderStruct;

HeaderStruct is a double linked list

Align
- memory.c

  1. typedef union {
  2. long l_dummy;
  3. double d_dummy;
  4. void *p_dummy;
  5. } Align;

the Align has exactly the same definition with Cell above.

u[HEAD_ALIGN_SIZE]
what u is ?
- crowbar.h

  1. struct Statement_tag {
  2. StatementType type;
  3. int line_number;
  4. union {
  5. Expression *expression_s;
  6. GlobalStatement global_s;
  7. IfStatement if_s;
  8. WhileStatement while_s;
  9. ForStatement for_s;
  10. ReturnStatement return_s;
  11. } u;
  12. };
  1. typedef struct {
  2. CRB_ValueType type;
  3. union {
  4. CRB_Boolean boolean_value;
  5. int int_value;
  6. double double_value;
  7. CRB_String *string_value;
  8. CRB_NativePointer native_pointer;
  9. } u;
  10. } CRB_Value;

st_default_controller
- memory.c

  1. static struct MEM_Controller_tag st_default_controller = {
  2. NULL,
  3. default_error_handler,
  4. MEM_FAIL_AND_EXIT
  5. };

sequential graph 2

Created with Raphaël 2.1.2interface.cinterface.cMEM.hMEM.hstorage.cstorage.cMEM_storage_mallocMEM_storage_malloc_func

sequential graph 3

Created with Raphaël 2.1.2interface.cinterface.cutil.cutil.cutilcutilcMEM.hMEM.hstorage.cstorage.cCRB_add_native_functionscrb_malloccrb_get_current_interpreterMEM_storage_mallocMEM_storage_malloc_func
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注