Merge pull request #2495 from kugelfuhr/kugelfuhr/dont-save-stuff-in-main

Generate shorter code for main() in some cases
This commit is contained in:
Bob Andrews 2024-09-01 22:54:45 +02:00 committed by GitHub
commit 2680bc8dec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 65 additions and 34 deletions

View File

@ -507,34 +507,39 @@ void g_enter (unsigned flags, unsigned argsize)
void g_leave (void)
void g_leave (int IsMainFunc)
/* Function epilogue */
{
/* How many bytes of locals do we have to drop? */
unsigned ToDrop = (unsigned) -StackPtr;
/* In the main function nothing has to be dropped because the program
** is terminated anyway.
*/
if (!IsMainFunc) {
/* How many bytes of locals do we have to drop? */
unsigned ToDrop = (unsigned) -StackPtr;
/* If we didn't have a variable argument list, don't call leave */
if (funcargs >= 0) {
/* If we didn't have a variable argument list, don't call leave */
if (funcargs >= 0) {
/* Drop stackframe if needed */
g_drop (ToDrop + funcargs);
/* Drop stackframe if needed */
g_drop (ToDrop + funcargs);
} else if (StackPtr != 0) {
} else if (StackPtr != 0) {
/* We've a stack frame to drop */
if (ToDrop > 255) {
g_drop (ToDrop); /* Inlines the code */
AddCodeLine ("jsr leave");
} else {
AddCodeLine ("ldy #$%02X", ToDrop);
AddCodeLine ("jsr leavey");
}
/* We've a stack frame to drop */
if (ToDrop > 255) {
g_drop (ToDrop); /* Inlines the code */
AddCodeLine ("jsr leave");
} else {
AddCodeLine ("ldy #$%02X", ToDrop);
AddCodeLine ("jsr leavey");
/* Nothing to drop */
AddCodeLine ("jsr leave");
}
} else {
/* Nothing to drop */
AddCodeLine ("jsr leave");
}
/* Add the final rts */

View File

@ -247,7 +247,7 @@ void g_scale (unsigned flags, long val);
void g_enter (unsigned flags, unsigned argsize);
/* Function prologue */
void g_leave (void);
void g_leave (int IsMainFunc);
/* Function epilogue */

View File

@ -646,11 +646,13 @@ void NewFunc (SymEntry* Func, FuncDesc* D)
/* Output the function exit code label */
g_defcodelabel (F_GetRetLab (CurrentFunc));
/* Restore the register variables */
F_RestoreRegVars (CurrentFunc);
/* Restore the register variables (not necessary for main function) */
if (!F_IsMainFunc (CurrentFunc)) {
F_RestoreRegVars (CurrentFunc);
}
/* Generate the exit code */
g_leave ();
g_leave (F_IsMainFunc (CurrentFunc));
/* Emit references to imports/exports */
EmitExternals ();

View File

@ -111,22 +111,31 @@ static void ParseRegisterDecl (Declarator* Decl, int Reg)
/* Get the size of the variable */
unsigned Size = SizeOf (Decl->Type);
/* Save the current contents of the register variable on stack */
F_AllocLocalSpace (CurrentFunc);
g_save_regvars (Reg, Size);
/* Add the symbol to the symbol table. We do that now, because for register
** variables the current stack pointer is implicitly used as location for
** the save area.
*/
Sym = AddLocalSym (Decl->Ident, Decl->Type, Decl->StorageClass, Reg);
/* Check for an optional initialization */
if (CurTok.Tok == TOK_ASSIGN) {
/* Skip the '=' */
NextToken ();
/* If the register variable is initialized, the initialization code may
** access other already declared variables. This means that we have to
** allocate them now.
*/
F_AllocLocalSpace (CurrentFunc);
/* Save the current contents of the register variable on stack. This is
** not necessary for the main function.
*/
if (!F_IsMainFunc (CurrentFunc)) {
g_save_regvars (Reg, Size);
}
/* Add the symbol to the symbol table. We do that now, because for
** register variables the current stack pointer is implicitly used
** as location for the save area (unused in case of main()).
*/
Sym = AddLocalSym (Decl->Ident, Decl->Type, Decl->StorageClass, Reg);
/* Special handling for compound types */
if (IsCompound) {
@ -173,6 +182,21 @@ static void ParseRegisterDecl (Declarator* Decl, int Reg)
/* Mark the variable as referenced */
Sym->Flags |= SC_REF;
} else {
/* Save the current contents of the register variable on stack. This is
** not necessary for the main function.
*/
if (!F_IsMainFunc (CurrentFunc)) {
F_AllocLocalSpace (CurrentFunc);
g_save_regvars (Reg, Size);
}
/* Add the symbol to the symbol table. We do that now, because for
** register variables the current stack pointer is implicitly used
** as location for the save area (unused in case of main()).
*/
Sym = AddLocalSym (Decl->Ident, Decl->Type, Decl->StorageClass, Reg);
}
/* Cannot allocate a variable of unknown size */