diff options
-rw-r--r-- | include/alu.h | 2 | ||||
-rw-r--r-- | include/assembler.h | 2 | ||||
-rw-r--r-- | include/exceptions.h | 4 | ||||
-rw-r--r-- | include/fpu.h | 2 | ||||
-rw-r--r-- | include/hash.h | 2 | ||||
-rw-r--r-- | include/interne.h | 2 | ||||
-rw-r--r-- | include/memoire.h | 2 | ||||
-rw-r--r-- | include/meta.h | 24 | ||||
-rw-r--r-- | include/numbers.h | 2 | ||||
-rw-r--r-- | include/parser.h | 26 | ||||
-rw-r--r-- | include/registre.h | 2 | ||||
-rw-r--r-- | lib/alu.c | 82 | ||||
-rw-r--r-- | lib/assembler.c | 294 | ||||
-rw-r--r-- | lib/fpu.c | 2 | ||||
-rw-r--r-- | lib/hash.c | 3 | ||||
-rw-r--r-- | lib/interne.c | 6 | ||||
-rw-r--r-- | lib/linker.c | 143 | ||||
-rw-r--r-- | lib/memoire.c | 4 | ||||
-rw-r--r-- | lib/meta.c | 12 | ||||
-rw-r--r-- | lib/registre.c | 19 | ||||
-rw-r--r-- | lib/simulator.c | 413 | ||||
-rw-r--r-- | src/compilo.c | 39 | ||||
-rw-r--r-- | src/linker.c | 37 | ||||
-rw-r--r-- | src/simul.c | 5 |
24 files changed, 616 insertions, 513 deletions
diff --git a/include/alu.h b/include/alu.h index 80a5d73..4427060 100644 --- a/include/alu.h +++ b/include/alu.h @@ -15,4 +15,4 @@ Uint32 OR(Uint32 a, Uint32 b); Uint32 SHL(Uint32 a); Uint32 SHR(Uint32 a); -#endif
\ No newline at end of file +#endif diff --git a/include/assembler.h b/include/assembler.h index f02f53e..c8c4da9 100644 --- a/include/assembler.h +++ b/include/assembler.h @@ -5,7 +5,7 @@ int assembler_init(void); void assembler_flush(void); void asm_eol(void); void asm_eof(FILE * f); -void assemble_file(char * i, char * o); +void assemble_file(char *i, char *o); void push_pile(char *); void act_pile(int); diff --git a/include/exceptions.h b/include/exceptions.h index 5ce9f60..672d8ca 100644 --- a/include/exceptions.h +++ b/include/exceptions.h @@ -3,8 +3,8 @@ #include <stdio.h> -char * Estrdup(char *); -void * Emalloc(size_t); +char *Estrdup(char *); +void *Emalloc(size_t); void exception(int, char *); void pushcontext(char *); diff --git a/include/fpu.h b/include/fpu.h index ea98e74..83e086a 100644 --- a/include/fpu.h +++ b/include/fpu.h @@ -3,4 +3,4 @@ #include "types.h" void fpu(Uint32 opcode); -#endif
\ No newline at end of file +#endif diff --git a/include/hash.h b/include/hash.h index 3420a51..a7948a1 100644 --- a/include/hash.h +++ b/include/hash.h @@ -4,7 +4,7 @@ #define TAILLECHAINEHACHAGE (26*2+1) #ifdef HAVE_CONFIG_H -typedef void * _TypeVariable; +typedef void *_TypeVariable; #else typedef int _TypeVariable; #endif diff --git a/include/interne.h b/include/interne.h index 2f91050..801365b 100644 --- a/include/interne.h +++ b/include/interne.h @@ -14,4 +14,4 @@ Uint32 Extension(Uint32 ins); Uint32 Champ1(Uint32 ins); Uint32 Champ2(Uint32 ins); Uint32 Champ3(Uint32 ins); -#endif
\ No newline at end of file +#endif diff --git a/include/memoire.h b/include/memoire.h index 0598039..290068d 100644 --- a/include/memoire.h +++ b/include/memoire.h @@ -3,4 +3,4 @@ #include "types.h" Uint32 LD(Uint32 offset); void ST(Uint32 offset, Uint32 valeur); -#endif
\ No newline at end of file +#endif diff --git a/include/meta.h b/include/meta.h index 471710d..1056246 100644 --- a/include/meta.h +++ b/include/meta.h @@ -19,30 +19,30 @@ typedef struct field_t { } field_t; typedef struct metaexpr_t { - char * name; + char *name; int type; - char * string; + char *string; /* left = right = NULL => feuille left = NULL => opérateur unaire sinon => opérateur binaire - */ - struct metaexpr_t * left, * right; + */ + struct metaexpr_t *left, *right; } metaexpr_t; typedef struct pattern_t { - char * name; - metaexpr_t ** expr; + char *name; + metaexpr_t **expr; int nbr; struct pattern_t *next; } pattern_t; typedef struct instruct_t { - char ** names; - char ** strings; - char ** implicits; - char ** istrings; - int * etypes, * itypes, * ivalues, nbexplicit, nbimplicit; - struct instruct_t * next; + char **names; + char **strings; + char **implicits; + char **istrings; + int *etypes, *itypes, *ivalues, nbexplicit, nbimplicit; + struct instruct_t *next; } instruct_t; extern phon_t *phons; diff --git a/include/numbers.h b/include/numbers.h index ad3fc2b..9f0b1ae 100644 --- a/include/numbers.h +++ b/include/numbers.h @@ -1,6 +1,6 @@ #ifndef __NUMBERS_H__ #define __NUMBERS_H__ -int char_to_number(char * st, int * valid); +int char_to_number(char *st, int *valid); #endif diff --git a/include/parser.h b/include/parser.h index f6d56ba..b38e047 100644 --- a/include/parser.h +++ b/include/parser.h @@ -5,20 +5,20 @@ #define PILECALL_MAX 50 enum { - OP_NEST, - OP_PLUS, - OP_MOINS, - OP_PLUS_UNARY, - OP_MOINS_UNARY, - OP_DIV, - OP_MUL, - OP_MOD, - OP_LPAREN, - OP_FUNC_CALL, - OP_DECAL, - OP_DIRECT + OP_NEST, + OP_PLUS, + OP_MOINS, + OP_PLUS_UNARY, + OP_MOINS_UNARY, + OP_DIV, + OP_MUL, + OP_MOD, + OP_LPAREN, + OP_FUNC_CALL, + OP_DECAL, + OP_DIRECT }; void parse_line(char *); -#endif
\ No newline at end of file +#endif diff --git a/include/registre.h b/include/registre.h index 19b817b..6ad9a6b 100644 --- a/include/registre.h +++ b/include/registre.h @@ -34,4 +34,4 @@ void ResetZero(void); void ResetSign(void); void ResetParity(void); void ResetRegistres(void); -#endif
\ No newline at end of file +#endif @@ -2,21 +2,69 @@ // rajouter les overflow... -/*****************************************/
-/** **/
-/** GESTION DE L'UNITE DE CALCUL **/
-/** **/
-/*****************************************/
+/*****************************************/
+/** **/
+/** GESTION DE L'UNITE DE CALCUL **/
+/** **/
+/*****************************************/
+
Uint32 AdditionNonSigne(Uint32 a, Uint32 b) +{ + return (a + b); +} + +
Uint32 AdditionSigne(Uint32 a, Uint32 b) +{ + return (a + b); +} + +
Uint32 SoustractionNonSigne(Uint32 a, Uint32 b) +{ + return (a - b); +} + +
Uint32 SoustractionSigne(Uint32 a, Uint32 b) +{ + return (a - b); +} + +
Uint32 MultiplicationNonSigne(Uint32 a, Uint32 b) +{ + return (a * b); +} + +
Uint32 MultiplicationSigne(Uint32 a, Uint32 b) +{ + return (a * b); +} + +
Uint32 DivisionNonSigne(Uint32 a, Uint32 b) +{ + return (a / b); +} + +
Uint32 DivisionSigne(Uint32 a, Uint32 b) +{ + return (a / b); +} + +
Uint32 AND(Uint32 a, Uint32 b) +{ + return (a & b); +} + +
Uint32 OR(Uint32 a, Uint32 b) +{ + return (a | b); +} + +
Uint32 SHL(Uint32 a) +{ + return (a >> 1); +} + +
Uint32 SHR(Uint32 a) +{ + return (a << 1); +} + -Uint32 AdditionNonSigne(Uint32 a, Uint32 b) { return (a + b); }
-Uint32 AdditionSigne(Uint32 a, Uint32 b) { return (a + b); }
-Uint32 SoustractionNonSigne(Uint32 a, Uint32 b) { return (a - b); }
-Uint32 SoustractionSigne(Uint32 a, Uint32 b) { return (a - b); }
-Uint32 MultiplicationNonSigne(Uint32 a, Uint32 b) { return (a * b); }
-Uint32 MultiplicationSigne(Uint32 a, Uint32 b) { return (a * b); }
-Uint32 DivisionNonSigne(Uint32 a, Uint32 b) { return (a / b); }
-Uint32 DivisionSigne(Uint32 a, Uint32 b) { return (a / b); }
-Uint32 AND(Uint32 a, Uint32 b) { return (a & b); }
-Uint32 OR(Uint32 a, Uint32 b) { return (a | b); }
-Uint32 SHL(Uint32 a) { return (a >> 1); }
-Uint32 SHR(Uint32 a) { return (a << 1); }
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 */ @@ -11,5 +11,5 @@ void fpu(Uint32 opcode) { - exception(1,_("FPU not implemented")); + exception(1, _("FPU not implemented")); } @@ -9,7 +9,7 @@ static char *CHAINEHACHAGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX static int FonctionHachage(char *clef) { unsigned int i; - + if (!clef) { exception(1, _("Internal error into hashing")); } @@ -39,6 +39,7 @@ static _ListeChaine InserTete(_ListeChaine l, _Element e) aux = (_ListeChaine) Emalloc(sizeof(struct _LstChn)); aux->Elem.NomVar = (char *) Emalloc(sizeof(char) * (strlen(e.NomVar) + 1)); + for (i = 0; i <= strlen(e.NomVar); i++) { aux->Elem.NomVar[i] = e.NomVar[i]; } diff --git a/lib/interne.c b/lib/interne.c index 5a563fe..a8e1db5 100644 --- a/lib/interne.c +++ b/lib/interne.c @@ -22,7 +22,7 @@ void Set(Uint32 * i) void ResetBit(Uint32 * i, int position) { if (position < 0 || position > 31) { - exception(1,_("ResetBit: Incorrect Value")); + exception(1, _("ResetBit: Incorrect Value")); } else { Uint32 aux = VAL_MAX - (1 << position); @@ -34,7 +34,7 @@ void ResetBit(Uint32 * i, int position) void SetBit(Uint32 * i, int position) { if (position < 0 || position > 31) { - exception(1,_("SetBit: Incorrect Value")); + exception(1, _("SetBit: Incorrect Value")); } else { Uint32 aux = 1 << position; @@ -46,7 +46,7 @@ void SetBit(Uint32 * i, int position) int ValeurBit(Uint32 nombre, int position) { if (position < 0 || position > 31) { - exception(1,_("ValeurBit: Incorrect Value")); + exception(1, _("ValeurBit: Incorrect Value")); return (-1); } return ((nombre >> position) & 1); diff --git a/lib/linker.c b/lib/linker.c index f28bc0f..b6930e3 100644 --- a/lib/linker.c +++ b/lib/linker.c @@ -13,31 +13,32 @@ /* Les quelques structures de données utiles */ typedef struct object_t { - Uint32 s_text, s_data, s_bss, * text, * data, textstart, datastart, bssstart; + Uint32 s_text, s_data, s_bss, *text, *data, textstart, datastart, bssstart; } object_t; typedef struct symbol_t { - char * name; + char *name; int objindex; Uint32 offset; int type; - struct symbol_t * next; + struct symbol_t *next; } symbol_t; /* Et les variables globales */ Uint32 startpoint = -1, textsize = 0, datasize = 0, bsssize = 0; -object_t ** objects; -symbol_t * symbols, * psymbols; +object_t **objects; +symbol_t *symbols, *psymbols; int objindex = 0, nbrsymbs = 0; _TableauVariable symbs; /* Quelques fonctions pour nous simplifier la vie... */ -static FILE * openfilewriting(char * name) { - FILE * f; +static FILE *openfilewriting(char *name) +{ + FILE *f; if (!(f = fopen(name, "wb"))) { pushcontext(strerror(errno)); @@ -46,8 +47,9 @@ static FILE * openfilewriting(char * name) { return f; } -static FILE * openfilereading(char * name) { - FILE * f; +static FILE *openfilereading(char *name) +{ + FILE *f; if (!(f = fopen(name, "rb"))) { pushcontext(strerror(errno)); @@ -56,7 +58,8 @@ static FILE * openfilereading(char * name) { return f; } -static void writeword(Uint32 a, FILE * f) { +static void writeword(Uint32 a, FILE * f) +{ if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) { if (ferror(f)) { pushcontext(strerror(errno)); @@ -65,7 +68,8 @@ static void writeword(Uint32 a, FILE * f) { } } -static Uint32 readword(FILE * f) { +static Uint32 readword(FILE * f) +{ Uint32 a; if (fread(&a, sizeof(a), 1, f) != 1) { @@ -74,9 +78,10 @@ static Uint32 readword(FILE * f) { return a; } -static char * readstring(FILE * f) { +static char *readstring(FILE * f) +{ Uint32 s; - char * r; + char *r; int i; s = readword(f); @@ -90,19 +95,20 @@ static char * readstring(FILE * f) { /* Rajoute un symbole dans la pile */ -static void addsymbol(char * name, int offset, int type) { - symbol_t * newsymbol; - +static void addsymbol(char *name, int offset, int type) +{ + symbol_t *newsymbol; + newsymbol = (symbol_t *) Emalloc(sizeof(symbol_t)); newsymbol->next = NULL; newsymbol->type = type; newsymbol->offset = offset; newsymbol->objindex = objindex; newsymbol->name = name; - + psymbols->next = newsymbol; psymbols = newsymbol; - + if (newsymbol->type & 1) { nbrsymbs++; } else { @@ -112,11 +118,12 @@ static void addsymbol(char * name, int offset, int type) { /* Rajoute un fichier dans les structures */ -void addfile(char * nom) { - FILE * f; +void addfile(char *nom) +{ + FILE *f; Uint32 start, nbsymbols, type, offset; int i; - char * snom, errctx[BUFSIZ]; + char *snom, errctx[BUFSIZ]; f = openfilereading(nom); sprintf(errctx, _("Processing file %s"), nom); @@ -125,18 +132,18 @@ void addfile(char * nom) { if (readword(f) != 0x4f424e4e) { exception(1, _("Bad signature")); } - - readword(f); /* Taille du fichier */ + + readword(f); /* Taille du fichier */ start = readword(f); if ((startpoint != -1) && (start != -1)) { exception(1, _("Startpoint already defined.")); } startpoint = start; - + objects[objindex]->s_text = readword(f); objects[objindex]->s_data = readword(f); objects[objindex]->s_bss = readword(f); - readword(f); /* Taille de la table des symboles */ + readword(f); /* Taille de la table des symboles */ nbsymbols = readword(f); pushcontext(_("Reading symbols")); @@ -147,7 +154,7 @@ void addfile(char * nom) { addsymbol(snom, offset, type); } popcontext(); - + objects[objindex]->textstart = textsize; objects[objindex]->datastart = datasize; objects[objindex]->bssstart = bsssize; @@ -165,7 +172,7 @@ void addfile(char * nom) { } popcontext(); fclose(f); - + textsize += objects[objindex]->s_text; datasize += objects[objindex]->s_data; bsssize += objects[objindex]->s_bss; @@ -176,7 +183,8 @@ void addfile(char * nom) { /* Simplification de vie... */ -static void dumptab(Uint32 * tab, int s, FILE * f) { +static void dumptab(Uint32 * tab, int s, FILE * f) +{ int i; for (i = 0; i < s; i++) { @@ -186,18 +194,21 @@ static void dumptab(Uint32 * tab, int s, FILE * f) { /* Nous dumpons la mémoire dans le fichier */ -static void dumptext(object_t * obj, FILE * f) { +static void dumptext(object_t * obj, FILE * f) +{ dumptab(obj->text, obj->s_text, f); } -static void dumpdata(object_t * obj, FILE * f) { +static void dumpdata(object_t * obj, FILE * f) +{ dumptab(obj->data, obj->s_data, f); } /* Cette fonction va calculer les quelques relogements statiques et dynamiques que nous a laissé l'assembleur */ -static void dumprelog(FILE * f) { - symbol_t * s = symbols, * t; +static void dumprelog(FILE * f) +{ + symbol_t *s = symbols, *t; char trouve, err[BUFSIZ]; for (s = s->next; s; s = s->next) { @@ -207,17 +218,20 @@ static void dumprelog(FILE * f) { sprintf(err, _("Symbol %s not found"), s->name); exception(1, err); } - switch(s->type) { - case 1: /* text */ - switch(t->type) { + switch (s->type) { + case 1: /* text */ + switch (t->type) { case 0: - objects[s->objindex]->text[s->offset] += objects[t->objindex]->textstart + t->offset; + objects[s->objindex]->text[s->offset] += + objects[t->objindex]->textstart + t->offset; break; case 2: - objects[s->objindex]->text[s->offset] += textsize + objects[t->objindex]->datastart + t->offset; + objects[s->objindex]->text[s->offset] += + textsize + objects[t->objindex]->datastart + t->offset; break; case 4: - objects[s->objindex]->text[s->offset] += textsize + datasize + objects[t->objindex]->bssstart + t->offset; + objects[s->objindex]->text[s->offset] += + textsize + datasize + objects[t->objindex]->bssstart + t->offset; break; default: exception(1, _("Internal error")); @@ -225,16 +239,19 @@ static void dumprelog(FILE * f) { } writeword(objects[s->objindex]->textstart + s->offset, f); break; - case 3: /* data */ - switch(t->type) { + case 3: /* data */ + switch (t->type) { case 0: - objects[s->objindex]->data[s->offset] += objects[t->objindex]->textstart + t->offset; + objects[s->objindex]->data[s->offset] += + objects[t->objindex]->textstart + t->offset; break; case 2: - objects[s->objindex]->data[s->offset] += textsize + objects[t->objindex]->datastart + t->offset; + objects[s->objindex]->data[s->offset] += + textsize + objects[t->objindex]->datastart + t->offset; break; case 4: - objects[s->objindex]->data[s->offset] += textsize + datasize + objects[t->objindex]->bssstart + t->offset; + objects[s->objindex]->data[s->offset] += + textsize + datasize + objects[t->objindex]->bssstart + t->offset; break; default: exception(1, _("Internal error")); @@ -252,17 +269,18 @@ static void dumprelog(FILE * f) { /* Cette fonction sert à écrire le fichier de sortie. */ -void dumpfile(char * nom) { - FILE * f; +void dumpfile(char *nom) +{ + FILE *f; int i; pushcontext(_("Writing output file")); f = openfilewriting(nom); - + if (startpoint == -1) { exception(1, _("No startpoint defined.")); } - + pushcontext(_("Writing headers")); writeword(0x58454e4e, f); writeword(nbrsymbs + textsize + datasize + 7, f); @@ -284,50 +302,55 @@ void dumpfile(char * nom) { dumpdata(objects[i], f); } popcontext(); - + popcontext(); - fprintf(stderr, _("Statistics: %i words of text, %i words of data and reserving %i words\n"), textsize, datasize, bsssize); + fprintf(stderr, _("Statistics: %i words of text, %i words of data and reserving %i words\n"), textsize, + datasize, bsssize); fprintf(stderr, _("Output file size: %i words containing %i relocating offsets.\n"), ftell(f), nbrsymbs); fclose(f); } /* Fonctions d'initialisations et de libération de mémoire */ -void init(int n) { +void init(int n) +{ int i; - + Initialise(&symbs); objects = (object_t **) Emalloc(n * sizeof(object_t *)); psymbols = symbols = (symbol_t *) Emalloc(sizeof(symbol_t)); - + for (i = 0; i < n; i++) { objects[i] = (object_t *) Emalloc(sizeof(object_t)); - objects[i]->s_text = objects[i]->s_data = objects[i]->s_bss = objects[i]->textstart = objects[i]->datastart = 0; + objects[i]->s_text = objects[i]->s_data = objects[i]->s_bss = objects[i]->textstart = + objects[i]->datastart = 0; objects[i]->text = objects[i]->data = NULL; } - + symbols->next = NULL; } -void free_symbol(symbol_t * s) { +void free_symbol(symbol_t * s) +{ if (s->next) free_symbol(s); - + free(s->name); free(s); } -void flush(void) { +void flush(void) +{ int i; - + DetruitTab(&symbs); for (i = 0; i < objindex; i++) { if (objects[i]->text) free(objects[i]->text); - if (objects[i]->data); - free(objects[i]->data); + if (objects[i]->data) ; + free(objects[i]->data); free(objects[i]); } free(objects); diff --git a/lib/memoire.c b/lib/memoire.c index e191735..36b2afa 100644 --- a/lib/memoire.c +++ b/lib/memoire.c @@ -22,7 +22,7 @@ Uint32 LD(Uint32 offset) Reset(&i); if (offset < 0 || offset >= TAILLE_MEMOIRE) { - exception(1,_("Invalid Memory Adress")); + exception(1, _("Invalid Memory Adress")); return (i); } else { i = memoire_principale[offset]; @@ -34,7 +34,7 @@ Uint32 LD(Uint32 offset) void ST(Uint32 offset, Uint32 valeur) { if (offset < 0 || offset >= TAILLE_MEMOIRE) - exception(1,_("Invalid Memory Adress")); + exception(1, _("Invalid Memory Adress")); else memoire_principale[offset] = valeur; } @@ -579,20 +579,20 @@ void main(void) fprintf(stderr, " + %s (%s) type: %s\n", pattern->expr[i]->name ? pattern->expr[i]->name : "Opérateur [", pattern->expr[i]->string ? pattern->expr[i]->string : "Aucune chaîne associée", - pattern->expr[i]->type ? "Constante prédéfinie" : pattern->expr[i]-> - left ? "Binaire" : pattern->expr[i]->right ? "Unaire" : "Feuille"); + pattern->expr[i]->type ? "Constante prédéfinie" : pattern-> + expr[i]->left ? "Binaire" : pattern->expr[i]->right ? "Unaire" : "Feuille"); if (pattern->expr[i]->left) { fprintf(stderr, " - gauche: %s (%s) type: %s\n", pattern->expr[i]->left->name ? pattern->expr[i]->left->name : "Opérateur [", - pattern->expr[i]->left->string ? pattern->expr[i]->left-> - string : "Aucune chaîne associée", + pattern->expr[i]->left->string ? pattern->expr[i]-> + left->string : "Aucune chaîne associée", pattern->expr[i]->left->type ? "Constante prédéfinie" : "Feuille"); } if (pattern->expr[i]->right) { fprintf(stderr, " - droite: %s (%s) type: %s\n", pattern->expr[i]->right->name ? pattern->expr[i]->right->name : "Opérateur [", - pattern->expr[i]->right->string ? pattern->expr[i]->right-> - string : "Aucune chaîne associée", + pattern->expr[i]->right->string ? pattern->expr[i]-> + right->string : "Aucune chaîne associée", pattern->expr[i]->right->type ? "Constante prédéfinie" : "Feuille"); } } diff --git a/lib/registre.c b/lib/registre.c index 4edb885..2d88d45 100644 --- a/lib/registre.c +++ b/lib/registre.c @@ -73,7 +73,7 @@ Uint32 LireRegistre(Uint32 champ_registre) if (ValeurBit(champ_registre, 5) == 0) { /* Test du bit S */ Reset(&i); if (champ_registre < 0 || champ_registre >= NB_REGISTRES_UTILISABLES) { /* Si on voudrait diminuer le nombre de registres */ - exception(1,_("Invalid Register Descriptor")); /* Il n'y a que 32 registres */ + exception(1, _("Invalid Register Descriptor")); /* Il n'y a que 32 registres */ return (i); } i = registre[champ_registre]; /* Registre classique */ @@ -89,7 +89,7 @@ Uint32 LireRegistre(Uint32 champ_registre) case 3: return (LireRegistreFLAG()); default:{ - exception(1,_("Invalid Register Descriptor")); + exception(1, _("Invalid Register Descriptor")); return (0); } } @@ -103,7 +103,7 @@ void EcrireRegistre(Uint32 champ_registre, Uint32 valeur) if (ValeurBit(champ_registre, 5) == 0) { /* Test du bit S */ Reset(&i); if (champ_registre < 0 || champ_registre >= NB_REGISTRES_UTILISABLES) - exception(1,_("Invalid Register Descriptor")); /* Il n'y a que 32 registres */ + exception(1, _("Invalid Register Descriptor")); /* Il n'y a que 32 registres */ else registre[champ_registre] = valeur; /* Registre classique */ } else /* Registre spécial */ @@ -117,7 +117,7 @@ void EcrireRegistre(Uint32 champ_registre, Uint32 valeur) case 3: EcrireRegistreFLAG(valeur); default: - exception(1,_("Invalid Register Descriptor")); + exception(1, _("Invalid Register Descriptor")); } } @@ -173,9 +173,10 @@ void ResetParity(void) } -void ResetRegistres(void) -{ int i; +void ResetRegistres(void) +{ + int i; - for (i=0; i<NB_REGISTRES_PHYSIQUES;i++) - registre[i]=0; -}
\ No newline at end of file + for (i = 0; i < NB_REGISTRES_PHYSIQUES; i++) + registre[i] = 0; +} diff --git a/lib/simulator.c b/lib/simulator.c index b046129..1fabf67 100644 --- a/lib/simulator.c +++ b/lib/simulator.c @@ -15,14 +15,12 @@ #include "exceptions.h" +/* CALL RET ??? + verifier le reste - -// CALL RET ??? -// verifier le reste - -// initialisation de la MP avec un malloc -// fermeture du prog et free -// catch du ctrl-c + initialisation de la MP avec un malloc + fermeture du prog et free + catch du ctrl-c */ Uint32 LireInstruction(void) { @@ -41,7 +39,7 @@ Uint32 Adresse(Uint32 u, Uint32 instruction) switch (champ(u, 2)) { case 0: exception(1, _("Adresse: Call With Invalid r/m Field State ( r/m=00 )")); - return(0); + return (0); case 1: tmp = LireInstruction(); IncrementeCompteurOrdinal(); @@ -54,7 +52,7 @@ Uint32 Adresse(Uint32 u, Uint32 instruction) return (tmp); default: exception(1, _("Adresse: Unmatched Addr Field")); - return(0); + return (0); } } @@ -82,204 +80,210 @@ void DecodeExec(Uint32 instruction, Uint32 entrypoint) case 3: case 4: case 5: - /* ALU */ - champ_registre_resultat = Champ1(instruction); /* Champ du registre dans lequel va etre stocké le résultat */ - val1 = LireRegistre(Champ2(instruction)); /* Premier entier qui va etre utilisé dans l'opération */ - if (ValeurBit(Extension(instruction), 0) == 0) - val2 = LireRegistre(Champ3(instruction)); /* Deuxième entier, stocké dans un registre, qui va etre utilisé dans l'opération */ - else { - val2 = LireInstruction(); /* Deuxième entier, stocké après l'instruction, qui va etre utilisé dans l'opération */ - IncrementeCompteurOrdinal(); - } - if (ValeurBit(Extension(instruction), 1) == 0) /* Teste si l'opération est signée ou pas */ - switch (Opcode(instruction)) { - case 0: - resultat = AdditionNonSigne(val1, val2); - break; - case 1: - resultat = SoustractionNonSigne(val1, val2); - break; - case 2: - resultat = MultiplicationNonSigne(val1, val2); - break; - case 3: - resultat = DivisionNonSigne(val1, val2); - break; - case 4: - resultat = AND(val1, val2); - break; - case 5: - resultat = OR(val1, val2); - break; - case 6: - resultat = SHL(val1); - break; - case 7: - resultat = SHR(val1); - break; - } else - switch (Opcode(instruction)) { - case 0: - resultat = AdditionSigne(val1, val2); - break; - case 1: - resultat = SoustractionSigne(val1, val2); - break; - case 2: - resultat = MultiplicationSigne(val1, val2); - break; - case 3: - resultat = DivisionSigne(val1, val2); - break; - case 4: - resultat = AND(val1, val2); - break; - case 5: - resultat = OR(val1, val2); - break; - case 6: - resultat = SHL(val1); - break; - case 7: - resultat = SHR(val1); - break; - } - EcrireRegistre(champ_registre_resultat, resultat); /* On écrit le résultat dans le registre de sortie */ - break; - case 8: /* MOV */ - if (ValeurBit(Extension(instruction), 4) == 1) { /* MOV conditionnel */ - if (ValeurBit(Extension(instruction), 5) == 0) { /* Test normal */ - switch (champ(Extension(instruction) >> 2, 4)) { /* teste les bits 2 et 3 */ - case 0: - if (Overflow() == 1) - goto fin; - case 1: - if (Zero() == 1) - goto fin; - case 2: - if (Sign() == 1) - goto fin; - case 3: - if (Parity() == 1) - goto fin; - } - } else { /* Negation du test */ - switch (champ(Extension(instruction) >> 2, 4)) { /* teste les bits 2 et 3 */ - case 0: - if (Overflow() == 0) - goto fin; - case 1: - if (Zero() == 0) - goto fin; - case 2: - if (Sign() == 0) - goto fin; - case 3: - if (Parity() == 0) - goto fin; - } - } - } - /* Pas de MOV conditionnel */ - if (ValeurBit(Extension(instruction), 1) == 0) { /* Mov arg1 arg2 */ - if (ValeurBit(Extension(instruction), 0) == 0) { /* arg2 = reg */ - if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ - EcrireRegistre(Champ3(instruction), LireRegistre(Champ2(instruction))); - } else { - ST(Adresse(Champ1(instruction), instruction), LireRegistre(Champ2(instruction))); - } - } else { /* arg2 = imm32 */ - if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ - EcrireRegistre(Champ3(instruction), LireInstruction()); - IncrementeCompteurOrdinal(); - } else { - ST(Adresse(Champ1(instruction), instruction), LireInstruction()); - IncrementeCompteurOrdinal(); - } - } - } else { - if (ValeurBit(Extension(instruction), 0) == 0) { /* arg2 = reg */ - if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ - EcrireRegistre(Champ2(instruction), LireRegistre(Champ3(instruction))); - - } else { - EcrireRegistre(Champ2(instruction), LD(Adresse(Champ1(instruction), instruction))); - } - } else { /* arg2 = imm32 */ - exception(1, _("MOV: Memory to Memory Forbidden On This Type Of Processor")); - } - } - fin: - break; - - case 9: /* NOP */ - /* Instruction nulle */ - break; - - case 10: /* J[cond] */ - case 11: - int test1, test2; - - switch (champ(Extension(instruction), 4)) { + case 6: + case 7: + /* ALU */ + champ_registre_resultat = Champ1(instruction); /* Champ du registre dans lequel va etre stocké le résultat */ + val1 = LireRegistre(Champ2(instruction)); /* Premier entier qui va etre utilisé dans l'opération */ + if (ValeurBit(Extension(instruction), 0) == 0) + val2 = LireRegistre(Champ3(instruction)); /* Deuxième entier, stocké dans un registre, qui va etre utilisé dans l'opération */ + else { + val2 = LireInstruction(); /* Deuxième entier, stocké après l'instruction, qui va etre utilisé dans l'opération */ + IncrementeCompteurOrdinal(); + } + if (ValeurBit(Extension(instruction), 1) == 0) /* Teste si l'opération est signée ou pas */ + switch (Opcode(instruction)) { case 0: - if (Champ1(instruction) == Champ2(instruction)) { - test1 = 1; - } else { - test1 = LireRegistre(Champ1(instruction)) == LireRegistre(Champ2(instruction)); - } + resultat = AdditionNonSigne(val1, val2); break; case 1: - test1 = LireRegistre(Champ1(instruction)) != LireRegistre(Champ2(instruction)); + resultat = SoustractionNonSigne(val1, val2); break; case 2: - test1 = LireRegistre(Champ1(instruction)) < LireRegistre(Champ2(instruction)); + resultat = MultiplicationNonSigne(val1, val2); break; case 3: - test1 = LireRegistre(Champ1(instruction)) <= LireRegistre(Champ2(instruction)); + resultat = DivisionNonSigne(val1, val2); break; - } - switch (champ(Extension(instruction) >> 2, 4)) { - case 0: - test2 = Overflow(); + case 4: + resultat = AND(val1, val2); break; - case 1: - test2 = Zero(); + case 5: + resultat = OR(val1, val2); break; - case 2: - test2 = Sign(); + case 6: + resultat = SHL(val1); break; - case 3: - test2 = Parity(); + case 7: + resultat = SHR(val1); break; - } - switch (champ(Extension(instruction) >> 4, 4)) { + } else + switch (Opcode(instruction)) { case 0: - test1 = test1; + resultat = AdditionSigne(val1, val2); break; case 1: - test1 = test1 || test2; + resultat = SoustractionSigne(val1, val2); break; case 2: - test1 = test1 && !test2; + resultat = MultiplicationSigne(val1, val2); break; case 3: - test1 = test1 || !test2; + resultat = DivisionSigne(val1, val2); + break; + case 4: + resultat = AND(val1, val2); + break; + case 5: + resultat = OR(val1, val2); + break; + case 6: + resultat = SHL(val1); break; + case 7: + resultat = SHR(val1); + break; + } + EcrireRegistre(champ_registre_resultat, resultat); /* On écrit le résultat dans le registre de sortie */ + break; + case 8: /* MOV */ + if (ValeurBit(Extension(instruction), 4) == 1) { /* MOV conditionnel */ + if (ValeurBit(Extension(instruction), 5) == 0) { /* Test normal */ + switch (champ(Extension(instruction) >> 2, 4)) { /* teste les bits 2 et 3 */ + case 0: + if (Overflow() == 1) + goto fin; + case 1: + if (Zero() == 1) + goto fin; + case 2: + if (Sign() == 1) + goto fin; + case 3: + if (Parity() == 1) + goto fin; + } + } else { /* Negation du test */ + switch (champ(Extension(instruction) >> 2, 4)) { /* teste les bits 2 et 3 */ + case 0: + if (Overflow() == 0) + goto fin; + case 1: + if (Zero() == 0) + goto fin; + case 2: + if (Sign() == 0) + goto fin; + case 3: + if (Parity() == 0) + goto fin; + } + } + } + /* Pas de MOV conditionnel */ + if (ValeurBit(Extension(instruction), 1) == 0) { /* Mov arg1 arg2 */ + if (ValeurBit(Extension(instruction), 0) == 0) { /* arg2 = reg */ + if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ + EcrireRegistre(Champ3(instruction), LireRegistre(Champ2(instruction))); + } else { + ST(Adresse(Champ1(instruction), instruction), + LireRegistre(Champ2(instruction))); + } + } else { /* arg2 = imm32 */ + if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ + EcrireRegistre(Champ3(instruction), LireInstruction()); + IncrementeCompteurOrdinal(); + } else { + ST(Adresse(Champ1(instruction), instruction), LireInstruction()); + IncrementeCompteurOrdinal(); + } } - if (test1) { Uint32 tmp; - tmp=LireInstruction(); - EcrireRegistrePC(tmp); + } else { + if (ValeurBit(Extension(instruction), 0) == 0) { /* arg2 = reg */ + if (champ(Champ1(instruction), 2) == 0) { /* r/m de arg1 = 0 */ + EcrireRegistre(Champ2(instruction), LireRegistre(Champ3(instruction))); + + } else { + EcrireRegistre(Champ2(instruction), + LD(Adresse(Champ1(instruction), instruction))); + } + } else { /* arg2 = imm32 */ + exception(1, _("MOV: Memory to Memory Forbidden On This Type Of Processor")); + } + } + fin: + break; + + case 9: /* NOP */ + /* Instruction nulle */ + break; + + case 10: /* J[cond] */ + case 11: + int test1, test2; + + switch (champ(Extension(instruction), 4)) { + case 0: + if (Champ1(instruction) == Champ2(instruction)) { + test1 = 1; + } else { + test1 = LireRegistre(Champ1(instruction)) == LireRegistre(Champ2(instruction)); } break; - + case 1: + test1 = LireRegistre(Champ1(instruction)) != LireRegistre(Champ2(instruction)); + break; + case 2: + test1 = LireRegistre(Champ1(instruction)) < LireRegistre(Champ2(instruction)); + break; + case 3: + test1 = LireRegistre(Champ1(instruction)) <= LireRegistre(Champ2(instruction)); + break; + } + switch (champ(Extension(instruction) >> 2, 4)) { + case 0: + test2 = Overflow(); + break; + case 1: + test2 = Zero(); + break; + case 2: + test2 = Sign(); + break; + case 3: + test2 = Parity(); + break; + } + switch (champ(Extension(instruction) >> 4, 4)) { + case 0: + test1 = test1; + break; + case 1: + test1 = test1 || test2; + break; + case 2: + test1 = test1 && !test2; + break; + case 3: + test1 = test1 || !test2; + break; + } + if (test1) { + Uint32 tmp; + + tmp = LireInstruction(); + EcrireRegistrePC(tmp); + } + break; + case 12: /* JMP */ case 13: if (ValeurBit(Extension(instruction), 0) == 0) { - /* RET */ - EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(),Champ1(instruction))); + /* RET */ + EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), Champ1(instruction))); EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); EcrireRegistrePC(LD(LireRegistreSP())); - + } else if (ValeurBit(Extension(instruction), 1) == 0) { /* JMP */ if (ValeurBit(Extension(instruction), 2) == 0) { EcrireRegistrePC(LireRegistre(Champ1(instruction))); @@ -287,8 +291,8 @@ void DecodeExec(Uint32 instruction, Uint32 entrypoint) EcrireRegistrePC(LireInstruction()); } } else { /* CALL */ - ST(LireRegistreSP(),LireRegistrePC()); - EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(),1)); + ST(LireRegistreSP(), LireRegistrePC()); + EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); if (ValeurBit(Extension(instruction), 2) == 0) { EcrireRegistrePC(LireRegistre(Champ1(instruction))); } else { @@ -297,31 +301,31 @@ void DecodeExec(Uint32 instruction, Uint32 entrypoint) } break; case 14: /* PUSH */ - Uint32 val; /* valeur qui va etre stockée */ + Uint32 val; /* valeur qui va etre stockée */ - if (ValeurBit(Extension(instruction), 0) == 0) - val = LireRegistre(Champ1(instruction)); - else { - val = LireInstruction(); - IncrementeCompteurOrdinal(); - } - ST(LireRegistreSP(), val); - EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); - break; + if (ValeurBit(Extension(instruction), 0) == 0) + val = LireRegistre(Champ1(instruction)); + else { + val = LireInstruction(); + IncrementeCompteurOrdinal(); + } + ST(LireRegistreSP(), val); + EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); + break; case 15: /* POP */ - EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); - EcrireRegistre(Champ1(instruction), LD(LireRegistreSP())); - - break; + EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); + EcrireRegistre(Champ1(instruction), LD(LireRegistreSP())); + + break; case 127: /* HALT-RESET */ - if (ValeurBit(Extension(instruction), 0)) - exit(0); /* Halt *//* *******************FIXMI********************* */ - else - ResetRegistres(); - Traitement(entrypoint); /* Reset *//* ************FIXMI************** */ - break; + if (ValeurBit(Extension(instruction), 0)) + exit(0); /* Halt *//* *******************FIXMI********************* */ + else + ResetRegistres(); + Traitement(entrypoint); /* Reset *//* ************FIXMI************** */ + break; default: - exception(1, _("DecodeExec: Invalid Opcode")); + exception(1, _("DecodeExec: Invalid Opcode")); } } } @@ -333,7 +337,7 @@ void Traitement(Uint32 entrypoint) while (!0) { instruction = LireInstruction(); IncrementeCompteurOrdinal(); - DecodeExec(instruction, entrypoint); + DecodeExec(instruction, entrypoint); } } @@ -351,7 +355,8 @@ void AfficheReg(void) // affiche reg } printf("\n"); } - printf("Rg: %08lX | Rd: %08lX | Flag: %08lX | PC: %08lX\n", LireRegistreRG(), LireRegistreRD(), LireRegistreFLAG(), registre[REG_PC]); + printf("Rg: %08lX | Rd: %08lX | Flag: %08lX | PC: %08lX\n", LireRegistreRG(), LireRegistreRD(), + LireRegistreFLAG(), registre[REG_PC]); printf("\n"); } diff --git a/src/compilo.c b/src/compilo.c index 61ea761..d63fb66 100644 --- a/src/compilo.c +++ b/src/compilo.c @@ -9,18 +9,21 @@ #include "hash.h" #include "assembler.h" -typedef void (*sighandler_t)(int); +typedef void (*sighandler_t) (int); -void invite(void) { +void invite(void) +{ fprintf(stderr, _("Assembler v1.0\n\n")); } -void usage(void) { +void usage(void) +{ fprintf(stderr, _("Usage: compilo program.asm objet.out\n")); exit(0); } -void init_all(void) { +void init_all(void) +{ fprintf(stderr, _(" o Initialising the meta engine... ")); if (meta_init()) { @@ -32,40 +35,44 @@ void init_all(void) { if (meta_load("instructions.txt")) { exception(1, _("Meta language loading failed.")); } - + fprintf(stderr, _(" Done!\n o Initialising the assembler core...")); - + if (assembler_init()) { exception(1, _("Assembler core init failed.")); } - + fprintf(stderr, _(" Done!\n")); } -void flush_all(void) { +void flush_all(void) +{ assembler_flush(); meta_flush(); } -void segfaulthand(int i) { +void segfaulthand(int i) +{ exception(1, _("Signal received: segfault")); } -int main(int argc, char ** argv) { +int main(int argc, char **argv) +{ invite(); - + /* signal(SIGSEGV, segfaulthand); */ - - if (argc != 3) usage(); + + if (argc != 3) + usage(); fprintf(stderr, _("\nPerforming initialisation...\n\n")); init_all(); assemble_file(argv[1], argv[2]); - + fprintf(stderr, _("\nPerforming shutdown...\n\n")); flush_all(); - + fprintf(stderr, _("Exitting, bye!\n")); - return 0; + return 0; } diff --git a/src/linker.c b/src/linker.c index 61301de..ba1583c 100644 --- a/src/linker.c +++ b/src/linker.c @@ -6,56 +6,63 @@ #include "exceptions.h" #include "linker.h" -typedef void (*sighandler_t)(int); +typedef void (*sighandler_t) (int); -void invite(void) { +void invite(void) +{ fprintf(stderr, _("Linker v1.0\n\n")); } -void usage(void) { +void usage(void) +{ fprintf(stderr, _("Usage: linker obj1 [obj2 [obj3 [...]]] binary\n")); exit(0); } -void init_all(int n) { +void init_all(int n) +{ fprintf(stderr, _(" o Initialising the linker... ")); init(n); fprintf(stderr, _(" Done!\n")); } -void flush_all(void) { +void flush_all(void) +{ flush(); } -void segfaulthand(int i) { +void segfaulthand(int i) +{ exception(1, _("Signal received: segfault")); } -int main(int argc, char ** argv) { +int main(int argc, char **argv) +{ int i; invite(); - + signal(SIGSEGV, segfaulthand); - - if (argc < 3) usage(); + + if (argc < 3) + usage(); fprintf(stderr, _("\nPerforming initialisation...\n\n")); init_all(argc - 2); - + fprintf(stderr, _("Linking files...\n")); - + for (i = 1; i < argc - 1; i++) { addfile(argv[i]); } - + fprintf(stderr, _("Generating output file...\n")); dumpfile(argv[i]); fprintf(stderr, _("\nPerforming shutdown...\n\n")); flush_all(); - + fprintf(stderr, _("Exitting, bye!\n")); - return 0; + return 0; } diff --git a/src/simul.c b/src/simul.c index 10593bf..11dd9d8 100644 --- a/src/simul.c +++ b/src/simul.c @@ -13,9 +13,8 @@ int main(void) { - Debogueur(); + Debogueur(); // Initialisation(); // Traitement(); - return (0); + return (0); } -
\ No newline at end of file |