Skip to content
Browse files

libfpvm: introduce "struct sym" as abstraction for an identifier

This has ripple effects all through libfpvm but shouldn't change
functionality.
  • Loading branch information...
1 parent 44acf61 commit 99f250201fac449b11d562043da59c9134a49ca9 @wpwrak wpwrak committed Jan 4, 2012
Showing with 52 additions and 42 deletions.
  1. +1 −12 software/include/fpvm/ast.h
  2. +11 −10 software/include/fpvm/fpvm.h
  3. +18 −0 software/include/fpvm/symbol.h
  4. +22 −20 software/libfpvm/fpvm.c
View
13 software/include/fpvm/ast.h
@@ -59,18 +59,7 @@ struct ast_branches {
struct ast_node {
enum ast_op op;
- /*
- * NOTE: don't use the old convention below. Use node->op and/or
- * node_is_op.
- *
- * label is an empty string:
- * node is a constant
- * label is not an empty string and branch A is null:
- * node is variable "label"
- * label is not an empty string and branch A is not null:
- * node is function/operator "label"
- */
- const char *label;
+ struct sym *sym;
union {
struct ast_branches branches;
float constant;
View
21 software/include/fpvm/fpvm.h
@@ -26,6 +26,7 @@
#include <fpvm/is.h>
#include <fpvm/ast.h>
+#include <fpvm/symbol.h>
#define FPVM_MAXBINDINGS 128
#define FPVM_MAXTBINDINGS 128
@@ -37,19 +38,19 @@
#define FPVM_MAXERRLEN 64
-typedef void (*fpvm_bind_callback)(void *, const char *, int);
+typedef void (*fpvm_bind_callback)(void *, struct sym *, int);
struct fpvm_binding {
int isvar;
union {
float c;
- const char *v;
+ struct sym *v;
} b;
};
struct fpvm_tbinding {
int reg;
- const char *sym;
+ struct sym *sym;
};
enum {
@@ -102,7 +103,7 @@ struct fpvm_fragment {
int vector_mode;
};
-extern const char *_Xi, *_Yi, *_Xo, *_Yo;
+extern struct sym *_Xi, *_Yi, *_Xo, *_Yo;
const char *fpvm_version(void);
@@ -111,13 +112,13 @@ const char *fpvm_get_last_error(struct fpvm_fragment *fragment);
void fpvm_set_bind_mode(struct fpvm_fragment *fragment, int bind_mode);
void fpvm_set_bind_callback(struct fpvm_fragment *fragment, fpvm_bind_callback callback, void *user);
-int fpvm_bind(struct fpvm_fragment *fragment, const char *sym);
-void fpvm_set_xin(struct fpvm_fragment *fragment, const char *sym);
-void fpvm_set_yin(struct fpvm_fragment *fragment, const char *sym);
-void fpvm_set_xout(struct fpvm_fragment *fragment, const char *sym);
-void fpvm_set_yout(struct fpvm_fragment *fragment, const char *sym);
+int fpvm_bind(struct fpvm_fragment *fragment, struct sym *sym);
+void fpvm_set_xin(struct fpvm_fragment *fragment, struct sym *sym);
+void fpvm_set_yin(struct fpvm_fragment *fragment, struct sym *sym);
+void fpvm_set_xout(struct fpvm_fragment *fragment, struct sym *sym);
+void fpvm_set_yout(struct fpvm_fragment *fragment, struct sym *sym);
-int fpvm_do_assign(struct fpvm_fragment *fragment, const char *dest,
+int fpvm_do_assign(struct fpvm_fragment *fragment, struct sym *dest,
struct ast_node *n);
void fpvm_get_references(struct fpvm_fragment *fragment, int *references);
View
18 software/include/fpvm/symbol.h
@@ -0,0 +1,18 @@
+/*
+ * symbol.h - Symbol table entry
+ *
+ * Copyright 2012 by Werner Almesberger
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ */
+
+#ifndef __FPVM_SYMBOL_H
+#define __FPVM_SYMBOL_H
+
+struct sym {
+ const char *name;
+};
+
+#endif /* __FPVM_SYMBOL_H */
View
42 software/libfpvm/fpvm.c
@@ -19,12 +19,14 @@
#include <base/version.h>
#include <fpvm/is.h>
+#include <fpvm/symbol.h>
#include <fpvm/fpvm.h>
#include <fpvm/ast.h>
-const char *_Xi, *_Yi, *_Xo, *_Yo; /* unique, provided by user of libfpvm */
+struct sym *_Xi, *_Yi, *_Xo, *_Yo; /* unique, provided by user of libfpvm */
+static struct sym dummy_sym = { .name = "" };
const char *fpvm_version(void)
{
@@ -44,7 +46,7 @@ void fpvm_do_init(struct fpvm_fragment *fragment, int vector_mode)
fragment->bindings[1].b.v = _Yi;
/* Prevent binding of R2 (we need it for "if") */
fragment->bindings[2].isvar = 1;
- fragment->bindings[2].b.v = "";
+ fragment->bindings[2].b.v = &dummy_sym;
fragment->ntbindings = 2;
fragment->tbindings[0].reg = -1;
@@ -78,13 +80,13 @@ void fpvm_set_bind_mode(struct fpvm_fragment *fragment, int bind_mode)
fragment->bind_mode = bind_mode;
}
-int fpvm_bind(struct fpvm_fragment *fragment, const char *sym)
+int fpvm_bind(struct fpvm_fragment *fragment, struct sym *sym)
{
int r;
if(fragment->nbindings == FPVM_MAXBINDINGS) {
snprintf(fragment->last_error, FPVM_MAXERRLEN,
- "Failed to allocate register for variable: %s", sym);
+ "Failed to allocate register for variable: %s", sym->name);
return FPVM_INVALID_REG;
}
r = fragment->nbindings++;
@@ -95,27 +97,27 @@ int fpvm_bind(struct fpvm_fragment *fragment, const char *sym)
return r;
}
-void fpvm_set_xin(struct fpvm_fragment *fragment, const char *sym)
+void fpvm_set_xin(struct fpvm_fragment *fragment, struct sym *sym)
{
fragment->bindings[0].b.v = sym;
}
-void fpvm_set_yin(struct fpvm_fragment *fragment, const char *sym)
+void fpvm_set_yin(struct fpvm_fragment *fragment, struct sym *sym)
{
fragment->bindings[1].b.v = sym;
}
-void fpvm_set_xout(struct fpvm_fragment *fragment, const char *sym)
+void fpvm_set_xout(struct fpvm_fragment *fragment, struct sym *sym)
{
fragment->tbindings[0].sym = sym;
}
-void fpvm_set_yout(struct fpvm_fragment *fragment, const char *sym)
+void fpvm_set_yout(struct fpvm_fragment *fragment, struct sym *sym)
{
fragment->tbindings[1].sym = sym;
}
-static int lookup(struct fpvm_fragment *fragment, const char *sym)
+static int lookup(struct fpvm_fragment *fragment, struct sym *sym)
{
int i;
@@ -132,11 +134,11 @@ static int lookup(struct fpvm_fragment *fragment, const char *sym)
return FPVM_INVALID_REG;
}
-static int tbind(struct fpvm_fragment *fragment, const char *sym)
+static int tbind(struct fpvm_fragment *fragment, struct sym *sym)
{
if(fragment->ntbindings == FPVM_MAXTBINDINGS) {
snprintf(fragment->last_error, FPVM_MAXERRLEN,
- "Failed to allocate register for variable: %s", sym);
+ "Failed to allocate register for variable: %s", sym->name);
return FPVM_INVALID_REG;
}
fragment->tbindings[fragment->ntbindings].reg = fragment->next_sur;
@@ -145,7 +147,7 @@ static int tbind(struct fpvm_fragment *fragment, const char *sym)
return fragment->next_sur--;
}
-static int rename_reg(struct fpvm_fragment *fragment, const char *sym, int reg)
+static int rename_reg(struct fpvm_fragment *fragment, struct sym *sym, int reg)
{
int i;
@@ -157,7 +159,7 @@ static int rename_reg(struct fpvm_fragment *fragment, const char *sym, int reg)
if(fragment->nrenamings == FPVM_MAXRENAMINGS) {
snprintf(fragment->last_error, FPVM_MAXERRLEN,
"Failed to allocate renamed register for variable: %s",
- sym);
+ sym->name);
return 0;
}
fragment->renamings[fragment->nrenamings].reg = reg;
@@ -166,7 +168,7 @@ static int rename_reg(struct fpvm_fragment *fragment, const char *sym, int reg)
return 1;
}
-static int sym_to_reg(struct fpvm_fragment *fragment, const char *sym,
+static int sym_to_reg(struct fpvm_fragment *fragment, struct sym *sym,
int dest, int *created)
{
int r;
@@ -361,15 +363,15 @@ static int compile(struct fpvm_fragment *fragment, int reg, struct ast_node *nod
case op_ident:
/* AST node is a variable */
if(fragment->bind_mode) {
- opa = sym_to_reg(fragment, node->label, 0, NULL);
+ opa = sym_to_reg(fragment, node->sym, 0, NULL);
if(opa == FPVM_INVALID_REG) return FPVM_INVALID_REG;
} else {
- opa = lookup(fragment, node->label);
+ opa = lookup(fragment, node->sym);
if((opa == FPVM_INVALID_REG)||
(opa == fragment->final_dest)) {
snprintf(fragment->last_error, FPVM_MAXERRLEN,
"Reading unbound variable: %s",
- node->label);
+ node->sym->name);
return FPVM_INVALID_REG;
}
}
@@ -555,7 +557,7 @@ static void fragment_restore(struct fpvm_fragment *fragment,
fragment->ninstructions = backup->ninstructions;
}
-int fpvm_do_assign(struct fpvm_fragment *fragment, const char *dest,
+int fpvm_do_assign(struct fpvm_fragment *fragment, struct sym *dest,
struct ast_node *n)
{
int dest_reg;
@@ -683,7 +685,7 @@ void fpvm_dump(struct fpvm_fragment *fragment)
for(i=0;i<fragment->nbindings;i++) {
printf("R%04d ", i);
if(fragment->bindings[i].isvar)
- printf("%s\n", fragment->bindings[i].b.v);
+ printf("%s\n", fragment->bindings[i].b.v->name);
else
#ifdef PRINTF_FLOAT
printf("%f\n", fragment->bindings[i].b.c);
@@ -694,7 +696,7 @@ void fpvm_dump(struct fpvm_fragment *fragment)
printf("== Transient bindings:\n");
for(i=0;i<fragment->ntbindings;i++) {
printf("R%04d ", fragment->tbindings[i].reg);
- printf("%s\n", fragment->tbindings[i].sym);
+ printf("%s\n", fragment->tbindings[i].sym->name);
}
printf("== Code:\n");
for(i=0;i<fragment->ninstructions;i++) {

0 comments on commit 99f2502

Please sign in to comment.
Something went wrong with that request. Please try again.