Add forward declaration of __asm code

This commit is contained in:
drmortalwombat 2024-12-08 12:05:02 +01:00
parent 7c8ab991be
commit 50c7e10814
4 changed files with 86 additions and 42 deletions

View File

@ -3072,7 +3072,7 @@ __asm inp_binop_sub_f32
#pragma bytecode(BC_BINOP_SUB_F32, inp_binop_sub_f32) #pragma bytecode(BC_BINOP_SUB_F32, inp_binop_sub_f32)
__asm fmul8 __asm crt_fmul8
{ {
sec sec
ror ror
@ -3106,7 +3106,7 @@ W1:
rts rts
} }
__asm fmul __asm crt_fmul
{ {
lda accu lda accu
ora accu + 1 ora accu + 1
@ -3147,13 +3147,13 @@ W2:
beq W5 beq W5
bne W6 bne W6
W4: W4:
jsr fmul8 jsr crt_fmul8
lda tmp + 1 lda tmp + 1
W6: W6:
jsr fmul8 jsr crt_fmul8
W5: W5:
lda tmp + 2 lda tmp + 2
jsr fmul8 jsr crt_fmul8
sec sec
lda tmp + 8 lda tmp + 8
@ -3210,12 +3210,12 @@ __asm inp_binop_mul_f32
{ {
jsr freg.split_exp jsr freg.split_exp
sty tmpy sty tmpy
jsr fmul jsr crt_fmul
ldy tmpy ldy tmpy
jmp startup.exec jmp startup.exec
} }
__asm fdiv __asm crt_fdiv
{ {
lda accu lda accu
ora accu + 1 ora accu + 1
@ -3337,13 +3337,13 @@ ZERO:
__asm inp_binop_div_f32 __asm inp_binop_div_f32
{ {
jsr freg.split_exp jsr freg.split_exp
jsr fdiv jsr crt_fdiv
jmp startup.exec jmp startup.exec
} }
#pragma bytecode(BC_BINOP_DIV_F32, inp_binop_div_f32) #pragma bytecode(BC_BINOP_DIV_F32, inp_binop_div_f32)
__asm fcmp __asm crt_fcmp
{ {
lda accu + 3 lda accu + 3
eor tmp + 3 eor tmp + 3
@ -4058,9 +4058,9 @@ __asm load32
#pragma runtime(fmergea, freg.merge_aexp) #pragma runtime(fmergea, freg.merge_aexp)
#pragma runtime(fadd, faddsub.fadd) #pragma runtime(fadd, faddsub.fadd)
#pragma runtime(fsub, faddsub.fsub) #pragma runtime(fsub, faddsub.fsub)
#pragma runtime(fmul, fmul) #pragma runtime(fmul, crt_fmul)
#pragma runtime(fdiv, fdiv) #pragma runtime(fdiv, crt_fdiv)
#pragma runtime(fcmp, fcmp) #pragma runtime(fcmp, crt_fcmp)
#pragma runtime(ffromi, sint16_to_float) #pragma runtime(ffromi, sint16_to_float)
#pragma runtime(ffromu, uint16_to_float) #pragma runtime(ffromu, uint16_to_float)
#pragma runtime(ftoi, f32_to_i16) #pragma runtime(ftoi, f32_to_i16)
@ -4558,7 +4558,7 @@ struct Heap {
#pragma section(heap, 0x0000, HeapStart, HeapEnd) #pragma section(heap, 0x0000, HeapStart, HeapEnd)
__asm malloc __asm crt_malloc
{ {
// make room for two additional bytes // make room for two additional bytes
// to store pointer to end of used memory // to store pointer to end of used memory
@ -4785,14 +4785,14 @@ hc2:
__asm inp_malloc __asm inp_malloc
{ {
sty tmpy sty tmpy
jsr malloc jsr crt_malloc
ldy tmpy ldy tmpy
jmp startup.exec jmp startup.exec
} }
#pragma bytecode(BC_MALLOC, inp_malloc) #pragma bytecode(BC_MALLOC, inp_malloc)
__asm free __asm crt_free
{ {
// check nullptr free // check nullptr free
@ -5009,21 +5009,21 @@ nostart:
__asm inp_free __asm inp_free
{ {
sty tmpy sty tmpy
jsr free jsr crt_free
ldy tmpy ldy tmpy
jmp startup.exec jmp startup.exec
} }
#pragma bytecode(BC_FREE, inp_free) #pragma bytecode(BC_FREE, inp_free)
__asm breakpoint __asm crt_breakpoint
{ {
rts rts
} }
#pragma runtime(malloc, malloc) #pragma runtime(malloc, crt_malloc)
#pragma runtime(free, free) #pragma runtime(free, crt_free)
#pragma runtime(breakpoint, breakpoint) #pragma runtime(breakpoint, crt_breakpoint)
#if 0 #if 0

View File

@ -830,6 +830,14 @@ void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration*
void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Declaration * adec,GrowingArray<Declaration*> * refvars) void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Declaration * adec,GrowingArray<Declaration*> * refvars)
{ {
Expression* exp = adec->mValue; Expression* exp = adec->mValue;
if (!adec->mValue)
{
mErrors->Error(adec->mLocation, EERR_UNDEFINED_OBJECT, "Call of undefined assembler function", adec->mIdent);
if (!adec->mLinkerObject)
adec->mLinkerObject = mLinker->AddObject(adec->mLocation, adec->mQualIdent, adec->mSection, LOT_NATIVE_CODE, adec->mAlignment);
return;
}
GrowingArray<int> offsetMap(-1); GrowingArray<int> offsetMap(-1);

View File

@ -11813,8 +11813,17 @@ Expression* Parser::ParseAssemblerBaseOperand(Declaration* pcasm, int pcoffset)
exp->mDecValue = ldec; exp->mDecValue = ldec;
exp->mDecType = TheUnsignedIntTypeDeclaration; exp->mDecType = TheUnsignedIntTypeDeclaration;
} }
else else if (exp->mDecValue->mFlags & DTF_DEFINED)
mErrors->Error(mScanner->mLocation, EERR_OBJECT_NOT_FOUND, "Assembler label not found", mScanner->mTokenIdent); mErrors->Error(mScanner->mLocation, EERR_OBJECT_NOT_FOUND, "Assembler label not found", mScanner->mTokenIdent);
else
{
Declaration * ldec = new Declaration(mScanner->mLocation, DT_LABEL);
ldec->mIdent = mScanner->mTokenIdent;
ldec->mQualIdent = mScanner->mTokenIdent;
exp->mDecType = TheUnsignedIntTypeDeclaration;
exp->mDecValue->mBase->mScope->Insert(ldec->mIdent, ldec);
exp->mDecValue = ldec;
}
} }
mScanner->NextToken(); mScanner->NextToken();
} }
@ -12126,9 +12135,24 @@ void Parser::AddAssemblerRegister(const Ident* ident, int value)
mScope->Insert(decaccu->mIdent, decaccu); mScope->Insert(decaccu->mIdent, decaccu);
} }
Expression* Parser::ParseAssembler(void) Expression* Parser::ParseAssembler(Declaration* vdasm)
{ {
DeclarationScope* scope = new DeclarationScope(mScope, SLEVEL_LOCAL); Declaration* dasm;
if (!vdasm)
{
dasm = new Declaration(mScanner->mLocation, DT_TYPE_ASSEMBLER);
dasm->mScope = new DeclarationScope(mScope, SLEVEL_LOCAL);
vdasm = new Declaration(mScanner->mLocation, DT_CONST_ASSEMBLER);
vdasm->mVarIndex = -1;
vdasm->mSection = mCodeSection;
vdasm->mBase = dasm;
}
else
dasm = vdasm->mBase;
DeclarationScope* scope = dasm->mScope;
mScope = scope; mScope = scope;
mScanner->SetAssemblerMode(true); mScanner->SetAssemblerMode(true);
@ -12153,21 +12177,13 @@ Expression* Parser::ParseAssembler(void)
decaccu->mInteger = BC_REG_ACCU; decaccu->mInteger = BC_REG_ACCU;
mScope->Insert(decaccu->mIdent, decaccu); mScope->Insert(decaccu->mIdent, decaccu);
Declaration* dassm = new Declaration(mScanner->mLocation, DT_TYPE_ASSEMBLER);
dassm->mScope = scope;
Declaration* vdasm = new Declaration(mScanner->mLocation, DT_CONST_ASSEMBLER);
vdasm->mVarIndex = -1;
vdasm->mSection = mCodeSection;
vdasm->mBase = dassm;
Expression* ifirst = new Expression(mScanner->mLocation, EX_ASSEMBLER); Expression* ifirst = new Expression(mScanner->mLocation, EX_ASSEMBLER);
Expression* ilast = ifirst, * ifinal = ifirst; Expression* ilast = ifirst, * ifinal = ifirst;
bool exitLabel = false; bool exitLabel = false;
ifirst->mDecType = dassm; ifirst->mDecType = dasm;
ifirst->mDecValue = vdasm; ifirst->mDecValue = vdasm;
vdasm->mValue = ifirst; vdasm->mValue = ifirst;
@ -12438,11 +12454,13 @@ Expression* Parser::ParseAssembler(void)
mScope->End(mScanner->mLocation); mScope->End(mScanner->mLocation);
mScope = mScope->mParent; mScope = mScope->mParent;
dassm->mSize = offset; dasm->mSize = offset;
dassm->mScope->mParent = nullptr; dasm->mScope->mParent = nullptr;
mScanner->SetAssemblerMode(false); mScanner->SetAssemblerMode(false);
vdasm->mFlags |= DTF_DEFINED;
return ifirst; return ifirst;
} }
@ -12785,7 +12803,7 @@ void Parser::ParsePragma(void)
} }
else else
{ {
mErrors->Error(mScanner->mLocation, EERR_OBJECT_NOT_FOUND, "Runtime function not found"); mErrors->Error(mScanner->mLocation, EERR_OBJECT_NOT_FOUND, "Runtime function not found", rtident);
mScanner->NextToken(); mScanner->NextToken();
} }
@ -13553,16 +13571,34 @@ void Parser::Parse(void)
const Ident* ident = mScanner->mTokenIdent; const Ident* ident = mScanner->mTokenIdent;
mScanner->NextToken(); mScanner->NextToken();
Declaration* vdasm = mScope->Lookup(ident);
if (vdasm)
{
if (vdasm->mType != DT_CONST_ASSEMBLER || (vdasm->mFlags & DTF_DEFINED))
{
mErrors->Error(mScanner->mLocation, EERR_DUPLICATE_DEFINITION, "Duplicate definition", ident);
mErrors->Error(vdasm->mLocation, EINFO_ORIGINAL_DEFINITION, "Original definition");
}
}
else
{
Declaration * dasm = new Declaration(mScanner->mLocation, DT_TYPE_ASSEMBLER);
dasm->mScope = new DeclarationScope(mScope, SLEVEL_LOCAL);
vdasm = new Declaration(mScanner->mLocation, DT_CONST_ASSEMBLER);
vdasm->mVarIndex = -1;
vdasm->mSection = mCodeSection;
vdasm->mBase = dasm;
vdasm->mIdent = ident;
vdasm->mQualIdent = ident;
mScope->Insert(ident, vdasm);
}
if (mScanner->mToken == TK_OPEN_BRACE) if (mScanner->mToken == TK_OPEN_BRACE)
{ {
mScanner->NextToken(); mScanner->NextToken();
Expression* exp = ParseAssembler(); Expression* exp = ParseAssembler(vdasm);
exp->mDecValue->mIdent = ident;
exp->mDecValue->mQualIdent = ident;
mScope->Insert(ident, exp->mDecValue);
if (mScanner->mToken == TK_CLOSE_BRACE) if (mScanner->mToken == TK_CLOSE_BRACE)
mScanner->NextToken(); mScanner->NextToken();

View File

@ -82,7 +82,7 @@ protected:
Declaration* ReverseDeclaration(Declaration* odec, Declaration* bdec); Declaration* ReverseDeclaration(Declaration* odec, Declaration* bdec);
Expression* ParseFunction(Declaration* dec); Expression* ParseFunction(Declaration* dec);
Expression* ParseAssembler(void); Expression* ParseAssembler(Declaration * vdassm = nullptr);
Expression* ParseAssemblerBaseOperand(Declaration* pcasm, int pcoffset); Expression* ParseAssemblerBaseOperand(Declaration* pcasm, int pcoffset);
Expression* ParseAssemblerMulOperand(Declaration* pcasm, int pcoffset); Expression* ParseAssemblerMulOperand(Declaration* pcasm, int pcoffset);