]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Switch flags field to be a union
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 29 Apr 2014 23:14:48 +0000 (19:14 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 29 Apr 2014 23:14:48 +0000 (19:14 -0400)
source/slvm/main.c
source/slvm/slvm.h

index 1b7c78a1486f337f0fd9fa1df4a4db786491783d..24d2db19529e5c7a10c30368be804ad863f3a58a 100644 (file)
@@ -82,7 +82,7 @@ defcode("wlink", wlink, 0, &latest){
 }
 
 defcode("wsize", wflags, 0, &wlink){
-    *(ArgStackPtr) = (val_t)(((word_t*)*(ArgStackPtr))->flags.codesize);
+    *(ArgStackPtr) = (val_t)(((word_t*)*(ArgStackPtr))->flags.attr.codesize);
 }
 
 defcode("wname", wname, 0, &wflags){
@@ -99,7 +99,7 @@ defcode("wcode", wcode, 0, &wfunc){
 
 defcode("here", here, 0, &wcode){
     ArgStackPtr++;
-    *(ArgStackPtr) = (val_t)((((word_t*)latest_val)->flags.codesize) - 1);
+    *(ArgStackPtr) = (val_t)((((word_t*)latest_val)->flags.attr.codesize) - 1);
 }
 
 /* Input/Output Words
@@ -207,7 +207,7 @@ defcode("findw", find_word, 0, &get_word){
     char* name = (char*)*(ArgStackPtr);
     while(curr)
     {
-        if (!(curr->flags.f_hidden) && (0 == strcmp(curr->name,name)))
+        if (!(curr->flags.attr.hidden) && (0 == strcmp(curr->name,name)))
         {
             break;
         }
@@ -267,9 +267,9 @@ defcode("create", create, 0, &rbrack){
     word_t* word   = (word_t*)malloc(sizeof(word_t));
     word->link     = (word_t*)latest_val;
     /* Initialize the flags (hidden and non-immediate by default) */
-    word->flags.f_immed  = 0;
-    word->flags.f_hidden = 1;
-    word->flags.codesize = 1;
+    word->flags.attr.immed    = 0;
+    word->flags.attr.hidden   = 1;
+    word->flags.attr.codesize = 1;
     /* Initialize the name, codeword, and bytecode */
     word->name     = name;
     word->codeword = &docolon;
@@ -285,21 +285,21 @@ defcode(",", comma, 0, &create){
     word_t* word  = (word_t*)latest_val;
     /* Put the next instruction in place of the terminating 'ret' that "here"
      * points too */
-    word->code[word->flags.codesize-1] = *(ArgStackPtr);
+    word->code[word->flags.attr.codesize-1] = *(ArgStackPtr);
     ArgStackPtr--;
     /* Resize the code section and relocate if necessary */
-    word->flags.codesize++;
-    word->code = (val_t*)realloc(word->code, word->flags.codesize * sizeof(val_t));
+    word->flags.attr.codesize++;
+    word->code = (val_t*)realloc(word->code, word->flags.attr.codesize * sizeof(val_t));
     /* Update "here" and terminate the code section */
-    word->code[word->flags.codesize-1] = (val_t)&ret;
+    word->code[word->flags.attr.codesize-1] = (val_t)&ret;
 }
 
 defcode("hidden", hidden, 1, &comma){
-    ((word_t*)*(ArgStackPtr))->flags.f_hidden ^= 1;
+    ((word_t*)*(ArgStackPtr))->flags.attr.hidden ^= 1;
 }
 
 defcode("immediate", immediate, 1, &hidden){
-    ((word_t*)*(ArgStackPtr))->flags.f_immed ^= 1;
+    ((word_t*)*(ArgStackPtr))->flags.attr.immed ^= 1;
 }
 
 defcode(":", colon, 0, &immediate){
@@ -353,7 +353,7 @@ defcode("interpret", interpret, 0, &parse_num){
     if (*ArgStackPtr)
     {
         /* If we are in immediate mode or the found word is marked immediate */
-        if((state_val == 0) || (((word_t*)*ArgStackPtr)->flags.f_immed))
+        if((state_val == 0) || (((word_t*)*ArgStackPtr)->flags.attr.immed))
         {
             /* Execute the word */
             EXEC(exec_word);
@@ -719,8 +719,8 @@ defcode("printdefw", printdefw, 0, &printallw){
     {
         printf("%s\t%ld %ld",
                word->name,
-               word->flags.f_immed,
-               word->flags.f_hidden);
+               word->flags.attr.immed,
+               word->flags.attr.hidden);
         word = word->link;
     }
 }
index f1b049a144409ad57ea73e86a29e29a32d07ca2c..33fdeaabbc80140a288c60b7ca52275d23d56993 100644 (file)
  */
 typedef void (*codeword_t)(val_t* code);
 
+/** A collection of flags describing attributes of the word. */
+typedef union {
+    /** The value of all flag bits combined */
+    val_t value;
+    /** Accessors for individual flag fields */
+    struct {
+        /** Flag if this word should be hidden from the interpreter */
+        val_t hidden   : 1;
+        /** flag if this word should be executed at compile time */
+        val_t immed    : 1;
+        /** Pads the flags to 8-bits */
+        val_t padding  : 6;
+        /** The length of the bytecode section of the word */
+        val_t codesize : CODE_SZ_BITS;
+    } attr;
+} flags_t;
+
 /**
     This structure contains all of the relevant attributes of a word definition
 */
@@ -46,12 +63,13 @@ typedef struct word_t {
     /** Pointer to the next most recently defined word in the dictionary. */
     struct word_t const* link;
     /** A collection of flags describing attributes of the word. */
-    struct {
-        val_t f_hidden : 1;  /*< Flag if this word should be hidden from the interpreter */
-        val_t f_immed  : 1;  /*< flag if this word should be executed at compile time */
-        val_t padding  : 6;  /*< Pads the flags to 8-bits */
-        val_t codesize : CODE_SZ_BITS; /*< The length of the bytecode section of the word */
-    } flags;
+    //struct {
+    //    val_t f_hidden : 1;  /*< Flag if this word should be hidden from the interpreter */
+    //    val_t f_immed  : 1;  /*< flag if this word should be executed at compile time */
+    //    val_t padding  : 6;  /*< Pads the flags to 8-bits */
+    //    val_t codesize : CODE_SZ_BITS; /*< The length of the bytecode section of the word */
+    //} flags;
+    flags_t flags;
     /** Pointer to the null terminated string that holds the name of the word. */
     char const* name;
     /**
@@ -75,15 +93,15 @@ typedef struct word_t {
 
 /**
  * Define a built-in word that executes native code */
-#define defcode(name_str,c_name,immed,prev)   \
+#define defcode(name_str,c_name,immed,prev)    \
     static void c_name##_code(val_t* code);    \
-    static word_t const c_name = {            \
-        prev,                                 \
-        { 0, immed, 0, 0 },                   \
-        name_str,                             \
-        &c_name##_code,                       \
-        0                                     \
-    };                                        \
+    static word_t const c_name = {             \
+        prev,                                  \
+        {.attr = { 0, immed, 0, 0 }},          \
+        name_str,                              \
+        &c_name##_code,                        \
+        0                                      \
+    };                                         \
     static void c_name##_code(val_t* inst_ptr) \
 
 /**