diff --git a/include/crt.c b/include/crt.c index d0111c9..b0d0f6d 100644 --- a/include/crt.c +++ b/include/crt.c @@ -3072,7 +3072,7 @@ __asm inp_binop_sub_f32 #pragma bytecode(BC_BINOP_SUB_F32, inp_binop_sub_f32) -__asm fmul8 +__asm crt_fmul8 { sec ror @@ -3106,7 +3106,7 @@ W1: rts } -__asm fmul +__asm crt_fmul { lda accu ora accu + 1 @@ -3147,13 +3147,13 @@ W2: beq W5 bne W6 W4: - jsr fmul8 + jsr crt_fmul8 lda tmp + 1 W6: - jsr fmul8 + jsr crt_fmul8 W5: lda tmp + 2 - jsr fmul8 + jsr crt_fmul8 sec lda tmp + 8 @@ -3210,12 +3210,12 @@ __asm inp_binop_mul_f32 { jsr freg.split_exp sty tmpy - jsr fmul + jsr crt_fmul ldy tmpy jmp startup.exec } -__asm fdiv +__asm crt_fdiv { lda accu ora accu + 1 @@ -3337,13 +3337,13 @@ ZERO: __asm inp_binop_div_f32 { jsr freg.split_exp - jsr fdiv + jsr crt_fdiv jmp startup.exec } #pragma bytecode(BC_BINOP_DIV_F32, inp_binop_div_f32) -__asm fcmp +__asm crt_fcmp { lda accu + 3 eor tmp + 3 @@ -4058,9 +4058,9 @@ __asm load32 #pragma runtime(fmergea, freg.merge_aexp) #pragma runtime(fadd, faddsub.fadd) #pragma runtime(fsub, faddsub.fsub) -#pragma runtime(fmul, fmul) -#pragma runtime(fdiv, fdiv) -#pragma runtime(fcmp, fcmp) +#pragma runtime(fmul, crt_fmul) +#pragma runtime(fdiv, crt_fdiv) +#pragma runtime(fcmp, crt_fcmp) #pragma runtime(ffromi, sint16_to_float) #pragma runtime(ffromu, uint16_to_float) #pragma runtime(ftoi, f32_to_i16) @@ -4558,7 +4558,7 @@ struct Heap { #pragma section(heap, 0x0000, HeapStart, HeapEnd) -__asm malloc +__asm crt_malloc { // make room for two additional bytes // to store pointer to end of used memory @@ -4785,14 +4785,14 @@ hc2: __asm inp_malloc { sty tmpy - jsr malloc + jsr crt_malloc ldy tmpy jmp startup.exec } #pragma bytecode(BC_MALLOC, inp_malloc) -__asm free +__asm crt_free { // check nullptr free @@ -5009,21 +5009,21 @@ nostart: __asm inp_free { sty tmpy - jsr free + jsr crt_free ldy tmpy jmp startup.exec } #pragma bytecode(BC_FREE, inp_free) -__asm breakpoint +__asm crt_breakpoint { rts } -#pragma runtime(malloc, malloc) -#pragma runtime(free, free) -#pragma runtime(breakpoint, breakpoint) +#pragma runtime(malloc, crt_malloc) +#pragma runtime(free, crt_free) +#pragma runtime(breakpoint, crt_breakpoint) #if 0 diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index df25b6c..c1e75aa 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -830,6 +830,14 @@ void InterCodeGenerator::InitGlobalVariable(InterCodeModule * mod, Declaration* void InterCodeGenerator::TranslateAssembler(InterCodeModule* mod, Declaration * adec,GrowingArray * refvars) { 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 offsetMap(-1); diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index b88a7ec..906fc21 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -11813,8 +11813,17 @@ Expression* Parser::ParseAssemblerBaseOperand(Declaration* pcasm, int pcoffset) exp->mDecValue = ldec; 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); + 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(); } @@ -12126,9 +12135,24 @@ void Parser::AddAssemblerRegister(const Ident* ident, int value) 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; mScanner->SetAssemblerMode(true); @@ -12153,21 +12177,13 @@ Expression* Parser::ParseAssembler(void) decaccu->mInteger = BC_REG_ACCU; 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* ilast = ifirst, * ifinal = ifirst; bool exitLabel = false; - ifirst->mDecType = dassm; + ifirst->mDecType = dasm; ifirst->mDecValue = vdasm; vdasm->mValue = ifirst; @@ -12438,11 +12454,13 @@ Expression* Parser::ParseAssembler(void) mScope->End(mScanner->mLocation); mScope = mScope->mParent; - dassm->mSize = offset; - dassm->mScope->mParent = nullptr; + dasm->mSize = offset; + dasm->mScope->mParent = nullptr; mScanner->SetAssemblerMode(false); + vdasm->mFlags |= DTF_DEFINED; + return ifirst; } @@ -12785,7 +12803,7 @@ void Parser::ParsePragma(void) } 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(); } @@ -13553,16 +13571,34 @@ void Parser::Parse(void) const Ident* ident = mScanner->mTokenIdent; 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) { mScanner->NextToken(); - Expression* exp = ParseAssembler(); - - exp->mDecValue->mIdent = ident; - exp->mDecValue->mQualIdent = ident; - - mScope->Insert(ident, exp->mDecValue); + Expression* exp = ParseAssembler(vdasm); if (mScanner->mToken == TK_CLOSE_BRACE) mScanner->NextToken(); diff --git a/oscar64/Parser.h b/oscar64/Parser.h index bc7581e..c2f54df 100644 --- a/oscar64/Parser.h +++ b/oscar64/Parser.h @@ -82,7 +82,7 @@ protected: Declaration* ReverseDeclaration(Declaration* odec, Declaration* bdec); Expression* ParseFunction(Declaration* dec); - Expression* ParseAssembler(void); + Expression* ParseAssembler(Declaration * vdassm = nullptr); Expression* ParseAssemblerBaseOperand(Declaration* pcasm, int pcoffset); Expression* ParseAssemblerMulOperand(Declaration* pcasm, int pcoffset);