summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/assembler.c129
1 files changed, 68 insertions, 61 deletions
diff --git a/lib/assembler.c b/lib/assembler.c
index 25b975b..c3bc4a0 100644
--- a/lib/assembler.c
+++ b/lib/assembler.c
@@ -70,7 +70,8 @@ int s_text = 0, s_data = 0, s_bss = 0;
static void debug_print_expression(expression_t * e)
{
- if (!e) return;
+ if (!e)
+ return;
switch (e->e_subtype) {
case E_OPERATION:
switch (e->op) {
@@ -454,7 +455,7 @@ void push_pile(char *a)
} else {
trouve = 0;
if ((*a != '"') && (*a != '\'')) {
- e_t = (expression_t *) NomVarToVar(a, defines, &trouve);
+ e_t = (expression_t *) NomVarToVar(a, defines, &trouve);
}
if (trouve) {
e = copy_expression(e_t);
@@ -466,14 +467,18 @@ void push_pile(char *a)
}
}
e->op = -1;
- e->prev = e_current;
+ if (wc) {
+ e->prev = e_current;
+ } else {
+ e->prev = NULL;
+ }
e->next = NULL;
- if (e_current) {
+ if (wc) {
e_current->next = e;
}
e->father = NULL;
e->child = NULL;
-
+
/* On prédevine le subtype sur quelques cas */
/* Cas des labels (en nom:) */
@@ -1127,7 +1132,7 @@ static void super_pattern(pattern_t * patterns, expression_t * e)
return;
if (!((e->op == OP_DECAL) || (e->op == OP_DIRECT)))
return;
-
+
for (patterns = patterns->next; patterns; patterns = patterns->next) {
for (i = 0; i < patterns->nbr; i++) {
if (!patterns->expr[i]->name) {
@@ -1239,13 +1244,23 @@ static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_
}
break;
case 'C':
- if ((t->e_subtype != E_VALUE) && (t->e_subtype != E_LABEL)) {
+ if ((t->e_subtype != E_VALUE) && (t->e_subtype != E_LABEL)
+ && (t->e_subtype != E_OPERATION)) {
go_out = 1;
if (t->e_subtype == E_OPERATION) {
if (t->op == OP_FUNC_CALL)
go_out = 0;
}
}
+
+ if ((t->e_subtype == E_OPERATION) && (!(((t->child->e_subtype == E_LABEL)
+ && (t->child->next->e_subtype == E_VALUE))
+ || ((t->child->e_subtype == E_VALUE)
+ && (t->child->next->e_subtype ==
+ E_LABEL))))) {
+ go_out = 1;
+ }
+
break;
case 'o':
/* Pour que quelque chose soit absolu, il nous suffit d'avoir un label dans l'expression */
@@ -1266,19 +1281,19 @@ static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_
}
}
- if (!go_out) {
+ if (!go_out && !t) {
break;
}
}
if (instructs) {
fprintf(stderr, "Nous avons reconnu une instruction:\n");
- fprintf(stderr, " o Instruction contenant %i champs explicites et %i champs implicites.\n",
+ fprintf(stderr,
+ " o Instruction contenant %i champs explicites et %i champs implicites.\n",
instructs->nbexplicit, instructs->nbimplicit);
fprintf(stderr, " => Champs explicites.\n");
for (i = 0; i < instructs->nbexplicit; i++) {
- fprintf(stderr, " + %s <= %s (type %s)\n",
- instructs->names[i],
+ fprintf(stderr, " + %s <= %s (type %s)\n", instructs->names[i],
instructs->strings[i] ? instructs->strings[i] : "Pas de chaîne associée",
instructs->etypes[i] ? "prédéfinit" : "direct");
}
@@ -1290,8 +1305,8 @@ static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_
instructs->istrings[i]);
break;
case 1:
- fprintf(stderr, " + %s <= %s (type prédéfinit)\n", instructs->implicits[i],
- instructs->istrings[i]);
+ fprintf(stderr, " + %s <= %s (type prédéfinit)\n",
+ instructs->implicits[i], instructs->istrings[i]);
break;
case 2:
fprintf(stderr, " + %s <= %i (type valeur)\n", instructs->implicits[i],
@@ -1315,11 +1330,9 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
if (!e->pattern) {
exception(1, _("Pattern not matching..."));
}
-
- fprintf(stderr, "Evaluation de la pattern %s, index %i\n", e->pattern->name, e->index);
+ fprintf(stderr, "Evaluation de la pattern %s, index %i\n", e->pattern->name, e->index);
m = e->pattern->expr[e->index];
-
if (m->name) {
fprintf(stderr, "La pattern a un nom\n");
if (m->string) {
@@ -1339,8 +1352,7 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
if (m->string[0] != 'P') {
exception(1,
_
- ("Error in the metalanguage (pattern should be here if not a constant type)"));
- }
+ ("Error in the metalanguage (pattern should be here if not a constant type)"));}
t = (expression_t *) Emalloc(sizeof(expression_t));
t->next = t->child = t->prev = t->father = NULL;
t->e_type = t->e_subtype = E_VALUE;
@@ -1358,7 +1370,8 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
m = m->left;
if (m->string) {
if (m->type) {
- if (strcmp(m->name, "I") || strcmp(m->string, "O") || strcmp(m->string, "o")) {
+ if (strcmp(m->name, "I") || strcmp(m->string, "O")
+ || strcmp(m->string, "o")) {
exception(1, _("Unknow constant type in the meta language"));
}
tv = 0;
@@ -1383,13 +1396,11 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
if (m->string[0] != 'P') {
exception(1,
_
- ("Error in the metalanguage (pattern should be here if not a constant type)"));
- }
+ ("Error in the metalanguage (pattern should be here if not a constant type)"));}
t = (expression_t *) Emalloc(sizeof(expression_t));
t->next = t->child = t->prev = t->father = NULL;
t->e_type = t->e_subtype = E_VALUE;
t->pattern = NULL;
-
if (e->child->next->e_subtype == E_PATTERN) {
t->avalue = e->child->next->index;
} else {
@@ -1413,8 +1424,7 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
if (m->string[0] != 'P') {
exception(1,
_
- ("Error in the metalanguage (pattern should be here if not a constant type)"));
- }
+ ("Error in the metalanguage (pattern should be here if not a constant type)"));}
t = (expression_t *) Emalloc(sizeof(expression_t));
t->next = t->child = t->prev = t->father = NULL;
t->e_type = t->e_subtype = E_VALUE;
@@ -1430,26 +1440,25 @@ static void evaluate_pattern(_TableauVariable * it, expression_t * e)
}
}
-static int evaluate_field(_TableauVariable it, char * field, field_t * fields) {
+static int evaluate_field(_TableauVariable it, char *field, field_t * fields)
+{
int i, r, e, n;
char trouve;
- expression_t * t;
+ expression_t *t;
for (fields = fields->next; fields; fields = fields->next) {
if (!strcmp(field + 1, fields->name)) {
break;
}
}
-
+
if (!(fields)) {
exception(1, _("Unknow field in metalanguage"));
}
-
+
r = 0;
n = 0;
-
fprintf(stderr, "Evaluation du champ %s\n", field);
-
for (i = 0; i < fields->nbr; i++) {
t = (expression_t *) NomVarToVar(fields->names[i], it, &trouve);
e = 0;
@@ -1467,7 +1476,7 @@ static int evaluate_field(_TableauVariable it, char * field, field_t * fields) {
}
r = (r << n) | e;
}
-
+
return r;
}
@@ -1496,10 +1505,9 @@ void asm_eol(void)
fprintf(stderr, "Fin de ligne sur:\n");
debug_print_expression(e_line);
fprintf(stderr, "-----\n");
-
e_current = e_line;
- if (!(e_current)) break;
-
+ if (!(e_current))
+ break;
/* Est-ce que le premier mot est un label terminant par ':' ? */
if (e_current->e_subtype == E_LABEL) {
if (e_current->symbol[strlen(e_current->symbol) - 1] == ':') {
@@ -1522,7 +1530,7 @@ void asm_eol(void)
}
}
-
+
switch (e_current->e_subtype) {
case E_INTERNAL:
switch (e_current->op) {
@@ -1592,7 +1600,8 @@ void asm_eol(void)
}
break;
case E_INSTRUCT:
- if (!e_current) break;
+ if (!e_current)
+ break;
if (segment != SEG_TEXT) {
exception(1, _("You can only have an instruction into a .text segment"));
}
@@ -1605,7 +1614,6 @@ void asm_eol(void)
Initialise(&it);
pi = pushdword(0, NULL);
-
for (i = 0; i < instr->nbexplicit; i++) {
if (instr->strings[i]) {
switch (instr->strings[i][0]) {
@@ -1620,12 +1628,10 @@ void asm_eol(void)
} else {
exception(1,
_
- ("Unknow constant type in the meta language"));
- }
+ ("Unknow constant type in the meta language"));}
} else {
InsererVarDansTab(&it,
- CreerElement(instr->names[i], e_current));
- }
+ CreerElement(instr->names[i], e_current));}
break;
case 'O':
if (instr->etypes) {
@@ -1638,24 +1644,24 @@ void asm_eol(void)
} else {
exception(1,
_
- ("Unknow constant type in the meta language"));
- }
+ ("Unknow constant type in the meta language"));}
}
break;
case 'o':
if (instr->etypes) {
if (!strcmp(instr->names[i], "I")) {
if (e_current->e_subtype == E_VALUE) {
- exception(1, _("Can't have a direct value for a relative offset"));
- } else {
+ exception(1,
+ _
+ ("Can't have a direct value for a relative offset"));}
+ else {
pi = pushdword(0, e_current);
}
pi->Encoded -= pi->offset;
} else {
exception(1,
_
- ("Unknow constant type in the meta language"));
- }
+ ("Unknow constant type in the meta language"));}
}
break;
case 'P':
@@ -1664,7 +1670,8 @@ void asm_eol(void)
t->avalue = e_current->index;
t->child = t->next = NULL;
t->pattern = NULL;
- fprintf(stderr, "Insertion de la variable %s, de valeur %i.\n", instr->names[i], t->avalue);
+ fprintf(stderr, "Insertion de la variable %s, de valeur %i.\n",
+ instr->names[i], t->avalue);
InsererVarDansTab(&it, CreerElement(instr->names[i], t));
evaluate_pattern(&it, e_current);
break;
@@ -1678,10 +1685,10 @@ void asm_eol(void)
free_expr(e_current);
e_current = e_line;
}
-
+
for (i = 0; i < instr->nbimplicit; i++) {
- switch(instr->itypes[i]) {
- case 0: /* type direct */
+ switch (instr->itypes[i]) {
+ case 0: /* type direct */
fprintf(stderr, "On cherche la variable %s\n", instr->istrings[i]);
t = (expression_t *) NomVarToVar(instr->istrings[i], it, &trouve);
if (!trouve) {
@@ -1690,11 +1697,12 @@ void asm_eol(void)
t->avalue = 0;
t->child = t->next = NULL;
t->pattern = NULL;
- fprintf(stderr, "Insertion de la variable %s, de valeur %i.\n", instr->istrings[i], t->avalue);
+ fprintf(stderr, "Insertion de la variable %s, de valeur %i.\n",
+ instr->istrings[i], t->avalue);
}
InsererVarDansTab(&it, CreerElement(instr->implicits[i], t));
break;
- case 1: /* type prédéfinit */
+ case 1: /* type prédéfinit */
t = (expression_t *) Emalloc(sizeof(expression_t));
t->e_type = t->e_subtype = E_VALUE;
if (instr->istrings[i][0] != 'F') {
@@ -1705,7 +1713,7 @@ void asm_eol(void)
t->pattern = NULL;
InsererVarDansTab(&it, CreerElement(instr->implicits[i], t));
break;
- case 2: /* type valeur */
+ case 2: /* type valeur */
t = (expression_t *) Emalloc(sizeof(expression_t));
t->e_type = t->e_subtype = E_VALUE;
t->avalue = instr->ivalues[i];
@@ -1715,13 +1723,11 @@ void asm_eol(void)
break;
}
}
-
+
fprintf(stderr, "Variables\n");
AfficheTableau(it);
-
pushdword(evaluate_field(it, "FI", fields), NULL);
DetruitTab(&it);
-
break;
default:
exception(1, _("Unknow instruction"));
@@ -1738,7 +1744,7 @@ void asm_eol(void)
void asm_eof(FILE * f)
{
-
+
}
static void delete_bytestream(bytestream_t * s)
@@ -1796,9 +1802,10 @@ static int process_file(char *name)
return 0;
}
-void assemble_file(char * iname, char * oname) {
- FILE * f;
-
+void assemble_file(char *iname, char *oname)
+{
+ FILE *f;
+
pushcontext(_("Opening output file"));
if (!(f = fopen(oname, "wb"))) {
pushcontext(strerror(errno));