Skip to content

Latest commit

 

History

History
298 lines (255 loc) · 9.72 KB

Specification.md

File metadata and controls

298 lines (255 loc) · 9.72 KB

Internal specifications of cc_sakura

2020 10.09 Takana Norimasa

enums

Tokenkind, Nodekind, Typekind.

Tokenkind

parsed tokens kind.

name description example
TK_TYPE type of function or variable int, char...
TK_RESERVED some symbol =, +, ==...
TK_IDENT variable or function name width, a_3, len...
TK_NUM number 0, 255, -1...
TK_IF if if
TK_ELSE else else
TK_SWITCH switch switch
TK_CASE case case
TK_DEFAULT default default
TK_WHILE while while
TK_FOR for for
TK_SIZEOF sizeof sizeof
TK_BLOCK code block {}
TK_STR string "hello"
TK_BREAK break break
TK_CONTINUE continue continue
TK_RETURN return return
TK_TYPEDEF typedef typedef
TK_EOF end of file

Nodekind

syntax tree node kind.

name description example
ND_ADD add symbol +
ND_SUB sub symbol -
ND_MUL multiple symbol *
ND_DIV divite symbol /
ND_MOD modulo symbol %
ND_GT greater than >
ND_GE greater than equal >=
ND_LT less than <
ND_LE less than equal <=
ND_EQ equal ==
ND_NE not equal !=
ND_AND and &&
ND_OR or ||
ND_ASSIGN assignment =
ND_COMPOUND compound assignment +=, -=, *=, /=
ND_POSTID post increment/decrement a++, a--
ND_DOT direct member selection struc.member
ND_ARROW direct member selection struc.member
ND_PREID pre increment/decrement ++a, --a
ND_ADDRESS get address &a
ND_DEREF dereference *a
ND_LVAR local variable (local variable)
ND_GVAR global variable (global variable)
ND_LARRAY local array (local array)
ND_GARRAY global array (global array)
ND_STR string "string"
ND_NUM number 0, 255, -1...
ND_TERNARY ternary operator (cond)? true : false
ND_IF if if
ND_ELSE else else
ND_IFELSE if-else if-else
ND_SWITCH switch switch
ND_CASE label case or default case:, default:
ND_WHILE while while
ND_FOR for for
ND_BLOCK code block {}
ND_ARG function argument function argument
ND_CALL_FUNC function call func();
ND_RETURN return return
ND_BREAK break loop break
ND_CONTINUE jump to the top of loop continue
ND_TYPE type of variable int, double, char...

Typekind

type of variable kind.

name description bit(byte)
VOID void --
CHAR char 8 (1byte)
INT int 32 (4byte)
PTR int *, char *... 64 (8byte)
ARRAY int a[], char a[]... 8 * size (n byte)
STRUC struct struc{int a; int b;}; 8 * sum size (n byte)
ENUM enum week{Sun,Mon,Tue,}; expanded at compile time

IncDeckind

type of increment/decrement.

name description example
PRE_INC pre increment ++p
PRE_DEC post increment p++
POST_INC pre decrement --p
POST_DEC post decrement p--

structs

Token, Node, LVar, GVar, Func, Struc, Member, Type, Str...

Token

struct of parsed token.

type/name description example
TokenKind kind kind of token TK_NUM
Token *next next chained list (next address of Token)
int val value of immediate 1970
char *str pointer at the head of the name "1970 + year..."
int len name of length 4

Node

struct of syntax tree node.

type/name description example
NodeKind kind kind of node ND_ASSIGN
Node *lhs node of left hand side (left hand node address)
Node *rhs node of right hand side (right hand node address)
Node *next see table below (next argument chain)
Node *block_code see table below NULL
Type type node type (struct Type)
int val see table below 0
int offset local variable offset 0
char *str name NULL

node->block_code

Nodekind node->block_code means
ND_BLOCK chain of block code

node->next

Nodekind node->next means
ND_ASSIGN chain of initialize formula
ND_CALL_FUNC head of argument chained lists
ND_SWITCH chain of case condition
ND_FOR exit conditions -> code or block code
ND_TERNARY else expression
ND_AND chain of arguments
ND_OR chain of arguments
ND_EQ chain of arguments
ND_NE chain of arguments
ND_GE chain of arguments
ND_GT chain of arguments
ND_LE chain of arguments
ND_LT chain of arguments
ND_ADD chain of arguments
ND_SUB chain of arguments
ND_MUL chain of arguments
ND_DIV chain of arguments
ND_MOD chain of arguments
ND_DEREF chain of arguments
ND_ADDRESS chain of arguments
ND_STR chain of arguments
ND_SIZEOF chain of arguments
ND_LVAR chain of arguments
ND_LARRAY chain of arguments
ND_GVAR chain of arguments
ND_GARRAY chain of arguments
ND_NUM chain of arguments

node->val

Nodekind node->val means
ND_NUM immediate, size of type
ND_STR string label
ND_SIZEOF size of type
ND_GVAR length of name
ND_CASE label id
ND_CALL_FUNC number of argument
ND_FOR assembly label
ND_SWITCH assembly label
ND_WHILE assembly label
ND_TERNARY assembly label
ND_IF assembly label
ND_IFLESE assembly label

Type

struct of type of variable kind.

type/name description example
TypeKind ty type of variable(INT,CHAR...) ARRAY
Type *ptr_to next chained list (next Type chain)
Member *member; chained list of Member NULL
int index_size size of array index 5
int size size of type 25
int align alignment 4

Def_Type

struct of defined type.

type/name description example
int len; name length 4
char *name; variable name "UINT; int main(){int x;"
Type type; variable type (struct Type)
Def_Type *next; chained list of Def_Type (next Def_Type chain)

Func

struct of function

type/name description example
int stack_size size to reserve on the stack 16
char *name function name add
Type type type of function (struct Type)
Node *args chained list of arguments (next argument chain)
Node *code[100] codes (codes list)
Func *next chained list of Func (next Func chain)

Str

struct of string

type/name description example
int len string length(excluding EoS) 6
int label_num string label 0
char *str string name hello!
Str *next chained list of Str (next Str chain)

Gvar

struct of global variable

type/name description example
int len; name length 1
int memsize; allocate memory size 4
char *name; variable name "x=3;\nint main(){\n\tint"
Type type; variable type (struct Type)
GVar *next; chained list of Gvar (next Gvar chain)
Node *init; node of initialize formula (node of initialize assign)

Lvar

struct of local variable

type/name description example
int len; name length 1
int offset; offset from rsp 16
char *name; variable name "y){\n\treturn x+y;"
Type type; variable type (struct Type)
LVar *next; chained list of Lvar (next Lvar chain)

Label

struct of label

type/name description example
int id; id of assemble label 1
Node *cond; condtion of case or default
Label *next; chained list of Label
LabelKind kind; LB_LABEL, LB_CASE...

Struc

struct of structure

type/name description example
int len; name length
int memsize; allocate memory size
char *name; struct name
Member *member; chained list of Member
Struc *next; chained list of Struc

Enum

struct of enum

type/name description example
int len; name length
char *name; struct name
Member *member; chained list of Member
Struc *next; chained list of Enum

Member

struct of structure member

type/name description example
int len; name length
int offset; offset from head of struct
int memsize; allocate memory size
char *name; member name
Type type; member variable type
Member *next; chained list of Member