]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - asm.c
the lexer now doesn't _allocate_ the token structure, also: the vector holding the...
[xonotic/gmqcc.git] / asm.c
diff --git a/asm.c b/asm.c
index adb7678abd0a43653b2a3c164347a84a221fe559..c73f25cf7956b4121a6f5e2c6c6088ef8a2991db 100644 (file)
--- a/asm.c
+++ b/asm.c
@@ -32,18 +32,19 @@ typedef enum {
 } asm_state;
 
 typedef struct {
-    char *name;   /* name of constant    */
-    char  type;   /* type, float, vector, string */
+    char *name;
+    char  type;   /* type, float, vector, string, function*/
     char  elem;   /* 0=x, 1=y, or 2=Z?   */
     int   offset; /* location in globals */
-} globals;
-VECTOR_MAKE(globals, assembly_constants);
+    bool  isconst;
+} asm_sym;
+VECTOR_MAKE(asm_sym, asm_symbols);
 
 /*
  * Assembly text processing: this handles the internal collection
  * of text to allow parsing and assemblation.
  */
-static char *const asm_getline(size_t *byte, FILE *fp) {
+static char* asm_getline(size_t *byte, FILE *fp) {
     char   *line = NULL;
     size_t  read = util_getline(&line, byte, fp);
     *byte = read;
@@ -64,13 +65,13 @@ void asm_init(const char *file, FILE **fp) {
 }
 void asm_close(FILE *fp) {
     fclose(fp);
-    code_write();
+    code_write("program.dat");
 }
 void asm_clear() {
     size_t i = 0;
-    for (; i < assembly_constants_elements; i++)
-        mem_d(assembly_constants_data[i].name);
-    mem_d(assembly_constants_data);
+    for (; i < asm_symbols_elements; i++)
+        mem_d(asm_symbols_data[i].name);
+    mem_d(asm_symbols_data);
 }
 
 /*
@@ -79,8 +80,9 @@ void asm_clear() {
  */
 void asm_dumps() {
     size_t i = 0;
-    for (; i < assembly_constants_elements; i++) {
-        globals *g = &assembly_constants_data[i];
+    for (; i < asm_symbols_elements; i++) {
+        asm_sym *g = &asm_symbols_data[i];
+        if (!g->isconst) continue;
         switch (g->type) {
             case TYPE_VECTOR: {
                 util_debug("ASM", "vector %s %c[%f]\n", g->name,
@@ -91,6 +93,9 @@ void asm_dumps() {
                 );
                 break;
             }
+            case TYPE_FUNCTION: {
+                util_debug("ASM", "function %s\n", g->name);
+            }
         }
     }
 }
@@ -102,11 +107,11 @@ void asm_dumps() {
  * are locals.
  */
 static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
-    if (!(strstr(skip, "FLOAT:")  == &skip[0]) &&
-         (strstr(skip, "VECTOR:") == &skip[0]) &&
-         (strstr(skip, "ENTITY:") == &skip[0]) &&
-         (strstr(skip, "FIELD:")  == &skip[0]) &&
-         (strstr(skip, "STRING:") == &skip[0])) return false;
+    if ((strstr(skip, "FLOAT:")  != &skip[0]) &&
+        (strstr(skip, "VECTOR:") != &skip[0]) &&
+        (strstr(skip, "ENTITY:") != &skip[0]) &&
+        (strstr(skip, "FIELD:")  != &skip[0]) &&
+        (strstr(skip, "STRING:") != &skip[0])) return false;
 
     /* TODO: determine if constant, global, or local */
     switch (*skip) {
@@ -114,7 +119,7 @@ static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state
             float   val1;
             float   val2;
             float   val3;
-            globals global;
+            asm_sym sym;
 
             char *find = (char*)skip + 7;
             char *name = (char*)skip + 7;
@@ -150,12 +155,12 @@ static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state
                 PARSE_ELEMENT(find, val2, { find ++; while (*find == ' ') { find ++; } });
                 PARSE_ELEMENT(find, val3, { find ++; /* no need to do anything here */ });
                 #undef  PARSE_ELEMENT
-                #define BUILD_ELEMENT(X,Y)                 \
-                    global.type   = TYPE_VECTOR;           \
-                    global.name   = util_strdup(name);     \
-                    global.elem   = (X);                   \
-                    global.offset = code_globals_elements; \
-                    assembly_constants_add(global);        \
+                #define BUILD_ELEMENT(X,Y)              \
+                    sym.type   = TYPE_VECTOR;           \
+                    sym.name   = util_strdup(name);     \
+                    sym.elem   = (X);                   \
+                    sym.offset = code_globals_elements; \
+                    asm_symbols_add(sym);               \
                     code_globals_add(FLT2INT(Y))
                 BUILD_ELEMENT(0, val1);
                 BUILD_ELEMENT(1, val2);
@@ -194,8 +199,19 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
         return false;
 
     if (strstr(skip, "FUNCTION:") == &skip[0]) {
-        char  *copy = util_strsws(skip+10);
-        char  *name = util_strchp(copy, strchr(copy, '\0'));
+        asm_sym  sym;
+        char    *look = util_strdup(skip+10);
+        char    *copy = look;
+        char    *name = NULL;
+        while  (*copy == ' ' || *copy == '\t') copy++;
+
+        memset(&sym, 0, sizeof(asm_sym));
+
+        /*
+         * Chop the function name out of the string, this allocates
+         * a new string.
+         */
+        name = util_strchp(copy, strchr(copy, '\0'));
 
         /* TODO: failure system, missing name */
         if (!name) {
@@ -217,10 +233,11 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
          * to determine this.
          */
         if (strchr(name, ',')) {
+            char *find = strchr(name, ',') + 1;
             prog_section_function function;
             prog_section_def      def;
-
-            char *find = strchr(name, ',') + 1;
+            memset(&function, 0, sizeof(prog_section_function));
+            memset(&def,      0, sizeof(prog_section_def));
 
             /* skip whitespace */
             while (*find == ' ' || *find == '\t')
@@ -258,12 +275,15 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
             def.type            = TYPE_FUNCTION;
             def.offset          = code_globals_elements;
             def.name            = code_chars_elements;
-            memset(function.argsize, 0, sizeof(function.argsize));
             code_functions_add(function);
             code_defs_add     (def);
             code_chars_put    (name, strlen(name));
             code_chars_add    ('\0');
-            
+            sym.type   = TYPE_FUNCTION;
+            sym.name   = util_strdup(name);
+            sym.offset = function.entry;
+            asm_symbols_add(sym);
+
             util_debug("ASM", "added internal function %s to function table\n", name);
 
             /*
@@ -287,13 +307,15 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
             int   size = 0;
             char *find = strchr(name, '#');
             char *peek = find;
-            
+
             /*
              * Code structures for filling after determining the correct
              * information to add to the code write system.
              */
             prog_section_function function;
             prog_section_def      def;
+            memset(&function, 0, sizeof(prog_section_function));
+            memset(&def,      0, sizeof(prog_section_def));
             if (find) {
                 find ++;
 
@@ -380,7 +402,7 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
              * We got valid function structure information now. Lets add
              * the function to the code writer function table.
              */
-            function.entry      = code_statements_elements-1;
+            function.entry      = code_statements_elements;
             function.firstlocal = 0;
             function.locals     = 0;
             function.profile    = 0;
@@ -391,16 +413,20 @@ static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state
             def.offset          = code_globals_elements;
             def.name            = code_chars_elements;
             code_functions_add(function);
-            code_globals_add(code_statements_elements);
+            code_globals_add  (code_statements_elements);
             code_chars_put    (name, strlen(name));
             code_chars_add    ('\0');
+            sym.type   = TYPE_FUNCTION;
+            sym.name   = util_strdup(name);
+            sym.offset = function.entry;
+            asm_symbols_add(sym);
 
             /* update assembly state */
-            
+
             *state = ASM_FUNCTION;
             util_debug("ASM", "added context function %s to function table\n", name);
         }
-        
+
         mem_d(copy);
         mem_d(name);
         return true;
@@ -418,9 +444,17 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
      * CALL* is used (depending on the amount of arguments the function
      * is expected to take)
      */
-    char                  *c = (char*)skip;
+    enum {
+        EXPECT_FUNCTION = 1,
+        EXPECT_VARIABLE = 2,
+        EXPECT_VALUE    = 3
+    };
+    
+    char                  *c      = (char*)skip;
+    size_t                 i      = 0;
+    char                   expect = 0;
     prog_section_statement s;
-    size_t                 i = 0;
+    memset(&s, 0, sizeof(prog_section_statement));
 
     /*
      * statements are only allowed when inside a function body
@@ -436,7 +470,7 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
      */
     while (*skip == ' ' || *skip == '\t')
         skip++;
-    
+
     for (; i < sizeof(asm_instr)/sizeof(*asm_instr); i++) {
         /*
          * Iterate all possible instructions and check if the selected
@@ -444,7 +478,36 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
          * instruction.
          */
         if (!strncmp(skip, asm_instr[i].m, asm_instr[i].l)) {
-            printf("found statement %s\n", asm_instr[i].m);
+
+            /*
+             * We hit the end of a function scope, retarget the state
+             * and add a DONE statement to the statment table.
+             */
+            if (i == AINSTR_END) {
+                s.opcode = i;
+                code_statements_add(s);
+                *state = ASM_NULL;
+                return true;
+            }
+
+            /*
+             * Check the instruction type to see what sort of data
+             * it's expected to have.
+             */
+            if (i >= INSTR_CALL0 && i <= INSTR_CALL8)
+                expect = EXPECT_FUNCTION;
+            else
+                expect = EXPECT_VARIABLE;
+            
+            util_debug(
+                "ASM",
+                "found statement %s expecting: `%s` (%ld operand(s))\n",
+                asm_instr[i].m,
+                (expect == EXPECT_FUNCTION)?"function name":(
+                (expect == EXPECT_VARIABLE)?"variable name":(
+                (expect == EXPECT_VALUE    ?"value"        : "unknown"))),
+                asm_instr[i].o
+            );
             /*
              * Parse the operands for `i` (the instruction). The order
              * of asm_instr is in the order of the menomic encoding so
@@ -456,7 +519,7 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
                  * Each instruction can have from 0-3 operands; and can
                  * be used with less or more operands depending on it's
                  * selected use.
-                 * 
+                 *
                  * DONE for example can use either 0 operands, or 1 (to
                  * emulate the effect of RETURN)
                  *
@@ -467,44 +530,57 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
                  * This needs to have a fall state, we start from the
                  * end of the string and work backwards.
                  */
-                #define OPFILL(X)                                      \
-                    do {                                               \
-                        size_t w = 0;                                  \
-                        if (!(c = strrchr(c, ','))) {                  \
-                            printf("error, expected more operands\n"); \
-                            return false;                              \
-                        }                                              \
-                        c++;                                           \
-                        w++;                                           \
-                        while (*c == ' ' || *c == '\t') {              \
-                            c++;                                       \
-                            w++;                                       \
-                        }                                              \
-                        X  = (const char*)c;                           \
-                        c -= w;                                        \
-                       *c  = '\0';                                     \
-                        c  = (char*)skip;                              \
+                #define OPEATS(X,Y) X##Y
+                #define OPCCAT(X,Y) OPEATS(X,Y)
+                #define OPLOAD(X,Y)                                                                \
+                    do {                                                                           \
+                        util_debug("ASM", "loading operand data ...\n");                           \
+                        if (expect == EXPECT_VARIABLE) {                                           \
+                            size_t f=0;                                                            \
+                            for (; f<asm_symbols_elements; f++) {                                  \
+                                if (!strncmp(asm_symbols_data[f].name, (Y), strlen(Y)) &&          \
+                                             asm_symbols_data[f].type != TYPE_FUNCTION) {          \
+                                    (X)=asm_symbols_data[f].offset;                                \
+                                    goto OPCCAT(foundv, __LINE__);                                 \
+                                }                                                                  \
+                            }                                                                      \
+                            printf("no variable named %s\n", (Y));                                 \
+                            break;                                                                 \
+                            OPCCAT(foundv,__LINE__) :                                              \
+                            printf("operand loaded for %s\n", (Y));                                \
+                        } else if (expect == EXPECT_FUNCTION) {                                    \
+                            /*                                                                     \
+                             * It's a function call not a variable association with an instruction \
+                             * these are harder to handle.                                         \
+                             */                                                                    \
+                            size_t f=0;                                                            \
+                            if (strchr(Y, ' ')) {                                                  \
+                                *strchr(Y, ' ')='\0';                                              \
+                            }                                                                      \
+                            for (; f<asm_symbols_elements; f++) {                                  \
+                                if (!strncmp(asm_symbols_data[f].name, (Y), strlen(Y)) &&          \
+                                            asm_symbols_data[f].type == TYPE_FUNCTION) {           \
+                                    (X)=asm_symbols_data[f].offset;                                \
+                                    goto OPCCAT(foundf, __LINE__);                                 \
+                                }                                                                  \
+                            }                                                                      \
+                            printf("no function named [%s]\n", (Y));                               \
+                            break;                                                                 \
+                            OPCCAT(foundf,__LINE__) :                                              \
+                            printf("operand loaded for [%s]\n", (Y));                              \
+                        }                                                                          \
                     } while (0)
-                    
-                case 3: {
-                    const char *data; OPFILL(data);
-                    printf("OP3: %s\n", data);
-                    s.o3.s1 = 0;
-                }
-                case 2: {
-                    const char *data; OPFILL(data);
-                    printf("OP2: %s\n", data);
-                    s.o2.s1 = 0;
-                }
+                case 3: { OPLOAD(s.o3.s1,c); break; }
+                case 2: { OPLOAD(s.o2.s1,c); break; }
                 case 1: {
                     while (*c == ' ' || *c == '\t') c++;
                     c += asm_instr[i].l;
                     while (*c == ' ' || *c == '\t') c++;
-                    
-                    printf("OP1: %s\n", c);
-                    s.o1.s1 = 0;
+                    OPLOAD(s.o1.s1, c);
+                    break;
                 }
-                #undef OPFILL
+                #undef OPLOAD
+                #undef OPCCAT
             }
             /* add the statement now */
             code_statements_add(s);
@@ -515,7 +591,6 @@ static GMQCC_INLINE bool asm_parse_stmt(const char *skip, size_t line, asm_state
 
 void asm_parse(FILE *fp) {
     char     *data  = NULL;
-    char     *skip  = NULL;
     long      line  = 1; /* current line */
     size_t    size  = 0; /* size of line */
     asm_state state = ASM_NULL;
@@ -523,22 +598,21 @@ void asm_parse(FILE *fp) {
     #define asm_end(x)            \
         do {                      \
             mem_d(data);          \
-            mem_d(copy);          \
-            line++;               \
+            line ++;              \
             util_debug("ASM", x); \
         } while (0); continue
 
     while ((data = asm_getline (&size, fp)) != NULL) {
-        char *copy = util_strsws(data); /* skip   whitespace */
-              skip = util_strrnl(copy); /* delete newline    */
-
-        /* TODO: statement END check */
-        if (state == ASM_FUNCTION)
-            state =  ASM_NULL;
-
-        if (asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
-        if (asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
-        if (asm_parse_stmt(skip, line, &state)){ asm_end("asm_parse_stmt\n"); }
+        char   *copy = data;
+        char   *skip = copy;
+        while (*copy == ' ' || *copy == '\t') copy++;
+        while (*skip != '\n')                 skip++;
+        *skip='\0';
+              
+        if (asm_parse_type(copy, line, &state)){ asm_end("asm_parse_type\n"); }
+        if (asm_parse_func(copy, line, &state)){ asm_end("asm_parse_func\n"); }
+        if (asm_parse_stmt(copy, line, &state)){ asm_end("asm_parse_stmt\n"); }
+        asm_end("asm_parse_white\n");
     }
     #undef asm_end
     asm_dumps();