summaryrefslogtreecommitdiff
path: root/lib/assembler.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/assembler.c')
-rw-r--r--lib/assembler.c294
1 files changed, 153 insertions, 141 deletions
diff --git a/lib/assembler.c b/lib/assembler.c
index affbdda..4abdb62 100644
--- a/lib/assembler.c
+++ b/lib/assembler.c
@@ -139,7 +139,7 @@ static bytestream_t *pushuninit(int size)
s->size = 1;
s->Label = NULL;
s->Expr = NULL;
-
+
s->relog = 1;
s->line = line;
@@ -212,7 +212,7 @@ static void pushstring(char *s)
{
char marker = *s, tstring[6];
int valid;
-
+
s++;
while ((*s) && (marker != *s)) {
@@ -339,7 +339,7 @@ static expression_t *copy_expression(expression_t * e)
t = Emalloc(sizeof(expression_t));
*t = *e;
-
+
if (t->symbol) {
trouve = 0;
if (e->e_subtype == E_LABEL) {
@@ -501,7 +501,7 @@ void push_pile(char *a)
}
e->father = NULL;
e->child = NULL;
-
+
/* On prédevine le subtype sur quelques cas */
/* Cas des labels (en nom:) */
@@ -1243,7 +1243,7 @@ static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_
if (!instructs->strings[i]) {
if (strcasecmp(instructs->names[i], i ? t->symbol : stringtolook)) {
go_out = 1;
- }
+ }
} else {
switch (instructs->strings[i][0]) {
case 'P':
@@ -1265,10 +1265,11 @@ static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_
}
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))))) {
+ && (t->child->next->e_subtype ==
+ E_VALUE))
+ || ((t->child->e_subtype == E_VALUE)
+ && (t->child->next->e_subtype ==
+ E_LABEL))))) {
go_out = 1;
}
@@ -1498,9 +1499,9 @@ void asm_eol(void)
switch (e_current->e_subtype) {
case E_INTERNAL:
switch (e_current->op) {
- case 1: /* DB */
- case 2: /* DW */
- case 3: /* DD */
+ case 1: /* DB */
+ case 2: /* DW */
+ case 3: /* DD */
do {
e_line = e_current->next;
e_current->next = NULL;
@@ -1527,7 +1528,7 @@ void asm_eol(void)
}
} while (1);
break;
- case 4: /* DS */
+ case 4: /* DS */
do {
e_line = e_current->next;
e_current->next = NULL;
@@ -1544,7 +1545,7 @@ void asm_eol(void)
}
} while (1);
break;
- case 5: /* DR */
+ case 5: /* DR */
do {
e_line = e_current->next;
e_current->next = NULL;
@@ -1687,7 +1688,8 @@ void asm_eol(void)
/* Ecrit un ou plusieurs mots dans le fichier spécifié, avec traitement des erreurs */
-static void writeword(unsigned long int a, FILE * f, int n) {
+static void writeword(unsigned long int a, FILE * f, int n)
+{
int i;
if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) {
@@ -1696,7 +1698,7 @@ static void writeword(unsigned long int a, FILE * f, int n) {
}
exception(1, _("Error writing file"));
}
-
+
for (i = 0; i < n - 1; i++) {
writeword(0, f, 1);
}
@@ -1704,7 +1706,8 @@ static void writeword(unsigned long int a, FILE * f, int n) {
/* Ecrit une chaine dans le fichier spécifié, avec traitement des erreurs */
-static void writestring(char * string, FILE * f) {
+static void writestring(char *string, FILE * f)
+{
for (; *string; string++) {
writeword(*string, f, 1);
}
@@ -1731,15 +1734,16 @@ static void writestring(char * string, FILE * f) {
/* Cette fonction ajoute un fichier à un autre */
-void appendfile(FILE * f, char * name) {
+void appendfile(FILE * f, char *name)
+{
unsigned long int a;
- FILE * g;
+ FILE *g;
if (!(g = fopen(name, "rb"))) {
pushcontext(strerror(errno));
exception(1, _("Error writing file"));
}
-
+
while (fread(&a, sizeof(a), 1, g) == 1) {
writeword(a, f, 1);
}
@@ -1748,7 +1752,7 @@ void appendfile(FILE * f, char * name) {
pushcontext(strerror(errno));
exception(1, _("Error reading file"));
}
-
+
fclose(g);
}
@@ -1757,12 +1761,12 @@ void appendfile(FILE * f, char * name) {
void asm_eof(FILE * f)
{
- bytestream_t * t, * u;
- FILE * f1, * f2;
+ bytestream_t *t, *u;
+ FILE *f1, *f2;
int nbsymbols = 0, a, nbi = 0, nbe = 0;
char errctx[BUFSIZ], trouve;
- bytestream_t * ttext = text, * tdata = data, * tbss = bss;
-
+ bytestream_t *ttext = text, *tdata = data, *tbss = bss;
+
pushcontext(_("Creating temporary files"));
if (!(f1 = fopen("__text__", "wb"))) {
pushcontext(strerror(errno));
@@ -1773,73 +1777,75 @@ void asm_eof(FILE * f)
exception(1, _("Error writing file __symbols__"));
}
popcontext();
-
+
pushcontext(_("Dumping memory into object file"));
-
-
+
+
/* Segment de texte */
pushcontext(_("Dumping text segment"));
for (ttext = ttext->next; ttext; ttext = ttext->next) {
- sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), ttext->offset, ttext->line, ttext->filename);
+ sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), ttext->offset,
+ ttext->line, ttext->filename);
pushcontext(errctx);
-
+
a = 0;
-
+
if (ttext->Expr) {
- switch(ttext->Expr->e_subtype) {
- case E_VALUE:
- a = ttext->Expr->avalue;
- break;
- case E_LABEL:
- a = ttext->Expr->avalue;
- nbsymbols++;
- writeword(1, f2, 1);
- writeword(ttext->offset, f2, 1);
- writeword(strlen(ttext->Expr->symbol), f2, 1);
- writestring(ttext->Expr->symbol, f2);
- nbe++;
- break;
- case E_OPERATION:
- if (ttext->Expr->op == OP_DIRECT) {
- if (ttext->Expr->child->e_subtype == E_VALUE) {
- a = ttext->Expr->child->avalue;
- break;
- }
- }
- if (ttext->Expr->op != OP_FUNC_CALL) {
- exception(1, _("Can't evaluate expression for a direct value"));
- }
- if (strcmp(ttext->Expr->symbol, "diff")) {
- exception(1, _("Can't evaluate expression for a direct value"));
- }
- if ((ttext->Expr->child->e_subtype != E_LABEL) || (ttext->Expr->child->next->e_subtype != E_LABEL)) {
- exception(1, _("Can only use the diff() function onto labels"));
- }
-
- t = (bytestream_t *) NomVarToVar(ttext->Expr->child->symbol, labels, &trouve);
- if (!trouve) {
- exception(1, _("Can only evaluate a diff on local symbols"));
- }
- u = (bytestream_t *) NomVarToVar(ttext->Expr->child->next->symbol, labels, &trouve);
- if (!trouve) {
- exception(1, _("Can only evaluate a diff on local symbols"));
- }
-
- if (t->segment != u->segment) {
- exception(1, _("Can only evaluate a diff on symbols from the same segment"));
+ switch (ttext->Expr->e_subtype) {
+ case E_VALUE:
+ a = ttext->Expr->avalue;
+ break;
+ case E_LABEL:
+ a = ttext->Expr->avalue;
+ nbsymbols++;
+ writeword(1, f2, 1);
+ writeword(ttext->offset, f2, 1);
+ writeword(strlen(ttext->Expr->symbol), f2, 1);
+ writestring(ttext->Expr->symbol, f2);
+ nbe++;
+ break;
+ case E_OPERATION:
+ if (ttext->Expr->op == OP_DIRECT) {
+ if (ttext->Expr->child->e_subtype == E_VALUE) {
+ a = ttext->Expr->child->avalue;
+ break;
}
-
- a = t->offset - u->offset;
- break;
- default:
- exception(1, _("Can't evaluate expression"));
+ }
+ if (ttext->Expr->op != OP_FUNC_CALL) {
+ exception(1, _("Can't evaluate expression for a direct value"));
+ }
+ if (strcmp(ttext->Expr->symbol, "diff")) {
+ exception(1, _("Can't evaluate expression for a direct value"));
+ }
+ if ((ttext->Expr->child->e_subtype != E_LABEL)
+ || (ttext->Expr->child->next->e_subtype != E_LABEL)) {
+ exception(1, _("Can only use the diff() function onto labels"));
+ }
+
+ t = (bytestream_t *) NomVarToVar(ttext->Expr->child->symbol, labels, &trouve);
+ if (!trouve) {
+ exception(1, _("Can only evaluate a diff on local symbols"));
+ }
+ u = (bytestream_t *) NomVarToVar(ttext->Expr->child->next->symbol, labels, &trouve);
+ if (!trouve) {
+ exception(1, _("Can only evaluate a diff on local symbols"));
+ }
+
+ if (t->segment != u->segment) {
+ exception(1, _("Can only evaluate a diff on symbols from the same segment"));
+ }
+
+ a = t->offset - u->offset;
+ break;
+ default:
+ exception(1, _("Can't evaluate expression"));
}
}
-
+
a += ttext->Encoded;
-
+
if (ttext->size) {
writeword(a, f1, text->size);
} else {
@@ -1858,70 +1864,72 @@ void asm_eof(FILE * f)
popcontext();
}
popcontext();
-
+
/* Segment de data */
pushcontext(_("Dumping data segment"));
for (tdata = tdata->next; tdata; tdata = tdata->next) {
- sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), tdata->offset, tdata->line, tdata->filename);
+ sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), tdata->offset,
+ tdata->line, tdata->filename);
pushcontext(errctx);
-
+
a = 0;
-
+
if (tdata->Expr) {
- switch(tdata->Expr->e_subtype) {
- case E_VALUE:
- a = tdata->Expr->avalue;
- break;
- case E_LABEL:
- a = tdata->Expr->avalue;
- nbsymbols++;
- writeword(3, f2, 1);
- writeword(tdata->offset + s_text, f2, 1);
- writeword(strlen(tdata->Expr->symbol), f2, 1);
- writestring(tdata->Expr->symbol, f2);
- nbe++;
- break;
- case E_OPERATION:
- if (tdata->Expr->op == OP_DIRECT) {
- if (tdata->Expr->child->e_subtype == E_VALUE) {
- a = tdata->Expr->child->avalue;
- break;
- }
- }
- if (tdata->Expr->op != OP_FUNC_CALL) {
- exception(1, _("Can't evaluate expression for a direct value"));
- }
- if (strcmp(tdata->Expr->symbol, "diff")) {
- exception(1, _("Can't evaluate expression for a direct value"));
- }
- if ((tdata->Expr->child->e_subtype != E_LABEL) || (tdata->Expr->child->next->e_subtype != E_LABEL)) {
- exception(1, _("Can only use the diff() function onto labels"));
- }
-
- t = (bytestream_t *) NomVarToVar(tdata->Expr->child->symbol, labels, &trouve);
- if (!trouve) {
- exception(1, _("Can only evaluate a diff on local symbols"));
- }
- u = (bytestream_t *) NomVarToVar(tdata->Expr->child->next->symbol, labels, &trouve);
- if (!trouve) {
- exception(1, _("Can only evaluate a diff on local symbols"));
- }
-
- if (t->segment != u->segment) {
- exception(1, _("Can only evaluate a diff on symbols from the same segment"));
+ switch (tdata->Expr->e_subtype) {
+ case E_VALUE:
+ a = tdata->Expr->avalue;
+ break;
+ case E_LABEL:
+ a = tdata->Expr->avalue;
+ nbsymbols++;
+ writeword(3, f2, 1);
+ writeword(tdata->offset + s_text, f2, 1);
+ writeword(strlen(tdata->Expr->symbol), f2, 1);
+ writestring(tdata->Expr->symbol, f2);
+ nbe++;
+ break;
+ case E_OPERATION:
+ if (tdata->Expr->op == OP_DIRECT) {
+ if (tdata->Expr->child->e_subtype == E_VALUE) {
+ a = tdata->Expr->child->avalue;
+ break;
}
-
- a = t->offset - u->offset;
- break;
- default:
- exception(1, _("Can't evaluate expression"));
+ }
+ if (tdata->Expr->op != OP_FUNC_CALL) {
+ exception(1, _("Can't evaluate expression for a direct value"));
+ }
+ if (strcmp(tdata->Expr->symbol, "diff")) {
+ exception(1, _("Can't evaluate expression for a direct value"));
+ }
+ if ((tdata->Expr->child->e_subtype != E_LABEL)
+ || (tdata->Expr->child->next->e_subtype != E_LABEL)) {
+ exception(1, _("Can only use the diff() function onto labels"));
+ }
+
+ t = (bytestream_t *) NomVarToVar(tdata->Expr->child->symbol, labels, &trouve);
+ if (!trouve) {
+ exception(1, _("Can only evaluate a diff on local symbols"));
+ }
+ u = (bytestream_t *) NomVarToVar(tdata->Expr->child->next->symbol, labels, &trouve);
+ if (!trouve) {
+ exception(1, _("Can only evaluate a diff on local symbols"));
+ }
+
+ if (t->segment != u->segment) {
+ exception(1, _("Can only evaluate a diff on symbols from the same segment"));
+ }
+
+ a = t->offset - u->offset;
+ break;
+ default:
+ exception(1, _("Can't evaluate expression"));
}
}
-
+
a += tdata->Encoded;
-
+
if (tdata->size) {
writeword(a, f1, text->size);
} else {
@@ -1937,16 +1945,17 @@ void asm_eof(FILE * f)
popcontext();
}
popcontext();
-
+
fclose(f1);
-
+
/* Segment bss */
pushcontext(_("Dumping bss segment"));
for (tbss = tbss->next; tbss; tbss = tbss->next) {
- sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), tbss->offset, tbss->line, tbss->filename);
+ sprintf(errctx, _("Processing word number %i coming from line %i of the file %s."), tbss->offset,
+ tbss->line, tbss->filename);
pushcontext(errctx);
-
+
if (!tbss->size) {
if (tbss->Label) {
nbsymbols++;
@@ -1954,7 +1963,7 @@ void asm_eof(FILE * f)
writeword(tbss->offset + s_text + s_data, f2, 1);
writeword(strlen(tbss->Label), f2, 1);
writestring(tbss->Label, f2);
- nbi++;
+ nbi++;
}
}
popcontext();
@@ -1970,22 +1979,25 @@ void asm_eof(FILE * f)
} else {
writeword(-1, f, 1);
}
-
+
writeword(s_text, f, 1);
writeword(s_data, f, 1);
writeword(s_bss, f, 1);
writeword(ftell(f2) + 1, f, 1);
fclose(f2);
-
+
writeword(nbsymbols, f, 1);
appendfile(f, "__symbols__");
appendfile(f, "__text__");
fclose(f);
-
+
unlink("__symbols__");
unlink("__text__");
-
- fprintf(stderr, _("Statistics: %i words of text, %i words of data, and %i words reserved.\n%i symbols generated with %i internal and %i external.\n"), s_text, s_data, s_bss, nbsymbols, nbi, nbe);
+
+ fprintf(stderr,
+ _
+ ("Statistics: %i words of text, %i words of data, and %i words reserved.\n%i symbols generated with %i internal and %i external.\n"),
+ s_text, s_data, s_bss, nbsymbols, nbi, nbe);
}
/* Diverses fonctions pour faire plein de free() partout */