mirror of
https://github.com/Alvin-Zilverstand/bob16.git
synced 2026-11-21 12:52:59 +01:00
2941 lines
136 KiB
C
2941 lines
136 KiB
C
#include "compiler.h"
|
|
|
|
#define COMPILER_FUNCTION_LIMIT 16u
|
|
#define COMPILER_CALL_LIMIT 64u
|
|
#define COMPILER_ARGUMENT_LIMIT 16u
|
|
#define COMPILER_GLOBAL_LIMIT 16u
|
|
#define COMPILER_STRUCT_LIMIT 8u
|
|
#define COMPILER_FIELD_LIMIT 16u
|
|
#define COMPILER_TYPEDEF_LIMIT 32u
|
|
#define COMPILER_ENUM_LIMIT 8u
|
|
#define COMPILER_ENUM_CONSTANT_LIMIT 64u
|
|
#define COMPILER_LOCAL_LIMIT 64u
|
|
#define COMPILER_LOCAL_FRAME_SIZE 1024u
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u16 CodeOffset;
|
|
u8 ReturnType,ArgumentCount,ArgumentTypes[COMPILER_ARGUMENT_LIMIT],Defined;
|
|
} BOB64_C_FUNCTION;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u16 DisplacementOffset;
|
|
u8 ArgumentCount,ArgumentTypes[COMPILER_ARGUMENT_LIMIT];
|
|
} BOB64_C_CALL;
|
|
|
|
#define COMPILER_CONTROL_LOOP 1u
|
|
#define COMPILER_CONTROL_SWITCH 2u
|
|
#define COMPILER_SWITCH_CASE_LIMIT 32u
|
|
typedef struct {
|
|
u16 ContinueTarget,BreakBranches[32],ContinueBranches[32];
|
|
u16 CaseTargets[COMPILER_SWITCH_CASE_LIMIT],DefaultTarget;
|
|
u64 CaseValues[COMPILER_SWITCH_CASE_LIMIT];
|
|
u8 Kind,BreakCount,ContinueCount,CaseCount,HasDefault;
|
|
} BOB64_C_CONTROL;
|
|
|
|
typedef struct {
|
|
u16 LocalBytes;
|
|
u8 VariableCount;
|
|
} BOB64_C_SCOPE;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u16 DataOffset,ArrayLength,InitTarget;
|
|
u8 Type,InitKind;
|
|
} BOB64_C_GLOBAL;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u16 Offset;
|
|
u8 Type;
|
|
} BOB64_C_FIELD;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u16 Size;
|
|
u8 Alignment,FieldCount;
|
|
BOB64_C_FIELD Fields[COMPILER_FIELD_LIMIT];
|
|
} BOB64_C_STRUCT;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u8 Type;
|
|
} BOB64_C_TYPEDEF;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
} BOB64_C_ENUM;
|
|
|
|
typedef struct {
|
|
char Name[32];
|
|
u64 Value;
|
|
} BOB64_C_ENUM_CONSTANT;
|
|
|
|
typedef struct {
|
|
const char *Source;
|
|
usize Length,Position,ErrorOffset;
|
|
u8 Code[BOB64_COMPILER_CODE_CAPACITY];
|
|
u8 Data[BOB64_COMPILER_DATA_CAPACITY];
|
|
usize CodePosition,DataLength;
|
|
char VariableNames[COMPILER_LOCAL_LIMIT][32];
|
|
s16 VariableDisplacement[COMPILER_LOCAL_LIMIT];
|
|
u8 VariableType[COMPILER_LOCAL_LIMIT],VariableCount;
|
|
u16 VariableArrayLength[COMPILER_LOCAL_LIMIT];
|
|
u16 LocalBytes,StackBytes;
|
|
BOB64_C_FUNCTION Functions[COMPILER_FUNCTION_LIMIT];
|
|
BOB64_C_GLOBAL Globals[COMPILER_GLOBAL_LIMIT];
|
|
BOB64_C_STRUCT Structs[COMPILER_STRUCT_LIMIT];
|
|
BOB64_C_TYPEDEF Typedefs[COMPILER_TYPEDEF_LIMIT];
|
|
BOB64_C_ENUM Enums[COMPILER_ENUM_LIMIT];
|
|
BOB64_C_ENUM_CONSTANT EnumConstants[COMPILER_ENUM_CONSTANT_LIMIT];
|
|
BOB64_C_CALL Calls[COMPILER_CALL_LIMIT];
|
|
BOB64_C_CONTROL Controls[16];
|
|
BOB64_C_SCOPE Scopes[32];
|
|
u16 FunctionSourceStart[COMPILER_FUNCTION_LIMIT];
|
|
u16 FunctionSourceEnd[COMPILER_FUNCTION_LIMIT];
|
|
u8 FunctionCount,GlobalCount,StructCount,TypedefCount,EnumCount;
|
|
u8 EnumConstantCount,CallCount;
|
|
u8 FunctionSourceCount,ScopeDepth,ControlCount;
|
|
u64 IntegerValue;
|
|
u8 ExpressionType,IntegerType;
|
|
u8 ReturnType,Return64,Failed,ControlDepth;
|
|
} BOB64_C_COMPILER;
|
|
|
|
static int compiler_emit(BOB64_C_COMPILER *compiler,u8 value);
|
|
static int compiler_emit_u32(BOB64_C_COMPILER *compiler,u32 value);
|
|
|
|
#define COMPILER_TYPE_INT 1u
|
|
#define COMPILER_TYPE_LONG_LONG 2u
|
|
#define COMPILER_TYPE_INT_POINTER 3u
|
|
#define COMPILER_TYPE_INT_ARRAY 4u
|
|
#define COMPILER_TYPE_CHAR 5u
|
|
#define COMPILER_TYPE_CHAR_POINTER 6u
|
|
#define COMPILER_TYPE_CHAR_ARRAY 7u
|
|
#define COMPILER_TYPE_SHORT 8u
|
|
#define COMPILER_TYPE_SHORT_POINTER 9u
|
|
#define COMPILER_TYPE_SHORT_ARRAY 10u
|
|
#define COMPILER_TYPE_USIZE 11u
|
|
#define COMPILER_TYPE_VOID 12u
|
|
#define COMPILER_TYPE_ISIZE_POINTER 13u
|
|
#define COMPILER_TYPE_USIZE_POINTER 14u
|
|
#define COMPILER_TYPE_LONG_LONG_ARRAY 15u
|
|
#define COMPILER_TYPE_STRUCT_BASE 16u
|
|
#define COMPILER_TYPE_USIZE_ARRAY 24u
|
|
#define COMPILER_TYPE_VOID_POINTER 25u
|
|
#define COMPILER_TYPE_STRUCT_POINTER_BASE 64u
|
|
|
|
static int compiler_is_pointer(u8 type) {
|
|
return type==COMPILER_TYPE_INT_POINTER||type==COMPILER_TYPE_CHAR_POINTER||
|
|
type==COMPILER_TYPE_SHORT_POINTER||type==COMPILER_TYPE_ISIZE_POINTER||
|
|
type==COMPILER_TYPE_USIZE_POINTER||type==COMPILER_TYPE_VOID_POINTER||
|
|
(type>=COMPILER_TYPE_STRUCT_POINTER_BASE&&
|
|
type<COMPILER_TYPE_STRUCT_POINTER_BASE+COMPILER_STRUCT_LIMIT);
|
|
}
|
|
|
|
static int compiler_pointer_types_compatible(u8 left,u8 right) {
|
|
return compiler_is_pointer(left)&&compiler_is_pointer(right)&&
|
|
(left==right||left==COMPILER_TYPE_VOID_POINTER||
|
|
right==COMPILER_TYPE_VOID_POINTER);
|
|
}
|
|
|
|
static int compiler_value_types_compatible(u8 expected,u8 actual) {
|
|
if(compiler_is_pointer(expected)||compiler_is_pointer(actual))
|
|
return compiler_pointer_types_compatible(expected,actual);
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_type_is_struct_pointer(u8 type) {
|
|
return type>=COMPILER_TYPE_STRUCT_POINTER_BASE&&
|
|
type<COMPILER_TYPE_STRUCT_POINTER_BASE+COMPILER_STRUCT_LIMIT;
|
|
}
|
|
|
|
static int compiler_type_is_array(u8 type) {
|
|
return type==COMPILER_TYPE_INT_ARRAY||type==COMPILER_TYPE_CHAR_ARRAY||
|
|
type==COMPILER_TYPE_SHORT_ARRAY||type==COMPILER_TYPE_LONG_LONG_ARRAY||
|
|
type==COMPILER_TYPE_USIZE_ARRAY;
|
|
}
|
|
|
|
static u32 compiler_array_element_size(u8 type) {
|
|
if(type==COMPILER_TYPE_CHAR_ARRAY)return 1u;
|
|
if(type==COMPILER_TYPE_SHORT_ARRAY)return 2u;
|
|
if(type==COMPILER_TYPE_LONG_LONG_ARRAY||type==COMPILER_TYPE_USIZE_ARRAY)return 8u;
|
|
return 4u;
|
|
}
|
|
|
|
static u32 compiler_pointer_scale(BOB64_C_COMPILER *compiler,u8 type) {
|
|
if(type==COMPILER_TYPE_VOID_POINTER)return 0u;
|
|
if(type==COMPILER_TYPE_CHAR_POINTER)return 1u;
|
|
if(type==COMPILER_TYPE_SHORT_POINTER)return 2u;
|
|
if(type==COMPILER_TYPE_ISIZE_POINTER||type==COMPILER_TYPE_USIZE_POINTER)return 8u;
|
|
if(compiler_type_is_struct_pointer(type))
|
|
return compiler->Structs[type-COMPILER_TYPE_STRUCT_POINTER_BASE].Size;
|
|
return 4u;
|
|
}
|
|
|
|
static u8 compiler_pointer_type(u8 value_type) {
|
|
if(value_type==COMPILER_TYPE_CHAR||value_type==COMPILER_TYPE_CHAR_ARRAY)
|
|
return COMPILER_TYPE_CHAR_POINTER;
|
|
if(value_type==COMPILER_TYPE_SHORT||value_type==COMPILER_TYPE_SHORT_ARRAY)
|
|
return COMPILER_TYPE_SHORT_POINTER;
|
|
if(value_type==COMPILER_TYPE_LONG_LONG||value_type==COMPILER_TYPE_LONG_LONG_ARRAY)
|
|
return COMPILER_TYPE_ISIZE_POINTER;
|
|
if(value_type==COMPILER_TYPE_USIZE||value_type==COMPILER_TYPE_USIZE_ARRAY)
|
|
return COMPILER_TYPE_USIZE_POINTER;
|
|
return COMPILER_TYPE_INT_POINTER;
|
|
}
|
|
|
|
static int compiler_emit_scale_register(BOB64_C_COMPILER *compiler,u32 scale,
|
|
u8 register_id) {
|
|
if(scale==1)return 1;
|
|
if(scale==2||scale==4||scale==8) {
|
|
u8 shift=scale==2?1:(scale==4?2:3);
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0xc1)&&
|
|
compiler_emit(compiler,register_id?0xe1:0xe0)&&
|
|
compiler_emit(compiler,shift);
|
|
}
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x69)&&
|
|
compiler_emit(compiler,register_id?0xc9:0xc0)&&
|
|
compiler_emit_u32(compiler,scale);
|
|
}
|
|
|
|
static void compiler_skip(BOB64_C_COMPILER *compiler) {
|
|
for(;;) {
|
|
while(compiler->Position<compiler->Length&&
|
|
(compiler->Source[compiler->Position]==' '||
|
|
compiler->Source[compiler->Position]=='\t'||
|
|
compiler->Source[compiler->Position]=='\r'||
|
|
compiler->Source[compiler->Position]=='\n'))compiler->Position++;
|
|
if(compiler->Position+1<compiler->Length&&
|
|
compiler->Source[compiler->Position]=='/'&&
|
|
compiler->Source[compiler->Position+1]=='/') {
|
|
while(compiler->Position<compiler->Length&&
|
|
compiler->Source[compiler->Position]!='\n')compiler->Position++;
|
|
} else if(compiler->Position+1<compiler->Length&&
|
|
compiler->Source[compiler->Position]=='/'&&
|
|
compiler->Source[compiler->Position+1]=='*') {
|
|
compiler->Position+=2;
|
|
while(compiler->Position+1<compiler->Length&&
|
|
!(compiler->Source[compiler->Position]=='*'&&
|
|
compiler->Source[compiler->Position+1]=='/'))compiler->Position++;
|
|
if(compiler->Position+1>=compiler->Length) {
|
|
compiler->Failed=1;compiler->ErrorOffset=compiler->Position;return;
|
|
}
|
|
compiler->Position+=2;
|
|
} else return;
|
|
}
|
|
}
|
|
|
|
static int compiler_take(BOB64_C_COMPILER *compiler,char value) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Failed||compiler->Position>=compiler->Length||
|
|
compiler->Source[compiler->Position]!=value)return 0;
|
|
compiler->Position++;return 1;
|
|
}
|
|
|
|
static int compiler_word(BOB64_C_COMPILER *compiler,const char *word) {
|
|
usize start,length=0;
|
|
compiler_skip(compiler);start=compiler->Position;
|
|
while(word[length])length++;
|
|
if(compiler->Failed||length>compiler->Length-start)return 0;
|
|
for(usize i=0;i<length;i++)if(compiler->Source[start+i]!=word[i])return 0;
|
|
if(start+length<compiler->Length) {
|
|
char next=compiler->Source[start+length];
|
|
if((next>='a'&&next<='z')||(next>='A'&&next<='Z')||
|
|
(next>='0'&&next<='9')||next=='_')return 0;
|
|
}
|
|
compiler->Position+=length;return 1;
|
|
}
|
|
|
|
static u8 compiler_long_type(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_word(compiler,"long"))return 0;
|
|
/* bob64 follows LLP64: long is 32-bit; long long is 64-bit. */
|
|
return compiler_word(compiler,"long")?COMPILER_TYPE_LONG_LONG:COMPILER_TYPE_INT;
|
|
}
|
|
|
|
static u8 compiler_integer_type(BOB64_C_COMPILER *compiler) {
|
|
usize saved=compiler->Position;
|
|
if(compiler_word(compiler,"unsigned")) {
|
|
if(compiler_word(compiler,"long")&&compiler_word(compiler,"long")) {
|
|
(void)compiler_word(compiler,"int");
|
|
return COMPILER_TYPE_USIZE;
|
|
}
|
|
compiler->Position=saved;
|
|
}
|
|
if(compiler_word(compiler,"int"))return COMPILER_TYPE_INT;
|
|
if(compiler_word(compiler,"short"))return COMPILER_TYPE_SHORT;
|
|
if(compiler_word(compiler,"usize")||compiler_word(compiler,"uintptr_t"))
|
|
return COMPILER_TYPE_USIZE;
|
|
if(compiler_word(compiler,"isize")||compiler_word(compiler,"intptr_t"))
|
|
return COMPILER_TYPE_LONG_LONG;
|
|
return compiler_long_type(compiler);
|
|
}
|
|
|
|
static int compiler_identifier(BOB64_C_COMPILER *compiler,char *name,usize capacity) {
|
|
usize length=0;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
char first=compiler->Source[compiler->Position];
|
|
if(!((first>='a'&&first<='z')||(first>='A'&&first<='Z')||first=='_'))return 0;
|
|
while(compiler->Position<compiler->Length) {
|
|
char next=compiler->Source[compiler->Position];
|
|
if(!((next>='a'&&next<='z')||(next>='A'&&next<='Z')||
|
|
(next>='0'&&next<='9')||next=='_'))break;
|
|
if(length+1>=capacity)return 0;
|
|
name[length++]=next;compiler->Position++;
|
|
}
|
|
name[length]=0;return 1;
|
|
}
|
|
|
|
static int compiler_struct_find(BOB64_C_COMPILER *compiler,const char *name);
|
|
static int compiler_type_is_struct(u8 type);
|
|
static int compiler_enum_find(BOB64_C_COMPILER *compiler,const char *name);
|
|
static int compiler_enum_constant_find(BOB64_C_COMPILER *compiler,const char *name);
|
|
|
|
static int compiler_typedef_find(BOB64_C_COMPILER *compiler,const char *name) {
|
|
for(u32 i=0;i<compiler->TypedefCount;i++) {
|
|
usize position=0;
|
|
while(compiler->Typedefs[i].Name[position]&&
|
|
compiler->Typedefs[i].Name[position]==name[position])position++;
|
|
if(!compiler->Typedefs[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static u8 compiler_type_specifier(BOB64_C_COMPILER *compiler) {
|
|
if(compiler_word(compiler,"char"))return COMPILER_TYPE_CHAR;
|
|
if(compiler_word(compiler,"void"))return COMPILER_TYPE_VOID;
|
|
if(compiler_word(compiler,"struct")) {
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int structure=compiler_struct_find(compiler,name);
|
|
return structure<0?0:(u8)(COMPILER_TYPE_STRUCT_BASE+(u32)structure);
|
|
}
|
|
if(compiler_word(compiler,"enum")) {
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name))||
|
|
compiler_enum_find(compiler,name)<0)return 0;
|
|
return COMPILER_TYPE_INT;
|
|
}
|
|
u8 type=compiler_integer_type(compiler);
|
|
if(type)return type;
|
|
usize saved=compiler->Position;
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int alias=compiler_typedef_find(compiler,name);
|
|
if(alias<0) {compiler->Position=saved;return 0;}
|
|
return compiler->Typedefs[alias].Type;
|
|
}
|
|
|
|
static u8 compiler_pointer_to(BOB64_C_COMPILER *compiler,u8 type) {
|
|
(void)compiler;
|
|
if(type==COMPILER_TYPE_VOID)return COMPILER_TYPE_VOID_POINTER;
|
|
if(compiler_type_is_struct(type))
|
|
return (u8)(COMPILER_TYPE_STRUCT_POINTER_BASE+type-COMPILER_TYPE_STRUCT_BASE);
|
|
if(!type||type==COMPILER_TYPE_VOID||compiler_is_pointer(type))return 0;
|
|
return compiler_pointer_type(type);
|
|
}
|
|
|
|
static u8 compiler_array_type(u8 type) {
|
|
if(type==COMPILER_TYPE_CHAR)return COMPILER_TYPE_CHAR_ARRAY;
|
|
if(type==COMPILER_TYPE_SHORT)return COMPILER_TYPE_SHORT_ARRAY;
|
|
if(type==COMPILER_TYPE_LONG_LONG)return COMPILER_TYPE_LONG_LONG_ARRAY;
|
|
if(type==COMPILER_TYPE_USIZE)return COMPILER_TYPE_USIZE_ARRAY;
|
|
if(type==COMPILER_TYPE_INT)return COMPILER_TYPE_INT_ARRAY;
|
|
return 0;
|
|
}
|
|
|
|
static int compiler_function_signature(BOB64_C_COMPILER *compiler,char *name,
|
|
usize name_capacity,u8 *return_type,u8 *argument_count,
|
|
u8 argument_types[COMPILER_ARGUMENT_LIMIT],
|
|
char parameter_names[COMPILER_ARGUMENT_LIMIT][32],int require_names) {
|
|
u32 count=0;
|
|
u8 value_type=compiler_type_specifier(compiler);
|
|
int struct_type=compiler_type_is_struct(value_type)?
|
|
(int)(value_type-COMPILER_TYPE_STRUCT_BASE):-1;
|
|
if(!value_type)return 0;
|
|
int return_pointer=compiler_take(compiler,'*');
|
|
if(struct_type>=0&&!return_pointer)return 0;
|
|
*return_type=return_pointer?compiler_pointer_to(compiler,value_type):value_type;
|
|
if(!*return_type)return 0;
|
|
if(!compiler_identifier(compiler,name,name_capacity)||!compiler_take(compiler,'('))return 0;
|
|
if(!compiler_take(compiler,')')) {
|
|
int pending_void=compiler_word(compiler,"void");
|
|
if(!(pending_void&&compiler_take(compiler,')'))) {
|
|
for(;;) {
|
|
int pointer,has_name;
|
|
if(count>=COMPILER_ARGUMENT_LIMIT)return 0;
|
|
u8 value_type=pending_void?COMPILER_TYPE_VOID:
|
|
compiler_type_specifier(compiler);
|
|
pending_void=0;
|
|
int struct_type=compiler_type_is_struct(value_type)?
|
|
(int)(value_type-COMPILER_TYPE_STRUCT_BASE):-1;
|
|
if(!value_type)return 0;
|
|
pointer=compiler_take(compiler,'*');
|
|
if(value_type==COMPILER_TYPE_VOID&&!pointer)return 0;
|
|
if(struct_type>=0&&!pointer)return 0;
|
|
compiler_skip(compiler);
|
|
has_name=compiler_identifier(compiler,parameter_names[count],
|
|
sizeof(parameter_names[count]));
|
|
if(require_names&&!has_name)return 0;
|
|
if(!has_name)parameter_names[count][0]=0;
|
|
if(compiler_take(compiler,'[')) {
|
|
if(pointer||!compiler_take(compiler,']'))return 0;
|
|
pointer=1;
|
|
}
|
|
argument_types[count]=pointer?
|
|
compiler_pointer_to(compiler,value_type):value_type;
|
|
if(!argument_types[count])return 0;
|
|
count++;
|
|
if(compiler_take(compiler,')'))break;
|
|
if(!compiler_take(compiler,','))return 0;
|
|
}
|
|
}
|
|
}
|
|
*argument_count=(u8)count;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_variable_find(BOB64_C_COMPILER *compiler,const char *name) {
|
|
for(u32 i=compiler->VariableCount;i>0;i--) {
|
|
usize position=0;
|
|
u32 variable=i-1;
|
|
while(compiler->VariableNames[variable][position]&&
|
|
compiler->VariableNames[variable][position]==name[position])position++;
|
|
if(!compiler->VariableNames[variable][position]&&!name[position])
|
|
return (int)variable;
|
|
}
|
|
for(u32 i=0;i<compiler->GlobalCount;i++) {
|
|
usize position=0;
|
|
while(compiler->Globals[i].Name[position]&&
|
|
compiler->Globals[i].Name[position]==name[position])position++;
|
|
if(!compiler->Globals[i].Name[position]&&!name[position])return (int)(0x8000u|i);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_scope_push(BOB64_C_COMPILER *compiler) {
|
|
if(compiler->ScopeDepth>=sizeof(compiler->Scopes)/sizeof(compiler->Scopes[0]))
|
|
return 0;
|
|
BOB64_C_SCOPE *scope=&compiler->Scopes[compiler->ScopeDepth++];
|
|
scope->VariableCount=compiler->VariableCount;
|
|
scope->LocalBytes=compiler->LocalBytes;
|
|
return 1;
|
|
}
|
|
|
|
static void compiler_scope_pop(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler->ScopeDepth)return;
|
|
BOB64_C_SCOPE *scope=&compiler->Scopes[--compiler->ScopeDepth];
|
|
compiler->VariableCount=scope->VariableCount;
|
|
compiler->LocalBytes=scope->LocalBytes;
|
|
}
|
|
|
|
static int compiler_struct_find(BOB64_C_COMPILER *compiler,const char *name) {
|
|
for(u32 i=0;i<compiler->StructCount;i++) {
|
|
usize position=0;
|
|
while(compiler->Structs[i].Name[position]&&
|
|
compiler->Structs[i].Name[position]==name[position])position++;
|
|
if(!compiler->Structs[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_enum_find(BOB64_C_COMPILER *compiler,const char *name) {
|
|
for(u32 i=0;i<compiler->EnumCount;i++) {
|
|
usize position=0;
|
|
while(compiler->Enums[i].Name[position]&&
|
|
compiler->Enums[i].Name[position]==name[position])position++;
|
|
if(!compiler->Enums[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_enum_constant_find(BOB64_C_COMPILER *compiler,
|
|
const char *name) {
|
|
for(u32 i=0;i<compiler->EnumConstantCount;i++) {
|
|
usize position=0;
|
|
while(compiler->EnumConstants[i].Name[position]&&
|
|
compiler->EnumConstants[i].Name[position]==name[position])position++;
|
|
if(!compiler->EnumConstants[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_type_is_struct(u8 type) {
|
|
return type>=COMPILER_TYPE_STRUCT_BASE&&
|
|
type<COMPILER_TYPE_STRUCT_BASE+COMPILER_STRUCT_LIMIT;
|
|
}
|
|
|
|
static u32 compiler_type_size(BOB64_C_COMPILER *compiler,u8 type) {
|
|
if(compiler_type_is_struct(type))
|
|
return compiler->Structs[type-COMPILER_TYPE_STRUCT_BASE].Size;
|
|
if(type==COMPILER_TYPE_CHAR)return 1;
|
|
if(type==COMPILER_TYPE_SHORT)return 2;
|
|
if(type==COMPILER_TYPE_SHORT_ARRAY)return 2;
|
|
if(type==COMPILER_TYPE_INT||type==COMPILER_TYPE_INT_ARRAY)return 4;
|
|
if(type==COMPILER_TYPE_LONG_LONG_ARRAY||type==COMPILER_TYPE_USIZE_ARRAY)return 8;
|
|
return 8;
|
|
}
|
|
|
|
static u32 compiler_type_alignment(BOB64_C_COMPILER *compiler,u8 type) {
|
|
if(compiler_type_is_struct(type))
|
|
return compiler->Structs[type-COMPILER_TYPE_STRUCT_BASE].Alignment;
|
|
u32 size=compiler_type_size(compiler,type);
|
|
return size>=8?8:(size>=4?4:(size>=2?2:1));
|
|
}
|
|
|
|
static int compiler_field_find(BOB64_C_COMPILER *compiler,u8 struct_type,
|
|
const char *name) {
|
|
if(!compiler_type_is_struct(struct_type))return -1;
|
|
BOB64_C_STRUCT *structure=&compiler->Structs[struct_type-COMPILER_TYPE_STRUCT_BASE];
|
|
for(u32 i=0;i<structure->FieldCount;i++) {
|
|
usize position=0;
|
|
while(structure->Fields[i].Name[position]&&
|
|
structure->Fields[i].Name[position]==name[position])position++;
|
|
if(!structure->Fields[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_variable_is_global(u32 variable) {
|
|
return (variable&0x8000u)!=0;
|
|
}
|
|
|
|
static int compiler_emit_rbp_operand(BOB64_C_COMPILER *compiler,
|
|
u8 disp8_modrm,s16 displacement) {
|
|
if(displacement>=-128&&displacement<=127)
|
|
return compiler_emit(compiler,disp8_modrm)&&
|
|
compiler_emit(compiler,(u8)(s8)displacement);
|
|
u8 disp32_modrm=(u8)((disp8_modrm&0x3fu)|0x80u);
|
|
return compiler_emit(compiler,disp32_modrm)&&
|
|
compiler_emit_u32(compiler,(u32)(s32)displacement);
|
|
}
|
|
|
|
static u8 compiler_variable_type(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
return compiler_variable_is_global(variable)?
|
|
compiler->Globals[variable&0x7fffu].Type:compiler->VariableType[variable];
|
|
}
|
|
|
|
static u32 compiler_variable_array_length(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
return compiler_variable_is_global(variable)?
|
|
compiler->Globals[variable&0x7fffu].ArrayLength:
|
|
compiler->VariableArrayLength[variable];
|
|
}
|
|
|
|
static int compiler_emit_global_displacement(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
BOB64_C_GLOBAL *global=&compiler->Globals[variable&0x7fffu];
|
|
s64 displacement=(s64)(BOB64_COMPILER_CODE_CAPACITY+global->DataOffset)-
|
|
(s64)(compiler->CodePosition+4u);
|
|
if(displacement<(-2147483647LL-1)||displacement>2147483647)return 0;
|
|
return compiler_emit_u32(compiler,(u32)(s32)displacement);
|
|
}
|
|
|
|
static int compiler_emit_data_displacement(BOB64_C_COMPILER *compiler,u16 data_offset) {
|
|
s64 displacement=(s64)(BOB64_COMPILER_CODE_CAPACITY+data_offset)-
|
|
(s64)(compiler->CodePosition+4u);
|
|
if(displacement<(-2147483647LL-1)||displacement>2147483647)return 0;
|
|
return compiler_emit_u32(compiler,(u32)(s32)displacement);
|
|
}
|
|
|
|
static int compiler_emit_global_initializers(BOB64_C_COMPILER *compiler) {
|
|
for(u32 i=0;i<compiler->GlobalCount;i++) {
|
|
BOB64_C_GLOBAL *global=&compiler->Globals[i];
|
|
if(!global->InitKind)continue;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8d)||
|
|
!compiler_emit(compiler,0x05)||
|
|
!compiler_emit_data_displacement(compiler,global->InitTarget)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x05)||
|
|
!compiler_emit_global_displacement(compiler,0x8000u|i))return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_function_find(BOB64_C_COMPILER *compiler,const char *name) {
|
|
for(u32 i=0;i<compiler->FunctionCount;i++) {
|
|
usize position=0;
|
|
while(compiler->Functions[i].Name[position]&&
|
|
compiler->Functions[i].Name[position]==name[position])position++;
|
|
if(!compiler->Functions[i].Name[position]&&!name[position])return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int compiler_function_declare(BOB64_C_COMPILER *compiler,const char *name,
|
|
u16 code_offset,u8 return_type,u8 argument_count,
|
|
const u8 *argument_types,int defined) {
|
|
if(argument_count>COMPILER_ARGUMENT_LIMIT||
|
|
compiler_enum_constant_find(compiler,name)>=0)return 0;
|
|
int existing=compiler_function_find(compiler,name);
|
|
if(existing>=0) {
|
|
BOB64_C_FUNCTION *function=&compiler->Functions[existing];
|
|
if(function->ReturnType!=return_type||function->ArgumentCount!=argument_count||
|
|
(defined&&function->Defined))return 0;
|
|
for(u32 i=0;i<argument_count;i++)
|
|
if(function->ArgumentTypes[i]!=argument_types[i])return 0;
|
|
if(defined) {
|
|
function->CodeOffset=code_offset;
|
|
function->Defined=1;
|
|
}
|
|
return 1;
|
|
}
|
|
if(compiler->FunctionCount>=COMPILER_FUNCTION_LIMIT)return 0;
|
|
u32 index=compiler->FunctionCount++;
|
|
BOB64_C_FUNCTION *function=&compiler->Functions[index];
|
|
usize position=0;
|
|
while(name[position]&&position+1<sizeof(function->Name)) {
|
|
function->Name[position]=name[position];position++;
|
|
}
|
|
if(name[position])return 0;
|
|
function->Name[position]=0;
|
|
function->CodeOffset=code_offset;
|
|
function->ReturnType=return_type;
|
|
function->ArgumentCount=argument_count;
|
|
function->Defined=(u8)defined;
|
|
for(u32 i=0;i<argument_count;i++)function->ArgumentTypes[i]=argument_types[i];
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_variable_load(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
if(compiler_variable_is_global(variable)) {
|
|
u8 type=compiler_variable_type(compiler,variable);
|
|
if(type==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbe)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
if(type==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbf)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
if(type==COMPILER_TYPE_LONG_LONG||type==COMPILER_TYPE_USIZE||
|
|
compiler_is_pointer(type))
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8b)&&
|
|
compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x63)&&
|
|
compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
}
|
|
s16 displacement=compiler->VariableDisplacement[variable];
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbe)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbf)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_LONG_LONG||
|
|
compiler->VariableType[variable]==COMPILER_TYPE_USIZE||
|
|
compiler_is_pointer(compiler->VariableType[variable]))
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8b)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x63)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
}
|
|
|
|
static int compiler_variable_store(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
if(compiler_variable_is_global(variable)) {
|
|
u8 type=compiler_variable_type(compiler,variable);
|
|
if(type==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x88)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
if(type==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x66)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
if(type==COMPILER_TYPE_LONG_LONG||type==COMPILER_TYPE_USIZE||
|
|
compiler_is_pointer(type))
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
return compiler_emit(compiler,0x89)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
}
|
|
s16 displacement=compiler->VariableDisplacement[variable];
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x88)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x66)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_LONG_LONG||
|
|
compiler->VariableType[variable]==COMPILER_TYPE_USIZE||
|
|
compiler_is_pointer(compiler->VariableType[variable]))
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
return compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
}
|
|
|
|
static int compiler_variable_address(BOB64_C_COMPILER *compiler,u32 variable,
|
|
u8 destination_register) {
|
|
if(compiler_variable_is_global(variable))
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8d)&&
|
|
compiler_emit(compiler,(u8)(0x05u|(destination_register<<3)))&&
|
|
compiler_emit_global_displacement(compiler,variable);
|
|
s16 displacement=compiler->VariableDisplacement[variable];
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8d)&&
|
|
compiler_emit_rbp_operand(compiler,
|
|
(u8)(0x45u|(destination_register<<3)),displacement);
|
|
}
|
|
|
|
static int compiler_member_address(BOB64_C_COMPILER *compiler,u32 variable,
|
|
const BOB64_C_FIELD *field) {
|
|
if(!compiler_variable_address(compiler,variable,0))return 0;
|
|
if(!field->Offset)return 1;
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_u32(compiler,field->Offset);
|
|
}
|
|
|
|
static int compiler_member_load(BOB64_C_COMPILER *compiler,u8 type) {
|
|
if(type==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbe)&&compiler_emit(compiler,0x00);
|
|
if(type==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x0f)&&
|
|
compiler_emit(compiler,0xbf)&&compiler_emit(compiler,0x00);
|
|
if(type==COMPILER_TYPE_INT)
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x63)&&
|
|
compiler_emit(compiler,0x00);
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8b)&&
|
|
compiler_emit(compiler,0x00);
|
|
}
|
|
|
|
static int compiler_member_store(BOB64_C_COMPILER *compiler,u8 type) {
|
|
if(type==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x88)&&compiler_emit(compiler,0x01);
|
|
if(type==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x66)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit(compiler,0x01);
|
|
if(type==COMPILER_TYPE_INT)
|
|
return compiler_emit(compiler,0x89)&&compiler_emit(compiler,0x01);
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit(compiler,0x01);
|
|
}
|
|
|
|
static int compiler_add_variable(BOB64_C_COMPILER *compiler,const char *name,
|
|
u8 type,u32 array_length,u32 *variable_out) {
|
|
usize length=0;
|
|
u32 bytes;
|
|
u32 scope_start=compiler->ScopeDepth?
|
|
compiler->Scopes[compiler->ScopeDepth-1].VariableCount:0;
|
|
for(u32 i=scope_start;i<compiler->VariableCount;i++) {
|
|
usize position=0;
|
|
while(compiler->VariableNames[i][position]&&
|
|
compiler->VariableNames[i][position]==name[position])position++;
|
|
if(!compiler->VariableNames[i][position]&&!name[position])return 0;
|
|
}
|
|
if(compiler->VariableCount>=COMPILER_LOCAL_LIMIT||
|
|
compiler_enum_constant_find(compiler,name)>=0)return 0;
|
|
if(type==COMPILER_TYPE_INT_ARRAY) {
|
|
if(!array_length||array_length>COMPILER_LOCAL_FRAME_SIZE/4u)return 0;
|
|
bytes=array_length*4;
|
|
compiler->LocalBytes=(u16)((compiler->LocalBytes+3u)&~3u);
|
|
} else if(type==COMPILER_TYPE_SHORT_ARRAY) {
|
|
if(!array_length||array_length>COMPILER_LOCAL_FRAME_SIZE/2u)return 0;
|
|
bytes=array_length*2;
|
|
compiler->LocalBytes=(u16)((compiler->LocalBytes+1u)&~1u);
|
|
} else if(type==COMPILER_TYPE_CHAR_ARRAY) {
|
|
if(!array_length||array_length>COMPILER_LOCAL_FRAME_SIZE)return 0;
|
|
bytes=array_length;
|
|
} else if(type==COMPILER_TYPE_LONG_LONG_ARRAY||type==COMPILER_TYPE_USIZE_ARRAY) {
|
|
if(!array_length||array_length>COMPILER_LOCAL_FRAME_SIZE/8u)return 0;
|
|
bytes=array_length*8u;
|
|
compiler->LocalBytes=(u16)((compiler->LocalBytes+7u)&~7u);
|
|
} else if(compiler_type_is_struct(type)) {
|
|
u32 alignment=compiler_type_alignment(compiler,type);
|
|
compiler->LocalBytes=(u16)((compiler->LocalBytes+alignment-1u)&~(alignment-1u));
|
|
bytes=compiler_type_size(compiler,type);
|
|
} else {
|
|
bytes=8;
|
|
compiler->LocalBytes=(u16)((compiler->LocalBytes+7u)&~7u);
|
|
}
|
|
if(compiler->LocalBytes>COMPILER_LOCAL_FRAME_SIZE||
|
|
bytes>COMPILER_LOCAL_FRAME_SIZE-compiler->LocalBytes)return 0;
|
|
compiler->LocalBytes=(u16)(compiler->LocalBytes+bytes);
|
|
u32 variable=compiler->VariableCount++;
|
|
while(name[length])length++;
|
|
for(usize i=0;i<=length;i++)compiler->VariableNames[variable][i]=name[i];
|
|
compiler->VariableType[variable]=type;
|
|
compiler->VariableArrayLength[variable]=(u16)array_length;
|
|
compiler->VariableDisplacement[variable]=-(s16)compiler->LocalBytes;
|
|
if(variable_out)*variable_out=variable;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_integer(BOB64_C_COMPILER *compiler,u64 *value) {
|
|
u64 number=0;u32 base=10;usize digits=0;
|
|
int has_unsigned=0,long_count=0,unsigned_after_long=0;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position+1<compiler->Length&&compiler->Source[compiler->Position]=='0'&&
|
|
(compiler->Source[compiler->Position+1]=='x'||
|
|
compiler->Source[compiler->Position+1]=='X')) {
|
|
base=16;compiler->Position+=2;
|
|
}
|
|
while(compiler->Position<compiler->Length) {
|
|
char character=compiler->Source[compiler->Position];
|
|
u32 digit;
|
|
if(character>='0'&&character<='9')digit=(u32)(character-'0');
|
|
else if(base==16&&character>='a'&&character<='f')digit=(u32)(character-'a'+10);
|
|
else if(base==16&&character>='A'&&character<='F')digit=(u32)(character-'A'+10);
|
|
else break;
|
|
if(digit>=base||number>(~(u64)0-digit)/base)return 0;
|
|
number=number*base+digit;digits++;compiler->Position++;
|
|
}
|
|
if(!digits)return 0;
|
|
while(compiler->Position<compiler->Length) {
|
|
char suffix=compiler->Source[compiler->Position];
|
|
if((suffix=='u'||suffix=='U')&&!has_unsigned) {
|
|
has_unsigned=1;if(long_count)unsigned_after_long=1;compiler->Position++;
|
|
} else if((suffix=='l'||suffix=='L')&&long_count<2) {
|
|
if(unsigned_after_long)return 0;
|
|
long_count++;compiler->Position++;
|
|
} else break;
|
|
}
|
|
if(compiler->Position<compiler->Length) {
|
|
char next=compiler->Source[compiler->Position];
|
|
if((next>='a'&&next<='z')||(next>='A'&&next<='Z')||next=='_')return 0;
|
|
}
|
|
*value=number;
|
|
compiler->IntegerType=has_unsigned?COMPILER_TYPE_USIZE:
|
|
((long_count>=2||number>0x7fffffffu)?COMPILER_TYPE_LONG_LONG:
|
|
COMPILER_TYPE_INT);
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_string(BOB64_C_COMPILER *compiler,usize *offset,usize *length) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length||compiler->Source[compiler->Position]!='"')return 0;
|
|
compiler->Position++;*offset=compiler->DataLength;*length=0;
|
|
while(compiler->Position<compiler->Length&&compiler->Source[compiler->Position]!='"') {
|
|
u8 value=(u8)compiler->Source[compiler->Position++];
|
|
if(value=='\\') {
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
value=(u8)compiler->Source[compiler->Position++];
|
|
if(value=='n')value='\n';else if(value=='r')value='\r';
|
|
else if(value=='t')value='\t';
|
|
else if(value!='\\'&&value!='"'&&value!='\'')return 0;
|
|
}
|
|
if(!value||*length>=BOB64_SYSCALL_MAX_BUFFER||
|
|
compiler->DataLength>=sizeof(compiler->Data))return 0;
|
|
compiler->Data[compiler->DataLength++]=value;(*length)++;
|
|
}
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
compiler->Position++;
|
|
if(compiler->DataLength>=sizeof(compiler->Data))return 0;
|
|
compiler->Data[compiler->DataLength++]=0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_character(BOB64_C_COMPILER *compiler,u64 *value) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length||compiler->Source[compiler->Position]!='\'')
|
|
return 0;
|
|
compiler->Position++;
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
u8 character=(u8)compiler->Source[compiler->Position++];
|
|
if(character=='\\') {
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
character=(u8)compiler->Source[compiler->Position++];
|
|
if(character=='n')character='\n';else if(character=='r')character='\r';
|
|
else if(character=='t')character='\t';else if(character=='0')character=0;
|
|
else if(character!='\\'&&character!='\''&&character!='"')return 0;
|
|
}
|
|
if(compiler->Position>=compiler->Length||
|
|
compiler->Source[compiler->Position++]!='\'')return 0;
|
|
*value=(u64)(s64)(s8)character;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_emit(BOB64_C_COMPILER *compiler,u8 value) {
|
|
if(compiler->CodePosition>=sizeof(compiler->Code))return 0;
|
|
compiler->Code[compiler->CodePosition++]=value;return 1;
|
|
}
|
|
|
|
static int compiler_emit_u32(BOB64_C_COMPILER *compiler,u32 value) {
|
|
for(u32 i=0;i<4;i++)if(!compiler_emit(compiler,(u8)(value>>(i*8))))return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_emit_u64(BOB64_C_COMPILER *compiler,u64 value) {
|
|
for(u32 i=0;i<8;i++)if(!compiler_emit(compiler,(u8)(value>>(i*8))))return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_emit_integer(BOB64_C_COMPILER *compiler,u64 value) {
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0xb8)&&
|
|
compiler_emit_u64(compiler,value);
|
|
}
|
|
|
|
static int compiler_expression(BOB64_C_COMPILER *compiler);
|
|
|
|
static int compiler_sizeof_type(BOB64_C_COMPILER *compiler,u32 *size_out) {
|
|
u8 type=compiler_type_specifier(compiler);
|
|
u64 count;
|
|
u32 size;
|
|
int pointer;
|
|
if(!type)return -1;
|
|
pointer=compiler_take(compiler,'*');
|
|
if(type==COMPILER_TYPE_VOID&&!pointer)return 0;
|
|
size=pointer?8u:compiler_type_size(compiler,type);
|
|
if(compiler_take(compiler,'[')) {
|
|
if(!compiler_integer(compiler,&count)||!count||
|
|
!compiler_take(compiler,']')||count>0xffffffffu/size)return 0;
|
|
size*=(u32)count;
|
|
}
|
|
if(!size)return 0;
|
|
if(!compiler_take(compiler,')'))return -1;
|
|
*size_out=size;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_sizeof_expression(BOB64_C_COMPILER *compiler,
|
|
u32 *size_out) {
|
|
usize position=compiler->Position;
|
|
char name[32];
|
|
u32 variable_size;
|
|
int variable;
|
|
if(compiler_identifier(compiler,name,sizeof(name))&&
|
|
compiler_take(compiler,')')) {
|
|
variable=compiler_variable_find(compiler,name);
|
|
if(variable>=0) {
|
|
u8 type=compiler_variable_type(compiler,(u32)variable);
|
|
if(compiler_type_is_array(type))
|
|
variable_size=compiler_array_element_size(type)*
|
|
compiler_variable_array_length(compiler,(u32)variable);
|
|
else variable_size=compiler_type_size(compiler,type);
|
|
if(variable_size) {*size_out=variable_size;return 1;}
|
|
}
|
|
compiler->Position=position;
|
|
} else compiler->Position=position;
|
|
if(position<compiler->Length&&compiler->Source[position]=='"') {
|
|
usize old_data_length=compiler->DataLength;
|
|
usize string_offset,string_length;
|
|
if(compiler_string(compiler,&string_offset,&string_length)&&
|
|
compiler_take(compiler,')')&&string_length<0xffffffffu) {
|
|
compiler->DataLength=old_data_length;
|
|
*size_out=(u32)string_length+1u;
|
|
return 1;
|
|
}
|
|
compiler->DataLength=old_data_length;
|
|
compiler->Position=position;
|
|
}
|
|
usize saved_code=compiler->CodePosition;
|
|
usize saved_data=compiler->DataLength;
|
|
u8 saved_calls=compiler->CallCount;
|
|
u16 saved_stack=compiler->StackBytes;
|
|
u8 saved_expression_type=compiler->ExpressionType;
|
|
u8 saved_integer_type=compiler->IntegerType;
|
|
if(!compiler_expression(compiler)||!compiler_take(compiler,')'))return 0;
|
|
u8 type=compiler->ExpressionType;
|
|
if(type==COMPILER_TYPE_VOID)return 0;
|
|
variable_size=compiler_type_size(compiler,type);
|
|
if(!variable_size)return 0;
|
|
compiler->CodePosition=saved_code;
|
|
compiler->DataLength=saved_data;
|
|
compiler->CallCount=saved_calls;
|
|
compiler->StackBytes=saved_stack;
|
|
compiler->ExpressionType=saved_expression_type;
|
|
compiler->IntegerType=saved_integer_type;
|
|
*size_out=variable_size;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_shift(BOB64_C_COMPILER *compiler);
|
|
static int compiler_relational(BOB64_C_COMPILER *compiler);
|
|
static int compiler_equality(BOB64_C_COMPILER *compiler);
|
|
static int compiler_bitwise_and(BOB64_C_COMPILER *compiler);
|
|
static int compiler_bitwise_xor(BOB64_C_COMPILER *compiler);
|
|
static int compiler_bitwise_or(BOB64_C_COMPILER *compiler);
|
|
static int compiler_emit_branch(BOB64_C_COMPILER *compiler,u8 opcode,
|
|
u16 *displacement_offset);
|
|
static int compiler_patch_branch(BOB64_C_COMPILER *compiler,u16 displacement_offset,
|
|
usize target);
|
|
static int compiler_for_post(BOB64_C_COMPILER *compiler);
|
|
|
|
static int compiler_emit_call(BOB64_C_COMPILER *compiler,const char *name) {
|
|
u8 argument_types[COMPILER_ARGUMENT_LIMIT];
|
|
u32 argument_count=0;
|
|
u16 saved_stack_bytes=compiler->StackBytes;
|
|
if(!compiler_take(compiler,'('))return 0;
|
|
if(!compiler_take(compiler,')')) {
|
|
for(;;) {
|
|
if(argument_count>=COMPILER_ARGUMENT_LIMIT||
|
|
!compiler_expression(compiler)||
|
|
compiler->ExpressionType==COMPILER_TYPE_VOID)return 0;
|
|
argument_types[argument_count++]=compiler->ExpressionType;
|
|
if(!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(compiler_take(compiler,')'))break;
|
|
if(!compiler_take(compiler,','))return 0;
|
|
}
|
|
}
|
|
if(compiler->CallCount>=COMPILER_CALL_LIMIT)return 0;
|
|
u32 stack_argument_bytes=argument_count>4?(argument_count-4u)*8u:0u;
|
|
u32 outgoing_bytes=32u+stack_argument_bytes;
|
|
u32 allocation=outgoing_bytes+
|
|
((16u-((compiler->StackBytes+outgoing_bytes)&15u))&15u);
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x83)||
|
|
!compiler_emit(compiler,0xec)||!compiler_emit(compiler,(u8)allocation))return 0;
|
|
compiler->StackBytes=(u16)(compiler->StackBytes+allocation);
|
|
for(u32 i=0;i<argument_count&&i<4;i++) {
|
|
u32 displacement=allocation+(argument_count-1u-i)*8u;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit(compiler,0x44)||!compiler_emit(compiler,0x24)||
|
|
!compiler_emit(compiler,(u8)displacement))return 0;
|
|
static const u8 move_argument[4][3]={
|
|
{0x48,0x89,0xc1},{0x48,0x89,0xc2},
|
|
{0x49,0x89,0xc0},{0x49,0x89,0xc1}
|
|
};
|
|
for(u32 byte=0;byte<3;byte++)
|
|
if(!compiler_emit(compiler,move_argument[i][byte]))return 0;
|
|
}
|
|
for(u32 i=4;i<argument_count;i++) {
|
|
u32 source_displacement=allocation+(argument_count-1u-i)*8u;
|
|
u32 target_displacement=32u+(i-4u)*8u;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit(compiler,0x44)||!compiler_emit(compiler,0x24)||
|
|
!compiler_emit(compiler,(u8)source_displacement)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x44)||!compiler_emit(compiler,0x24)||
|
|
!compiler_emit(compiler,(u8)target_displacement))return 0;
|
|
}
|
|
BOB64_C_CALL *call=&compiler->Calls[compiler->CallCount++];
|
|
usize name_length=0;
|
|
while(name[name_length]&&name_length+1<sizeof(call->Name)) {
|
|
call->Name[name_length]=name[name_length];name_length++;
|
|
}
|
|
if(name[name_length])return 0;
|
|
call->Name[name_length]=0;
|
|
call->ArgumentCount=(u8)argument_count;
|
|
for(u32 i=0;i<argument_count;i++)call->ArgumentTypes[i]=argument_types[i];
|
|
if(!compiler_emit(compiler,0xe8))return 0;
|
|
call->DisplacementOffset=(u16)compiler->CodePosition;
|
|
if(!compiler_emit_u32(compiler,0))return 0;
|
|
u32 cleanup=allocation+argument_count*8u;
|
|
if(cleanup<=127u) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x83)||
|
|
!compiler_emit(compiler,0xc4)||!compiler_emit(compiler,(u8)cleanup))return 0;
|
|
} else if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x81)||
|
|
!compiler_emit(compiler,0xc4)||
|
|
!compiler_emit_u32(compiler,cleanup))return 0;
|
|
/* Argument evaluation and the outgoing call frame are both temporary.
|
|
Restore the depth that was present before evaluating the arguments;
|
|
subtracting cleanup from the tracked depth underflows for ordinary calls. */
|
|
compiler->StackBytes=saved_stack_bytes;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_index_address(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
u8 type=compiler_variable_type(compiler,variable);
|
|
if(compiler_type_is_struct_pointer(type))return 0;
|
|
if(compiler_type_is_array(type)) {
|
|
if(!compiler_variable_address(compiler,variable,2))return 0;
|
|
} else if(compiler_is_pointer(type)) {
|
|
if(!compiler_variable_load(compiler,variable)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0xc2))return 0;
|
|
} else return 0;
|
|
if(!compiler_expression(compiler)||!compiler_take(compiler,']'))return 0;
|
|
if(compiler_is_pointer(compiler->ExpressionType))return 0;
|
|
/* Scale pointer offsets by the pointed-to element width. */
|
|
u8 pointer_type=compiler_type_is_array(type)?compiler_pointer_type(type):type;
|
|
u32 scale=compiler_pointer_scale(compiler,pointer_type);
|
|
if(scale==4) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8d)||
|
|
!compiler_emit(compiler,0x04)||!compiler_emit(compiler,0x82))return 0;
|
|
} else {
|
|
if(!compiler_emit_scale_register(compiler,scale,0)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x01)||
|
|
!compiler_emit(compiler,0xc2)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0xd0))return 0;
|
|
}
|
|
compiler->ExpressionType=pointer_type;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_increment_variable(BOB64_C_COMPILER *compiler,u32 variable,
|
|
int postfix,char operation) {
|
|
u8 type=compiler_variable_type(compiler,variable);
|
|
u32 amount=1;
|
|
if(compiler_type_is_array(type)||compiler_type_is_struct(type))return 0;
|
|
if(compiler_is_pointer(type)) {
|
|
amount=compiler_pointer_scale(compiler,type);
|
|
if(!amount)return 0;
|
|
} else if(type!=COMPILER_TYPE_CHAR&&type!=COMPILER_TYPE_SHORT&&
|
|
type!=COMPILER_TYPE_INT&&type!=COMPILER_TYPE_LONG_LONG&&
|
|
type!=COMPILER_TYPE_USIZE)return 0;
|
|
if(!compiler_variable_load(compiler,variable))return 0;
|
|
if(postfix) {
|
|
if(!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
}
|
|
if(amount==1u) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x83)||
|
|
!compiler_emit(compiler,operation=='+'?0xc0:0xe8)||
|
|
!compiler_emit(compiler,1))return 0;
|
|
} else if(!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,operation=='+'?0x05:0x2d)||
|
|
!compiler_emit_u32(compiler,amount))return 0;
|
|
if(!compiler_variable_store(compiler,variable))return 0;
|
|
if(postfix) {
|
|
if(!compiler_emit(compiler,0x58))return 0;
|
|
compiler->StackBytes-=8;
|
|
} else if(type==COMPILER_TYPE_CHAR||type==COMPILER_TYPE_SHORT) {
|
|
if(!compiler_variable_load(compiler,variable))return 0;
|
|
}
|
|
compiler->ExpressionType=type==COMPILER_TYPE_CHAR||type==COMPILER_TYPE_SHORT?
|
|
COMPILER_TYPE_INT:type;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_increment_pair(BOB64_C_COMPILER *compiler,char *operation) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position+1>=compiler->Length)return 0;
|
|
char first=compiler->Source[compiler->Position];
|
|
if((first!='+'&&first!='-')||compiler->Source[compiler->Position+1]!=first)
|
|
return 0;
|
|
compiler->Position+=2;
|
|
if(operation)*operation=first;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_primary(BOB64_C_COMPILER *compiler) {
|
|
u64 value;
|
|
char name[32];
|
|
if(compiler_take(compiler,'(')) {
|
|
if(!compiler_expression(compiler)||!compiler_take(compiler,')'))return 0;
|
|
return 1;
|
|
}
|
|
if(compiler_integer(compiler,&value)) {
|
|
compiler->ExpressionType=compiler->IntegerType;
|
|
return compiler_emit_integer(compiler,value);
|
|
}
|
|
if(compiler_character(compiler,&value)) {
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
return compiler_emit_integer(compiler,value);
|
|
}
|
|
usize string_offset,string_length;
|
|
if(compiler_string(compiler,&string_offset,&string_length)) {
|
|
(void)string_length;
|
|
s64 displacement=(s64)(BOB64_COMPILER_CODE_CAPACITY+string_offset)-
|
|
(s64)(compiler->CodePosition+7);
|
|
if(displacement<(-2147483647LL-1)||displacement>2147483647||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8d)||
|
|
!compiler_emit(compiler,0x05)||
|
|
!compiler_emit_u32(compiler,(u32)(s32)displacement))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_CHAR_POINTER;
|
|
return 1;
|
|
}
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position<compiler->Length&&compiler->Source[compiler->Position]=='(') {
|
|
int function=compiler_function_find(compiler,name);
|
|
if(compiler_variable_find(compiler,name)>=0||
|
|
!compiler_emit_call(compiler,name))return 0;
|
|
compiler->ExpressionType=function>=0?
|
|
compiler->Functions[function].ReturnType:COMPILER_TYPE_INT;
|
|
if(compiler->ExpressionType==COMPILER_TYPE_VOID)return 0;
|
|
return 1;
|
|
}
|
|
int enumerator=compiler_enum_constant_find(compiler,name);
|
|
if(enumerator>=0) {
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
return compiler_emit_integer(compiler,
|
|
compiler->EnumConstants[enumerator].Value);
|
|
}
|
|
int variable=compiler_variable_find(compiler,name);
|
|
if(variable<0)return 0;
|
|
u8 variable_type=compiler_variable_type(compiler,(u32)variable);
|
|
int pointer_member=compiler_type_is_struct_pointer(variable_type);
|
|
if(compiler_type_is_struct(variable_type)||
|
|
(pointer_member&&compiler->Position+1<compiler->Length&&
|
|
compiler->Source[compiler->Position]=='-'&&
|
|
compiler->Source[compiler->Position+1]=='>')) {
|
|
char field_name[32];
|
|
if((pointer_member?(!compiler_take(compiler,'-')||!compiler_take(compiler,'>')):
|
|
!compiler_take(compiler,'.'))||
|
|
!compiler_identifier(compiler,field_name,sizeof(field_name)))return 0;
|
|
u8 struct_type=pointer_member?
|
|
(u8)(COMPILER_TYPE_STRUCT_BASE+variable_type-COMPILER_TYPE_STRUCT_POINTER_BASE):
|
|
variable_type;
|
|
int field_index=compiler_field_find(compiler,struct_type,field_name);
|
|
if(field_index<0)return 0;
|
|
BOB64_C_FIELD *field=&compiler->Structs[struct_type-COMPILER_TYPE_STRUCT_BASE].Fields[field_index];
|
|
int address_ok=pointer_member?
|
|
(compiler_variable_load(compiler,(u32)variable)&&
|
|
(!field->Offset||(compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_u32(compiler,field->Offset)))):
|
|
compiler_member_address(compiler,(u32)variable,field);
|
|
if(!address_ok||
|
|
!compiler_member_load(compiler,field->Type))return 0;
|
|
compiler->ExpressionType=field->Type;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'[')) {
|
|
if(!compiler_index_address(compiler,(u32)variable))return 0;
|
|
if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_CHAR_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_CHAR_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbe)||!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_CHAR;
|
|
} else if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_SHORT_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_SHORT_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbf)||!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_SHORT;
|
|
} else if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_ISIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_ARRAY||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_LONG_LONG_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=
|
|
(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_ARRAY)?
|
|
COMPILER_TYPE_USIZE:COMPILER_TYPE_LONG_LONG;
|
|
} else {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x63)||
|
|
!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
}
|
|
return 1;
|
|
}
|
|
if(compiler_type_is_array(compiler_variable_type(compiler,(u32)variable))) {
|
|
compiler->ExpressionType=compiler_pointer_type(
|
|
compiler_variable_type(compiler,(u32)variable));
|
|
return compiler_variable_address(compiler,(u32)variable,0);
|
|
}
|
|
char operation;
|
|
if(compiler_increment_pair(compiler,&operation))
|
|
return compiler_increment_variable(compiler,(u32)variable,1,operation);
|
|
compiler->ExpressionType=compiler_variable_type(compiler,(u32)variable);
|
|
return compiler_variable_load(compiler,(u32)variable);
|
|
}
|
|
|
|
static int compiler_unary(BOB64_C_COMPILER *compiler) {
|
|
{
|
|
char operation,name[32];
|
|
if(compiler_increment_pair(compiler,&operation)) {
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int variable=compiler_variable_find(compiler,name);
|
|
if(variable<0||!compiler_increment_variable(compiler,(u32)variable,
|
|
0,operation))return 0;
|
|
return 1;
|
|
}
|
|
}
|
|
if(compiler_word(compiler,"sizeof")) {
|
|
u32 size;
|
|
if(!compiler_take(compiler,'('))return 0;
|
|
int type_result=compiler_sizeof_type(compiler,&size);
|
|
if(!type_result)return 0;
|
|
if(type_result<0&&!compiler_sizeof_expression(compiler,&size))return 0;
|
|
if(!compiler_emit_integer(compiler,size))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_USIZE;
|
|
return 1;
|
|
}
|
|
{
|
|
usize saved_position=compiler->Position;
|
|
if(compiler_take(compiler,'(')) {
|
|
u8 value_type=compiler_type_specifier(compiler);
|
|
int pointer=compiler_take(compiler,'*');
|
|
if(value_type&&compiler_take(compiler,')')) {
|
|
u8 target_type=pointer?compiler_pointer_to(compiler,value_type):value_type;
|
|
if(!target_type||(compiler_type_is_struct(value_type)&&!pointer)||
|
|
(!pointer&&value_type==COMPILER_TYPE_VOID)||
|
|
!compiler_unary(compiler)||
|
|
compiler->ExpressionType==COMPILER_TYPE_VOID||
|
|
compiler_type_is_struct(compiler->ExpressionType))return 0;
|
|
if(!pointer) {
|
|
if(target_type==COMPILER_TYPE_CHAR) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbe)||!compiler_emit(compiler,0xc0))
|
|
return 0;
|
|
} else if(target_type==COMPILER_TYPE_SHORT) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbf)||!compiler_emit(compiler,0xc0))
|
|
return 0;
|
|
} else if(target_type==COMPILER_TYPE_INT) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x63)||
|
|
!compiler_emit(compiler,0xc0))return 0;
|
|
} else if(target_type!=COMPILER_TYPE_LONG_LONG&&
|
|
target_type!=COMPILER_TYPE_USIZE)return 0;
|
|
}
|
|
compiler->ExpressionType=target_type;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved_position;
|
|
}
|
|
}
|
|
if(compiler_take(compiler,'&')) {
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int variable=compiler_variable_find(compiler,name);
|
|
u8 type=variable>=0?compiler_variable_type(compiler,(u32)variable):0;
|
|
if(variable<0||(type!=COMPILER_TYPE_INT&&type!=COMPILER_TYPE_CHAR&&
|
|
type!=COMPILER_TYPE_SHORT&&type!=COMPILER_TYPE_LONG_LONG&&
|
|
type!=COMPILER_TYPE_USIZE&&type!=COMPILER_TYPE_INT_ARRAY&&
|
|
type!=COMPILER_TYPE_CHAR_ARRAY&&type!=COMPILER_TYPE_SHORT_ARRAY&&
|
|
type!=COMPILER_TYPE_LONG_LONG_ARRAY&&type!=COMPILER_TYPE_USIZE_ARRAY&&
|
|
!compiler_type_is_struct(type)))return 0;
|
|
compiler->ExpressionType=compiler_type_is_struct(type)?
|
|
(u8)(COMPILER_TYPE_STRUCT_POINTER_BASE+type-COMPILER_TYPE_STRUCT_BASE):
|
|
compiler_pointer_type(type==COMPILER_TYPE_CHAR_ARRAY?COMPILER_TYPE_CHAR:
|
|
(type==COMPILER_TYPE_SHORT_ARRAY?COMPILER_TYPE_SHORT:type));
|
|
return compiler_variable_address(compiler,(u32)variable,0);
|
|
}
|
|
if(compiler_take(compiler,'*')) {
|
|
if(!compiler_unary(compiler)||!compiler_is_pointer(compiler->ExpressionType)||
|
|
compiler->ExpressionType==COMPILER_TYPE_VOID_POINTER||
|
|
compiler_type_is_struct_pointer(compiler->ExpressionType))
|
|
return 0;
|
|
if(compiler->ExpressionType==COMPILER_TYPE_CHAR_POINTER) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbe)||!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_CHAR;
|
|
} else if(compiler->ExpressionType==COMPILER_TYPE_SHORT_POINTER) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbf)||!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_SHORT;
|
|
} else if(compiler->ExpressionType==COMPILER_TYPE_USIZE_POINTER||
|
|
compiler->ExpressionType==COMPILER_TYPE_ISIZE_POINTER) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=compiler->ExpressionType==COMPILER_TYPE_USIZE_POINTER?
|
|
COMPILER_TYPE_USIZE:COMPILER_TYPE_LONG_LONG;
|
|
} else {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x63)||
|
|
!compiler_emit(compiler,0x00))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
}
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'!')) {
|
|
if(!compiler_unary(compiler)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x85)||!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit(compiler,0x0f)||!compiler_emit(compiler,0x94)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xb6)||!compiler_emit(compiler,0xc0))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'~')) {
|
|
if(!compiler_unary(compiler)||compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xf7)||
|
|
!compiler_emit(compiler,0xd0))return 0;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'-')) {
|
|
if(!compiler_unary(compiler)||compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xf7)||
|
|
!compiler_emit(compiler,0xd8))return 0;
|
|
if(compiler->ExpressionType!=COMPILER_TYPE_USIZE)
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
return 1;
|
|
}
|
|
return compiler_primary(compiler);
|
|
}
|
|
|
|
static int compiler_term(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_unary(compiler))return 0;
|
|
for(;;) {
|
|
u8 left_type=compiler->ExpressionType;
|
|
char operation;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length)return 1;
|
|
operation=compiler->Source[compiler->Position];
|
|
if(operation!='*'&&operation!='/'&&operation!='%')return 1;
|
|
compiler->Position++;
|
|
if(compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_unary(compiler)||
|
|
compiler_is_pointer(compiler->ExpressionType))return 0;
|
|
u8 right_type=compiler->ExpressionType;
|
|
if(operation=='*') {
|
|
if(!compiler_emit(compiler,0x59)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xaf)||!compiler_emit(compiler,0xc1))return 0;
|
|
compiler->StackBytes-=8;
|
|
} else {
|
|
int unsigned_operation=left_type==COMPILER_TYPE_USIZE||
|
|
right_type==COMPILER_TYPE_USIZE;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0xc1)||!compiler_emit(compiler,0x58))return 0;
|
|
compiler->StackBytes-=8;
|
|
if(unsigned_operation) {
|
|
if(!compiler_emit(compiler,0x31)||!compiler_emit(compiler,0xd2)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xf7)||
|
|
!compiler_emit(compiler,0xf1))return 0;
|
|
} else if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x99)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xf7)||
|
|
!compiler_emit(compiler,0xf9))return 0;
|
|
if(operation=='%'&&
|
|
(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0xd0)))return 0;
|
|
}
|
|
compiler->ExpressionType=left_type==COMPILER_TYPE_USIZE||
|
|
right_type==COMPILER_TYPE_USIZE?
|
|
COMPILER_TYPE_USIZE:
|
|
(left_type==COMPILER_TYPE_LONG_LONG||
|
|
right_type==COMPILER_TYPE_LONG_LONG?
|
|
COMPILER_TYPE_LONG_LONG:COMPILER_TYPE_INT);
|
|
}
|
|
}
|
|
|
|
static int compiler_additive(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_term(compiler))return 0;
|
|
for(;;) {
|
|
char operation;
|
|
u8 left_type=compiler->ExpressionType,right_type;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length)return 1;
|
|
operation=compiler->Source[compiler->Position];
|
|
if(operation!='+'&&operation!='-')return 1;
|
|
compiler->Position++;
|
|
if(!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_term(compiler))return 0;
|
|
right_type=compiler->ExpressionType;
|
|
if(compiler_is_pointer(left_type)&&compiler_is_pointer(right_type)) {
|
|
if(!compiler_pointer_types_compatible(left_type,right_type)||
|
|
left_type==COMPILER_TYPE_VOID_POINTER||
|
|
right_type==COMPILER_TYPE_VOID_POINTER)return 0;
|
|
if(operation!='-'||!compiler_emit(compiler,0x59)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x29)||
|
|
!compiler_emit(compiler,0xc1)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0xc8))return 0;
|
|
u32 scale=compiler_pointer_scale(compiler,left_type);
|
|
if(scale==2||scale==4||scale==8) {
|
|
u8 shift=scale==2?1:(scale==4?2:3);
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xc1)||
|
|
!compiler_emit(compiler,0xf8)||!compiler_emit(compiler,shift))return 0;
|
|
} else if(scale!=1&&
|
|
(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x99)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xb9)||
|
|
!compiler_emit_u64(compiler,scale)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0xf7)||!compiler_emit(compiler,0xf9)))return 0;
|
|
compiler->StackBytes-=8;
|
|
compiler->ExpressionType=COMPILER_TYPE_LONG_LONG;
|
|
continue;
|
|
}
|
|
if(compiler_is_pointer(left_type)) {
|
|
if(left_type==COMPILER_TYPE_VOID_POINTER)return 0;
|
|
if(!compiler_emit_scale_register(compiler,
|
|
compiler_pointer_scale(compiler,left_type),0))return 0;
|
|
} else if(compiler_is_pointer(right_type)) {
|
|
if(right_type==COMPILER_TYPE_VOID_POINTER||operation!='+'||!compiler_emit(compiler,0x59)||
|
|
!compiler_emit_scale_register(compiler,
|
|
compiler_pointer_scale(compiler,right_type),1)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x01)||
|
|
!compiler_emit(compiler,0xc8))return 0;
|
|
compiler->StackBytes-=8;
|
|
compiler->ExpressionType=right_type;
|
|
continue;
|
|
}
|
|
if(!compiler_emit(compiler,0x59))return 0;
|
|
compiler->StackBytes-=8;
|
|
if(operation=='+') {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x01)||
|
|
!compiler_emit(compiler,0xc8))return 0;
|
|
} else if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x29)||
|
|
!compiler_emit(compiler,0xc1)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0xc8))return 0;
|
|
compiler->ExpressionType=compiler_is_pointer(left_type)?left_type:
|
|
(left_type==COMPILER_TYPE_USIZE||right_type==COMPILER_TYPE_USIZE?
|
|
COMPILER_TYPE_USIZE:
|
|
(left_type==COMPILER_TYPE_LONG_LONG||right_type==COMPILER_TYPE_LONG_LONG?
|
|
COMPILER_TYPE_LONG_LONG:COMPILER_TYPE_INT));
|
|
}
|
|
}
|
|
|
|
typedef int (*COMPILER_EXPRESSION_PARSER)(BOB64_C_COMPILER *compiler);
|
|
|
|
static int compiler_shift(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_additive(compiler))return 0;
|
|
for(;;) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position+1>=compiler->Length||
|
|
(compiler->Source[compiler->Position]!='<'&&
|
|
compiler->Source[compiler->Position]!='>')||
|
|
compiler->Source[compiler->Position+1]!=compiler->Source[compiler->Position])
|
|
return 1;
|
|
char operation=compiler->Source[compiler->Position];
|
|
u8 left_type=compiler->ExpressionType;
|
|
compiler->Position+=2;
|
|
if(compiler_is_pointer(left_type)||!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_additive(compiler)||compiler_is_pointer(compiler->ExpressionType))return 0;
|
|
u8 right_type=compiler->ExpressionType;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0xc1)||!compiler_emit(compiler,0x58))return 0;
|
|
compiler->StackBytes-=8;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xd3)||
|
|
!compiler_emit(compiler,operation=='<'?0xe0:
|
|
(left_type==COMPILER_TYPE_USIZE?0xe8:0xf8)))return 0;
|
|
compiler->ExpressionType=left_type==COMPILER_TYPE_USIZE||
|
|
right_type==COMPILER_TYPE_USIZE?COMPILER_TYPE_USIZE:
|
|
(left_type==COMPILER_TYPE_LONG_LONG||right_type==COMPILER_TYPE_LONG_LONG?
|
|
COMPILER_TYPE_LONG_LONG:COMPILER_TYPE_INT);
|
|
}
|
|
}
|
|
|
|
static int compiler_compare_level(BOB64_C_COMPILER *compiler,
|
|
COMPILER_EXPRESSION_PARSER lower,int equality) {
|
|
typedef struct { char First,Second;u8 Length,SetCondition,UnsignedCondition; } OP;
|
|
static const OP equal_ops[]={
|
|
{'=', '=',2,0x94,0x94},{'!', '=',2,0x95,0x95}
|
|
};
|
|
static const OP relational_ops[]={
|
|
{'<', '=',2,0x9e,0x96},{'>', '=',2,0x9d,0x93},
|
|
{'<', 0,1,0x9c,0x92},{'>', 0,1,0x9f,0x97}
|
|
};
|
|
const OP *operations=equality?equal_ops:relational_ops;
|
|
u32 count=equality?2:4;
|
|
if(!lower(compiler))return 0;
|
|
for(;;) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length)return 1;
|
|
int operation=-1;
|
|
for(u32 i=0;i<count;i++) {
|
|
const OP *candidate=&operations[i];
|
|
if(compiler->Source[compiler->Position]!=candidate->First)continue;
|
|
if(candidate->Length==2) {
|
|
if(compiler->Position+1>=compiler->Length||
|
|
compiler->Source[compiler->Position+1]!=candidate->Second)continue;
|
|
} else if(compiler->Position+1<compiler->Length&&
|
|
compiler->Source[compiler->Position+1]=='=')continue;
|
|
operation=(int)i;break;
|
|
}
|
|
if(operation<0)return 1;
|
|
u8 left_type=compiler->ExpressionType;
|
|
compiler->Position+=operations[operation].Length;
|
|
if(!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!lower(compiler))return 0;
|
|
u8 right_type=compiler->ExpressionType;
|
|
int pointer_comparison=compiler_is_pointer(left_type)||
|
|
compiler_is_pointer(right_type);
|
|
if(pointer_comparison&&(!compiler_pointer_types_compatible(left_type,right_type)||
|
|
!compiler_is_pointer(left_type)||!equality))return 0;
|
|
u8 condition=left_type==COMPILER_TYPE_USIZE||right_type==COMPILER_TYPE_USIZE?
|
|
operations[operation].UnsignedCondition:operations[operation].SetCondition;
|
|
if(!compiler_emit(compiler,0x59)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x39)||!compiler_emit(compiler,0xc1)||
|
|
!compiler_emit(compiler,0x0f)||!compiler_emit(compiler,condition)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xb6)||!compiler_emit(compiler,0xc0))return 0;
|
|
compiler->StackBytes-=8;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
}
|
|
}
|
|
|
|
static int compiler_relational(BOB64_C_COMPILER *compiler) {
|
|
return compiler_compare_level(compiler,compiler_shift,0);
|
|
}
|
|
|
|
static int compiler_equality(BOB64_C_COMPILER *compiler) {
|
|
return compiler_compare_level(compiler,compiler_relational,1);
|
|
}
|
|
|
|
static int compiler_bitwise_level(BOB64_C_COMPILER *compiler,
|
|
COMPILER_EXPRESSION_PARSER lower,char operation,u8 opcode) {
|
|
if(!lower(compiler))return 0;
|
|
for(;;) {
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length||
|
|
compiler->Source[compiler->Position]!=operation)return 1;
|
|
if((operation=='&'||operation=='|')&&
|
|
compiler->Position+1<compiler->Length&&
|
|
compiler->Source[compiler->Position+1]==operation)return 1;
|
|
u8 left_type=compiler->ExpressionType;
|
|
if(compiler_is_pointer(left_type))return 0;
|
|
compiler->Position++;
|
|
if(!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!lower(compiler)||compiler_is_pointer(compiler->ExpressionType))return 0;
|
|
u8 right_type=compiler->ExpressionType;
|
|
if(!compiler_emit(compiler,0x59)||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,opcode)||!compiler_emit(compiler,0xc8))return 0;
|
|
compiler->StackBytes-=8;
|
|
compiler->ExpressionType=left_type==COMPILER_TYPE_USIZE||
|
|
right_type==COMPILER_TYPE_USIZE?COMPILER_TYPE_USIZE:
|
|
(left_type==COMPILER_TYPE_LONG_LONG||right_type==COMPILER_TYPE_LONG_LONG?
|
|
COMPILER_TYPE_LONG_LONG:COMPILER_TYPE_INT);
|
|
}
|
|
}
|
|
|
|
static int compiler_bitwise_and(BOB64_C_COMPILER *compiler) {
|
|
return compiler_bitwise_level(compiler,compiler_equality,'&',0x21);
|
|
}
|
|
|
|
static int compiler_bitwise_xor(BOB64_C_COMPILER *compiler) {
|
|
return compiler_bitwise_level(compiler,compiler_bitwise_and,'^',0x31);
|
|
}
|
|
|
|
static int compiler_bitwise_or(BOB64_C_COMPILER *compiler) {
|
|
return compiler_bitwise_level(compiler,compiler_bitwise_xor,'|',0x09);
|
|
}
|
|
|
|
static int compiler_logical_and(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_bitwise_or(compiler))return 0;
|
|
for(;;) {
|
|
u16 false_branch,end_branch;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position+1>=compiler->Length||
|
|
compiler->Source[compiler->Position]!='&'||
|
|
compiler->Source[compiler->Position+1]!='&')return 1;
|
|
compiler->Position+=2;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x84,&false_branch)||
|
|
!compiler_bitwise_or(compiler)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0x95)||!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit(compiler,0x0f)||!compiler_emit(compiler,0xb6)||
|
|
!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit_branch(compiler,0xe9,&end_branch)||
|
|
!compiler_patch_branch(compiler,false_branch,compiler->CodePosition)||
|
|
!compiler_emit(compiler,0x31)||!compiler_emit(compiler,0xc0)||
|
|
!compiler_patch_branch(compiler,end_branch,compiler->CodePosition))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
}
|
|
}
|
|
|
|
static int compiler_expression(BOB64_C_COMPILER *compiler) {
|
|
if(!compiler_logical_and(compiler))return 0;
|
|
for(;;) {
|
|
u16 true_branch,end_branch;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position+1>=compiler->Length||
|
|
compiler->Source[compiler->Position]!='|'||
|
|
compiler->Source[compiler->Position+1]!='|')return 1;
|
|
compiler->Position+=2;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x85,&true_branch)||
|
|
!compiler_logical_and(compiler)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0x95)||!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit(compiler,0x0f)||!compiler_emit(compiler,0xb6)||
|
|
!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit_branch(compiler,0xe9,&end_branch)||
|
|
!compiler_patch_branch(compiler,true_branch,compiler->CodePosition)||
|
|
!compiler_emit(compiler,0xb8)||!compiler_emit_u32(compiler,1)||
|
|
!compiler_patch_branch(compiler,end_branch,compiler->CodePosition))return 0;
|
|
compiler->ExpressionType=COMPILER_TYPE_INT;
|
|
}
|
|
}
|
|
|
|
static int compiler_array_element_address(BOB64_C_COMPILER *compiler,
|
|
u32 variable,u32 element) {
|
|
u32 type=compiler->VariableType[variable];
|
|
u32 scale=compiler_array_element_size((u8)type);
|
|
s16 displacement=(s16)(compiler->VariableDisplacement[variable]+(s16)(element*scale));
|
|
if(element>=compiler->VariableArrayLength[variable])return 0;
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x8d)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
}
|
|
|
|
static int compiler_array_initializer(BOB64_C_COMPILER *compiler,u32 variable) {
|
|
u32 element=0;
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_CHAR_ARRAY) {
|
|
usize offset,length;
|
|
if(compiler_string(compiler,&offset,&length)) {
|
|
u32 array_length=compiler_variable_array_length(compiler,variable);
|
|
if(length>array_length)return 0;
|
|
for(u32 i=0;i<array_length;i++) {
|
|
u8 value=i<length?compiler->Data[offset+i]:0;
|
|
if(!compiler_array_element_address(compiler,variable,i)||
|
|
!compiler_emit(compiler,0xc6)||
|
|
!compiler_emit_rbp_operand(compiler,0x45,
|
|
(s16)(compiler->VariableDisplacement[variable]+(s16)i))||
|
|
!compiler_emit(compiler,value))return 0;
|
|
}
|
|
return compiler_take(compiler,';');
|
|
}
|
|
}
|
|
if(!compiler_take(compiler,'{'))return 0;
|
|
for(;;) {
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(element>=compiler_variable_array_length(compiler,variable)||
|
|
!compiler_array_element_address(compiler,variable,element)||
|
|
!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_expression(compiler)||
|
|
compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_emit(compiler,0x59))return 0;
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_CHAR_ARRAY) {
|
|
if(!compiler_emit(compiler,0x88)||!compiler_emit(compiler,0x01))return 0;
|
|
} else if(compiler->VariableType[variable]==COMPILER_TYPE_SHORT_ARRAY) {
|
|
if(!compiler_emit(compiler,0x66)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return 0;
|
|
} else if(compiler->VariableType[variable]==COMPILER_TYPE_LONG_LONG_ARRAY||
|
|
compiler->VariableType[variable]==COMPILER_TYPE_USIZE_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return 0;
|
|
} else if(!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0x01))return 0;
|
|
compiler->StackBytes-=8;
|
|
element++;
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(!compiler_take(compiler,','))return 0;
|
|
}
|
|
while(element<compiler_variable_array_length(compiler,variable)) {
|
|
if(!compiler_array_element_address(compiler,variable,element++)||
|
|
!compiler_emit(compiler,0x50))return 0;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_emit(compiler,0x31)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x59))return 0;
|
|
if(compiler->VariableType[variable]==COMPILER_TYPE_CHAR_ARRAY) {
|
|
if(!compiler_emit(compiler,0x88)||!compiler_emit(compiler,0x01))return 0;
|
|
} else if(compiler->VariableType[variable]==COMPILER_TYPE_SHORT_ARRAY) {
|
|
if(!compiler_emit(compiler,0x66)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return 0;
|
|
} else if(compiler->VariableType[variable]==COMPILER_TYPE_LONG_LONG_ARRAY||
|
|
compiler->VariableType[variable]==COMPILER_TYPE_USIZE_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return 0;
|
|
} else if(!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0x01))return 0;
|
|
compiler->StackBytes-=8;
|
|
}
|
|
return compiler_take(compiler,';');
|
|
}
|
|
|
|
/* Return 1 for a consumed local declaration/assignment, 0 for another statement. */
|
|
static int compiler_local_statement(BOB64_C_COMPILER *compiler) {
|
|
usize saved=compiler->Position;
|
|
char name[32];
|
|
u8 declared_type=compiler_type_specifier(compiler);
|
|
int is_char=0,is_short=0,is_pointer=0,is_struct=0,variable;
|
|
if(!declared_type) {
|
|
compiler->Position=saved;
|
|
if(compiler_take(compiler,'*')) {
|
|
if(!compiler_identifier(compiler,name,sizeof(name))) {
|
|
compiler->Position=saved;return 0;
|
|
}
|
|
variable=compiler_variable_find(compiler,name);
|
|
if(variable<0||!compiler_is_pointer(compiler_variable_type(compiler,(u32)variable))||
|
|
!compiler_take(compiler,'=')) {
|
|
compiler->Position=saved;return 0;
|
|
}
|
|
if(!compiler_variable_load(compiler,(u32)variable)||
|
|
!compiler_emit(compiler,0x50))return -1;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_expression(compiler)||
|
|
compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_take(compiler,';')||!compiler_emit(compiler,0x59)||
|
|
(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_CHAR_POINTER?
|
|
(!compiler_emit(compiler,0x88)||!compiler_emit(compiler,0x01)):
|
|
(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_SHORT_POINTER?
|
|
(!compiler_emit(compiler,0x66)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01)):
|
|
((compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_ISIZE_POINTER)?
|
|
(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01)):
|
|
(!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0x01))))))return -1;
|
|
compiler->StackBytes-=8;
|
|
return 1;
|
|
}
|
|
if(!compiler_identifier(compiler,name,sizeof(name))) {
|
|
compiler->Position=saved;return 0;
|
|
}
|
|
variable=compiler_variable_find(compiler,name);
|
|
if(variable<0) {
|
|
compiler->Position=saved;return 0;
|
|
}
|
|
u8 variable_type=compiler_variable_type(compiler,(u32)variable);
|
|
char increment_operation;
|
|
if(compiler_increment_pair(compiler,&increment_operation)) {
|
|
if(!compiler_increment_variable(compiler,(u32)variable,1,
|
|
increment_operation)||
|
|
!compiler_take(compiler,';'))return -1;
|
|
return 1;
|
|
}
|
|
int pointer_member=compiler_type_is_struct_pointer(variable_type);
|
|
if(compiler_type_is_struct(variable_type)||pointer_member) {
|
|
char field_name[32];
|
|
if((pointer_member?(!compiler_take(compiler,'-')||!compiler_take(compiler,'>')):
|
|
!compiler_take(compiler,'.'))||
|
|
!compiler_identifier(compiler,field_name,sizeof(field_name)))return -1;
|
|
u8 struct_type=pointer_member?
|
|
(u8)(COMPILER_TYPE_STRUCT_BASE+variable_type-COMPILER_TYPE_STRUCT_POINTER_BASE):
|
|
variable_type;
|
|
int field_index=compiler_field_find(compiler,struct_type,field_name);
|
|
if(field_index<0)return -1;
|
|
BOB64_C_FIELD *field=&compiler->Structs[
|
|
struct_type-COMPILER_TYPE_STRUCT_BASE].Fields[field_index];
|
|
int address_ok=pointer_member?
|
|
(compiler_variable_load(compiler,(u32)variable)&&
|
|
(!field->Offset||(compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x05)&&
|
|
compiler_emit_u32(compiler,field->Offset)))):
|
|
compiler_member_address(compiler,(u32)variable,field);
|
|
if(!compiler_take(compiler,'=')||!address_ok||
|
|
!compiler_emit(compiler,0x50))return -1;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_expression(compiler)||
|
|
!compiler_value_types_compatible(field->Type,compiler->ExpressionType)||
|
|
!compiler_take(compiler,';')||!compiler_emit(compiler,0x59)||
|
|
!compiler_member_store(compiler,field->Type))return -1;
|
|
compiler->StackBytes-=8;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'[')) {
|
|
if((!compiler_type_is_array(compiler_variable_type(compiler,(u32)variable))&&
|
|
!compiler_is_pointer(compiler_variable_type(compiler,(u32)variable)))||
|
|
!compiler_index_address(compiler,(u32)variable)||
|
|
!compiler_take(compiler,'=')||!compiler_emit(compiler,0x50))return -1;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_expression(compiler)||
|
|
compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_take(compiler,';')||!compiler_emit(compiler,0x59))return -1;
|
|
if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_CHAR_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_CHAR_ARRAY) {
|
|
if(!compiler_emit(compiler,0x88)||!compiler_emit(compiler,0x01))return -1;
|
|
} else if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_SHORT_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_SHORT_ARRAY) {
|
|
if(!compiler_emit(compiler,0x66)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return -1;
|
|
} else if(compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_ISIZE_POINTER||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_USIZE_ARRAY||
|
|
compiler_variable_type(compiler,(u32)variable)==COMPILER_TYPE_LONG_LONG_ARRAY) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0x01))return -1;
|
|
} else if(!compiler_emit(compiler,0x89)||!compiler_emit(compiler,0x01))return -1;
|
|
compiler->StackBytes-=8;
|
|
return 1;
|
|
}
|
|
if(compiler_type_is_array(compiler_variable_type(compiler,(u32)variable))||
|
|
!compiler_take(compiler,'=')) {
|
|
compiler->Position=saved;return 0;
|
|
}
|
|
if(!compiler_expression(compiler)||
|
|
!compiler_value_types_compatible(
|
|
compiler_variable_type(compiler,(u32)variable),
|
|
compiler->ExpressionType)||
|
|
!compiler_take(compiler,';')||
|
|
!compiler_variable_store(compiler,(u32)variable))return -1;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'*')) {
|
|
declared_type=compiler_pointer_to(compiler,declared_type);
|
|
if(!declared_type)return -1;
|
|
}
|
|
if(declared_type==COMPILER_TYPE_VOID)return -1;
|
|
is_struct=compiler_type_is_struct(declared_type);
|
|
is_pointer=compiler_is_pointer(declared_type);
|
|
is_char=declared_type==COMPILER_TYPE_CHAR;
|
|
is_short=declared_type==COMPILER_TYPE_SHORT;
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return -1;
|
|
if(is_struct) {
|
|
if(!compiler_take(compiler,';')||
|
|
!compiler_add_variable(compiler,name,declared_type,0,0))return -1;
|
|
return 1;
|
|
}
|
|
if(compiler_take(compiler,'[')) {
|
|
u64 array_length;
|
|
u8 array_type=compiler_array_type(declared_type);
|
|
if(!array_type)return -1;
|
|
u32 array_limit=is_char?COMPILER_LOCAL_FRAME_SIZE:
|
|
(is_short?COMPILER_LOCAL_FRAME_SIZE/2u:
|
|
((array_type==COMPILER_TYPE_LONG_LONG_ARRAY||
|
|
array_type==COMPILER_TYPE_USIZE_ARRAY)?
|
|
COMPILER_LOCAL_FRAME_SIZE/8u:COMPILER_LOCAL_FRAME_SIZE/4u));
|
|
if(is_pointer||!compiler_integer(compiler,&array_length)||
|
|
array_length>array_limit||!compiler_take(compiler,']')||
|
|
!compiler_add_variable(compiler,name,array_type,
|
|
(u32)array_length,0))return -1;
|
|
variable=compiler_variable_find(compiler,name);
|
|
if(compiler_take(compiler,';'))return 1;
|
|
if(!compiler_take(compiler,'=')||
|
|
!compiler_array_initializer(compiler,(u32)variable))return -1;
|
|
return 1;
|
|
}
|
|
u8 type=declared_type;
|
|
if(!compiler_add_variable(compiler,name,type,0,0))return -1;
|
|
variable=compiler_variable_find(compiler,name);
|
|
if(compiler_take(compiler,';'))return 1;
|
|
if(!compiler_take(compiler,'='))return -1;
|
|
if(!compiler_expression(compiler)||
|
|
!compiler_value_types_compatible(type,compiler->ExpressionType)||
|
|
!compiler_take(compiler,';')||
|
|
!compiler_variable_store(compiler,(u32)variable))return -1;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_function_prologue(BOB64_C_COMPILER *compiler) {
|
|
return compiler_emit(compiler,0x55)&&compiler_emit(compiler,0x48)&&
|
|
compiler_emit(compiler,0x89)&&compiler_emit(compiler,0xe5)&&
|
|
compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x81)&&
|
|
compiler_emit(compiler,0xec)&&
|
|
compiler_emit_u32(compiler,COMPILER_LOCAL_FRAME_SIZE);
|
|
}
|
|
|
|
static int compiler_function_epilogue(BOB64_C_COMPILER *compiler) {
|
|
if(compiler->ReturnType==COMPILER_TYPE_CHAR) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbe)||!compiler_emit(compiler,0xc0))return 0;
|
|
} else if(compiler->ReturnType==COMPILER_TYPE_SHORT) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit(compiler,0xbf)||!compiler_emit(compiler,0xc0))return 0;
|
|
} else if(compiler->ReturnType!=COMPILER_TYPE_VOID&&!compiler->Return64&&
|
|
(!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x63)||!compiler_emit(compiler,0xc0)))return 0;
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit(compiler,0xec)&&compiler_emit(compiler,0x5d)&&
|
|
compiler_emit(compiler,0xc3);
|
|
}
|
|
|
|
static int compiler_return_type_matches(BOB64_C_COMPILER *compiler) {
|
|
if(compiler->ReturnType==COMPILER_TYPE_VOID||
|
|
compiler->ExpressionType==COMPILER_TYPE_VOID)return 0;
|
|
int result_pointer=compiler_is_pointer(compiler->ReturnType);
|
|
int expression_pointer=compiler_is_pointer(compiler->ExpressionType);
|
|
if(result_pointer)return expression_pointer&&
|
|
compiler_pointer_types_compatible(compiler->ReturnType,compiler->ExpressionType);
|
|
return !expression_pointer||compiler->Return64;
|
|
}
|
|
|
|
static int compiler_statement(BOB64_C_COMPILER *compiler);
|
|
|
|
static int compiler_emit_branch(BOB64_C_COMPILER *compiler,u8 opcode,
|
|
u16 *displacement_offset) {
|
|
if(!compiler_emit(compiler,opcode))return 0;
|
|
*displacement_offset=(u16)compiler->CodePosition;
|
|
return compiler_emit_u32(compiler,0);
|
|
}
|
|
|
|
static int compiler_patch_branch(BOB64_C_COMPILER *compiler,u16 displacement_offset,
|
|
usize target) {
|
|
s64 displacement=(s64)target-(s64)(displacement_offset+4u);
|
|
if(displacement<(-2147483647LL-1)||displacement>2147483647||
|
|
displacement_offset+4u>compiler->CodePosition)return 0;
|
|
u32 encoded=(u32)(s32)displacement;
|
|
for(u32 byte=0;byte<4;byte++)
|
|
compiler->Code[displacement_offset+byte]=(u8)(encoded>>(byte*8));
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_loop_push(BOB64_C_COMPILER *compiler,usize continue_target) {
|
|
if(compiler->ControlCount>=sizeof(compiler->Controls)/sizeof(compiler->Controls[0])||
|
|
continue_target>0xffffu)return 0;
|
|
BOB64_C_CONTROL *loop=&compiler->Controls[compiler->ControlCount++];
|
|
loop->Kind=COMPILER_CONTROL_LOOP;
|
|
loop->ContinueTarget=(u16)continue_target;
|
|
loop->BreakCount=0;loop->ContinueCount=0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_loop_finish(BOB64_C_COMPILER *compiler,usize break_target) {
|
|
if(!compiler->ControlCount||
|
|
compiler->Controls[compiler->ControlCount-1].Kind!=COMPILER_CONTROL_LOOP)
|
|
return 0;
|
|
BOB64_C_CONTROL *loop=&compiler->Controls[--compiler->ControlCount];
|
|
for(u32 i=0;i<loop->BreakCount;i++)
|
|
if(!compiler_patch_branch(compiler,loop->BreakBranches[i],break_target))return 0;
|
|
for(u32 i=0;i<loop->ContinueCount;i++)
|
|
if(!compiler_patch_branch(compiler,loop->ContinueBranches[i],loop->ContinueTarget))return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_switch_push(BOB64_C_COMPILER *compiler,
|
|
BOB64_C_CONTROL **control) {
|
|
if(compiler->ControlCount>=sizeof(compiler->Controls)/sizeof(compiler->Controls[0]))
|
|
return 0;
|
|
BOB64_C_CONTROL *entry=&compiler->Controls[compiler->ControlCount++];
|
|
entry->Kind=COMPILER_CONTROL_SWITCH;entry->BreakCount=0;
|
|
entry->ContinueCount=0;entry->CaseCount=0;entry->HasDefault=0;
|
|
*control=entry;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_switch_finish(BOB64_C_COMPILER *compiler,
|
|
usize break_target) {
|
|
if(!compiler->ControlCount||
|
|
compiler->Controls[compiler->ControlCount-1].Kind!=COMPILER_CONTROL_SWITCH)
|
|
return 0;
|
|
BOB64_C_CONTROL *control=&compiler->Controls[--compiler->ControlCount];
|
|
for(u32 i=0;i<control->BreakCount;i++)
|
|
if(!compiler_patch_branch(compiler,control->BreakBranches[i],break_target))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_switch_temporary(BOB64_C_COMPILER *compiler,u8 type,
|
|
u32 *variable) {
|
|
for(u32 candidate=0;candidate<100;candidate++) {
|
|
char name[16]={'_','b','6','4','s','w',
|
|
(char)('0'+candidate/10),(char)('0'+candidate%10),0};
|
|
if(compiler_variable_find(compiler,name)<0) {
|
|
if(!compiler_add_variable(compiler,name,type,0,0))return 0;
|
|
int found=compiler_variable_find(compiler,name);
|
|
if(found<0||found>=0x8000)return 0;
|
|
*variable=(u32)found;
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int compiler_switch_constant(BOB64_C_COMPILER *compiler,u64 *value) {
|
|
int negative=compiler_take(compiler,'-');
|
|
if(!negative)compiler_take(compiler,'+');
|
|
if(compiler_character(compiler,value)) {
|
|
if(negative)*value=0u-*value;
|
|
return 1;
|
|
}
|
|
if(!compiler_integer(compiler,value)) {
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int enumerator=compiler_enum_constant_find(compiler,name);
|
|
if(enumerator<0)return 0;
|
|
*value=compiler->EnumConstants[enumerator].Value;
|
|
if(negative)*value=0u-*value;
|
|
return 1;
|
|
}
|
|
if(negative) {
|
|
if(*value>0x8000000000000000ULL)return 0;
|
|
*value=0u-*value;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_statement(BOB64_C_COMPILER *compiler) {
|
|
usize saved=compiler->Position;
|
|
{
|
|
char operation,name[32];
|
|
if(compiler_increment_pair(compiler,&operation)) {
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return -1;
|
|
int variable=compiler_variable_find(compiler,name);
|
|
if(variable<0||!compiler_increment_variable(compiler,(u32)variable,
|
|
0,operation)||
|
|
!compiler_take(compiler,';'))return -1;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
}
|
|
if(compiler_word(compiler,"return")) {
|
|
if(compiler->ReturnType==COMPILER_TYPE_VOID) {
|
|
if(compiler->StackBytes||!compiler_take(compiler,';')||
|
|
!compiler_function_epilogue(compiler))return -1;
|
|
return 1;
|
|
}
|
|
if(compiler->StackBytes||!compiler_expression(compiler)||
|
|
!compiler_return_type_matches(compiler)||
|
|
!compiler_take(compiler,';')||!compiler_function_epilogue(compiler))return -1;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"break")) {
|
|
if(!compiler->ControlCount||!compiler_take(compiler,';'))return -1;
|
|
BOB64_C_CONTROL *control=&compiler->Controls[compiler->ControlCount-1];
|
|
if(control->BreakCount>=sizeof(control->BreakBranches)/
|
|
sizeof(control->BreakBranches[0])||
|
|
!compiler_emit_branch(compiler,0xe9,
|
|
&control->BreakBranches[control->BreakCount]))return -1;
|
|
control->BreakCount++;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"continue")) {
|
|
BOB64_C_CONTROL *loop=0;
|
|
for(u32 depth=compiler->ControlCount;depth>0;depth--)
|
|
if(compiler->Controls[depth-1].Kind==COMPILER_CONTROL_LOOP) {
|
|
loop=&compiler->Controls[depth-1];break;
|
|
}
|
|
if(!loop||!compiler_take(compiler,';'))return -1;
|
|
if(loop->ContinueCount>=sizeof(loop->ContinueBranches)/
|
|
sizeof(loop->ContinueBranches[0])||
|
|
!compiler_emit_branch(compiler,0xe9,
|
|
&loop->ContinueBranches[loop->ContinueCount]))return -1;
|
|
loop->ContinueCount++;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"if")) {
|
|
u16 false_branch,end_branch;
|
|
if(compiler->ControlDepth>=32||!compiler_take(compiler,'(')||
|
|
!compiler_expression(compiler)||!compiler_take(compiler,')')||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x84,&false_branch))return -1;
|
|
compiler->ControlDepth++;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
compiler->ControlDepth--;
|
|
if(compiler_word(compiler,"else")) {
|
|
if(!compiler_emit_branch(compiler,0xe9,&end_branch)||
|
|
!compiler_patch_branch(compiler,false_branch,compiler->CodePosition))return -1;
|
|
compiler->ControlDepth++;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
compiler->ControlDepth--;
|
|
if(!compiler_patch_branch(compiler,end_branch,compiler->CodePosition))return -1;
|
|
} else if(!compiler_patch_branch(compiler,false_branch,compiler->CodePosition))return -1;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"switch")) {
|
|
BOB64_C_CONTROL *control;
|
|
u16 enter_dispatch,body_exit,end_branch=0;
|
|
u32 value_variable;
|
|
if(compiler->ControlDepth>=32||!compiler_scope_push(compiler)||
|
|
!compiler_take(compiler,'(')||!compiler_expression(compiler))return -1;
|
|
u8 switch_type=compiler->ExpressionType;
|
|
if(compiler_is_pointer(switch_type)||compiler_type_is_array(switch_type)||
|
|
compiler_type_is_struct(switch_type)||switch_type==COMPILER_TYPE_VOID||
|
|
!compiler_switch_temporary(compiler,switch_type,&value_variable)||
|
|
!compiler_variable_store(compiler,value_variable)||
|
|
!compiler_take(compiler,')')||!compiler_take(compiler,'{')||
|
|
!compiler_switch_push(compiler,&control)||
|
|
!compiler_emit_branch(compiler,0xe9,&enter_dispatch))return -1;
|
|
compiler->ControlDepth++;
|
|
for(;;) {
|
|
usize statement_start=compiler->Position;
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(compiler_word(compiler,"case")) {
|
|
u64 value;
|
|
if(control->CaseCount>=COMPILER_SWITCH_CASE_LIMIT||
|
|
!compiler_switch_constant(compiler,&value)||
|
|
!compiler_take(compiler,':'))return -1;
|
|
for(u32 i=0;i<control->CaseCount;i++)
|
|
if(control->CaseValues[i]==value)return -1;
|
|
u32 index=control->CaseCount++;
|
|
control->CaseValues[index]=value;
|
|
control->CaseTargets[index]=(u16)compiler->CodePosition;
|
|
continue;
|
|
}
|
|
compiler->Position=statement_start;
|
|
if(compiler_word(compiler,"default")) {
|
|
if(control->HasDefault||!compiler_take(compiler,':'))return -1;
|
|
control->HasDefault=1;
|
|
control->DefaultTarget=(u16)compiler->CodePosition;
|
|
continue;
|
|
}
|
|
compiler->Position=statement_start;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
}
|
|
compiler->ControlDepth--;
|
|
if(!compiler_emit_branch(compiler,0xe9,&body_exit)||
|
|
!compiler_patch_branch(compiler,enter_dispatch,
|
|
compiler->CodePosition))return -1;
|
|
for(u32 i=0;i<control->CaseCount;i++) {
|
|
if(!compiler_variable_load(compiler,value_variable)||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0xb9)||
|
|
!compiler_emit_u64(compiler,control->CaseValues[i])||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x39)||
|
|
!compiler_emit(compiler,0xc8)||!compiler_emit(compiler,0x0f))
|
|
return -1;
|
|
u16 case_branch;
|
|
if(!compiler_emit_branch(compiler,0x84,&case_branch)||
|
|
!compiler_patch_branch(compiler,case_branch,
|
|
control->CaseTargets[i]))return -1;
|
|
}
|
|
if(control->HasDefault) {
|
|
if(!compiler_emit_branch(compiler,0xe9,&end_branch)||
|
|
!compiler_patch_branch(compiler,end_branch,
|
|
control->DefaultTarget))return -1;
|
|
} else if(!compiler_emit_branch(compiler,0xe9,&end_branch))return -1;
|
|
usize switch_end=compiler->CodePosition;
|
|
if(!compiler_patch_branch(compiler,body_exit,switch_end)||
|
|
(!control->HasDefault&&
|
|
!compiler_patch_branch(compiler,end_branch,switch_end))||
|
|
!compiler_switch_finish(compiler,switch_end))return -1;
|
|
compiler_scope_pop(compiler);
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"for")) {
|
|
u16 loop_exit,body_branch,loop_back;
|
|
usize loop_start,post_start,body_start;
|
|
if(compiler->ControlDepth>=32||!compiler_take(compiler,'(')||
|
|
!compiler_scope_push(compiler))return -1;
|
|
if(!compiler_take(compiler,';')) {
|
|
int init=compiler_local_statement(compiler);
|
|
if(init<=0)return -1;
|
|
}
|
|
loop_start=compiler->CodePosition;
|
|
if(compiler_take(compiler,';')) {
|
|
if(!compiler_emit_integer(compiler,1))return -1;
|
|
} else if(!compiler_expression(compiler)||!compiler_take(compiler,';'))return -1;
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x84,&loop_exit)||
|
|
!compiler_emit_branch(compiler,0xe9,&body_branch))return -1;
|
|
post_start=compiler->CodePosition;
|
|
if(!compiler_take(compiler,')')&&
|
|
(!compiler_for_post(compiler)||!compiler_take(compiler,')')))return -1;
|
|
if(!compiler_emit_branch(compiler,0xe9,&loop_back)||
|
|
!compiler_patch_branch(compiler,loop_back,loop_start))return -1;
|
|
body_start=compiler->CodePosition;
|
|
if(!compiler_patch_branch(compiler,body_branch,body_start))return -1;
|
|
if(!compiler_loop_push(compiler,post_start))return -1;
|
|
compiler->ControlDepth++;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
compiler->ControlDepth--;
|
|
u16 body_back;
|
|
if(!compiler_emit_branch(compiler,0xe9,&body_back)||
|
|
!compiler_patch_branch(compiler,body_back,post_start)||
|
|
!compiler_patch_branch(compiler,loop_exit,compiler->CodePosition)||
|
|
!compiler_loop_finish(compiler,compiler->CodePosition))return -1;
|
|
compiler_scope_pop(compiler);
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"do")) {
|
|
usize body_start=compiler->CodePosition,condition_start;
|
|
u16 loop_back;
|
|
if(compiler->ControlDepth>=32||!compiler_loop_push(compiler,0))return -1;
|
|
compiler->ControlDepth++;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
compiler->ControlDepth--;
|
|
condition_start=compiler->CodePosition;
|
|
if(condition_start>0xffffu||!compiler->ControlCount||
|
|
compiler->Controls[compiler->ControlCount-1].Kind!=COMPILER_CONTROL_LOOP)
|
|
return -1;
|
|
compiler->Controls[compiler->ControlCount-1].ContinueTarget=
|
|
(u16)condition_start;
|
|
if(!compiler_word(compiler,"while")||!compiler_take(compiler,'(')||
|
|
!compiler_expression(compiler)||!compiler_take(compiler,')')||
|
|
!compiler_take(compiler,';')||!compiler_emit(compiler,0x48)||
|
|
!compiler_emit(compiler,0x85)||!compiler_emit(compiler,0xc0)||
|
|
!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x85,&loop_back)||
|
|
!compiler_patch_branch(compiler,loop_back,body_start)||
|
|
!compiler_loop_finish(compiler,compiler->CodePosition))return -1;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"while")) {
|
|
usize loop_start=compiler->CodePosition;
|
|
u16 loop_exit,loop_back;
|
|
if(compiler->ControlDepth>=32||!compiler_take(compiler,'(')||
|
|
!compiler_expression(compiler)||!compiler_take(compiler,')')||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x85)||
|
|
!compiler_emit(compiler,0xc0)||!compiler_emit(compiler,0x0f)||
|
|
!compiler_emit_branch(compiler,0x84,&loop_exit))return -1;
|
|
if(!compiler_loop_push(compiler,loop_start))return -1;
|
|
compiler->ControlDepth++;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
compiler->ControlDepth--;
|
|
if(!compiler_emit_branch(compiler,0xe9,&loop_back)||
|
|
!compiler_patch_branch(compiler,loop_back,loop_start)||
|
|
!compiler_patch_branch(compiler,loop_exit,compiler->CodePosition)||
|
|
!compiler_loop_finish(compiler,compiler->CodePosition))return -1;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_take(compiler,'{')) {
|
|
if(compiler->ControlDepth>=32||!compiler_scope_push(compiler))return -1;
|
|
compiler->ControlDepth++;
|
|
for(;;) {
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(compiler_statement(compiler)<=0)return -1;
|
|
}
|
|
compiler->ControlDepth--;
|
|
compiler_scope_pop(compiler);
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
char call_name[32];
|
|
if(compiler_identifier(compiler,call_name,sizeof(call_name))) {
|
|
compiler_skip(compiler);
|
|
if(compiler_function_find(compiler,call_name)>=0&&
|
|
compiler_variable_find(compiler,call_name)<0&&
|
|
compiler->Position<compiler->Length&&
|
|
compiler->Source[compiler->Position]=='(') {
|
|
compiler->Position=saved;
|
|
if(!compiler_identifier(compiler,call_name,sizeof(call_name))||
|
|
!compiler_emit_call(compiler,call_name)||
|
|
!compiler_take(compiler,';'))return -1;
|
|
return 1;
|
|
}
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_word(compiler,"bob64_app_write")) {
|
|
if(!compiler_take(compiler,'(')||!compiler_expression(compiler)||
|
|
compiler->ExpressionType!=COMPILER_TYPE_CHAR_POINTER||
|
|
!compiler_take(compiler,',')||!compiler_emit(compiler,0x50))return -1;
|
|
compiler->StackBytes+=8;
|
|
if(!compiler_expression(compiler)||
|
|
compiler_is_pointer(compiler->ExpressionType)||
|
|
!compiler_take(compiler,')')||!compiler_take(compiler,';')||
|
|
!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x89)||
|
|
!compiler_emit(compiler,0xc2)||!compiler_emit(compiler,0x59)||
|
|
!compiler_emit(compiler,0xb8)||
|
|
!compiler_emit_u32(compiler,BOB64_SYSCALL_WRITE_BUFFER)||
|
|
!compiler_emit(compiler,0xcd)||!compiler_emit(compiler,0x80))return -1;
|
|
compiler->StackBytes-=8;
|
|
return 1;
|
|
}
|
|
compiler->Position=saved;
|
|
if(compiler_take(compiler,';'))return 1;
|
|
return compiler_local_statement(compiler);
|
|
}
|
|
|
|
static int compiler_for_post(BOB64_C_COMPILER *compiler) {
|
|
char name[32];
|
|
char operation;
|
|
if(compiler_increment_pair(compiler,&operation)) {
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int variable=compiler_variable_find(compiler,name);
|
|
return variable>=0&&compiler_increment_variable(compiler,(u32)variable,
|
|
0,operation);
|
|
}
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int variable=compiler_variable_find(compiler,name);
|
|
if(variable>=0&&compiler_increment_pair(compiler,&operation))
|
|
return compiler_increment_variable(compiler,(u32)variable,1,operation);
|
|
if(variable<0||compiler_type_is_array(compiler_variable_type(compiler,(u32)variable))||
|
|
!compiler_take(compiler,'=')||!compiler_expression(compiler)||
|
|
(compiler_is_pointer(compiler_variable_type(compiler,(u32)variable))!=
|
|
compiler_is_pointer(compiler->ExpressionType)||
|
|
(compiler_is_pointer(compiler_variable_type(compiler,(u32)variable))&&
|
|
compiler_variable_type(compiler,(u32)variable)!=compiler->ExpressionType))||
|
|
!compiler_variable_store(compiler,(u32)variable))return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_function_body(BOB64_C_COMPILER *compiler) {
|
|
for(;;) {
|
|
usize saved=compiler->Position;
|
|
if(compiler_word(compiler,"return")) {
|
|
compiler->Position=saved;
|
|
break;
|
|
}
|
|
compiler->Position=saved;
|
|
int statement=compiler_statement(compiler);
|
|
if(statement<0)return 0;
|
|
if(statement>0)continue;
|
|
break;
|
|
}
|
|
if(compiler->ReturnType==COMPILER_TYPE_VOID) {
|
|
if(compiler->StackBytes)return 0;
|
|
if(compiler_word(compiler,"return")) {
|
|
if(!compiler_take(compiler,';')||!compiler_take(compiler,'}'))return 0;
|
|
} else if(!compiler_take(compiler,'}'))return 0;
|
|
return compiler_function_epilogue(compiler);
|
|
}
|
|
if(compiler->StackBytes||!compiler_word(compiler,"return")||
|
|
!compiler_expression(compiler)||
|
|
!compiler_return_type_matches(compiler)||
|
|
!compiler_take(compiler,';')||!compiler_take(compiler,'}')||
|
|
!compiler_function_epilogue(compiler))return 0;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_store_parameter(BOB64_C_COMPILER *compiler,u32 variable,
|
|
u32 argument) {
|
|
s16 displacement=compiler->VariableDisplacement[variable];
|
|
u8 type=compiler->VariableType[variable];
|
|
if(argument>=4) {
|
|
u32 incoming=16u+argument*8u;
|
|
u8 load_modrm=incoming<=127u?0x45:0x85;
|
|
if(type==COMPILER_TYPE_SHORT)
|
|
return compiler_emit(compiler,0x0f)&&compiler_emit(compiler,0xbf)&&
|
|
compiler_emit_rbp_operand(compiler,load_modrm,(s16)incoming)&&
|
|
compiler_emit(compiler,0x66)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(type==COMPILER_TYPE_CHAR)
|
|
return compiler_emit(compiler,0x0f)&&compiler_emit(compiler,0xbe)&&
|
|
compiler_emit_rbp_operand(compiler,load_modrm,(s16)incoming)&&
|
|
compiler_emit(compiler,0x88)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
if(compiler_is_pointer(type)||type==COMPILER_TYPE_USIZE||
|
|
type==COMPILER_TYPE_LONG_LONG) {
|
|
if(!compiler_emit(compiler,0x48)||!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit_rbp_operand(compiler,load_modrm,(s16)incoming))return 0;
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
}
|
|
if(!compiler_emit(compiler,0x8b)||
|
|
!compiler_emit_rbp_operand(compiler,load_modrm,(s16)incoming))return 0;
|
|
return compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x45,displacement);
|
|
}
|
|
if(compiler_is_pointer(type)||type==COMPILER_TYPE_USIZE||
|
|
type==COMPILER_TYPE_LONG_LONG) {
|
|
if(argument<2) {
|
|
u8 modrm=argument==0?0x4d:0x55;
|
|
return compiler_emit(compiler,0x48)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,modrm,displacement);
|
|
}
|
|
return compiler_emit(compiler,0x4c)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,
|
|
(u8)(argument==2?0x45:0x4d),displacement);
|
|
}
|
|
if(type==COMPILER_TYPE_CHAR) {
|
|
if(argument==0)return compiler_emit(compiler,0x88)&&
|
|
compiler_emit_rbp_operand(compiler,0x4d,displacement);
|
|
if(argument==1)return compiler_emit(compiler,0x88)&&
|
|
compiler_emit_rbp_operand(compiler,0x55,displacement);
|
|
return compiler_emit(compiler,0x41)&&compiler_emit(compiler,0x88)&&
|
|
compiler_emit_rbp_operand(compiler,
|
|
(u8)(argument==2?0x45:0x4d),displacement);
|
|
}
|
|
if(type==COMPILER_TYPE_SHORT) {
|
|
static const u8 short_stores[4][4]={
|
|
{0x66,0x89,0x4d,0},{0x66,0x89,0x55,0},
|
|
{0x66,0x44,0x89,0x45},{0x66,0x44,0x89,0x4d}
|
|
};
|
|
u32 instruction_bytes=argument<2?3u:4u;
|
|
for(u32 byte=0;byte<instruction_bytes;byte++)
|
|
if(byte+1u<instruction_bytes&&
|
|
!compiler_emit(compiler,short_stores[argument][byte]))return 0;
|
|
return compiler_emit_rbp_operand(compiler,
|
|
short_stores[argument][instruction_bytes-1],displacement);
|
|
}
|
|
if(argument==0)return compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x4d,displacement);
|
|
if(argument==1)return compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,0x55,displacement);
|
|
return compiler_emit(compiler,0x44)&&compiler_emit(compiler,0x89)&&
|
|
compiler_emit_rbp_operand(compiler,
|
|
(u8)(argument==2?0x45:0x4d),displacement);
|
|
}
|
|
|
|
static int compiler_function_definition(BOB64_C_COMPILER *compiler) {
|
|
char name[32],parameter_names[COMPILER_ARGUMENT_LIMIT][32];
|
|
u8 argument_types[COMPILER_ARGUMENT_LIMIT],return_type,argument_count=0;
|
|
if(!compiler_function_signature(compiler,name,sizeof(name),&return_type,
|
|
&argument_count,argument_types,parameter_names,1))return 0;
|
|
int is_main=name[0]=='m'&&name[1]=='a'&&name[2]=='i'&&name[3]=='n'&&!name[4];
|
|
if((is_main&&(argument_count||return_type==COMPILER_TYPE_VOID))||
|
|
!compiler_take(compiler,'{')||
|
|
!compiler_function_declare(compiler,name,(u16)compiler->CodePosition,
|
|
return_type,(u8)argument_count,argument_types,1))return 0;
|
|
compiler->VariableCount=0;compiler->LocalBytes=0;compiler->ScopeDepth=0;
|
|
compiler->ReturnType=return_type;
|
|
compiler->Return64=return_type==COMPILER_TYPE_LONG_LONG||
|
|
return_type==COMPILER_TYPE_USIZE||
|
|
compiler_is_pointer(return_type);
|
|
compiler->StackBytes=0;compiler->ControlDepth=0;
|
|
if(!compiler_function_prologue(compiler)||
|
|
(is_main&&!compiler_emit_global_initializers(compiler)))return 0;
|
|
for(u32 i=0;i<argument_count;i++) {
|
|
u32 variable;
|
|
if(!compiler_add_variable(compiler,parameter_names[i],argument_types[i],0,
|
|
&variable)||
|
|
!compiler_store_parameter(compiler,variable,i))return 0;
|
|
}
|
|
return compiler_function_body(compiler);
|
|
}
|
|
|
|
static int compiler_resolve_calls(BOB64_C_COMPILER *compiler) {
|
|
for(u32 i=0;i<compiler->CallCount;i++) {
|
|
BOB64_C_CALL *call=&compiler->Calls[i];
|
|
int function=compiler_function_find(compiler,call->Name);
|
|
if(function<0||!compiler->Functions[function].Defined||
|
|
compiler->Functions[function].ArgumentCount!=call->ArgumentCount)
|
|
return 0;
|
|
for(u32 argument=0;argument<call->ArgumentCount;argument++) {
|
|
u8 expected=compiler->Functions[function].ArgumentTypes[argument];
|
|
u8 actual=call->ArgumentTypes[argument];
|
|
if(!compiler_value_types_compatible(expected,actual))return 0;
|
|
}
|
|
s64 displacement=(s64)compiler->Functions[function].CodeOffset-
|
|
(s64)(call->DisplacementOffset+4u);
|
|
if(displacement<(-2147483647LL-1)||displacement>2147483647)return 0;
|
|
u32 encoded=(u32)(s32)displacement;
|
|
for(u32 byte=0;byte<4;byte++)
|
|
compiler->Code[call->DisplacementOffset+byte]=(u8)(encoded>>(byte*8));
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static u32 compiler_crc32(const u8 *bytes,usize length) {
|
|
u32 value=0xffffffffu;
|
|
for(usize i=0;i<length;i++) {
|
|
value^=bytes[i];
|
|
for(u32 bit=0;bit<8;bit++)
|
|
value=(value>>1)^(0xedb88320u&-(value&1u));
|
|
}
|
|
return ~value;
|
|
}
|
|
|
|
static void compiler_write16(u8 *bytes,u16 value) {
|
|
bytes[0]=(u8)value;bytes[1]=(u8)(value>>8);
|
|
}
|
|
|
|
static void compiler_write32(u8 *bytes,u32 value) {
|
|
for(u32 i=0;i<4;i++)bytes[i]=(u8)(value>>(i*8));
|
|
}
|
|
|
|
static void compiler_write64(u8 *bytes,u64 value) {
|
|
compiler_write32(bytes,(u32)value);compiler_write32(bytes+4,(u32)(value>>32));
|
|
}
|
|
|
|
static int compiler_scan_skip_quote(BOB64_C_COMPILER *compiler,usize *position) {
|
|
char quote=compiler->Source[(*position)++];
|
|
while(*position<compiler->Length) {
|
|
char value=compiler->Source[(*position)++];
|
|
if(value=='\\') {
|
|
if(*position>=compiler->Length)return 0;
|
|
(*position)++;
|
|
} else if(value==quote)return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int compiler_scan_skip_comment(BOB64_C_COMPILER *compiler,usize *position) {
|
|
if(*position+1>=compiler->Length||compiler->Source[*position]!='/')return 0;
|
|
if(compiler->Source[*position+1]=='/') {
|
|
*position+=2;
|
|
while(*position<compiler->Length&&compiler->Source[*position]!='\n')(*position)++;
|
|
return 1;
|
|
}
|
|
if(compiler->Source[*position+1]=='*') {
|
|
*position+=2;
|
|
while(*position+1<compiler->Length&&
|
|
!(compiler->Source[*position]=='*'&&compiler->Source[*position+1]=='/'))
|
|
(*position)++;
|
|
if(*position+1>=compiler->Length)return -1;
|
|
*position+=2;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int compiler_global_add(BOB64_C_COMPILER *compiler,const char *name,
|
|
u8 type,u32 array_length,u32 bytes,u32 *global_index) {
|
|
usize name_length=0;
|
|
if(compiler->GlobalCount>=COMPILER_GLOBAL_LIMIT||
|
|
compiler_variable_find(compiler,name)>=0||
|
|
compiler_enum_constant_find(compiler,name)>=0||
|
|
bytes>sizeof(compiler->Data)-compiler->DataLength)
|
|
return 0;
|
|
u32 alignment=compiler_type_is_struct(type)?compiler_type_alignment(compiler,type):
|
|
((type==COMPILER_TYPE_LONG_LONG||type==COMPILER_TYPE_USIZE||
|
|
type==COMPILER_TYPE_LONG_LONG_ARRAY||type==COMPILER_TYPE_USIZE_ARRAY||
|
|
compiler_is_pointer(type))?8u:
|
|
(type==COMPILER_TYPE_INT||type==COMPILER_TYPE_INT_ARRAY?4u:
|
|
(type==COMPILER_TYPE_SHORT||type==COMPILER_TYPE_SHORT_ARRAY?2u:1u)));
|
|
compiler->DataLength=(compiler->DataLength+alignment-1u)&~(alignment-1u);
|
|
if(bytes>sizeof(compiler->Data)-compiler->DataLength)return 0;
|
|
u32 index=compiler->GlobalCount++;
|
|
BOB64_C_GLOBAL *global=&compiler->Globals[index];
|
|
while(name[name_length]&&name_length+1<sizeof(global->Name)) {
|
|
global->Name[name_length]=name[name_length];name_length++;
|
|
}
|
|
if(name[name_length])return 0;
|
|
global->Name[name_length]=0;
|
|
global->Type=type;
|
|
global->ArrayLength=(u16)array_length;
|
|
global->InitKind=0;global->InitTarget=0;
|
|
global->DataOffset=(u16)compiler->DataLength;
|
|
for(u32 i=0;i<bytes;i++)compiler->Data[compiler->DataLength+i]=0;
|
|
compiler->DataLength+=bytes;
|
|
if(global_index)*global_index=index;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_struct_definition(BOB64_C_COMPILER *compiler,const char *name) {
|
|
usize name_length=0;
|
|
if(compiler->StructCount>=COMPILER_STRUCT_LIMIT||
|
|
compiler_struct_find(compiler,name)>=0)return 0;
|
|
u32 index=compiler->StructCount++;
|
|
BOB64_C_STRUCT *structure=&compiler->Structs[index];
|
|
while(name[name_length]&&name_length+1<sizeof(structure->Name)) {
|
|
structure->Name[name_length]=name[name_length];name_length++;
|
|
}
|
|
if(name[name_length])return 0;
|
|
structure->Name[name_length]=0;structure->Size=0;
|
|
structure->Alignment=1;structure->FieldCount=0;
|
|
u32 offset=0;
|
|
for(;;) {
|
|
if(compiler_take(compiler,'}'))break;
|
|
u8 base_type=compiler_type_specifier(compiler);
|
|
int struct_index=compiler_type_is_struct(base_type)?
|
|
(int)(base_type-COMPILER_TYPE_STRUCT_BASE):-1;
|
|
if(!base_type)return 0;
|
|
int pointer=compiler_take(compiler,'*');
|
|
if(base_type==COMPILER_TYPE_VOID&&!pointer)return 0;
|
|
if(struct_index>=0&&!pointer)return 0;
|
|
char field_name[32];
|
|
if(!compiler_identifier(compiler,field_name,sizeof(field_name))||
|
|
!compiler_take(compiler,';')||structure->FieldCount>=COMPILER_FIELD_LIMIT||
|
|
compiler_field_find(compiler,(u8)(COMPILER_TYPE_STRUCT_BASE+index),field_name)>=0)
|
|
return 0;
|
|
u8 field_type=pointer?compiler_pointer_to(compiler,base_type):base_type;
|
|
if(!field_type)return 0;
|
|
u32 alignment=compiler_type_alignment(compiler,field_type);
|
|
u32 size=compiler_type_size(compiler,field_type);
|
|
offset=(offset+alignment-1u)&~(alignment-1u);
|
|
BOB64_C_FIELD *field=&structure->Fields[structure->FieldCount++];
|
|
usize field_length=0;
|
|
while(field_name[field_length]) {
|
|
field->Name[field_length]=field_name[field_length];field_length++;
|
|
}
|
|
field->Name[field_length]=0;field->Offset=(u16)offset;field->Type=field_type;
|
|
offset+=size;
|
|
if(alignment>structure->Alignment)structure->Alignment=(u8)alignment;
|
|
if(offset>2048)return 0;
|
|
}
|
|
if(!structure->FieldCount)return 0;
|
|
structure->Size=(u16)((offset+structure->Alignment-1u)&~(structure->Alignment-1u));
|
|
return compiler_take(compiler,';');
|
|
}
|
|
|
|
/* Returns 1 for a definition, 0 when this is an enum-typed declaration, -1 on error. */
|
|
static int compiler_enum_definition(BOB64_C_COMPILER *compiler) {
|
|
usize saved=compiler->Position;
|
|
char tag[32];
|
|
if(!compiler_word(compiler,"enum")||
|
|
!compiler_identifier(compiler,tag,sizeof(tag))||
|
|
!compiler_take(compiler,'{')) {
|
|
compiler->Position=saved;
|
|
return 0;
|
|
}
|
|
if(compiler->EnumCount>=COMPILER_ENUM_LIMIT||
|
|
compiler_enum_find(compiler,tag)>=0)return -1;
|
|
u32 enum_index=compiler->EnumCount++;
|
|
usize tag_length=0;
|
|
while(tag[tag_length]) {
|
|
compiler->Enums[enum_index].Name[tag_length]=tag[tag_length];
|
|
tag_length++;
|
|
}
|
|
compiler->Enums[enum_index].Name[tag_length]=0;
|
|
u64 next_value=0;
|
|
u32 count=0;
|
|
for(;;) {
|
|
char name[32];
|
|
if(!compiler_identifier(compiler,name,sizeof(name))||
|
|
compiler_enum_constant_find(compiler,name)>=0||
|
|
compiler_variable_find(compiler,name)>=0||
|
|
compiler_function_find(compiler,name)>=0||
|
|
compiler_typedef_find(compiler,name)>=0||
|
|
compiler->EnumConstantCount>=COMPILER_ENUM_CONSTANT_LIMIT)return -1;
|
|
BOB64_C_ENUM_CONSTANT *constant=
|
|
&compiler->EnumConstants[compiler->EnumConstantCount++];
|
|
usize length=0;
|
|
while(name[length]) {constant->Name[length]=name[length];length++;}
|
|
constant->Name[length]=0;
|
|
if(compiler_take(compiler,'=')) {
|
|
if(!compiler_switch_constant(compiler,&next_value))return -1;
|
|
}
|
|
constant->Value=next_value;
|
|
count++;
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(!compiler_take(compiler,','))return -1;
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(next_value==~(u64)0)return -1;
|
|
next_value++;
|
|
}
|
|
if(!count||!compiler_take(compiler,';'))return -1;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_typedef_name_reserved(const char *name) {
|
|
static const char *reserved[]={"void","char","short","int","long",
|
|
"usize","uintptr_t","isize","intptr_t","struct","enum"};
|
|
for(u32 i=0;i<sizeof(reserved)/sizeof(reserved[0]);i++) {
|
|
usize position=0;
|
|
while(name[position]&&name[position]==reserved[i][position])position++;
|
|
if(!name[position]&&!reserved[i][position])return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int compiler_typedef_declaration(BOB64_C_COMPILER *compiler) {
|
|
char name[32];
|
|
if(!compiler_word(compiler,"typedef"))return 0;
|
|
u8 type=compiler_type_specifier(compiler);
|
|
if(!type)return 0;
|
|
int pointer=compiler_take(compiler,'*');
|
|
if(type==COMPILER_TYPE_VOID&&!pointer)return 0;
|
|
if(pointer) {
|
|
type=compiler_pointer_to(compiler,type);
|
|
if(!type)return 0;
|
|
}
|
|
if(!compiler_identifier(compiler,name,sizeof(name))||
|
|
compiler_typedef_name_reserved(name)||
|
|
compiler_typedef_find(compiler,name)>=0||
|
|
compiler_enum_constant_find(compiler,name)>=0||
|
|
compiler->TypedefCount>=COMPILER_TYPEDEF_LIMIT||
|
|
!compiler_take(compiler,';'))return 0;
|
|
BOB64_C_TYPEDEF *alias=&compiler->Typedefs[compiler->TypedefCount++];
|
|
usize length=0;
|
|
while(name[length]) {alias->Name[length]=name[length];length++;}
|
|
alias->Name[length]=0;alias->Type=type;
|
|
return 1;
|
|
}
|
|
|
|
static int compiler_global_declaration(BOB64_C_COMPILER *compiler) {
|
|
char name[32];
|
|
u8 type,base_type;
|
|
u32 array_length=0,bytes,index;
|
|
int is_pointer,is_struct;
|
|
if(compiler_word(compiler,"static")) { }
|
|
if(compiler_word(compiler,"struct")) {
|
|
char struct_name[32];
|
|
if(!compiler_identifier(compiler,struct_name,sizeof(struct_name)))return 0;
|
|
if(compiler_take(compiler,'{'))return compiler_struct_definition(compiler,struct_name);
|
|
int structure=compiler_struct_find(compiler,struct_name);
|
|
if(structure<0)return 0;
|
|
base_type=(u8)(COMPILER_TYPE_STRUCT_BASE+(u32)structure);
|
|
} else base_type=compiler_type_specifier(compiler);
|
|
if(!base_type)return 0;
|
|
int pointer=compiler_take(compiler,'*');
|
|
if(base_type==COMPILER_TYPE_VOID&&!pointer)return 0;
|
|
if(pointer) {
|
|
base_type=compiler_pointer_to(compiler,base_type);
|
|
if(!base_type)return 0;
|
|
}
|
|
type=base_type;
|
|
is_struct=compiler_type_is_struct(type);
|
|
is_pointer=compiler_is_pointer(type);
|
|
if(!compiler_identifier(compiler,name,sizeof(name)))return 0;
|
|
int array=compiler_take(compiler,'[');
|
|
if(array) {
|
|
u64 size;
|
|
if(is_pointer||is_struct||!compiler_integer(compiler,&size)||!size||
|
|
!compiler_take(compiler,']'))return 0;
|
|
type=compiler_array_type(base_type);
|
|
if(!type)return 0;
|
|
u32 element_size=compiler_array_element_size(type);
|
|
u32 maximum=sizeof(compiler->Data)/element_size;
|
|
if(size>maximum)return 0;
|
|
array_length=(u32)size;
|
|
bytes=array_length*element_size;
|
|
} else bytes=is_struct?compiler_type_size(compiler,type):
|
|
(is_pointer?8u:compiler_type_size(compiler,type));
|
|
if(!compiler_global_add(compiler,name,type,array_length,bytes,&index))return 0;
|
|
BOB64_C_GLOBAL *global=&compiler->Globals[index];
|
|
if(compiler_take(compiler,'=')) {
|
|
if(is_struct)return 0;
|
|
if(is_pointer) {
|
|
usize string_offset,string_length;
|
|
if((type==COMPILER_TYPE_CHAR_POINTER||
|
|
type==COMPILER_TYPE_VOID_POINTER)&&
|
|
compiler_string(compiler,&string_offset,&string_length)) {
|
|
(void)string_length;
|
|
global->InitKind=2;
|
|
global->InitTarget=(u16)string_offset;
|
|
} else {
|
|
int address=compiler_take(compiler,'&');
|
|
char target_name[32];
|
|
if(compiler_identifier(compiler,target_name,sizeof(target_name))) {
|
|
int target=compiler_variable_find(compiler,target_name);
|
|
if(target<0||!compiler_variable_is_global((u32)target))return 0;
|
|
BOB64_C_GLOBAL *target_global=&compiler->Globals[(u32)target&0x7fffu];
|
|
u8 target_type=target_global->Type;
|
|
u8 target_pointer_type;
|
|
if(compiler_type_is_struct(target_type))
|
|
target_pointer_type=(u8)(COMPILER_TYPE_STRUCT_POINTER_BASE+
|
|
target_type-COMPILER_TYPE_STRUCT_BASE);
|
|
else if(compiler_type_is_array(target_type))
|
|
target_pointer_type=compiler_pointer_type(target_type);
|
|
else if(address)
|
|
target_pointer_type=compiler_pointer_type(target_type);
|
|
else target_pointer_type=0;
|
|
if(!compiler_value_types_compatible(type,target_pointer_type)||
|
|
(compiler_type_is_struct(target_type)&&!address))return 0;
|
|
global->InitKind=1;
|
|
global->InitTarget=target_global->DataOffset;
|
|
} else {
|
|
u64 value;
|
|
if(!compiler_integer(compiler,&value)||value!=0)return 0;
|
|
}
|
|
}
|
|
} else if(array&&type==COMPILER_TYPE_CHAR_ARRAY) {
|
|
usize string_offset,string_length;
|
|
if(compiler_string(compiler,&string_offset,&string_length)) {
|
|
if(string_length>array_length)return 0;
|
|
for(u32 i=0;i<string_length;i++)
|
|
compiler->Data[global->DataOffset+i]=compiler->Data[string_offset+i];
|
|
} else {
|
|
if(!compiler_take(compiler,'{'))return 0;
|
|
u32 element=0;
|
|
for(;;) {
|
|
if(compiler_take(compiler,'}'))break;
|
|
u64 value;
|
|
if(element>=array_length||
|
|
!compiler_switch_constant(compiler,&value))return 0;
|
|
compiler->Data[global->DataOffset+element++]=(u8)value;
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(!compiler_take(compiler,','))return 0;
|
|
}
|
|
}
|
|
} else if(array) {
|
|
if(!compiler_take(compiler,'{'))return 0;
|
|
u32 element=0;
|
|
for(;;) {
|
|
if(compiler_take(compiler,'}'))break;
|
|
u64 value;
|
|
if(element>=array_length||
|
|
!compiler_switch_constant(compiler,&value))return 0;
|
|
u32 element_size=compiler_array_element_size((u8)type);
|
|
u32 offset=global->DataOffset+element++*element_size;
|
|
for(u32 byte=0;byte<element_size;byte++)
|
|
compiler->Data[offset+byte]=(u8)(value>>(byte*8));
|
|
if(compiler_take(compiler,'}'))break;
|
|
if(!compiler_take(compiler,','))return 0;
|
|
}
|
|
} else {
|
|
u64 value;
|
|
if(!compiler_switch_constant(compiler,&value))return 0;
|
|
for(u32 byte=0;byte<bytes;byte++)
|
|
compiler->Data[global->DataOffset+byte]=(u8)(value>>(byte*8));
|
|
}
|
|
}
|
|
return compiler_take(compiler,';');
|
|
}
|
|
|
|
static int compiler_collect_function_sources(BOB64_C_COMPILER *compiler,
|
|
u32 *main_source_index) {
|
|
usize scan=0;
|
|
u32 main_count=0;
|
|
compiler->FunctionSourceCount=0;
|
|
while(scan<compiler->Length) {
|
|
compiler->Position=scan;
|
|
compiler_skip(compiler);
|
|
if(compiler->Failed)return 0;
|
|
if(compiler->Position>=compiler->Length)break;
|
|
usize start=compiler->Position;
|
|
if(compiler_word(compiler,"enum")) {
|
|
compiler->Position=start;
|
|
int enum_result=compiler_enum_definition(compiler);
|
|
if(enum_result<0)return 0;
|
|
if(enum_result>0) {
|
|
scan=compiler->Position;
|
|
continue;
|
|
}
|
|
}
|
|
if(compiler_word(compiler,"typedef")) {
|
|
compiler->Position=start;
|
|
if(!compiler_typedef_declaration(compiler))return 0;
|
|
scan=compiler->Position;
|
|
continue;
|
|
}
|
|
char name[32],parameter_names[COMPILER_ARGUMENT_LIMIT][32];
|
|
u8 return_type,argument_count,argument_types[COMPILER_ARGUMENT_LIMIT];
|
|
if(!compiler_function_signature(compiler,name,sizeof(name),&return_type,
|
|
&argument_count,argument_types,parameter_names,0)) {
|
|
compiler->Position=start;
|
|
if(!compiler_global_declaration(compiler))return 0;
|
|
scan=compiler->Position;
|
|
continue;
|
|
}
|
|
if(!compiler_function_declare(compiler,name,0,return_type,argument_count,
|
|
argument_types,0))return 0;
|
|
compiler_skip(compiler);
|
|
if(compiler->Position>=compiler->Length)return 0;
|
|
if(compiler->Source[compiler->Position]==';') {
|
|
compiler->Position++;
|
|
scan=compiler->Position;
|
|
continue;
|
|
}
|
|
if(compiler->Source[compiler->Position]!='{'||
|
|
compiler->FunctionSourceCount>=COMPILER_FUNCTION_LIMIT)return 0;
|
|
compiler->Position++;
|
|
usize position=compiler->Position;
|
|
u32 depth=1;
|
|
while(position<compiler->Length&&depth) {
|
|
char value=compiler->Source[position];
|
|
if(value=='/'&&position+1<compiler->Length) {
|
|
usize next=position;
|
|
int skipped=compiler_scan_skip_comment(compiler,&next);
|
|
if(skipped<0)return 0;
|
|
if(skipped) { position=next;continue; }
|
|
}
|
|
if(value=='\''||value=='"') {
|
|
if(!compiler_scan_skip_quote(compiler,&position))return 0;
|
|
continue;
|
|
}
|
|
if(value=='{')depth++;
|
|
else if(value=='}')depth--;
|
|
position++;
|
|
}
|
|
if(depth)return 0;
|
|
usize end=position;
|
|
u32 source_index=compiler->FunctionSourceCount++;
|
|
compiler->FunctionSourceStart[source_index]=(u16)start;
|
|
compiler->FunctionSourceEnd[source_index]=(u16)end;
|
|
if(name[0]=='m'&&name[1]=='a'&&name[2]=='i'&&name[3]=='n'&&!name[4]) {
|
|
*main_source_index=source_index;
|
|
main_count++;
|
|
}
|
|
scan=end;
|
|
}
|
|
compiler->Position=0;
|
|
return main_count==1;
|
|
}
|
|
|
|
int bob64_compile_c(const char *source,usize source_length,void *output,
|
|
usize output_capacity,usize *output_length,
|
|
usize *error_offset) {
|
|
/* The initial kernel is single-tasked; a later compiler job queue needs per-job state. */
|
|
static BOB64_C_COMPILER compiler;
|
|
static u8 payload[BOB64_COMPILER_CODE_CAPACITY+
|
|
BOB64_COMPILER_DATA_CAPACITY];
|
|
u8 *file=(u8 *)output;
|
|
u32 main_source_index=0;
|
|
if(output_length)*output_length=0;
|
|
if(error_offset)*error_offset=0;
|
|
if(!source||!output||!output_length||source_length>BOB64_COMPILER_SOURCE_LIMIT||
|
|
output_capacity<BOB64_COMPILER_IMAGE_LIMIT)return -1;
|
|
compiler.Source=source;compiler.Length=source_length;compiler.Position=0;
|
|
compiler.ErrorOffset=0;compiler.CodePosition=0;compiler.DataLength=0;
|
|
compiler.IntegerValue=0;compiler.Return64=0;compiler.Failed=0;
|
|
compiler.VariableCount=0;
|
|
compiler.FunctionCount=0;compiler.GlobalCount=0;compiler.StructCount=0;
|
|
compiler.TypedefCount=0;compiler.EnumCount=0;compiler.EnumConstantCount=0;
|
|
compiler.CallCount=0;compiler.ControlCount=0;
|
|
compiler.FunctionSourceCount=0;
|
|
for(usize i=0;i<sizeof(compiler.Data);i++)compiler.Data[i]=0;
|
|
for(usize i=0;i<sizeof(compiler.VariableNames);i++)
|
|
((u8 *)compiler.VariableNames)[i]=0;
|
|
for(usize i=0;i<sizeof(compiler.VariableDisplacement);i++)
|
|
((u8 *)compiler.VariableDisplacement)[i]=0;
|
|
for(usize i=0;i<sizeof(compiler.VariableType);i++)compiler.VariableType[i]=0;
|
|
for(usize i=0;i<sizeof(compiler.VariableArrayLength)/
|
|
sizeof(compiler.VariableArrayLength[0]);i++)
|
|
compiler.VariableArrayLength[i]=0;
|
|
compiler.LocalBytes=0;compiler.StackBytes=0;compiler.ControlDepth=0;
|
|
if(!compiler_collect_function_sources(&compiler,&main_source_index))goto syntax_error;
|
|
/* The entry shim follows Microsoft x64: shadow space, call, then syscall exit. */
|
|
compiler.CodePosition=0;
|
|
if(!compiler_emit(&compiler,0x48)||!compiler_emit(&compiler,0x83)||
|
|
!compiler_emit(&compiler,0xec)||!compiler_emit(&compiler,0x28)||
|
|
!compiler_emit(&compiler,0xe8)||!compiler_emit_u32(&compiler,32-9)||
|
|
!compiler_emit(&compiler,0x48)||!compiler_emit(&compiler,0x89)||
|
|
!compiler_emit(&compiler,0xc1)||!compiler_emit(&compiler,0xb8)||
|
|
!compiler_emit_u32(&compiler,BOB64_SYSCALL_EXIT)||
|
|
!compiler_emit(&compiler,0xcd)||!compiler_emit(&compiler,0x80)||
|
|
!compiler_emit(&compiler,0x0f)||!compiler_emit(&compiler,0x0b))goto syntax_error;
|
|
while(compiler.CodePosition<32)if(!compiler_emit(&compiler,0x90))goto syntax_error;
|
|
compiler.Position=compiler.FunctionSourceStart[main_source_index];
|
|
if(!compiler_function_definition(&compiler)||
|
|
compiler.Position!=compiler.FunctionSourceEnd[main_source_index])goto syntax_error;
|
|
for(u32 function=0;function<compiler.FunctionSourceCount;function++) {
|
|
if(function==main_source_index)continue;
|
|
compiler.Position=compiler.FunctionSourceStart[function];
|
|
if(!compiler_function_definition(&compiler)||
|
|
compiler.Position!=compiler.FunctionSourceEnd[function])goto syntax_error;
|
|
}
|
|
if(compiler.Failed||!compiler_resolve_calls(&compiler))goto syntax_error;
|
|
for(usize i=0;i<sizeof(compiler.Code);i++)
|
|
payload[i]=i<compiler.CodePosition?compiler.Code[i]:0x90;
|
|
for(usize i=0;i<sizeof(compiler.Data);i++)
|
|
payload[BOB64_COMPILER_CODE_CAPACITY+i]=compiler.Data[i];
|
|
for(usize i=0;i<BOB64_EXEC_HEADER_SIZE;i++)file[i]=0;
|
|
file[0]='B';file[1]='6';file[2]='4';file[3]='E';
|
|
compiler_write16(file+4,BOB64_EXEC_VERSION);
|
|
compiler_write16(file+6,BOB64_EXEC_HEADER_SIZE);
|
|
compiler_write32(file+8,BOB64_APP_ABI_VERSION);
|
|
compiler_write64(file+16,sizeof(payload));
|
|
compiler_write64(file+24,sizeof(payload));
|
|
compiler_write64(file+32,0);
|
|
compiler_write64(file+40,BOB64_COMPILER_CODE_CAPACITY);
|
|
compiler_write32(file+48,compiler_crc32(payload,sizeof(payload)));
|
|
for(usize i=0;i<sizeof(payload);i++)file[BOB64_EXEC_HEADER_SIZE+i]=payload[i];
|
|
*output_length=BOB64_COMPILER_IMAGE_LIMIT;
|
|
return 0;
|
|
syntax_error:
|
|
if(error_offset)*error_offset=compiler.Position<compiler.Length?
|
|
compiler.Position:compiler.Length;
|
|
return -2;
|
|
}
|