Get rid of external reads into struct class fields.
authorIlari Liusvaara <[email protected]>
Fri, 17 Aug 2007 14:53:15 +0000 (17 17:53 +0300)
committerIlari Liusvaara <[email protected]>
Fri, 17 Aug 2007 14:53:15 +0000 (17 17:53 +0300)
capfuck.c
class.c
class.h

index afdfc47..f5d0938 100644 (file)
--- a/capfuck.c
+++ b/capfuck.c
@@ -353,7 +353,7 @@ static void singlestep_context(struct context* ctx, struct message* msg,
        mutex_lock(&ctx->c_lock);
 
        if(ctx->c_alt_flag) {
-               switch(tc->c_body[ctx->c_ip]) {
+               switch(class_get_ins(tc, ctx->c_ip)) {
                case 'f':
                case 'F':
                case 'l':
@@ -371,16 +371,7 @@ static void singlestep_context(struct context* ctx, struct message* msg,
                }
        }
 
-       switch(tc->c_body[ctx->c_ip]) {
-       case '\\':
-               while(1) {
-                       if(ctx->c_ip >= tc->c_body_length)
-                               break;
-                       if(!sane_isspace(tc->c_body[ctx->c_ip]))
-                               break;
-                       ctx->c_ip++;
-               }
-               break;
+       switch(class_get_ins(tc, ctx->c_ip)) {
        case '+':
                ctx->c_numreg++;
                break;
@@ -395,11 +386,11 @@ static void singlestep_context(struct context* ctx, struct message* msg,
                break;
        case 'f':
                tmp1 = pop_stack(ctx);
-               if(ctx->c_numreg < tc->c_fields
+               if(ctx->c_numreg < class_field_count(tc)
                        t->o_fields[ctx->c_numreg] = tmp1;
                break;
        case 'F':
-               if(ctx->c_numreg < tc->c_fields)
+               if(ctx->c_numreg < class_field_count(tc))
                        tmp1 = t->o_fields[ctx->c_numreg];
                else
                        tmp1 = NULL;
@@ -407,11 +398,11 @@ static void singlestep_context(struct context* ctx, struct message* msg,
                break;
        case 'l':
                tmp1 = pop_stack(ctx);
-               if(ctx->c_numreg < tc->c_locals)
+               if(ctx->c_numreg < class_local_count(tc))
                        ctx->c_locals[ctx->c_numreg] = tmp1;
                break;
        case 'L':
-               if(ctx->c_numreg < tc->c_locals)
+               if(ctx->c_numreg < class_local_count(tc))
                        tmp1 = ctx->c_locals[ctx->c_numreg];
                else
                        tmp1 = NULL;
@@ -461,7 +452,7 @@ static void singlestep_context(struct context* ctx, struct message* msg,
                fields = 0;
                tmp5 = class_search(ctx->c_numreg);
                if(tmp5)
-                       fields = tmp5->c_fields;
+                       fields = class_field_count(tmp5);
                tmp1 = malloc(sizeof(struct object) + fields *
                        sizeof(struct object*));
                if(!tmp1) {
@@ -508,7 +499,7 @@ static unsigned execute_msghandler(struct context* ctx)
                return 1;
        }
 
-       while(ctx->c_ip < tc->c_body_length) {
+       while(ctx->c_ip < class_ins_count(tc)) {
                singlestep_context(ctx, m, t, tc);
        }
 
@@ -565,7 +556,7 @@ static void recursive_mark_live(struct object* object)
                return;
 
        /* Recurse. */
-       for(size_t i = 0; i < object->o_class->c_fields; i++)
+       for(size_t i = 0; i < class_field_count(object->o_class); i++)
                recursive_mark_live(object->o_fields[i]);
 }
 
@@ -713,7 +704,7 @@ int main(int argc, char** argv)
                exit(1);
        }
        struct object* object = malloc(sizeof(struct object) + 
-               class->c_fields * sizeof(struct object*));
+               class_field_count(class) * sizeof(struct object*));
        struct message* message = malloc(sizeof(struct message) +
                3 * sizeof(struct object*));
        struct vat* vat = malloc(sizeof(struct vat));
@@ -735,7 +726,7 @@ int main(int argc, char** argv)
        object->o_special = NULL;
        object->o_prev = NULL;
        object->o_next = capfuck_stdin;
-       for(size_t i = 0; i < class->c_fields; i++)
+       for(size_t i = 0; i < class_field_count(class); i++)
                object->o_fields[i] = NULL;
        capfuck_stdin->o_flags = 0;
        capfuck_stdin->o_class = NULL;
diff --git a/class.c b/class.c
index 24f0c90..98de286 100644 (file)
--- a/class.c
+++ b/class.c
@@ -46,3 +46,29 @@ void class_unregister_all()
                free(class);
        }
 }
+
+/*****************************************************************************/
+size_t class_field_count(struct class* class)
+{
+       return class->c_fields;
+}
+
+/*****************************************************************************/
+size_t class_local_count(struct class* class)
+{
+       return class->c_locals;
+}
+
+/*****************************************************************************/
+size_t class_ins_count(struct class* class)
+{
+       return class->c_body_length;
+}
+
+/*****************************************************************************/
+unsigned char class_get_ins(struct class* class, size_t position)
+{
+       if(position >= class->c_body_length)
+               return 0;
+       return class->c_body[position];
+}
diff --git a/class.h b/class.h
index fd923dc..d6b61fc 100644 (file)
--- a/class.h
+++ b/class.h
@@ -53,4 +53,62 @@ void class_register(struct class* class);
  *****************************************************************************/
 void class_unregister_all();
 
+/******************************************************************************
+ *
+ * DESCRIPTION:
+ *     Get number of fields in class.
+ *
+ * PARAMETERS:
+ *     class                   The class to interrogate.
+ *
+ * RETURN VALUE:
+ *     Number of fields.
+ *
+ *****************************************************************************/
+size_t class_field_count(struct class* class);
+
+/******************************************************************************
+ *
+ * DESCRIPTION:
+ *     Get number of locals in class.
+ *
+ * PARAMETERS:
+ *     class                   The class to interrogate.
+ *
+ * RETURN VALUE:
+ *     Number of locals.
+ *
+ *****************************************************************************/
+size_t class_local_count(struct class* class);
+
+/******************************************************************************
+ *
+ * DESCRIPTION:
+ *     Get number of instructions in class.
+ *
+ * PARAMETERS:
+ *     class                   The class to interrogate.
+ *
+ * RETURN VALUE:
+ *     Number of instructions.
+ *
+ *****************************************************************************/
+size_t class_ins_count(struct class* class);
+
+/******************************************************************************
+ *
+ * DESCRIPTION:
+ *     Get instruction from class.
+ *
+ * PARAMETERS:
+ *     class                   The class to interrogate.
+ *     position                The position of instruction (zero based).
+ *
+ * RETURN VALUE:
+ *     The instruction, or 0 if index is out of range.
+ *
+ *****************************************************************************/
+unsigned char class_get_ins(struct class* class, size_t position);
+
+
 #endif