]> git.sur5r.net Git - cc65/blobdiff - src/cc65/function.c
Added the io module
[cc65] / src / cc65 / function.c
index e8fe845750d8f40431f8ba5dc3a506bd18ad8797..7f26312d94ef6200f9a63a22acfcbf6f34a7fb71 100644 (file)
@@ -33,6 +33,8 @@
 
 
 
+#include "../common/xmalloc.h"
+
 #include "asmcode.h"
 #include "asmlabel.h"
 #include "codegen.h"
@@ -40,7 +42,6 @@
 #include "funcdesc.h"
 #include "litpool.h"
 #include "locals.h"
-#include "mem.h"
 #include "scanner.h"
 #include "stmt.h"
 #include "symtab.h"
@@ -60,9 +61,8 @@ struct Function {
     type*              ReturnType;     /* Function return type */
     struct FuncDesc*   Desc;           /* Function descriptor */
     CodeMark           EntryCode;      /* Backpatch addr for entry code */
-    int                        LocalMax;       /* Total space for locals */
-    int                        LocalSize;      /* Current space for locals */
-    unsigned           RetLab;         /* Return code label */
+    int                        Reserved;       /* Reserved local space */
+    unsigned           RetLab;         /* Return code label */
 };
 
 /* Pointer to current function */
@@ -87,8 +87,7 @@ static Function* NewFunction (struct SymEntry* Sym)
     F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
     F->Desc      = DecodePtr (Sym->Type + 1);
     F->EntryCode  = 0;
-    F->LocalMax          = 0;
-    F->LocalSize  = 0;
+    F->Reserved          = 0;
     F->RetLab    = GetLabel ();
 
     /* Return the new structure */
@@ -153,39 +152,42 @@ unsigned GetRetLab (const Function* F)
 
 
 
-int AllocLocalSpace (Function* F, unsigned Size)
-/* Allocate space for the function locals, return stack offset */
+int ReserveLocalSpace (Function* F, unsigned Size)
+/* Reserve (but don't allocate) the given local space and return the stack
+ * offset.
+ */
 {
-    /* Add the size */
-    F->LocalSize += Size;                      
-    if (F->LocalSize > F->LocalMax) {
-       F->LocalMax = F->LocalSize;
-    }
-
-    /* Return the offset, it is below the initial stack pointer */
-    return -F->LocalSize;;
+    F->Reserved += Size;
+    return oursp - F->Reserved;
 }
 
 
 
-void FreeLocalSpace (Function* F, unsigned Size)
-/* Free space allocated for function locals */
+void AllocLocalSpace (Function* F)
+/* Allocate any local space previously reserved. The function will do
+ * nothing if there is no reserved local space.
+ */
 {
-    F->LocalSize -= Size;
-}
+    if (F->Reserved > 0) {
 
+       /* Switch to the code segment */
+       g_usecode ();
 
+       /* Create space on the stack */
+       g_space (F->Reserved);
 
-unsigned GetLocalSpace (const Function* F)
-/* Get the local variable space needed for the function */
-{
-    return F->LocalMax;
+       /* Correct the stack pointer */
+       oursp -= F->Reserved;
+
+       /* Nothing more reserved */
+       F->Reserved = 0;
+    }
 }
 
 
 
 /*****************************************************************************/
-/*                                          code                                    */
+/*                                          code                                    */
 /*****************************************************************************/
 
 
@@ -213,7 +215,7 @@ void NewFunc (SymEntry* Func)
     }
 
     /* Need a starting curly brace */
-    if (curtok != LCURLY) {
+    if (curtok != TOK_LCURLY) {
        Error (ERR_LCURLY_EXPECTED);
     }