From 5aed7634c8993e3366817c4b20fca1aa18eacf21 Mon Sep 17 00:00:00 2001 From: Pixel <> Date: Tue, 17 Apr 2001 04:05:52 +0000 Subject: Indentation plus faible --- lib/alu.c | 1363 +++++++++++---------- lib/assembler.c | 3604 ++++++++++++++++++++++++++++-------------------------- lib/exceptions.c | 68 +- lib/fpu.c | 2 +- lib/hash.c | 245 ++-- lib/interne.c | 58 +- lib/linker.c | 498 ++++---- lib/memoire.c | 158 +-- lib/meta.c | 1058 ++++++++-------- lib/numbers.c | 95 +- lib/parser.c | 483 ++++---- lib/registre.c | 142 +-- lib/simulator.c | 884 +++++++------- lib/terminal.c | 28 +- 14 files changed, 4493 insertions(+), 4193 deletions(-) (limited to 'lib') diff --git a/lib/alu.c b/lib/alu.c index 1acb64a..0b4525a 100644 --- a/lib/alu.c +++ b/lib/alu.c @@ -17,258 +17,259 @@ Uint32 SecondResult = 0; Uint32 RNot(Uint32 a) { - return (~a) + 1; + return (~a) + 1; } Uint32 RAdditionNonSigne(Uint32 a, Uint32 b) { - unsigned long long tr = a, masq = 1; + unsigned long long tr = a, masq = 1; - tr += b; + tr += b; - masq <<= 32; - if (masq & tr) { - SetOverflow(); - } else { - ResetOverflow(); - } + masq <<= 32; + if (masq & tr) { + SetOverflow(); + } else { + ResetOverflow(); + } - if (tr) { - ResetZero(); - } else { - SetZero(); - } + if (tr) { + ResetZero(); + } else { + SetZero(); + } - if (tr & 1) { - SetParity(); - } else { - ResetParity(); - } + if (tr & 1) { + SetParity(); + } else { + ResetParity(); + } - ResetSign(); - return tr; + ResetSign(); + return tr; } Uint32 RAdditionSigne(long int a, long int b) { - long int tr = a; - - tr += b; - - if (((a & 0x80000000) && (b & 0x80000000) && !(tr & 0x80000000)) || (!(a & 0x80000000) && !(b & 0x80000000) && (tr & 0x80000000))) { - SetOverflow(); - } else { - ResetOverflow(); - } - - if (tr) { - ResetZero(); - } else { - SetZero(); - } + long int tr = a; - if (tr & 1) { - SetParity(); - } else { - ResetParity(); - } + tr += b; - if (tr & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - return tr; + if (((a & 0x80000000) && (b & 0x80000000) && !(tr & 0x80000000)) + || (!(a & 0x80000000) && !(b & 0x80000000) && (tr & 0x80000000))) { + SetOverflow(); + } else { + ResetOverflow(); + } + + if (tr) { + ResetZero(); + } else { + SetZero(); + } + + if (tr & 1) { + SetParity(); + } else { + ResetParity(); + } + + if (tr & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return tr; } Uint32 RSoustractionNonSigne(Uint32 a, Uint32 b) { - return RAdditionNonSigne(a, RNot(b)); + return RAdditionNonSigne(a, RNot(b)); } Uint32 RSoustractionSigne(Uint32 a, Uint32 b) { - return RAdditionSigne(a, RNot(b)); + return RAdditionSigne(a, RNot(b)); } Uint32 RMultiplicationNonSigne(Uint32 a, Uint32 b) { - unsigned long long temp = a; - - temp *= b; - SecondResult = temp >> 32; - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - ResetOverflow(); - ResetSign(); - return (temp); + unsigned long long temp = a; + + temp *= b; + SecondResult = temp >> 32; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + ResetOverflow(); + ResetSign(); + return (temp); } Uint32 RMultiplicationSigne(long int a, long int b) { - long long temp = a; - - temp *= b; - SecondResult = temp >> 32; - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - ResetOverflow(); - return (temp); + long long temp = a; + + temp *= b; + SecondResult = temp >> 32; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + ResetOverflow(); + return (temp); } Uint32 RDivisionNonSigne(Uint32 a, Uint32 b) { - unsigned long long temp = a; - - temp /= b; - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - ResetOverflow(); - ResetSign(); - SecondResult = a % b; - return temp; + unsigned long long temp = a; + + temp /= b; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + ResetOverflow(); + ResetSign(); + SecondResult = a % b; + return temp; } Uint32 RDivisionSigne(long int a, long int b) { - long long temp = a; - - temp /= b; - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - ResetOverflow(); - SecondResult = a % b; - return temp; + long long temp = a; + + temp /= b; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + ResetOverflow(); + SecondResult = a % b; + return temp; } Uint32 RAND(Uint32 a, Uint32 b) { - Uint32 temp = a & b; - - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - return temp; + Uint32 temp = a & b; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 ROR(Uint32 a, Uint32 b) { - Uint32 temp = a | b; - - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - return temp; + Uint32 temp = a | b; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 RSHL(Uint32 a) { - Uint32 temp = a << 1; - - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - return temp; + Uint32 temp = a << 1; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 RSHR(Uint32 a) { - Uint32 temp = a >> 1; - - if (temp & 1) { - SetParity(); - } else { - ResetParity(); - } - if (temp) { - ResetZero(); - } else { - SetZero(); - } - if (temp & 0x80000000) { - SetSign(); - } else { - ResetSign(); - } - return temp; + Uint32 temp = a >> 1; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } @@ -279,14 +280,14 @@ Uint32 RSHR(Uint32 a) /*Return la valeur du ieme bit en position 0 */ Uint32 ValeurIbitsAuDeb(Uint32 nb, int i) { - Uint32 val, un = 1; + Uint32 val, un = 1; - if ((i > 31) || (i < 0)) { - exception(1, _("ValeurIbitsAuDeb: position not in interval")); - } - val = nb >> i; - val = val & un; - return (val); + if ((i > 31) || (i < 0)) { + exception(1, _("ValeurIbitsAuDeb: position not in interval")); + } + val = nb >> i; + val = val & un; + return (val); } @@ -308,684 +309,696 @@ Uint32 ValeurIbit(Uint32 nb, int i) { /*Met a vrai le ieme bit */ Uint32 MettreAVIbit(Uint32 nb, int i) { - Uint32 val = 1; + Uint32 val = 1; - return (nb | (val << i)); + return (nb | (val << i)); } /* Met a Vrai le Uint32 */ Uint32 MetAVUint32(void) { - int i; + int i; - Uint32 val = 0, un = 1; + Uint32 val = 0, un = 1; - for (i = 0; i <= 31; i++) { - val = (val | un); - un <<= 1; - } - return (val); + for (i = 0; i <= 31; i++) { + val = (val | un); + un <<= 1; + } + return (val); } /* Met a Faux le Uint32 */ Uint32 MetAFUint32(void) { - return (0); + return (0); } /* Inverse la valeur du i eme bit */ Uint32 InverseIbit(Uint32 nb, int i) { - Uint32 un = 1; + Uint32 un = 1; - if ((i > 31) || (i < 0)) { - exception(1, _("InverseIbit: position not in interval")); - } - un = (un << i); - return (nb ^ un); + if ((i > 31) || (i < 0)) { + exception(1, _("InverseIbit: position not in interval")); + } + un = (un << i); + return (nb ^ un); } /* Inverse le Uint32 */ Uint32 InverseUint32(Uint32 x) { - int i; + int i; - Uint32 val = x; + Uint32 val = x; - for (i = 0; i <= 31; i++) { - val = InverseIbit(val, i); - } - return (val); + for (i = 0; i <= 31; i++) { + val = InverseIbit(val, i); + } + return (val); } Uint32 NAND(Uint32 x, Uint32 y) { - Uint32 m = 1, z = 0; - int i; + Uint32 m = 1, z = 0; + int i; - for (i = 0; i < 32; i++) { - z |= (x & m) & (y & m); - m <<= 1; - } - return (z); + for (i = 0; i < 32; i++) { + z |= (x & m) & (y & m); + m <<= 1; + } + return (z); } Uint32 NOR(Uint32 x, Uint32 y) { - Uint32 m = 1, z = 0; - int i; + Uint32 m = 1, z = 0; + int i; - for (i = 0; i < 32; i++) { - z |= (x & m) | (y & m); - m <<= 1; - } - return (z); + for (i = 0; i < 32; i++) { + z |= (x & m) | (y & m); + m <<= 1; + } + return (z); } Uint32 NSHLi(Uint32 x, int i) { - return (x << i); + return (x << i); } Uint32 NSHRi(Uint32 x, int i) { - return (x >> i); + return (x >> i); } Uint32 NSHL(Uint32 x) { - return NSHLi(x, 1); + return NSHLi(x, 1); } Uint32 NSHR(Uint32 x) { - return NSHRi(x, 1); + return NSHRi(x, 1); } Uint32 NAdditionNonSigne(Uint32 x, Uint32 y) { - int i; - - Uint32 a, b, tp, add = 0, ret = 0; - - if (x == 0) - return (y); - if (y == 0) - return (x); - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - tp = a + b + ret; - if (tp == 3) { - ret = 1; - tp = 1; - tp <<= i; - add |= tp; + int i; + + Uint32 a, b, tp, add = 0, ret = 0; + + if (x == 0) + return (y); + if (y == 0) + return (x); + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + tp = a + b + ret; + if (tp == 3) { + ret = 1; + tp = 1; + tp <<= i; + add |= tp; + } + + else { + if (tp == 2) { + ret = 1; + tp = 0; + tp <<= i; + add |= tp; + } + + else { + if (tp == 1) { + ret = 0; + tp = 1; + tp <<= i; + add |= tp; } else { - if (tp == 2) { - ret = 1; - tp = 0; - tp <<= i; - add |= tp; - } - - else { - if (tp == 1) { - ret = 0; - tp = 1; - tp <<= i; - add |= tp; - } - - else { - ret = 0; - } - } - } - if (i == 31) { - if (ret == 1) { - errRet = 1; - } + ret = 0; } + } + } + if (i == 31) { + if (ret == 1) { + errRet = 1; + } } - return (add); + } + return (add); } Uint32 NAdditionSigne(Uint32 x, Uint32 y) { - int i; - - Uint32 a, b, tp, add = 0, ret = 0; - - if (x == 0) - return (y); - if (y == 0) - return (x); - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - tp = a + b + ret; - if (tp == 3) { - ret = 1; - tp = 1; - tp <<= i; - add |= tp; + int i; + + Uint32 a, b, tp, add = 0, ret = 0; + + if (x == 0) + return (y); + if (y == 0) + return (x); + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + tp = a + b + ret; + if (tp == 3) { + ret = 1; + tp = 1; + tp <<= i; + add |= tp; + } + + else { + if (tp == 2) { + ret = 1; + tp = 0; + tp <<= i; + add |= tp; + } + + else { + if (tp == 1) { + ret = 0; + tp = 1; + tp <<= i; + add |= tp; } else { - if (tp == 2) { - ret = 1; - tp = 0; - tp <<= i; - add |= tp; - } - - else { - if (tp == 1) { - ret = 0; - tp = 1; - tp <<= i; - add |= tp; - } - - else { - ret = 0; - } - } - } - if (i == 30) { - if (ret == 1) { - errRet = 1; - } + ret = 0; } + } } - return (add); + if (i == 30) { + if (ret == 1) { + errRet = 1; + } + } + } + return (add); } Uint32 NSoustractionNonSigne(Uint32 x, Uint32 y) { /* x - y */ - int i; - - Uint32 a, b, tp, sou = 0, ret = 0; - - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - if (((a == 0) && (b == 0) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 0) && (ret == 1))) { - ret = 0; - tp = 0; - tp <<= i; - sou |= tp; + int i; + + Uint32 a, b, tp, sou = 0, ret = 0; + + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + if (((a == 0) && (b == 0) && (ret == 0)) + || ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) + && (b == 0) + && (ret == 1))) { + ret = 0; + tp = 0; + tp <<= i; + sou |= tp; + } + + else { + if (((a == 0) && (b == 0) && (ret == 1)) + || ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) + && (b == 1) + && (ret == 1))) { + ret = 1; + tp = 1; + tp <<= i; + sou |= tp; + } + + else { + if ((a == 1) && (b == 0) && (ret == 0) || 0) { + ret = 0; + tp = 1; + tp <<= i; + sou |= tp; } else { - if (((a == 0) && (b == 0) && (ret == 1)) || ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 1))) { - ret = 1; - tp = 1; - tp <<= i; - sou |= tp; - } - - else { - if ((a == 1) && (b == 0) && (ret == 0) || 0) { - ret = 0; - tp = 1; - tp <<= i; - sou |= tp; - } - - else { - ret = 1; - tp = 0; - tp <<= i; - sou |= tp; - } - } - } - if (i == 31) { - if (ret == 1) { - errRet = 1; - } + ret = 1; + tp = 0; + tp <<= i; + sou |= tp; } + } } - return (sou); + if (i == 31) { + if (ret == 1) { + errRet = 1; + } + } + } + return (sou); } Uint32 NSoustractionSigne(Uint32 x, Uint32 y) { /* x - y */ - int i; - - Uint32 a, b, tp, sou = 0, ret = 0; - - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - if (((a == 0) && (b == 0) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 0) && (ret == 1))) { - ret = 0; - tp = 0; - tp <<= i; - sou |= tp; + int i; + + Uint32 a, b, tp, sou = 0, ret = 0; + + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + if (((a == 0) && (b == 0) && (ret == 0)) + || ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) + && (b == 0) + && (ret == 1))) { + ret = 0; + tp = 0; + tp <<= i; + sou |= tp; + } + + else { + if (((a == 0) && (b == 0) && (ret == 1)) + || ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) + && (b == 1) + && (ret == 1))) { + ret = 1; + tp = 1; + tp <<= i; + sou |= tp; + } + + else { + if ((a == 1) && (b == 0) && (ret == 0) || 0) { + ret = 0; + tp = 1; + tp <<= i; + sou |= tp; } else { - if (((a == 0) && (b == 0) && (ret == 1)) || ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 1))) { - ret = 1; - tp = 1; - tp <<= i; - sou |= tp; - } - - else { - if ((a == 1) && (b == 0) && (ret == 0) || 0) { - ret = 0; - tp = 1; - tp <<= i; - sou |= tp; - } - - else { - ret = 1; - tp = 0; - tp <<= i; - sou |= tp; - } - } - } - if (i == 30) { - if (ret == 1) { - errRet = 1; - } + ret = 1; + tp = 0; + tp <<= i; + sou |= tp; } + } + } + if (i == 30) { + if (ret == 1) { + errRet = 1; + } } - return (sou); + } + return (sou); } couple Addition(Uint32 x, Uint32 y) { /* Renvoye le resultat + eventuellement une retenue */ - int i; - - couple z; - Uint32 a, b, tp, add = 0, ret = 0; - - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - tp = a + b + ret; - if (tp == 3) { - ret = 1; - tp = 1; - tp <<= i; - add |= tp; + int i; + + couple z; + Uint32 a, b, tp, add = 0, ret = 0; + + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + tp = a + b + ret; + if (tp == 3) { + ret = 1; + tp = 1; + tp <<= i; + add |= tp; + } + + else { + if (tp == 2) { + ret = 1; + tp = 0; + tp <<= i; + add |= tp; + } + + else { + if (tp == 1) { + ret = 0; + tp = 1; + tp <<= i; + add |= tp; } else { - if (tp == 2) { - ret = 1; - tp = 0; - tp <<= i; - add |= tp; - } - - else { - if (tp == 1) { - ret = 0; - tp = 1; - tp <<= i; - add |= tp; - } - - else { - ret = 0; - } - } - } - if (i == 31) { - if (ret == 1) { - z.deb = add; - z.fin = 1; - return (z); - } + ret = 0; } + } } - z.deb = add; - z.fin = 0; - return (z); + if (i == 31) { + if (ret == 1) { + z.deb = add; + z.fin = 1; + return (z); + } + } + } + z.deb = add; + z.fin = 0; + return (z); } couple Add30Mul(Uint32 x, Uint32 y) { - int i; - - couple z; - Uint32 a, b, tp, add = 0, ret = 0; - - for (i = 0; i < 31; i++) { - a = ValeurIbitsAuDeb(x, i); - b = ValeurIbitsAuDeb(y, i); - tp = a + b + ret; - if (tp == 3) { - ret = 1; - tp = 1; - tp <<= i; - add |= tp; + int i; + + couple z; + Uint32 a, b, tp, add = 0, ret = 0; + + for (i = 0; i < 31; i++) { + a = ValeurIbitsAuDeb(x, i); + b = ValeurIbitsAuDeb(y, i); + tp = a + b + ret; + if (tp == 3) { + ret = 1; + tp = 1; + tp <<= i; + add |= tp; + } + + else { + if (tp == 2) { + ret = 1; + tp = 0; + tp <<= i; + add |= tp; + } + + else { + if (tp == 1) { + ret = 0; + tp = 1; + tp <<= i; + add |= tp; } else { - if (tp == 2) { - ret = 1; - tp = 0; - tp <<= i; - add |= tp; - } - - else { - if (tp == 1) { - ret = 0; - tp = 1; - tp <<= i; - add |= tp; - } - - else { - ret = 0; - } - } - } - if (i == 30) { - if (ret == 1) { - z.deb = add; - z.fin = 1; - return (z); - } + ret = 0; } + } } - z.deb = add; - z.fin = 0; - return (z); + if (i == 30) { + if (ret == 1) { + z.deb = add; + z.fin = 1; + return (z); + } + } + } + z.deb = add; + z.fin = 0; + return (z); } Uint32 MultChifNomb(Uint32 x, Uint32 y) { - int i; + int i; - Uint32 mul = 0, a, un = 1; + Uint32 mul = 0, a, un = 1; - if (y == 0) - return (0); - if (x == 0) - return (0); - if (y == 1) - return (x); - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(x, i); - if (a == 1) { - un <<= i; - mul |= un; - } + if (y == 0) + return (0); + if (x == 0) + return (0); + if (y == 1) + return (x); + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(x, i); + if (a == 1) { + un <<= i; + mul |= un; } - return (mul); + } + return (mul); } couple MultipliNonSig(Uint32 x, Uint32 y) { - couple z, w; - int i; - - Uint32 tp, dec, add, a, pa1 = 0, pa2 = 0; - - for (i = 0; i < 32; i++) { - a = ValeurIbitsAuDeb(y, i); - tp = MultChifNomb(x, a); - dec = (tp >> (32 - i)); - pa2 = NAdditionNonSigne(pa2, dec); - add = tp << i; - z = Addition(pa1, add); - pa1 = z.deb; - if (z.fin == 1) { - w = Addition(pa2, 1); - pa2 = w.deb; - } + couple z, w; + int i; + + Uint32 tp, dec, add, a, pa1 = 0, pa2 = 0; + + for (i = 0; i < 32; i++) { + a = ValeurIbitsAuDeb(y, i); + tp = MultChifNomb(x, a); + dec = (tp >> (32 - i)); + pa2 = NAdditionNonSigne(pa2, dec); + add = tp << i; + z = Addition(pa1, add); + pa1 = z.deb; + if (z.fin == 1) { + w = Addition(pa2, 1); + pa2 = w.deb; } - z.deb = pa1; - z.fin = pa2; + } + z.deb = pa1; + z.fin = pa2; - printf("\n"); - return (z); + printf("\n"); + return (z); } couple MultipliSig(Uint32 x, Uint32 y) { - couple z, w; - int i; + couple z, w; + int i; - Uint32 tp, dec, add, a, b, pa1 = 0, pa2 = 0, x1 = x, y1 = y; + Uint32 tp, dec, add, a, b, pa1 = 0, pa2 = 0, x1 = x, y1 = y; - a = ValeurIbitsAuDeb(x, 31); - b = ValeurIbitsAuDeb(y, 31); - if (((a == 0) && (b == 0)) || ((a == 1) && (b == 1))) { /* Le resultat sera pos */ - if ((a == 1) && (b == 1)) { - x1 = InverseIbit(x, 31); - y1 = InverseIbit(y, 31); - } - for (i = 0; i < 31; i++) { - a = ValeurIbitsAuDeb(y1, i); - tp = MultChifNomb(x1, a); - dec = tp >> (31 - i); - w = Addition(pa2, dec); - pa2 = w.deb; - add = tp << i; - z = Add30Mul(pa1, add); - pa1 = z.deb; - if (z.fin == 1) { - w = Addition(pa2, 1); - pa2 = w.deb; - } - } - z.deb = pa1; - z.fin = pa2; - return (z); + a = ValeurIbitsAuDeb(x, 31); + b = ValeurIbitsAuDeb(y, 31); + if (((a == 0) && (b == 0)) || ((a == 1) && (b == 1))) { /* Le resultat sera pos */ + if ((a == 1) && (b == 1)) { + x1 = InverseIbit(x, 31); + y1 = InverseIbit(y, 31); } - if (a == 1) - x1 = InverseIbit(x, 31); - if (b == 1) - y1 = InverseIbit(y, 31); for (i = 0; i < 31; i++) { - a = ValeurIbitsAuDeb(y1, i); - tp = MultChifNomb(x1, a); - dec = tp >> (31 - i); - pa2 = NAdditionNonSigne(pa2, dec); - add = tp << i; - z = Add30Mul(pa1, add); - pa1 = z.deb; - if (z.fin == 1) { - pa2 = NAdditionNonSigne(pa2, 1); - } + a = ValeurIbitsAuDeb(y1, i); + tp = MultChifNomb(x1, a); + dec = tp >> (31 - i); + w = Addition(pa2, dec); + pa2 = w.deb; + add = tp << i; + z = Add30Mul(pa1, add); + pa1 = z.deb; + if (z.fin == 1) { + w = Addition(pa2, 1); + pa2 = w.deb; + } } - pa1 = MettreAVIbit(pa1, 31); - pa2 = MettreAVIbit(pa2, 31); z.deb = pa1; z.fin = pa2; return (z); + } + if (a == 1) + x1 = InverseIbit(x, 31); + if (b == 1) + y1 = InverseIbit(y, 31); + for (i = 0; i < 31; i++) { + a = ValeurIbitsAuDeb(y1, i); + tp = MultChifNomb(x1, a); + dec = tp >> (31 - i); + pa2 = NAdditionNonSigne(pa2, dec); + add = tp << i; + z = Add30Mul(pa1, add); + pa1 = z.deb; + if (z.fin == 1) { + pa2 = NAdditionNonSigne(pa2, 1); + } + } + pa1 = MettreAVIbit(pa1, 31); + pa2 = MettreAVIbit(pa2, 31); + z.deb = pa1; + z.fin = pa2; + return (z); } Uint32 NDivisionNonSigne(Uint32 a, Uint32 b) { - Uint32 quot, rest; + Uint32 quot, rest; - if (b > a) { - quot = 0; - rest = a; + if (b > a) { + quot = 0; + rest = a; + } else { + if (b == a) { + quot = 1; + rest = 0; } else { - if (b == a) { - quot = 1; - rest = 0; - } else { - quot = 0; - rest = a; - while (rest >= b) { - rest = NSoustractionNonSigne(rest, b); - quot++; - } - } + quot = 0; + rest = a; + while (rest >= b) { + rest = NSoustractionNonSigne(rest, b); + quot++; + } } - SecondResult = rest; - return quot; + } + SecondResult = rest; + return quot; } Uint32 NDivisionSigne(long int a, long int b) { - long int quot, rest; + long int quot, rest; - if (b > a) { - quot = 0; - rest = a; + if (b > a) { + quot = 0; + rest = a; + } else { + if (b == a) { + quot = 1; + rest = 0; } else { - if (b == a) { - quot = 1; - rest = 0; - } else { - quot = 0; - rest = a; - while (rest >= b) { - rest = NSoustractionNonSigne(rest, b); - quot++; - } - } + quot = 0; + rest = a; + while (rest >= b) { + rest = NSoustractionNonSigne(rest, b); + quot++; + } } - SecondResult = rest; - return quot; + } + SecondResult = rest; + return quot; } Uint32 NMultiplicationNonSigne(Uint32 a, Uint32 b) { - couple z; + couple z; - z = MultipliNonSig(a, b); + z = MultipliNonSig(a, b); - SecondResult = z.fin; - return z.deb; + SecondResult = z.fin; + return z.deb; } Uint32 NMultiplicationSigne(Uint32 a, Uint32 b) { - couple z; + couple z; - z = MultipliNonSig(a, b); + z = MultipliNonSig(a, b); - SecondResult = z.fin; - return z.deb; + SecondResult = z.fin; + return z.deb; } Uint32 AdditionNonSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RAdditionNonSigne(a, b); - } else { - return NAdditionNonSigne(a, b); - } + if (Rapide) { + return RAdditionNonSigne(a, b); + } else { + return NAdditionNonSigne(a, b); + } } Uint32 AdditionSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RAdditionSigne(a, b); - } else { - return NAdditionSigne(a, b); - } + if (Rapide) { + return RAdditionSigne(a, b); + } else { + return NAdditionSigne(a, b); + } } Uint32 SoustractionNonSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RSoustractionNonSigne(a, b); - } else { - return NSoustractionNonSigne(a, b); - } + if (Rapide) { + return RSoustractionNonSigne(a, b); + } else { + return NSoustractionNonSigne(a, b); + } } Uint32 SoustractionSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RSoustractionSigne(a, b); - } else { - return NSoustractionSigne(a, b); - } + if (Rapide) { + return RSoustractionSigne(a, b); + } else { + return NSoustractionSigne(a, b); + } } Uint32 MultiplicationNonSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RMultiplicationNonSigne(a, b); - } else { - return NMultiplicationNonSigne(a, b); - } + if (Rapide) { + return RMultiplicationNonSigne(a, b); + } else { + return NMultiplicationNonSigne(a, b); + } } Uint32 MultiplicationSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RMultiplicationSigne(a, b); - } else { - return NMultiplicationSigne(a, b); - } + if (Rapide) { + return RMultiplicationSigne(a, b); + } else { + return NMultiplicationSigne(a, b); + } } Uint32 DivisionNonSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RDivisionNonSigne(a, b); - } else { - return NDivisionNonSigne(a, b); - } + if (Rapide) { + return RDivisionNonSigne(a, b); + } else { + return NDivisionNonSigne(a, b); + } } Uint32 DivisionSigne(Uint32 a, Uint32 b) { - if (Rapide) { - return RDivisionSigne(a, b); - } else { - return NDivisionSigne(a, b); - } + if (Rapide) { + return RDivisionSigne(a, b); + } else { + return NDivisionSigne(a, b); + } } Uint32 AND(Uint32 a, Uint32 b) { - if (Rapide) { - return RAND(a, b); - } else { - return NAND(a, b); - } + if (Rapide) { + return RAND(a, b); + } else { + return NAND(a, b); + } } Uint32 OR(Uint32 a, Uint32 b) { - if (Rapide) { - return ROR(a, b); - } else { - return NOR(a, b); - } + if (Rapide) { + return ROR(a, b); + } else { + return NOR(a, b); + } } Uint32 SHL(Uint32 a) { - if (Rapide) { - return RSHL(a); - } else { - return NSHL(a); - } + if (Rapide) { + return RSHL(a); + } else { + return NSHL(a); + } } Uint32 SHR(Uint32 a) { - if (Rapide) { - return RSHR(a); - } else { - return NSHR(a); - } + if (Rapide) { + return RSHR(a); + } else { + return NSHR(a); + } } diff --git a/lib/assembler.c b/lib/assembler.c index e3a49bf..0beb2df 100644 --- a/lib/assembler.c +++ b/lib/assembler.c @@ -21,42 +21,42 @@ static int process_file(char *); /* Les differentes structures de données nécessaires au traitement */ typedef struct expression_t { - int e_type; - int e_subtype; - Uint32 avalue; - char *symbol; - pattern_t *pattern; - int index; - int op; - struct expression_t *next, *prev, *child, *father; + int e_type; + int e_subtype; + Uint32 avalue; + char *symbol; + pattern_t *pattern; + int index; + int op; + struct expression_t *next, *prev, *child, *father; } expression_t; typedef struct bytestream_t { - unsigned long int Encoded; - int offset, segment, size; - char *Label; - expression_t *Expr; + unsigned long int Encoded; + int offset, segment, size; + char *Label; + expression_t *Expr; - struct bytestream_t *next; + struct bytestream_t *next; - int line, relog; - char *filename; + int line, relog; + char *filename; } bytestream_t; enum { - SEG_TEXT, - SEG_DATA, - SEG_BSS + SEG_TEXT, + SEG_DATA, + SEG_BSS }; enum { - E_STRING, - E_VALUE, - E_OPERATION, - E_LABEL, - E_INTERNAL, - E_PATTERN, - E_INSTRUCT + E_STRING, + E_VALUE, + E_OPERATION, + E_LABEL, + E_INTERNAL, + E_PATTERN, + E_INSTRUCT }; /* Nos variables globales */ @@ -76,52 +76,52 @@ int s_text = 0, s_data = 0, s_bss = 0; static void debug_print_expression(expression_t * e) { - if (!e) - return; - switch (e->e_subtype) { - case E_OPERATION: - switch (e->op) { - case OP_PLUS: - fprintf(stderr, "Operation plus\n"); - break; - case OP_MOINS: - fprintf(stderr, "Operation moins\n"); - break; - case OP_DECAL: - fprintf(stderr, "Operation decal\n"); - break; - case OP_DIRECT: - fprintf(stderr, "Operation direct\n"); - break; - default: - fprintf(stderr, "Operation diverse...\n"); - break; - } - debug_print_expression(e->child); - fprintf(stderr, "--\n"); - break; - case E_VALUE: - fprintf(stderr, "Valeur: %li\n", e->avalue); - break; - case E_LABEL: - fprintf(stderr, "Label: %s\n", e->symbol); - break; - case E_STRING: - fprintf(stderr, "String: %s\n", e->symbol); - break; - case E_PATTERN: - fprintf(stderr, "Pattern: %s, index %i\n", e->pattern->name, e->index); - break; - case E_INTERNAL: - fprintf(stderr, "Instruction interne: %s\n", e->symbol); - break; - case E_INSTRUCT: - fprintf(stderr, "Instruction: %s\n", e->symbol); - break; - } - if (e->next) { - debug_print_expression(e->next); + if (!e) + return; + switch (e->e_subtype) { + case E_OPERATION: + switch (e->op) { + case OP_PLUS: + fprintf(stderr, "Operation plus\n"); + break; + case OP_MOINS: + fprintf(stderr, "Operation moins\n"); + break; + case OP_DECAL: + fprintf(stderr, "Operation decal\n"); + break; + case OP_DIRECT: + fprintf(stderr, "Operation direct\n"); + break; + default: + fprintf(stderr, "Operation diverse...\n"); + break; } + debug_print_expression(e->child); + fprintf(stderr, "--\n"); + break; + case E_VALUE: + fprintf(stderr, "Valeur: %li\n", e->avalue); + break; + case E_LABEL: + fprintf(stderr, "Label: %s\n", e->symbol); + break; + case E_STRING: + fprintf(stderr, "String: %s\n", e->symbol); + break; + case E_PATTERN: + fprintf(stderr, "Pattern: %s, index %i\n", e->pattern->name, e->index); + break; + case E_INTERNAL: + fprintf(stderr, "Instruction interne: %s\n", e->symbol); + break; + case E_INSTRUCT: + fprintf(stderr, "Instruction: %s\n", e->symbol); + break; + } + if (e->next) { + debug_print_expression(e->next); + } } static expression_t *copy_expression(expression_t * e); @@ -130,574 +130,588 @@ static expression_t *copy_expression(expression_t * e); static bytestream_t *pushuninit(int size) { - bytestream_t *s; - - s = (bytestream_t *) Emalloc(sizeof(bytestream_t)); - s->next = NULL; - s->Encoded = 0; - s->segment = segment; - s->size = 1; - s->Label = NULL; - s->Expr = NULL; - - s->relog = 1; - - s->line = line; - s->filename = filenames[nestedinc - 1]; - - - switch (segment) { - case SEG_TEXT: - s->offset = s_text; - p_text->next = s; - p_text = s; - s_text += size; - break; - case SEG_DATA: - s->offset = s_data; - p_data->next = s; - p_data = s; - s_data += size; - break; - case SEG_BSS: - s->offset = s_bss; - p_bss->next = s; - p_bss = s; - s_bss += size; - break; - - } - - return s; + bytestream_t *s; + + s = (bytestream_t *) Emalloc(sizeof(bytestream_t)); + s->next = NULL; + s->Encoded = 0; + s->segment = segment; + s->size = 1; + s->Label = NULL; + s->Expr = NULL; + + s->relog = 1; + + s->line = line; + s->filename = filenames[nestedinc - 1]; + + + switch (segment) { + case SEG_TEXT: + s->offset = s_text; + p_text->next = s; + p_text = s; + s_text += size; + break; + case SEG_DATA: + s->offset = s_data; + p_data->next = s; + p_data = s; + s_data += size; + break; + case SEG_BSS: + s->offset = s_bss; + p_bss->next = s; + p_bss = s; + s_bss += size; + break; + + } + + return s; } static bytestream_t *pushdword(unsigned long int d, expression_t * e) { - bytestream_t *s; + bytestream_t *s; - if ((segment < 0) || (segment > 1)) { - exception(1, _("You have to be into the .text or the .data segment to define a value.")); - } + if ((segment < 0) || (segment > 1)) { + exception(1, + _ + ("You have to be into the .text or the .data segment to define a value.")); + } - s = pushuninit(1); - s->Encoded = d; - if (e) { - s->Expr = copy_expression(e); - } + s = pushuninit(1); + s->Encoded = d; + if (e) { + s->Expr = copy_expression(e); + } - return s; + return s; } static bytestream_t *pushlabel(char *label) { - bytestream_t *s; - char trouve; + bytestream_t *s; + char trouve; - s = pushuninit(0); - s->Label = Estrdup(label); - s->size = 0; + s = pushuninit(0); + s->Label = Estrdup(label); + s->size = 0; - NomVarToVar(label, labels, &trouve); + NomVarToVar(label, labels, &trouve); - if (trouve) { - exception(1, _("Label already defined")); - } + if (trouve) { + exception(1, _("Label already defined")); + } - InsererVarDansTab(&labels, CreerElement(label, s)); + InsererVarDansTab(&labels, CreerElement(label, s)); - return s; + return s; } static void pushstring(char *s) { - char marker = *s, tstring[6]; - int valid; + char marker = *s, tstring[6]; + int valid; - s++; + s++; - while ((*s) && (marker != *s)) { - if (*s == '\\') { - s++; - switch (*s) { - case '0': - pushdword('\0', NULL); - break; - case 'a': - pushdword('\a', NULL); - break; - case 'b': - pushdword('\b', NULL); - break; - case 'f': - pushdword('\f', NULL); - break; - case 'n': - pushdword('\n', NULL); - break; - case 'r': - pushdword('\r', NULL); - break; - case 't': - pushdword('\t', NULL); - break; - case 'v': - pushdword('\v', NULL); - break; - case '\'': - case '\"': - case '\\': - pushdword(*s, NULL); - break; - case 'x': - tstring[0] = '0'; - strncpy(tstring + 1, s, 4); - pushdword(char_to_number(tstring, &valid), NULL); - break; - } - } else { - pushdword(*s, NULL); - } - s++; + while ((*s) && (marker != *s)) { + if (*s == '\\') { + s++; + switch (*s) { + case '0': + pushdword('\0', NULL); + break; + case 'a': + pushdword('\a', NULL); + break; + case 'b': + pushdword('\b', NULL); + break; + case 'f': + pushdword('\f', NULL); + break; + case 'n': + pushdword('\n', NULL); + break; + case 'r': + pushdword('\r', NULL); + break; + case 't': + pushdword('\t', NULL); + break; + case 'v': + pushdword('\v', NULL); + break; + case '\'': + case '\"': + case '\\': + pushdword(*s, NULL); + break; + case 'x': + tstring[0] = '0'; + strncpy(tstring + 1, s, 4); + pushdword(char_to_number(tstring, &valid), NULL); + break; + } + } else { + pushdword(*s, NULL); } - pushdword(0, NULL); + s++; + } + pushdword(0, NULL); } static void pushstart(void) { - if (segment != SEG_TEXT) { - exception(1, _("You can't have the startpoint elsewhere than the .text segment")); - } - pushlabel("__start__"); + if (segment != SEG_TEXT) { + exception(1, + _ + ("You can't have the startpoint elsewhere than the .text segment")); + } + pushlabel("__start__"); } /* Nous recherchons si une string est une pattern "simple" (pas composée) */ static void look4pattern(pattern_t * patterns, expression_t * expression) { - int i; - - for (patterns = patterns->next; patterns; patterns = patterns->next) { - for (i = 0; i < patterns->nbr; i++) { - if (!(patterns->expr[i]->type || patterns->expr[i]->string || !patterns->expr[i]->name)) { - if (!strcasecmp(patterns->expr[i]->name, expression->symbol)) { - expression->e_subtype = E_PATTERN; - expression->pattern = patterns; - expression->index = i; - return; - } - } + int i; + + for (patterns = patterns->next; patterns; patterns = patterns->next) { + for (i = 0; i < patterns->nbr; i++) { + if (! + (patterns->expr[i]->type + || patterns->expr[i]->string || !patterns->expr[i]->name)) { + if (!strcasecmp(patterns->expr[i]->name, expression->symbol)) { + expression->e_subtype = E_PATTERN; + expression->pattern = patterns; + expression->index = i; + return; } + } } + } } /* Nous vérifions rapidement si la string en cours fait partie des instructions recensées */ static void look4instr(phon_t * phons, instruct_t * instructs, expression_t * e) { - char *stringtolook = e->symbol; + char *stringtolook = e->symbol; - for (phons = phons->next; phons; phons = phons->next) { - if (!strcasecmp(phons->p1, stringtolook)) { - stringtolook = phons->p2; - break; - } + for (phons = phons->next; phons; phons = phons->next) { + if (!strcasecmp(phons->p1, stringtolook)) { + stringtolook = phons->p2; + break; } + } - for (instructs = instructs->next; instructs; instructs = instructs->next) { - if (!strcasecmp(instructs->names[0], stringtolook)) { - e->e_subtype = E_INSTRUCT; - return; - } + for (instructs = instructs->next; instructs; instructs = instructs->next) { + if (!strcasecmp(instructs->names[0], stringtolook)) { + e->e_subtype = E_INSTRUCT; + return; } + } } /* Libère récursivement une expression */ static void free_expr(expression_t * e) { - if (e->child) { - free_expr(e->child); - } + if (e->child) { + free_expr(e->child); + } - if (e->next) { - free_expr(e->next); - } + if (e->next) { + free_expr(e->next); + } - if (e->symbol) { - free(e->symbol); - } + if (e->symbol) { + free(e->symbol); + } - free(e); + free(e); } /* Copie récursivement une expression */ static expression_t *copy_expression(expression_t * e) { - expression_t *t, *e_t; - char trouve; + expression_t *t, *e_t; + char trouve; - t = Emalloc(sizeof(expression_t)); - *t = *e; + t = Emalloc(sizeof(expression_t)); + *t = *e; - if (t->symbol) { - trouve = 0; - if (e->e_subtype == E_LABEL) { - e_t = (expression_t *) NomVarToVar(e->symbol, defines, &trouve); - } - if ((trouve) && (t->e_type = E_STRING)) { - free(t); - t = copy_expression(e_t); - return t; - } else { - t->symbol = Estrdup(t->symbol); - } + if (t->symbol) { + trouve = 0; + if (e->e_subtype == E_LABEL) { + e_t = (expression_t *) NomVarToVar(e->symbol, defines, &trouve); } - - if (t->child) { - t->child = copy_expression(t->child); + if ((trouve) && (t->e_type = E_STRING)) { + free(t); + t = copy_expression(e_t); + return t; + } else { + t->symbol = Estrdup(t->symbol); } + } - if (t->next) { - t->next = copy_expression(t->next); - } + if (t->child) { + t->child = copy_expression(t->child); + } - if (t->next) { - t->next->prev = t; - } + if (t->next) { + t->next = copy_expression(t->next); + } + + if (t->next) { + t->next->prev = t; + } - return t; + return t; } /* Cette fonction est appelée par le parseur. Elle rajoute un symbole sur la pile des expressions pour la ligne en cours */ void push_pile(char *a) { - int valid, number; - expression_t *e, *e_t; - char trouve; - static char err[BUFSIZ]; - - if (!wc) { - special = 0; - switch (downcase(*a)) { - case '.': - valid = 0; - special = 1; - switch (downcase(*(a + 1))) { - case 'd': - if (!(strcasecmp(a + 2, "ata"))) { - segment = SEG_DATA; - valid = 1; - wc++; - return; - } - break; - case 't': - if (!(strcasecmp(a + 2, "ext"))) { - segment = SEG_TEXT; - valid = 1; - wc++; - return; - } - break; - case 'b': - if (!(strcasecmp(a + 2, "ss"))) { - segment = SEG_BSS; - valid = 1; - wc++; - return; - } - break; - case 's': - if (!(strcasecmp(a + 2, "tart"))) { - pushstart(); - valid = 1; - wc++; - return; - } - break; - } - if (!valid) { - exception(1, _("Not a valid . directive")); - } - break; - case '#': - valid = 0; - switch (downcase(*(a + 1))) { - case 'd': - if (!(strcasecmp(a + 2, "efine"))) { - special = 2; - valid = 1; - } - wc++; - return; - case 'u': - if (!(strcasecmp(a + 2, "ndef"))) { - special = 3; - valid = 1; - } - wc++; - return; - case 'i': - if (!(strcasecmp(a + 2, "nclude"))) { - special = 4; - valid = 1; - } - wc++; - return; - } - if (!valid) { - exception(1, _("Not a valid # directive")); - } - break; - } - } + int valid, number; + expression_t *e, *e_t; + char trouve; + static char err[BUFSIZ]; - switch (special) { - case 1: /* Cas des directives . */ - exception(1, _("Error: extra parameters to a . directive.")); + if (!wc) { + special = 0; + switch (downcase(*a)) { + case '.': + valid = 0; + special = 1; + switch (downcase(*(a + 1))) { + case 'd': + if (!(strcasecmp(a + 2, "ata"))) { + segment = SEG_DATA; + valid = 1; + wc++; + return; + } break; - case 2: /* Cas de #define */ - if (wc == 1) { - defined = Estrdup(a); - break; + case 't': + if (!(strcasecmp(a + 2, "ext"))) { + segment = SEG_TEXT; + valid = 1; + wc++; + return; } - case 0: /* Cas normal */ - e = (expression_t *) Emalloc(sizeof(expression_t)); - if (!e_line) { - e_line = e_current = e; + break; + case 'b': + if (!(strcasecmp(a + 2, "ss"))) { + segment = SEG_BSS; + valid = 1; + wc++; + return; } - e->pattern = NULL; - number = char_to_number(a, &valid); - if (valid) { - e->e_type = E_VALUE; - e->e_subtype = E_VALUE; - e->avalue = number; - e->symbol = NULL; - } else { - trouve = 0; - if ((*a != '"') && (*a != '\'')) { - e_t = (expression_t *) NomVarToVar(a, defines, &trouve); - } - if (trouve) { - e = copy_expression(e_t); - } else { - e->e_type = E_STRING; - e->e_subtype = E_STRING; - e->avalue = 0; - e->symbol = Estrdup(a); - } + break; + case 's': + if (!(strcasecmp(a + 2, "tart"))) { + pushstart(); + valid = 1; + wc++; + return; } - e->op = -1; - if (wc) { - e->prev = e_current; - } else { - e->prev = NULL; + break; + } + if (!valid) { + exception(1, _("Not a valid . directive")); + } + break; + case '#': + valid = 0; + switch (downcase(*(a + 1))) { + case 'd': + if (!(strcasecmp(a + 2, "efine"))) { + special = 2; + valid = 1; + } + wc++; + return; + case 'u': + if (!(strcasecmp(a + 2, "ndef"))) { + special = 3; + valid = 1; } - e->next = NULL; - if (wc) { - e_current->next = e; + wc++; + return; + case 'i': + if (!(strcasecmp(a + 2, "nclude"))) { + special = 4; + valid = 1; } - e->father = NULL; - e->child = NULL; - - /* On prédevine le subtype sur quelques cas */ + wc++; + return; + } + if (!valid) { + exception(1, _("Not a valid # directive")); + } + break; + } + } + + switch (special) { + case 1: /* Cas des directives . */ + exception(1, _("Error: extra parameters to a . directive.")); + break; + case 2: /* Cas de #define */ + if (wc == 1) { + defined = Estrdup(a); + break; + } + case 0: /* Cas normal */ + e = (expression_t *) Emalloc(sizeof(expression_t)); + if (!e_line) { + e_line = e_current = e; + } + e->pattern = NULL; + number = char_to_number(a, &valid); + if (valid) { + e->e_type = E_VALUE; + e->e_subtype = E_VALUE; + e->avalue = number; + e->symbol = NULL; + } else { + trouve = 0; + if ((*a != '"') && (*a != '\'')) { + e_t = (expression_t *) NomVarToVar(a, defines, &trouve); + } + if (trouve) { + e = copy_expression(e_t); + } else { + e->e_type = E_STRING; + e->e_subtype = E_STRING; + e->avalue = 0; + e->symbol = Estrdup(a); + } + } + e->op = -1; + if (wc) { + e->prev = e_current; + } else { + e->prev = NULL; + } + e->next = NULL; + if (wc) { + e_current->next = e; + } + e->father = NULL; + e->child = NULL; - /* Cas des labels (en nom:) */ + /* On prédevine le subtype sur quelques cas */ - if ((wc == 0) && (a[strlen(a) - 1] == ':')) { - e->e_subtype = E_LABEL; - } + /* Cas des labels (en nom:) */ - /* Cas des pseudos instructions {DB, DW, DD, DS, DR} */ + if ((wc == 0) && (a[strlen(a) - 1] == ':')) { + e->e_subtype = E_LABEL; + } - if (wc <= 1) { + /* Cas des pseudos instructions {DB, DW, DD, DS, DR} */ + + if (wc <= 1) { + trouve = 0; + switch (downcase(*a)) { + case 'd': + if (!*(a + 2)) { + trouve = 1; + switch (downcase(*(a + 1))) { + case 'b': + e->op = 1; + break; + case 'w': + e->op = 2; + break; + case 'd': + e->op = 3; + break; + case 's': + e->op = 4; + break; + case 'r': + e->op = 5; + break; + default: trouve = 0; - switch (downcase(*a)) { - case 'd': - if (!*(a + 2)) { - trouve = 1; - switch (downcase(*(a + 1))) { - case 'b': - e->op = 1; - break; - case 'w': - e->op = 2; - break; - case 'd': - e->op = 3; - break; - case 's': - e->op = 4; - break; - case 'r': - e->op = 5; - break; - default: - trouve = 0; - } - } - } - if (trouve) { - e->e_subtype = E_INTERNAL; - if (e->prev) { - e->prev->e_subtype = E_LABEL; - } - } + } + } + } + if (trouve) { + e->e_subtype = E_INTERNAL; + if (e->prev) { + e->prev->e_subtype = E_LABEL; } + } + } - /* Cas des patterns */ + /* Cas des patterns */ - if (e->e_subtype == E_STRING) { - look4pattern(patterns, e); - } + if (e->e_subtype == E_STRING) { + look4pattern(patterns, e); + } - /* On regarde si cela n'est pas une instruction connue dans le meta langage */ + /* On regarde si cela n'est pas une instruction connue dans le meta langage */ - if (((wc == 0) && (e->e_subtype == E_STRING)) - || ((wc == 1) && (e->prev->e_subtype == E_LABEL))) { - look4instr(phons, instructs, e); - } + if (((wc == 0) && (e->e_subtype == E_STRING)) + || ((wc == 1) && (e->prev->e_subtype == E_LABEL))) { + look4instr(phons, instructs, e); + } - /* Dans tous les autres cas, nous considerons qu'il s'agit d'une référence à un label... */ + /* Dans tous les autres cas, nous considerons qu'il s'agit d'une référence à un label... */ - if (e->e_subtype == E_STRING) { - if (((e->symbol[0] != '\"') && (e->symbol[0] != '\'')) && (strcmp(e->symbol, "?"))) { - e->e_subtype = E_LABEL; - } - } + if (e->e_subtype == E_STRING) { + if (((e->symbol[0] != '\"') && (e->symbol[0] != '\'')) + && (strcmp(e->symbol, "?"))) { + e->e_subtype = E_LABEL; + } + } - e_current = e; - break; - case 3: /* Cas de #undef */ - if (wc != 1) { - exception(1, _("Too much arguments to #undef")); - } - NomVarToVar(a, defines, &trouve); - if (!trouve) { - exception(1, _("Defined symbol not found.")); - } - SupprimerDansTab(&defines, a); - break; - case 4: /* Cas de #include */ - if (wc != 1) { - exception(1, _("Too much arguments to #include")); - } - sprintf(err, _("Including file at line %i"), line); - pushcontext(err); - if (process_file(a)) { - exception(1, _("Error reading include file")); - } - popcontext(); - break; + e_current = e; + break; + case 3: /* Cas de #undef */ + if (wc != 1) { + exception(1, _("Too much arguments to #undef")); + } + NomVarToVar(a, defines, &trouve); + if (!trouve) { + exception(1, _("Defined symbol not found.")); } + SupprimerDansTab(&defines, a); + break; + case 4: /* Cas de #include */ + if (wc != 1) { + exception(1, _("Too much arguments to #include")); + } + sprintf(err, _("Including file at line %i"), line); + pushcontext(err); + if (process_file(a)) { + exception(1, _("Error reading include file")); + } + popcontext(); + break; + } - wc++; + wc++; } /* Cette fonction évalue l'expression en cours. */ static void evaluate(expression_t * e) { - expression_t *t, *u; - - if (e->e_subtype != E_OPERATION) - return; - - switch (e->op) { - case OP_PLUS: - case OP_MOINS: - case OP_MUL: - case OP_DIV: - case OP_MOD: - if ((e->child->e_subtype == E_VALUE) && (e->child->next->e_subtype == E_VALUE)) { - switch (e->op) { - case OP_PLUS: - e->avalue = e->child->avalue + e->child->next->avalue; - break; - case OP_MOINS: - e->avalue = e->child->avalue - e->child->next->avalue; - break; - case OP_MUL: - e->avalue = e->child->avalue * e->child->next->avalue; - break; - case OP_DIV: - if (!e->child->next->avalue) { - exception(1, _("Zero divide.")); - } - e->avalue = e->child->avalue / e->child->next->avalue; - break; - case OP_MOD: - if (!e->child->next->avalue) { - exception(1, _("Zero divide.")); - } - e->avalue = e->child->avalue % e->child->next->avalue; - break; - } - free(e->child->next); - free(e->child); - e->child = NULL; - e->e_type = e->e_subtype = E_VALUE; - } else { - /* un seul cas particulier supporté... */ - if (!((e->op == OP_PLUS) - || ((e->op == OP_MOINS) && (e->child->next->e_subtype = E_VALUE)))) { - exception(1, _("Error: unable to compute the immediate value")); - } - } + expression_t *t, *u; + + if (e->e_subtype != E_OPERATION) + return; + + switch (e->op) { + case OP_PLUS: + case OP_MOINS: + case OP_MUL: + case OP_DIV: + case OP_MOD: + if ((e->child->e_subtype == E_VALUE) + && (e->child->next->e_subtype == E_VALUE)) { + switch (e->op) { + case OP_PLUS: + e->avalue = e->child->avalue + e->child->next->avalue; break; - case OP_PLUS_UNARY: - case OP_MOINS_UNARY: - if (!e->child->e_subtype == E_VALUE) { - exception(1, _("Error: unable to compute the immediate value")); - } - e->avalue = (e->op == OP_MOINS_UNARY) ? -e->child->avalue : e->child->avalue; - free(e->child); - e->child = NULL; - e->e_type = e->e_subtype = E_VALUE; + case OP_MOINS: + e->avalue = e->child->avalue - e->child->next->avalue; + break; + case OP_MUL: + e->avalue = e->child->avalue * e->child->next->avalue; break; - case OP_FUNC_CALL: - if (strcasecmp(e->symbol, "diff")) { - exception(1, _("Function unknow")); + case OP_DIV: + if (!e->child->next->avalue) { + exception(1, _("Zero divide.")); } + e->avalue = e->child->avalue / e->child->next->avalue; break; - /* Jusqu'ici les évaluations étaient faciles. Nous allons attaquer la partie la plus ardue - de l'évaluation, celle de l'évaluation (disons plutôt de la simplification) des adresses */ - case OP_DECAL: - if ((e->child->e_subtype == E_LABEL) && (e->child->next->e_subtype == E_LABEL)) { - exception(1, _("Addresses addition not allowed")); + case OP_MOD: + if (!e->child->next->avalue) { + exception(1, _("Zero divide.")); } + e->avalue = e->child->avalue % e->child->next->avalue; + break; + } + free(e->child->next); + free(e->child); + e->child = NULL; + e->e_type = e->e_subtype = E_VALUE; + } else { + /* un seul cas particulier supporté... */ + if (!((e->op == OP_PLUS) + || ((e->op == OP_MOINS) + && (e->child->next->e_subtype = E_VALUE)))) { + exception(1, _("Error: unable to compute the immediate value")); + } + } + break; + case OP_PLUS_UNARY: + case OP_MOINS_UNARY: + if (!e->child->e_subtype == E_VALUE) { + exception(1, _("Error: unable to compute the immediate value")); + } + e->avalue = + (e->op == OP_MOINS_UNARY) ? -e->child->avalue : e->child->avalue; + free(e->child); + e->child = NULL; + e->e_type = e->e_subtype = E_VALUE; + break; + case OP_FUNC_CALL: + if (strcasecmp(e->symbol, "diff")) { + exception(1, _("Function unknow")); + } + break; + /* Jusqu'ici les évaluations étaient faciles. Nous allons attaquer la partie la plus ardue + de l'évaluation, celle de l'évaluation (disons plutôt de la simplification) des adresses */ + case OP_DECAL: + if ((e->child->e_subtype == E_LABEL) + && (e->child->next->e_subtype == E_LABEL)) { + exception(1, _("Addresses addition not allowed")); + } - if (e->child->e_subtype != E_LABEL) { - exception(1, _("You can only use the decal operator on labels")); - } + if (e->child->e_subtype != E_LABEL) { + exception(1, _("You can only use the decal operator on labels")); + } - if (e->child->next->e_subtype == E_OPERATION) { - if ((e->child->next->op != OP_MOINS) && (e->child->next->op != OP_PLUS)) { - exception(1, _("Address operation invalid")); - } - if ((e->child->next->op == OP_MOINS) && (e->child->next->child->next->e_subtype != E_VALUE)) { - exception(1, _("Address operation invalid")); - } - if (e->child->next->child->e_subtype == E_LABEL) { - exception(1, _("Addresses operations not allowed")); - } - if (e->child->next->child->e_subtype == e->child->next->child->next->e_subtype) { - exception(1, _("Expression too complex or invalid")); - } - /* On veut obtenir quelque chose sous la forme LABEL[VALUE + PATTERN] */ - if (e->child->next->child->e_subtype == E_PATTERN) { - t = e->child->next->child; - e->child->next->child = t->next; - t->next->prev = NULL; - t->next->next = t; - t->prev = t->next; - t->next = NULL; - } - } - break; - case OP_DIRECT: + if (e->child->next->e_subtype == E_OPERATION) { + if ((e->child->next->op != OP_MOINS) + && (e->child->next->op != OP_PLUS)) { + exception(1, _("Address operation invalid")); + } + if ((e->child->next->op == OP_MOINS) + && (e->child->next->child->next->e_subtype != E_VALUE)) { + exception(1, _("Address operation invalid")); + } + if (e->child->next->child->e_subtype == E_LABEL) { + exception(1, _("Addresses operations not allowed")); + } + if (e->child->next->child->e_subtype == + e->child->next->child->next->e_subtype) { + exception(1, _("Expression too complex or invalid")); + } + /* On veut obtenir quelque chose sous la forme LABEL[VALUE + PATTERN] */ + if (e->child->next->child->e_subtype == E_PATTERN) { + t = e->child->next->child; + e->child->next->child = t->next; + t->next->prev = NULL; + t->next->next = t; + t->prev = t->next; + t->next = NULL; + } + } + break; + case OP_DIRECT: /*******************************************************************\ | Le code qui suit est sans doute immonde mais il est nécessaire... | | Attention à la foret d'ifs, ça fait mal quand on s'y pert... | @@ -706,37 +720,38 @@ static void evaluate(expression_t * e) | Nous la modifierons au fur et a mesure de sorte à lui donner une | | forme canonique afin de pouvoir la traiter correctement plus tard | \*******************************************************************/ - switch (e->child->e_subtype) { - case E_OPERATION: - if (!((e->child->op == OP_PLUS) || (e->child->op == OP_MOINS))) { - exception(1, _("Address operation invalid")); - } - if ((e->child->child->e_subtype == E_LABEL) - && (e->child->child->next->e_subtype == E_LABEL)) { - exception(1, _("Addresses operations not allowed")); - } - if (e->child->child->next->e_subtype == E_LABEL) { - if (e->child->op == OP_MOINS) { - exception(1, _("Address type not supported")); - } - /* Sous la forme [A + LABEL], on inverse... */ - t = e->child->child; - e->child->child = t->next; - t->next->prev = NULL; - t->next->next = t; - t->prev = t->next; - t->next = NULL; - } - if (e->child->child->e_subtype == E_LABEL) { - if ((e->child->op != OP_PLUS) - && (e->child->child->next->e_subtype != E_VALUE)) { - exception(1, _("Address type not supported")); - } - } else { - if ((e->child->child->e_subtype == E_OPERATION) - && ((e->child->child->op == OP_PLUS) - || (e->child->child->op == OP_MOINS))) { - /* On est sous la forme [(A +- B) +- C], on inverse... */ + switch (e->child->e_subtype) { + case E_OPERATION: + if (!((e->child->op == OP_PLUS) + || (e->child->op == OP_MOINS))) { + exception(1, _("Address operation invalid")); + } + if ((e->child->child->e_subtype == E_LABEL) + && (e->child->child->next->e_subtype == E_LABEL)) { + exception(1, _("Addresses operations not allowed")); + } + if (e->child->child->next->e_subtype == E_LABEL) { + if (e->child->op == OP_MOINS) { + exception(1, _("Address type not supported")); + } + /* Sous la forme [A + LABEL], on inverse... */ + t = e->child->child; + e->child->child = t->next; + t->next->prev = NULL; + t->next->next = t; + t->prev = t->next; + t->next = NULL; + } + if (e->child->child->e_subtype == E_LABEL) { + if ((e->child->op != OP_PLUS) + && (e->child->child->next->e_subtype != E_VALUE)) { + exception(1, _("Address type not supported")); + } + } else { + if ((e->child->child->e_subtype == E_OPERATION) + && ((e->child->child->op == OP_PLUS) + || (e->child->child->op == OP_MOINS))) { + /* On est sous la forme [(A +- B) +- C], on inverse... */ /***********************************************\ | | @@ -762,427 +777,455 @@ static void evaluate(expression_t * e) | | \***********************************************/ - t = e->child->child->child->next; /* t pointe sur B */ + t = e->child->child->child->next; /* t pointe sur B */ - e->child->child->child->next = e->child; /* Op1 devient fils droit de Op2 */ - e->child->father = e->child->child; - e->child->prev = e->child->child->child; + e->child->child->child->next = e->child; /* Op1 devient fils droit de Op2 */ + e->child->father = e->child->child; + e->child->prev = e->child->child->child; - e->child = e->child->child; /* Fils de Direct devient Op2 */ - e->child->father = e; + e->child = e->child->child; /* Fils de Direct devient Op2 */ + e->child->father = e; - e->child->child->next->child = t; /* B devient fils gauche de Op1 */ - t->next = e->child->next; - t->next->prev = t; - t->father = t->next->father; - t->prev = NULL; + e->child->child->next->child = t; /* B devient fils gauche de Op1 */ + t->next = e->child->next; + t->next->prev = t; + t->father = t->next->father; + t->prev = NULL; - } - if ((t = e->child->child->next)->e_subtype == E_OPERATION) { - /* On est sous la forme [A +- (B +- C)], on vérifie l'intégrité de la sous opération. - Cela ne peut pas être [A - (B +- C)] car il faut que (B +- C) soit une valeur - immediate et donc aurait été calculé avant. */ - if (e->child->op == OP_MOINS) { - exception(1, _("Address type not supported")); - } - switch (e->child->child->e_subtype) { - /* quoique similiares, les trois cas suivant nécessitent trois traitements - différents */ - case E_LABEL: - /* On a donc [LABEL + (B +- C)], on va vérifier si C n'est pas une PATTERN, - et si c'est le cas, il ne faut pas que ce soit - C. Sinon nous ne devons - pas avoir de labels pour B ou C */ - - if ((e->child->child->next->child->next->e_subtype == E_PATTERN) && (e->child->child->next->op == OP_MOINS)) { - exception(1, _("Address type not supported")); - } - - if (e->child->child->next->child->e_subtype == E_LABEL) { - exception(1, _("Address addition not supported")); - } - - /* Si B et C sont du même type, on jette l'éponge... */ - - if (e->child->child->next->child->e_subtype == e->child->child->next->child->next->e_subtype) { - exception(1, _("Expression too complex or invalid")); - } - - /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle - est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce - qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ - - if ((t = e->child->child->next->child)->e_subtype == E_PATTERN) { - t->prev = t->next; - t->next = NULL; - t->prev->next = t; - t->prev->prev = NULL; - t->father->child = t->prev; - if (t->father->op == OP_MOINS) { - t->father->op = OP_PLUS; - t->prev->avalue = -t->prev->avalue; - } - } - break; - case E_PATTERN: - /* On a donc [PATTERN + (B +- C)], on va vérifier si C n'est pas un LABEL, - et si c'est le cas, il ne faut pas que ce soit - C. Sinon nous ne devons - pas avoir de patterns pour B ou C. */ - - if ((e->child->child->next->child->next->e_subtype == E_LABEL) && (e->child->child->next->op == OP_MOINS)) { - exception(1, _("Address type not supported")); - } - - if (e->child->child->next->child->e_subtype == E_PATTERN) { - exception(1, _("Expression invalid")); - } - - /* Si B et C sont du même type, on jette l'éponge... */ - - if (e->child->child->next->child->e_subtype == e->child->child->next->child->next->e_subtype) { - exception(1, _("Expression too complex or invalid")); - } - - /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle - est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce - qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ - - /* Pas mal de boulot ici... */ - if ((t = e->child->child->next->child)->e_subtype == E_LABEL) { - /* Nous avons [PATTERN + (LABEL +- VALEUR)], on inverse LABEL et VALEUR */ - t->prev = t->next; - t->next = NULL; - t->prev->next = t; - t->prev->prev = NULL; - t->father->child = t->prev; - if (t->father->op == OP_MOINS) { - t->father->op = OP_PLUS; - t->prev->avalue = -t->prev->avalue; - } - } - - /* Nous avons [PATTERN + (VALEUR + LABEL)] */ - - /* On bouge le LABEL */ - t = e->child->child->next->child->next; - t->father = e->child; - t->prev = NULL; - t->next = e->child->child->next; - - /* On bouge le PATTERN */ - t = e->child->child; - t->father = t->next; - t->prev = t->next->child; - t->next = NULL; - - /* On rétablit l'arbre correct */ - e->child->child = t->prev->next; - e->child->child->next->prev = e->child->child; - t->prev->next = t; - break; - case E_VALUE: - /* On a donc [VALUE + (B +- C)], si B ou C sont des valeurs, on intervient. */ - - if (e->child->child->next->child->e_subtype == E_VALUE) { - if (e->child->child->next->op == OP_MOINS) { - exception(1, _("Expression invalid")); - } - e->child->child->avalue += e->child->child->next->child->avalue; - t = e->child->child->next; - e->child->child->next = t->child->next; - free_expr(t); - /* Magie, on a attérit sur le cas [VALUE + qquechose] ... - On va pas s'embeter, on va réévaluer la chose :) */ - evaluate(e); - return; - } else if (e->child->child->next->child->next->e_subtype == E_VALUE) { - /* Quasiment la même chose qu'au dessus... */ - if (e->child->child->next->op == OP_MOINS) { - e->child->child->avalue -= e->child->child->next->child->avalue; - } else { - e->child->child->avalue += e->child->child->next->child->avalue; - } - t = e->child->child->next; - e->child->child->next = t->child; - free_expr(t); - evaluate(e); - return; - } else { - /* Si B et C sont du même type, on jette l'éponge... */ - - if (e->child->child->next->child->e_subtype == e->child->child->next->child->next->e_subtype) { - exception(1, _("Expression too complex or invalid")); - } - - /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle - est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce - qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ - - /* Pas mal de boulot ici... */ - if ((t = e->child->child->next->child)->e_subtype == E_PATTERN) { - /* Nous avons [VALEUR + (PATTERN + LABEL)], on inverse LABEL et PATTERN */ - t->prev = t->next; - t->next = NULL; - t->prev->next = t; - t->prev->prev = NULL; - t->father->child = t->prev; - if (t->father->op == OP_MOINS) { - t->father->op = OP_PLUS; - t->prev->avalue = -t->prev->avalue; - } - } - - /* Nous avons [VALEUR + (LABEL + PATTERN)] */ - - /* On bouge le LABEL */ - t = e->child->child->next->child; - t->father = e->child; - t->prev = NULL; - u = t->next; - t->next = e->child->child->next; - - /* On bouge la VALEUR */ - t = e->child->child; - t->father = t->next; - t->next = u; - t->prev = NULL; - - /* On rétablit l'arbre correct */ - e->child->child = u->prev; - e->child->child->next->prev = e->child->child; - e->child->child->next->child = t; - u->prev = t; - } - default: - /* Bon si l'on est ici, c'est pas bon signe non plus... */ - exception(1, _("Expression too complex")); - break; - } - } else { - /* Bon si l'on est ici, c'est que l'on est dans le cas [PATTERN + VALUE] - ou [VALUE + PATTERN]. On se met sur la forme [VALUE + PATTERN] */ - if (e->child->child->e_subtype == E_PATTERN) { - t = e->child->child; - e->child->child = t->next; - t->next->prev = NULL; - t->next->next = t; - t->prev = t->next; - t->next = NULL; - } - } + } + if ((t = e->child->child->next)->e_subtype == E_OPERATION) { + /* On est sous la forme [A +- (B +- C)], on vérifie l'intégrité de la sous opération. + Cela ne peut pas être [A - (B +- C)] car il faut que (B +- C) soit une valeur + immediate et donc aurait été calculé avant. */ + if (e->child->op == OP_MOINS) { + exception(1, _("Address type not supported")); + } + switch (e->child->child->e_subtype) { + /* quoique similiares, les trois cas suivant nécessitent trois traitements + différents */ + case E_LABEL: + /* On a donc [LABEL + (B +- C)], on va vérifier si C n'est pas une PATTERN, + et si c'est le cas, il ne faut pas que ce soit - C. Sinon nous ne devons + pas avoir de labels pour B ou C */ + + if ( + (e->child->child->next->child->next->e_subtype == + E_PATTERN) + && (e->child->child->next->op == OP_MOINS)) { + exception(1, _("Address type not supported")); } - /* Bon si on arrive ici sain et sauf, c'est que l'expression est sous la forme - canonique: [LABEL + A] avec A quelque chose de valide. On va donc transformer - l'expression en LABEL[A] */ - - t = e->child; - e->child = t->child; - e->child->father = e; - e->child->next->father = e; - if (t->op == OP_MOINS) { - /* On est sous la forme [LABEL - IMM], on met LABEL[-A] */ - e->child->next->avalue = -e->child->next->avalue; + + if (e->child->child->next->child->e_subtype == E_LABEL) { + exception(1, _("Address addition not supported")); + } + + /* Si B et C sont du même type, on jette l'éponge... */ + + if (e->child->child->next->child->e_subtype == + e->child->child->next->child->next->e_subtype) { + exception(1, + _("Expression too complex or invalid")); + } + + /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle + est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce + qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ + + if ( + (t = + e->child->child->next->child)->e_subtype == + E_PATTERN) { + t->prev = t->next; + t->next = NULL; + t->prev->next = t; + t->prev->prev = NULL; + t->father->child = t->prev; + if (t->father->op == OP_MOINS) { + t->father->op = OP_PLUS; + t->prev->avalue = -t->prev->avalue; + } } - free(t); - e->op = OP_DECAL; - break; /* case OPERATION */ - case E_LABEL: - /* On est sous la forme [LABEL], on va mettre sous la forme LABEL[0] */ - t = (expression_t *) Emalloc(sizeof(expression_t)); - t->e_type = t->e_subtype = E_VALUE; - t->avalue = 0; - t->symbol = NULL; - t->pattern = NULL; - t->next = t->child = NULL; - t->prev = e->child; - e->child->next = t; - t->father = e; - e->op = OP_DECAL; break; + case E_PATTERN: + /* On a donc [PATTERN + (B +- C)], on va vérifier si C n'est pas un LABEL, + et si c'est le cas, il ne faut pas que ce soit - C. Sinon nous ne devons + pas avoir de patterns pour B ou C. */ + + if ( + (e->child->child->next->child->next->e_subtype == + E_LABEL) + && (e->child->child->next->op == OP_MOINS)) { + exception(1, _("Address type not supported")); + } + + if (e->child->child->next->child->e_subtype == + E_PATTERN) { + exception(1, _("Expression invalid")); + } + + /* Si B et C sont du même type, on jette l'éponge... */ + + if (e->child->child->next->child->e_subtype == + e->child->child->next->child->next->e_subtype) { + exception(1, + _("Expression too complex or invalid")); + } + + /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle + est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce + qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ + + /* Pas mal de boulot ici... */ + if ( + (t = + e->child->child->next->child)->e_subtype == + E_LABEL) { + /* Nous avons [PATTERN + (LABEL +- VALEUR)], on inverse LABEL et VALEUR */ + t->prev = t->next; + t->next = NULL; + t->prev->next = t; + t->prev->prev = NULL; + t->father->child = t->prev; + if (t->father->op == OP_MOINS) { + t->father->op = OP_PLUS; + t->prev->avalue = -t->prev->avalue; + } + } + + /* Nous avons [PATTERN + (VALEUR + LABEL)] */ + + /* On bouge le LABEL */ + t = e->child->child->next->child->next; + t->father = e->child; + t->prev = NULL; + t->next = e->child->child->next; + + /* On bouge le PATTERN */ + t = e->child->child; + t->father = t->next; + t->prev = t->next->child; + t->next = NULL; + + /* On rétablit l'arbre correct */ + e->child->child = t->prev->next; + e->child->child->next->prev = e->child->child; + t->prev->next = t; + break; + case E_VALUE: + /* On a donc [VALUE + (B +- C)], si B ou C sont des valeurs, on intervient. */ + + if (e->child->child->next->child->e_subtype == E_VALUE) { + if (e->child->child->next->op == OP_MOINS) { + exception(1, _("Expression invalid")); + } + e->child->child->avalue += + e->child->child->next->child->avalue; + t = e->child->child->next; + e->child->child->next = t->child->next; + free_expr(t); + /* Magie, on a attérit sur le cas [VALUE + qquechose] ... + On va pas s'embeter, on va réévaluer la chose :) */ + evaluate(e); + return; + } else if (e->child->child->next->child-> + next->e_subtype == E_VALUE) { + /* Quasiment la même chose qu'au dessus... */ + if (e->child->child->next->op == OP_MOINS) { + e->child->child->avalue + -= e->child->child->next->child->avalue; + } else { + e->child->child->avalue + += e->child->child->next->child->avalue; + } + t = e->child->child->next; + e->child->child->next = t->child; + free_expr(t); + evaluate(e); + return; + } else { + /* Si B et C sont du même type, on jette l'éponge... */ + + if (e->child->child->next->child->e_subtype == + e->child->child->next->child->next->e_subtype) { + exception(1, + _ + ("Expression too complex or invalid")); + } + + /* Ok, si notre expression a réussi à franchir toutes ses étapes, c'est qu'elle + est correcte.... Enfin j'espère :) Je vais modifier l'expression de sorte a ce + qu'elle ait la forme [LABEL + (VALEUR +- PATTERN)] */ + + /* Pas mal de boulot ici... */ + if ( + (t = + e->child->child->next->child)->e_subtype == + E_PATTERN) { + /* Nous avons [VALEUR + (PATTERN + LABEL)], on inverse LABEL et PATTERN */ + t->prev = t->next; + t->next = NULL; + t->prev->next = t; + t->prev->prev = NULL; + t->father->child = t->prev; + if (t->father->op == OP_MOINS) { + t->father->op = OP_PLUS; + t->prev->avalue = -t->prev->avalue; + } + } + + /* Nous avons [VALEUR + (LABEL + PATTERN)] */ + + /* On bouge le LABEL */ + t = e->child->child->next->child; + t->father = e->child; + t->prev = NULL; + u = t->next; + t->next = e->child->child->next; + + /* On bouge la VALEUR */ + t = e->child->child; + t->father = t->next; + t->next = u; + t->prev = NULL; + + /* On rétablit l'arbre correct */ + e->child->child = u->prev; + e->child->child->next->prev = e->child->child; + e->child->child->next->child = t; + u->prev = t; + } + default: + /* Bon si l'on est ici, c'est pas bon signe non plus... */ + exception(1, _("Expression too complex")); + break; + } + } else { + /* Bon si l'on est ici, c'est que l'on est dans le cas [PATTERN + VALUE] + ou [VALUE + PATTERN]. On se met sur la forme [VALUE + PATTERN] */ + if (e->child->child->e_subtype == E_PATTERN) { + t = e->child->child; + e->child->child = t->next; + t->next->prev = NULL; + t->next->next = t; + t->prev = t->next; + t->next = NULL; + } } - break; + } + /* Bon si on arrive ici sain et sauf, c'est que l'expression est sous la forme + canonique: [LABEL + A] avec A quelque chose de valide. On va donc transformer + l'expression en LABEL[A] */ + + t = e->child; + e->child = t->child; + e->child->father = e; + e->child->next->father = e; + if (t->op == OP_MOINS) { + /* On est sous la forme [LABEL - IMM], on met LABEL[-A] */ + e->child->next->avalue = -e->child->next->avalue; + } + free(t); + e->op = OP_DECAL; + break; /* case OPERATION */ + case E_LABEL: + /* On est sous la forme [LABEL], on va mettre sous la forme LABEL[0] */ + t = (expression_t *) Emalloc(sizeof(expression_t)); + t->e_type = t->e_subtype = E_VALUE; + t->avalue = 0; + t->symbol = NULL; + t->pattern = NULL; + t->next = t->child = NULL; + t->prev = e->child; + e->child->next = t; + t->father = e; + e->op = OP_DECAL; + break; } + break; + } } /* Cette fonction est appelée depuis le parseur. Elle ajit sur la pile en effectuant une opération */ void act_pile(int o) { - expression_t *e, *e1, *e2; - int i, nbargs; - - e = Emalloc(sizeof(expression_t)); - e->pattern = NULL; - e->op = o; - e->avalue = 0; - e->symbol = NULL; - e->e_type = E_OPERATION; - e->e_subtype = E_OPERATION; - e->next = e->prev = e->father = e->child = NULL; - switch (o) { - case OP_NEST: - exception(1, _("Something wrong, nested operator called...")); - break; - case OP_PLUS: - case OP_MOINS: - case OP_MUL: - case OP_DIV: - case OP_MOD: - case OP_DECAL: - e1 = e_current->prev; - e2 = e_current; - if (e_current->prev->prev) { - e_current->prev->prev->next = NULL; - e_current = e1->prev; - } else { - e_current = NULL; - } - e1->prev = NULL; - e2->next = NULL; - e->child = e1; - e1->father = e2->father = e; - break; - case OP_PLUS_UNARY: - case OP_MOINS_UNARY: - case OP_DIRECT: - e1 = e_current; - if (e1->prev) { - e_current->prev->next = NULL; - e_current = e1->prev; - } else { - e_current = NULL; - } + expression_t *e, *e1, *e2; + int i, nbargs; + + e = Emalloc(sizeof(expression_t)); + e->pattern = NULL; + e->op = o; + e->avalue = 0; + e->symbol = NULL; + e->e_type = E_OPERATION; + e->e_subtype = E_OPERATION; + e->next = e->prev = e->father = e->child = NULL; + switch (o) { + case OP_NEST: + exception(1, _("Something wrong, nested operator called...")); + break; + case OP_PLUS: + case OP_MOINS: + case OP_MUL: + case OP_DIV: + case OP_MOD: + case OP_DECAL: + e1 = e_current->prev; + e2 = e_current; + if (e_current->prev->prev) { + e_current->prev->prev->next = NULL; + e_current = e1->prev; + } else { + e_current = NULL; + } + e1->prev = NULL; + e2->next = NULL; + e->child = e1; + e1->father = e2->father = e; + break; + case OP_PLUS_UNARY: + case OP_MOINS_UNARY: + case OP_DIRECT: + e1 = e_current; + if (e1->prev) { + e_current->prev->next = NULL; + e_current = e1->prev; + } else { + e_current = NULL; + } + e1->prev = NULL; + e->child = e1; + e1->father = e; + break; + case OP_FUNC_CALL: + e1 = e_current; + e_current->prev->next = NULL; + e_current = e1->prev; + nbargs = e1->avalue; + free(e1); + e1 = e2 = NULL; + for (i = 0; i < nbargs; i++) { + e1 = e_current; + e_current->prev->next = NULL; + e_current = e1->prev; + if (e2) { + e2->next = e1; + e2->prev = NULL; + e1->prev = e2; + } else { e1->prev = NULL; - e->child = e1; - e1->father = e; - break; - case OP_FUNC_CALL: - e1 = e_current; - e_current->prev->next = NULL; - e_current = e1->prev; - nbargs = e1->avalue; - free(e1); - e1 = e2 = NULL; - for (i = 0; i < nbargs; i++) { - e1 = e_current; - e_current->prev->next = NULL; - e_current = e1->prev; - if (e2) { - e2->next = e1; - e2->prev = NULL; - e1->prev = e2; - } else { - e1->prev = NULL; - } - e2 = e1; - } - - e2 = e_current; - if (e2->prev) { - e_current->prev->next = NULL; - e_current = e1->prev; - } - e->symbol = e2->symbol; - free(e2); - e->avalue = nbargs; - for (e2 = e1; e2; e2 = e2->next) { - e2->father = e; - } - e->child = e1; - break; - case OP_LPAREN: - exception(1, _("Something wrong, lparenthesis operator called...")); - break; - default: - exception(1, _("Something wrong, should never got here...")); - break; + } + e2 = e1; } - if (e_current) { - e_current->next = e; - e->prev = e_current; + e2 = e_current; + if (e2->prev) { + e_current->prev->next = NULL; + e_current = e1->prev; } - - e_current = e; - evaluate(e_current); + e->symbol = e2->symbol; + free(e2); + e->avalue = nbargs; + for (e2 = e1; e2; e2 = e2->next) { + e2->father = e; + } + e->child = e1; + break; + case OP_LPAREN: + exception(1, _("Something wrong, lparenthesis operator called...")); + break; + default: + exception(1, _("Something wrong, should never got here...")); + break; + } + + if (e_current) { + e_current->next = e; + e->prev = e_current; + } + + e_current = e; + evaluate(e_current); } /* Fonction d'initialisation de l'assembleur */ int assembler_init(void) { - Initialise(&defines); - if (!defines) - return 1; - Initialise(&labels); - if (!defines) - return 1; - if (!(p_text = text = (bytestream_t *) malloc(sizeof(bytestream_t)))) - return 1; - if (!(p_data = data = (bytestream_t *) malloc(sizeof(bytestream_t)))) - return 1; - if (!(p_bss = bss = (bytestream_t *) malloc(sizeof(bytestream_t)))) - return 1; - - text->next = data->next = bss->next = NULL; - text->size = data->size = bss->size = 0; - text->Label = data->Label = bss->Label = NULL; - - return 0; + Initialise(&defines); + if (!defines) + return 1; + Initialise(&labels); + if (!defines) + return 1; + if (!(p_text = text = (bytestream_t *) malloc(sizeof(bytestream_t)))) + return 1; + if (!(p_data = data = (bytestream_t *) malloc(sizeof(bytestream_t)))) + return 1; + if (!(p_bss = bss = (bytestream_t *) malloc(sizeof(bytestream_t)))) + return 1; + + text->next = data->next = bss->next = NULL; + text->size = data->size = bss->size = 0; + text->Label = data->Label = bss->Label = NULL; + + return 0; } /* Nous vérifions si l'expression peut correspondre à une pattern "complexe" (composée) */ static void super_pattern(pattern_t * patterns, expression_t * e) { - int i; - - if (!(e->e_subtype == E_OPERATION)) - 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) { - if (e->e_subtype == E_OPERATION) { - if (e->op == OP_DIRECT) { - if (!patterns->expr[i]->left) { - e->pattern = patterns; - e->index = i; - return; - } - } else { - if (patterns->expr[i]->left) { - e->pattern = patterns; - e->index = i; - return; - } - } - } + int i; + + if (!(e->e_subtype == E_OPERATION)) + 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) { + if (e->e_subtype == E_OPERATION) { + if (e->op == OP_DIRECT) { + if (!patterns->expr[i]->left) { + e->pattern = patterns; + e->index = i; + return; + } + } else { + if (patterns->expr[i]->left) { + e->pattern = patterns; + e->index = i; + return; + } + } + } + } else { + if (patterns->expr[i]->string) { + if (patterns->expr[i]->string[0] == 'O') { + if (e->e_subtype == E_OPERATION) { + if (((e->op == OP_DIRECT) + && (e->child->e_subtype == E_VALUE)) + || ((e->op == OP_DECAL) + && (e->child->next->e_subtype == E_VALUE))) { + e->pattern = patterns; + e->index = i; + return; + } } else { - if (patterns->expr[i]->string) { - if (patterns->expr[i]->string[0] == 'O') { - if (e->e_subtype == E_OPERATION) { - if (((e->op == OP_DIRECT) && (e->child->e_subtype == E_VALUE)) - || ((e->op == OP_DECAL) - && (e->child->next->e_subtype == E_VALUE))) { - e->pattern = patterns; - e->index = i; - return; - } - } else { - if (e->e_subtype == E_LABEL) { - e->pattern = patterns; - e->index = i; - return; - } - } - } - } + if (e->e_subtype == E_LABEL) { + e->pattern = patterns; + e->index = i; + return; + } } + } } + } } + } } /* Ces deux fonctions récursives croisées nous servent à déterminer su une expression est un label */ @@ -1191,556 +1234,614 @@ static int islabel(expression_t *); static int islabel2(expression_t * e) { - if (islabel(e)) - return 1; - if (e->next) { - return islabel2(e->next); - } - return 0; + if (islabel(e)) + return 1; + if (e->next) { + return islabel2(e->next); + } + return 0; } static int islabel(expression_t * e) { - if (e->e_subtype == E_LABEL) - return 1; - if (e->child) { - return islabel2(e->child); - } - return 0; + if (e->e_subtype == E_LABEL) + return 1; + if (e->child) { + return islabel2(e->child); + } + return 0; } /* Cette fonction cherche une instruction pouvant correspondre à l'expression. */ -static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, expression_t * e) +static instruct_t *e_match_i(phon_t * phons, instruct_t * instructs, + expression_t * e) { - char *stringtolook = e->symbol; - expression_t *t; - int i, go_out; - - for (t = e->next; t; t = t->next) { - if (!t->pattern) - super_pattern(patterns, t); - } - - for (phons = phons->next; phons; phons = phons->next) { - if (!strcasecmp(phons->p1, stringtolook)) { - stringtolook = phons->p2; - break; - } + char *stringtolook = e->symbol; + expression_t *t; + int i, go_out; + + for (t = e->next; t; t = t->next) { + if (!t->pattern) + super_pattern(patterns, t); + } + + for (phons = phons->next; phons; phons = phons->next) { + if (!strcasecmp(phons->p1, stringtolook)) { + stringtolook = phons->p2; + break; } + } #ifdef DEBUG - debug_print_expression(e); + debug_print_expression(e); #endif - for (instructs = instructs->next; instructs; instructs = instructs->next) { - t = e; - go_out = 0; - for (i = 0; (!go_out) && (i < instructs->nbexplicit); i++) { - if (!instructs->strings[i]) { - if (strcasecmp(instructs->names[i], i ? t->symbol : stringtolook)) { - go_out = 1; - } - } else { - switch (instructs->strings[i][0]) { - case 'P': - if (!t->pattern) { - go_out = 1; - } else { - if (strcasecmp(instructs->strings[i] + 1, t->pattern->name)) - go_out = 1; - } - break; - case 'C': - if ((t->e_subtype != E_VALUE) && (t->e_subtype != E_LABEL)) { - go_out = 1; - } - - if ((t->e_subtype == E_OPERATION) - && ((t->op == OP_PLUS) || (t->op == OP_MOINS))) { - if (((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 = 0; - } - } - - if ((t->e_subtype == E_OPERATION) && (t->op == OP_FUNC_CALL)) { - go_out = 0; - } - - break; - case 'O': - /* C'est le cas le plus simple ou presque, il suffit de tout prendre */ - break; - default: - exception(1, _("Unknow predefined string into the meta language")); - } + for (instructs = instructs->next; instructs; instructs = instructs->next) { + t = e; + go_out = 0; + for (i = 0; (!go_out) && (i < instructs->nbexplicit); i++) { + if (!instructs->strings[i]) { + if (strcasecmp + (instructs->names[i], i ? t->symbol : stringtolook)) { + go_out = 1; + } + } else { + switch (instructs->strings[i][0]) { + case 'P': + if (!t->pattern) { + go_out = 1; + } else { + if (strcasecmp + (instructs->strings[i] + 1, + t->pattern->name)) go_out = 1; + } + break; + case 'C': + if ((t->e_subtype != E_VALUE) + && (t->e_subtype != E_LABEL)) { + go_out = 1; + } + + if ((t->e_subtype == E_OPERATION) + && ((t->op == OP_PLUS) + || (t->op == OP_MOINS))) { + if (((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 = 0; } + } - if (!(t = t->next) && ((i + 1) < instructs->nbexplicit)) { - go_out = 1; - } - } + if ((t->e_subtype == E_OPERATION) + && (t->op == OP_FUNC_CALL)) { + go_out = 0; + } - if (!go_out && !t) { - break; + break; + case 'O': + /* C'est le cas le plus simple ou presque, il suffit de tout prendre */ + break; + default: + exception(1, + _ + ("Unknow predefined string into the meta language")); } + } + + if (!(t = t->next) && ((i + 1) < instructs->nbexplicit)) { + go_out = 1; + } } + + if (!go_out && !t) { + break; + } + } #ifdef DEBUG - if (instructs) { - 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], instructs->strings[i] ? instructs->strings[i] : "Pas de chaîne associée", instructs->etypes[i] ? "prédéfinit" : "direct"); - } - fprintf(stderr, " => Champs implicites.\n"); - for (i = 0; i < instructs->nbimplicit; i++) { - switch (instructs->itypes[i]) { - case 0: - fprintf(stderr, " + %s <= %s (type direct)\n", instructs->implicits[i], instructs->istrings[i]); - break; - case 1: - 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], instructs->ivalues[i]); - break; - } - } + if (instructs) { + 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], + instructs-> + strings[i] ? instructs->strings[i] : + "Pas de chaîne associée", + instructs->etypes[i] ? "prédéfinit" : "direct"); + } + fprintf(stderr, " => Champs implicites.\n"); + for (i = 0; i < instructs->nbimplicit; i++) { + switch (instructs->itypes[i]) { + case 0: + fprintf(stderr, + " + %s <= %s (type direct)\n", + instructs->implicits[i], instructs->istrings[i]); + break; + case 1: + 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], instructs->ivalues[i]); + break; + } } + } #endif - return instructs; + return instructs; } /* Cette fonction va évaluer une pattern et rajouter les variable dans la table de hachage correspondante */ static void evaluate_pattern(_TableauVariable * it, expression_t * e) { - metaexpr_t *m, *n; - expression_t *t; - int tv; - - if (!e->pattern) { - exception(1, _("Pattern not matching...")); - } - - m = e->pattern->expr[e->index]; - if (m->name) { + metaexpr_t *m, *n; + expression_t *t; + int tv; + + if (!e->pattern) { + exception(1, _("Pattern not matching...")); + } + + m = e->pattern->expr[e->index]; + if (m->name) { + if (m->string) { + if (m->type) { + if (strcmp(m->name, "I")) { + exception(1, + _("Unknow constant type in the meta language")); + } + if (e->e_subtype == E_VALUE) { + pushdword(e->avalue, NULL); + } else { + pushdword(0, e); + } + } else { if (m->string) { - if (m->type) { - if (strcmp(m->name, "I")) { - exception(1, _("Unknow constant type in the meta language")); - } - if (e->e_subtype == E_VALUE) { - pushdword(e->avalue, NULL); - } else { - pushdword(0, e); - } - } else { - if (m->string) { - if (m->string[0] != 'P') { - exception(1, _("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->avalue = e->index; - t->symbol = NULL; - t->pattern = NULL; - InsererVarDansTab(it, CreerElement(m->name, t)); - } - } + if (m->string[0] != 'P') { + exception(1, + _ + ("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->avalue = e->index; + t->symbol = NULL; + t->pattern = NULL; + InsererVarDansTab(it, CreerElement(m->name, t)); } - } else { - n = m; - if (m->left) { /* Binaire */ - m = m->left; - if (m->string) { - if (m->type) { - if (strcmp(m->name, "I") || strcmp(m->string, "O")) { - exception(1, _("Unknow constant type in the meta language")); - } - tv = 0; - if (e->child->next->e_subtype == E_VALUE) { - tv = e->child->next->avalue; - } - if ((e->child->next->e_subtype == E_OPERATION) - && (e->child->next->child->e_subtype == E_VALUE)) { - tv = e->child->next->child->avalue; - } - pushdword(tv, e->child); - } else { - exception(1, _("Logical error in meta language")); - } + } + } + } else { + n = m; + if (m->left) { /* Binaire */ + m = m->left; + if (m->string) { + if (m->type) { + if (strcmp(m->name, "I") + || strcmp(m->string, "O")) { + exception(1, + _ + ("Unknow constant type in the meta language")); + } + tv = 0; + if (e->child->next->e_subtype == E_VALUE) { + tv = e->child->next->avalue; + } + if ((e->child->next->e_subtype == E_OPERATION) + && (e->child->next->child->e_subtype == E_VALUE)) { + tv = e->child->next->child->avalue; + } + pushdword(tv, e->child); + } else { + exception(1, _("Logical error in meta language")); + } + } + m = n; + m = m->right; + if (m->string) { + if (m->type) { + } else { + if (m->string) { + if (m->string[0] != 'P') { + exception(1, + _ + ("Error in the metalanguage (pattern should be here if not a constant type)")); } - m = n; - m = m->right; - if (m->string) { - if (m->type) { - } else { - if (m->string) { - if (m->string[0] != 'P') { - exception(1, _("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 { - t->avalue = e->child->next->child->next->index; - } - t->symbol = NULL; - InsererVarDansTab(it, CreerElement(m->name, t)); - } else { - exception(1, _("Logical error in meta language")); - } - } + 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 { + t->avalue = e->child->next->child->next->index; } - } else { /* Unaire */ - m = m->right; - if (m->string) { - if (m->type) { - exception(1, _("Logical error in meta language")); - } else { - if (m->string) { - if (m->string[0] != 'P') { - exception(1, _("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->avalue = e->child->index; - t->symbol = NULL; - t->pattern = NULL; - InsererVarDansTab(it, CreerElement(m->name, t)); - } - } + t->symbol = NULL; + InsererVarDansTab(it, CreerElement(m->name, t)); + } else { + exception(1, _("Logical error in meta language")); + } + } + } + } else { /* Unaire */ + m = m->right; + if (m->string) { + if (m->type) { + exception(1, _("Logical error in meta language")); + } else { + if (m->string) { + if (m->string[0] != 'P') { + exception(1, + _ + ("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->avalue = e->child->index; + t->symbol = NULL; + t->pattern = NULL; + InsererVarDansTab(it, CreerElement(m->name, t)); + } } + } } + } } /* Cette fonction va évaluer un champ et en renvoyer la valeur, à l'aide de la table de hachage passée en argument */ static int evaluate_field(_TableauVariable it, char *field, field_t * fields) { - int i, r, e, n; - char trouve; - expression_t *t; + int i, r, e, n; + char trouve; + expression_t *t; - for (fields = fields->next; fields; fields = fields->next) { - if (!strcmp(field + 1, fields->name)) { - break; - } + for (fields = fields->next; fields; fields = fields->next) { + if (!strcmp(field + 1, fields->name)) { + break; } - if (!(fields)) { - exception(1, _("Unknow field in metalanguage")); + } + if (!(fields)) { + exception(1, _("Unknow field in metalanguage")); + } + + r = 0; + n = 0; + for (i = 0; i < fields->nbr; i++) { + t = (expression_t *) NomVarToVar(fields->names[i], it, &trouve); + e = 0; + if (trouve) { + if (t->e_subtype != E_VALUE) { + exception(1, _("Can't evaluate directly expression")); + } + e = t->avalue; } - - r = 0; - n = 0; - for (i = 0; i < fields->nbr; i++) { - t = (expression_t *) NomVarToVar(fields->names[i], it, &trouve); - e = 0; - if (trouve) { - if (t->e_subtype != E_VALUE) { - exception(1, _("Can't evaluate directly expression")); - } - e = t->avalue; - } - n = fields->sizes[i]; - if ((e & ((1 << n) - 1)) != e) { - exception(1, _("Value too large for field")); - } - r = (r << n) | e; + n = fields->sizes[i]; + if ((e & ((1 << n) - 1)) != e) { + exception(1, _("Value too large for field")); } + r = (r << n) | e; + } - return r; + return r; } /* Fonction servant à effectuer une fin de ligne. Force l'évaluation complète de la ligne */ void asm_eol(void) { - instruct_t *instr; - expression_t *t; - int i; - char trouve; - _TableauVariable it; - bytestream_t *pi; - - switch (special) { - case 2: /* Cas de #define */ - InsererVarDansTab(&defines, CreerElement(defined, e_line)); - free(defined); - break; - case 1: /* Cas des directives . */ - case 3: /* Cas de #undef */ - case 4: /* Cas de #include */ - /* Rien à faire ici... tout est déjà fait dans la routine push_pile() */ - break; - case 0: /* Cas normal */ - /* C'est ici la bonne histoire... Il faut reconnaitre l'instruction */ + instruct_t *instr; + expression_t *t; + int i; + char trouve; + _TableauVariable it; + bytestream_t *pi; + + switch (special) { + case 2: /* Cas de #define */ + InsererVarDansTab(&defines, CreerElement(defined, e_line)); + free(defined); + break; + case 1: /* Cas des directives . */ + case 3: /* Cas de #undef */ + case 4: /* Cas de #include */ + /* Rien à faire ici... tout est déjà fait dans la routine push_pile() */ + break; + case 0: /* Cas normal */ + /* C'est ici la bonne histoire... Il faut reconnaitre l'instruction */ + e_current = e_line; + 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] == ':') { + pushlabel(e_current->symbol); + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); e_current = e_line; - 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] == ':') { - pushlabel(e_current->symbol); - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - } else { - /* C'est peut-être une instruction spéciale */ - if ((e_current->next) && (e_current->next->e_subtype == E_INTERNAL)) { - pushlabel(e_current->symbol); - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - } else { - exception(1, _("Unknow instruction")); - } - } + } else { + /* C'est peut-être une instruction spéciale */ + if ((e_current->next) + && (e_current->next->e_subtype == E_INTERNAL)) { + pushlabel(e_current->symbol); + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); + e_current = e_line; + } else { + exception(1, _("Unknow instruction")); } + } + } - switch (e_current->e_subtype) { - case E_INTERNAL: - switch (e_current->op) { - case 1: /* DB */ - case 2: /* DW */ - case 3: /* DD */ - do { - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - if (!e_current) - break; - switch (e_current->e_subtype) { - case E_VALUE: - pushdword(e_current->avalue, NULL); - break; - case E_STRING: - if (!strcmp(e_current->symbol, "?")) { - exception(1, _("Unknow constant")); - } - pushuninit(1); - break; - case E_LABEL: - case E_OPERATION: - pushdword(0, e_current); - break; - default: - exception(1, _("Bad constant for an immediate value")); - } - } while (1); - break; - case 4: /* DS */ - do { - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - if (!e_current) - break; - switch (e_current->e_subtype) { - case E_STRING: - pushstring(e_current->symbol); - break; - default: - exception(1, _("Bad constant for a string")); - } - } while (1); - break; - case 5: /* DR */ - do { - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - if (!e_current) - break; - switch (e_current->e_subtype) { - case E_VALUE: - pushuninit(e_current->avalue); - break; - default: - exception(1, _("Bad array size")); - } - } while (1); - break; + switch (e_current->e_subtype) { + case E_INTERNAL: + switch (e_current->op) { + case 1: /* DB */ + case 2: /* DW */ + case 3: /* DD */ + do { + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); + e_current = e_line; + if (!e_current) + break; + switch (e_current->e_subtype) { + case E_VALUE: + pushdword(e_current->avalue, NULL); + break; + case E_STRING: + if (!strcmp(e_current->symbol, "?")) { + exception(1, _("Unknow constant")); } + pushuninit(1); + break; + case E_LABEL: + case E_OPERATION: + pushdword(0, e_current); + break; + default: + exception(1, _("Bad constant for an immediate value")); + } + } while (1); + break; + case 4: /* DS */ + do { + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); + e_current = e_line; + if (!e_current) + break; + switch (e_current->e_subtype) { + case E_STRING: + pushstring(e_current->symbol); break; - case E_INSTRUCT: - if (!e_current) - break; - if (segment != SEG_TEXT) { - exception(1, _("You can only have an instruction into a .text segment")); + default: + exception(1, _("Bad constant for a string")); + } + } while (1); + break; + case 5: /* DR */ + do { + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); + e_current = e_line; + if (!e_current) + break; + switch (e_current->e_subtype) { + case E_VALUE: + pushuninit(e_current->avalue); + break; + default: + exception(1, _("Bad array size")); + } + } while (1); + break; + } + break; + case E_INSTRUCT: + if (!e_current) + break; + if (segment != SEG_TEXT) { + exception(1, + _ + ("You can only have an instruction into a .text segment")); + } + if (!(instr = e_match_i(phons, instructs, e_current))) { + exception(1, _("Unmatched instruction")); + } + + /* Operation cruciale: nous avons l'instruction qui correspond le mieux à notre + expression, on va tenter de l'évaluer */ + + pi = pushuninit(1); + + Initialise(&it); + for (i = 0; i < instr->nbexplicit; i++) { + if (instr->strings[i]) { + switch (instr->strings[i][0]) { + case 'C': + if (instr->etypes[i]) { + if (!strcmp(instr->names[i], "I")) { + if (e_current->e_subtype == E_VALUE) { + pushdword(e_current->avalue, 0); + } else { + pushdword(0, e_current); + } + } else { + exception(1, + _ + ("Unknow constant type in the meta language")); + } + } else { + InsererVarDansTab(&it, + CreerElement + (instr->names[i], e_current)); } - if (!(instr = e_match_i(phons, instructs, e_current))) { - exception(1, _("Unmatched instruction")); + break; + case 'O': + if (instr->etypes) { + if (!strcmp(instr->names[i], "I")) { + if (e_current->e_subtype == E_VALUE) { + pushdword(e_current->avalue, 0); + } else { + pushdword(0, e_current); + } + } else { + exception(1, + _ + ("Unknow constant type in the meta language")); + } } - - /* Operation cruciale: nous avons l'instruction qui correspond le mieux à notre - expression, on va tenter de l'évaluer */ - - pi = pushuninit(1); - - Initialise(&it); - for (i = 0; i < instr->nbexplicit; i++) { - if (instr->strings[i]) { - switch (instr->strings[i][0]) { - case 'C': - if (instr->etypes[i]) { - if (!strcmp(instr->names[i], "I")) { - if (e_current->e_subtype == E_VALUE) { - pushdword(e_current->avalue, 0); - } else { - pushdword(0, e_current); - } - } else { - exception(1, _("Unknow constant type in the meta language")); - } - } else { - InsererVarDansTab(&it, CreerElement(instr->names[i], e_current)); - } - break; - case 'O': - if (instr->etypes) { - if (!strcmp(instr->names[i], "I")) { - if (e_current->e_subtype == E_VALUE) { - pushdword(e_current->avalue, 0); - } else { - pushdword(0, e_current); - } - } else { - exception(1, _("Unknow constant type in the meta language")); - } - } - break; - case 'P': - t = (expression_t *) Emalloc(sizeof(expression_t)); - t->e_type = t->e_subtype = E_VALUE; - t->avalue = e_current->index; - t->child = t->next = NULL; - t->pattern = NULL; - t->symbol = NULL; - InsererVarDansTab(&it, CreerElement(instr->names[i], t)); + break; + case 'P': + t = (expression_t *) + Emalloc(sizeof(expression_t)); + t->e_type = t->e_subtype = E_VALUE; + t->avalue = e_current->index; + t->child = t->next = NULL; + t->pattern = NULL; + t->symbol = NULL; + InsererVarDansTab(&it, + CreerElement(instr->names[i], t)); #ifdef DEBUG - fprintf(stderr, "On a %s qui vaut %i\n", instr->names[i], e_current->index); + fprintf(stderr, + "On a %s qui vaut %i\n", + instr->names[i], e_current->index); #endif - evaluate_pattern(&it, e_current); - break; - default: - exception(1, _("Unknow constant type in the meta language")); - } - } - - e_line = e_current->next; - e_current->next = NULL; - free_expr(e_current); - e_current = e_line; - } + evaluate_pattern(&it, e_current); + break; + default: + exception(1, + _ + ("Unknow constant type in the meta language")); + } + } - for (i = 0; i < instr->nbimplicit; i++) { - switch (instr->itypes[i]) { - case 0: /* type direct */ - t = (expression_t *) NomVarToVar(instr->istrings[i], it, &trouve); - if (!trouve) { - t = (expression_t *) Emalloc(sizeof(expression_t)); - t->e_type = t->e_subtype = E_VALUE; - t->avalue = 0; - t->child = t->next = NULL; - t->pattern = NULL; - t->symbol = NULL; - } + e_line = e_current->next; + e_current->next = NULL; + free_expr(e_current); + e_current = e_line; + } + + for (i = 0; i < instr->nbimplicit; i++) { + switch (instr->itypes[i]) { + case 0: /* type direct */ + t = (expression_t *) + NomVarToVar(instr->istrings[i], it, &trouve); + if (!trouve) { + t = (expression_t *) + Emalloc(sizeof(expression_t)); + t->e_type = t->e_subtype = E_VALUE; + t->avalue = 0; + t->child = t->next = NULL; + t->pattern = NULL; + t->symbol = NULL; + } #ifdef DEBUG - fprintf(stderr, "On a %s qui vaut %i\n", instr->implicits[i], t->avalue); + fprintf(stderr, "On a %s qui vaut %i\n", + instr->implicits[i], t->avalue); #endif - InsererVarDansTab(&it, CreerElement(instr->implicits[i], t)); - break; - 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') { - exception(1, _("Logical error in meta language")); - } - t->avalue = evaluate_field(it, instr->istrings[i], fields);; - t->child = t->next = NULL; - t->pattern = NULL; - t->symbol = NULL; + InsererVarDansTab(&it, + CreerElement(instr->implicits[i], t)); + break; + 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') { + exception(1, _("Logical error in meta language")); + } + t->avalue = evaluate_field(it, instr->istrings[i], fields);; + t->child = t->next = NULL; + t->pattern = NULL; + t->symbol = NULL; #ifdef DEBUG - fprintf(stderr, "On a %s qui vaut %i\n", instr->implicits[i], t->avalue); + fprintf(stderr, "On a %s qui vaut %i\n", + instr->implicits[i], t->avalue); #endif - InsererVarDansTab(&it, CreerElement(instr->implicits[i], t)); - break; - case 2: /* type valeur */ - t = (expression_t *) Emalloc(sizeof(expression_t)); - t->e_type = t->e_subtype = E_VALUE; - t->avalue = instr->ivalues[i]; - t->child = t->next = NULL; - t->pattern = NULL; - t->symbol = NULL; + InsererVarDansTab(&it, + CreerElement(instr->implicits[i], t)); + break; + case 2: /* type valeur */ + t = (expression_t *) + Emalloc(sizeof(expression_t)); + t->e_type = t->e_subtype = E_VALUE; + t->avalue = instr->ivalues[i]; + t->child = t->next = NULL; + t->pattern = NULL; + t->symbol = NULL; #ifdef DEBUG - fprintf(stderr, "On a %s qui vaut %i\n", instr->implicits[i], t->avalue); + fprintf(stderr, "On a %s qui vaut %i\n", + instr->implicits[i], t->avalue); #endif - InsererVarDansTab(&it, CreerElement(instr->implicits[i], t)); - break; - } - } - - pi->Encoded = evaluate_field(it, "FI", fields); - DetruitTab(&it); - break; - default: - exception(1, _("Unknow instruction")); - break; + InsererVarDansTab(&it, + CreerElement(instr->implicits[i], t)); + break; } + } - break; + pi->Encoded = evaluate_field(it, "FI", fields); + DetruitTab(&it); + break; + default: + exception(1, _("Unknow instruction")); + break; } - wc = 0; - special = 0; - e_current = e_line = NULL; + break; + } + + wc = 0; + special = 0; + e_current = e_line = NULL; } /* 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) { - int i; + int i; #ifdef DEBUG - fprintf(stderr, "ÉCriture de %08lX sur %i\n", a, fileno(f)); + fprintf(stderr, "ÉCriture de %08lX sur %i\n", a, fileno(f)); #endif - if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) { - if (ferror(f)) { - pushcontext(strerror(errno)); - } - exception(1, _("Error writing file")); + if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) { + if (ferror(f)) { + pushcontext(strerror(errno)); } + exception(1, _("Error writing file")); + } - for (i = 0; i < n - 1; i++) { - writeword(0, f, 1); - } + for (i = 0; i < n - 1; i++) { + writeword(0, f, 1); + } } /* Ecrit une chaine dans le fichier spécifié, avec traitement des erreurs */ static void writestring(char *string, FILE * f) { - for (; *string; string++) { - writeword(*string, f, 1); - } + for (; *string; string++) { + writeword(*string, f, 1); + } } /****************\ @@ -1763,24 +1864,24 @@ static void writestring(char *string, FILE * f) \****************//* Cette fonction ajoute un fichier à un autre */ void appendfile(FILE * f, char *name) { - unsigned long int a; - FILE *g; + unsigned long int a; + FILE *g; - if (!(g = fopen(name, "rb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error writing file")); - } + 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); - } + while (fread(&a, sizeof(a), 1, g) == 1) { + writeword(a, f, 1); + } - if (ferror(f)) { - pushcontext(strerror(errno)); - exception(1, _("Error reading file")); - } + if (ferror(f)) { + pushcontext(strerror(errno)); + exception(1, _("Error reading file")); + } - fclose(g); + fclose(g); } /* Cette fonction est appelée pour signaler la fin du fichier. Elle va écrire tous les segments dans le fichier de sortie, @@ -1788,380 +1889,433 @@ void appendfile(FILE * f, char *name) void asm_eof(FILE * f) { - 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; - - pushcontext(_("Creating temporary files")); - if (!(f1 = fopen("__text__", "wb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error writing file __text__")); - } - if (!(f2 = fopen("__symbols__", "wb"))) { - pushcontext(strerror(errno)); - 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); + 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; + + pushcontext(_("Creating temporary files")); + if (!(f1 = fopen("__text__", "wb"))) { + pushcontext(strerror(errno)); + exception(1, _("Error writing file __text__")); + } + if (!(f2 = fopen("__symbols__", "wb"))) { + pushcontext(strerror(errno)); + 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); #ifdef DEBUG - fprintf(stderr, "%s\n", errctx); + fprintf(stderr, "%s\n", errctx); #endif - pushcontext(errctx); + pushcontext(errctx); - a = 0; + a = 0; - if (ttext->Expr) { - switch (ttext->Expr->e_subtype) { - case E_VALUE: - a = ttext->Expr->avalue; - break; - case E_LABEL: + if (ttext->Expr) { + switch (ttext->Expr->e_subtype) { + case E_VALUE: + a = ttext->Expr->avalue; + break; + case E_LABEL: #ifdef DEBUG - fprintf(stderr, "Symbole externe %s\n", ttext->Expr->symbol); + fprintf(stderr, "Symbole externe %s\n", ttext->Expr->symbol); #endif - 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_DECAL) { - if ((ttext->Expr->child->e_subtype == E_LABEL) - && (ttext->Expr->child->next->e_subtype == E_VALUE)) { + 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_DECAL) { + if ((ttext->Expr->child->e_subtype == E_LABEL) + && (ttext->Expr->child->next->e_subtype == E_VALUE)) { #ifdef DEBUG - fprintf(stderr, "Symbole externe %s\n", ttext->Expr->child->symbol); + fprintf(stderr, + "Symbole externe %s\n", + ttext->Expr->child->symbol); #endif - a = ttext->Expr->child->next->avalue; - nbsymbols++; - writeword(1, f2, 1); - writeword(ttext->offset, f2, 1); - writeword(strlen(ttext->Expr->child->symbol), f2, 1); - writestring(ttext->Expr->child->symbol, f2); - nbe++; - break; - } - } - if (ttext->Expr->op == OP_PLUS) { - if (((ttext->Expr->child->e_subtype == E_LABEL) - && (ttext->Expr->child->next->e_subtype == E_VALUE)) - || ((ttext->Expr->child->e_subtype == E_VALUE) - && (ttext->Expr->child->next->e_subtype == E_LABEL))) { - if (ttext->Expr->child->e_subtype == E_LABEL) { + a = ttext->Expr->child->next->avalue; + nbsymbols++; + writeword(1, f2, 1); + writeword(ttext->offset, f2, 1); + writeword(strlen(ttext->Expr->child->symbol), f2, 1); + writestring(ttext->Expr->child->symbol, f2); + nbe++; + break; + } + } + if (ttext->Expr->op == OP_PLUS) { + if (((ttext->Expr->child->e_subtype == E_LABEL) + && (ttext->Expr->child->next->e_subtype == E_VALUE)) + || ((ttext->Expr->child->e_subtype == E_VALUE) + && (ttext->Expr->child->next->e_subtype == + E_LABEL))) { + if (ttext->Expr->child->e_subtype == E_LABEL) { #ifdef DEBUG - fprintf(stderr, "Symbole externe %s\n", ttext->Expr->child->symbol); + fprintf(stderr, + "Symbole externe %s\n", + ttext->Expr->child->symbol); #endif - a = ttext->Expr->child->next->avalue; - nbsymbols++; - writeword(1, f2, 1); - writeword(ttext->offset, f2, 1); - writeword(strlen(ttext->Expr->child->symbol), f2, 1); - writestring(ttext->Expr->child->symbol, f2); - nbe++; - } else { + a = ttext->Expr->child->next->avalue; + nbsymbols++; + writeword(1, f2, 1); + writeword(ttext->offset, f2, 1); + writeword(strlen + (ttext->Expr->child->symbol), f2, 1); + writestring(ttext->Expr->child->symbol, f2); + nbe++; + } else { #ifdef DEBUG - fprintf(stderr, "Symbole externe %s\n", ttext->Expr->child->next->symbol); + fprintf(stderr, + "Symbole externe %s\n", + ttext->Expr->child->next->symbol); #endif - a = ttext->Expr->child->avalue; - nbsymbols++; - writeword(1, f2, 1); - writeword(ttext->offset, f2, 1); - writeword(strlen(ttext->Expr->child->next->symbol), f2, 1); - writestring(ttext->Expr->child->next->symbol, f2); - nbe++; - } - 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")); - } + a = ttext->Expr->child->avalue; + nbsymbols++; + writeword(1, f2, 1); + writeword(ttext->offset, f2, 1); + writeword(strlen + (ttext->Expr->child->next->symbol), f2, + 1); + writestring(ttext->Expr->child->next->symbol, f2); + nbe++; + } + 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")); - } + 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")); - } + 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 = t->offset - u->offset; + break; + default: + exception(1, _("Can't evaluate expression")); - } - } + } + } - a += ttext->Encoded; + a += ttext->Encoded; - if (ttext->size) { - writeword(a, f1, text->size); - } else { - if (ttext->Label) { - nbsymbols++; + if (ttext->size) { + writeword(a, f1, text->size); + } else { + if (ttext->Label) { + nbsymbols++; #ifdef DEBUG - fprintf(stderr, "Symbole interne %s\n", ttext->Label); + fprintf(stderr, "Symbole interne %s\n", ttext->Label); #endif - writeword(0, f2, 1); - writeword(ttext->offset, f2, 1); - if (ttext->Label[strlen(ttext->Label) - 1] == ':') { - ttext->Label[strlen(ttext->Label) - 1] = '\0'; - } - writeword(strlen(ttext->Label), f2, 1); - writestring(ttext->Label, f2); - nbi++; - } + writeword(0, f2, 1); + writeword(ttext->offset, f2, 1); + if (ttext->Label[strlen(ttext->Label) - 1] == ':') { + ttext->Label[strlen(ttext->Label) - 1] = '\0'; } - popcontext(); + writeword(strlen(ttext->Label), f2, 1); + writestring(ttext->Label, f2); + nbi++; + } } popcontext(); + } + popcontext(); - /* Segment de data */ + /* 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); + 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); #ifdef DEBUG - fprintf(stderr, "%s\n", errctx); + fprintf(stderr, "%s\n", errctx); #endif - pushcontext(errctx); + pushcontext(errctx); - a = 0; + 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; + if (tdata->Expr) { + switch (tdata->Expr->e_subtype) { + case E_VALUE: + a = tdata->Expr->avalue; + break; + case E_LABEL: + a = tdata->Expr->avalue; #ifdef DEBUG - fprintf(stderr, "Symbole externe %s\n", tdata->Expr->child->symbol); + fprintf(stderr, "Symbole externe %s\n", + tdata->Expr->child->symbol); #endif - nbsymbols++; - writeword(3, f2, 1); - writeword(tdata->offset, f2, 1); - writeword(strlen(tdata->Expr->symbol), f2, 1); - writestring(tdata->Expr->symbol, f2); - nbe++; - break; - case E_OPERATION: - 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")); - } + nbsymbols++; + writeword(3, f2, 1); + writeword(tdata->offset, f2, 1); + writeword(strlen(tdata->Expr->symbol), f2, 1); + writestring(tdata->Expr->symbol, f2); + nbe++; + break; + case E_OPERATION: + 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")); - } + 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")); - } + 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 = t->offset - u->offset; + break; + default: + exception(1, _("Can't evaluate expression")); - } - } + } + } - a += tdata->Encoded; + a += tdata->Encoded; - if (tdata->size) { - writeword(a, f1, text->size); - } else { - if (tdata->Label) { - nbsymbols++; + if (tdata->size) { + writeword(a, f1, text->size); + } else { + if (tdata->Label) { + nbsymbols++; #ifdef DEBUG - fprintf(stderr, "Symbole interne %s\n", tdata->Label); + fprintf(stderr, "Symbole interne %s\n", tdata->Label); #endif - writeword(2, f2, 1); - writeword(tdata->offset, f2, 1); - writeword(strlen(tdata->Label), f2, 1); - writestring(tdata->Label, f2); - nbi++; - } - } - popcontext(); + writeword(2, f2, 1); + writeword(tdata->offset, f2, 1); + writeword(strlen(tdata->Label), f2, 1); + writestring(tdata->Label, f2); + nbi++; + } } popcontext(); + } + popcontext(); - fclose(f1); + fclose(f1); - /* Segment bss */ + /* 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); - pushcontext(errctx); + 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); + pushcontext(errctx); - if (!tbss->size) { - if (tbss->Label) { - nbsymbols++; - writeword(4, f2, 1); + if (!tbss->size) { + if (tbss->Label) { + nbsymbols++; + writeword(4, f2, 1); #ifdef DEBUG - fprintf(stderr, "Symbole interne %s\n", tbss->Label); + fprintf(stderr, "Symbole interne %s\n", tbss->Label); #endif - writeword(tbss->offset, f2, 1); - writeword(strlen(tbss->Label), f2, 1); - writestring(tbss->Label, f2); - nbi++; - } - } - popcontext(); + writeword(tbss->offset, f2, 1); + writeword(strlen(tbss->Label), f2, 1); + writestring(tbss->Label, f2); + nbi++; + } } popcontext(); - popcontext(); - - writeword(0x4f424e4e, f, 1); - writeword(ftell(f2) + s_data + s_text + 7, f, 1); - t = (bytestream_t *) NomVarToVar("__start__", labels, &trouve); - if (trouve) { - writeword(t->offset, f, 1); - } 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); + } + popcontext(); + popcontext(); + + writeword(0x4f424e4e, f, 1); + writeword(ftell(f2) + s_data + s_text + 7, f, 1); + t = (bytestream_t *) NomVarToVar("__start__", labels, &trouve); + if (trouve) { + writeword(t->offset, f, 1); + } 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); } /* Diverses fonctions pour faire plein de free() partout */ static void delete_bytestream(bytestream_t * s) { - if (s->next) - delete_bytestream(s->next); - if (s->Label) - free(s->Label); - free(s); + if (s->next) + delete_bytestream(s->next); + if (s->Label) + free(s->Label); + free(s); } void assembler_flush(void) { - DetruitTab(&defines); - DetruitTab(&labels); - delete_bytestream(text); - delete_bytestream(data); - delete_bytestream(bss); + DetruitTab(&defines); + DetruitTab(&labels); + delete_bytestream(text); + delete_bytestream(data); + delete_bytestream(bss); } -/* Fonction générique qui va traiter un fichier entier */ static int process_file(char *name) +/* Fonction générique qui va traiter un fichier entier */ static int +process_file(char *name) { - FILE *f; - char buf[BUFSIZ], errctx[BUFSIZ], *p; - int i = 0; - - pushcontext(_("Loading file")); - sprintf(errctx, _("Opening file '%s'"), name); + FILE *f; + char buf[BUFSIZ], errctx[BUFSIZ], *p; + int i = 0; + + pushcontext(_("Loading file")); + sprintf(errctx, _("Opening file '%s'"), name); + pushcontext(errctx); + filenames[nestedinc++] = Estrdup(name); + if (!(f = fopen(name, "r"))) { + pushcontext(strerror(errno)); + return 1; + } + popcontext(); + + pushcontext(_("Reading file")); + while (fgets(buf, BUFSIZ, f)) { + sprintf(errctx, _("Reading line %i"), line = ++i); pushcontext(errctx); - filenames[nestedinc++] = Estrdup(name); - if (!(f = fopen(name, "r"))) { - pushcontext(strerror(errno)); - return 1; + if ((p = strchr(buf, '\r'))) { + *p = '\0'; } - popcontext(); - - pushcontext(_("Reading file")); - while (fgets(buf, BUFSIZ, f)) { - sprintf(errctx, _("Reading line %i"), line = ++i); - pushcontext(errctx); - if ((p = strchr(buf, '\r'))) { - *p = '\0'; - } - if ((p = strchr(buf, '\n'))) { - *p = '\0'; - } - parse_line(buf); - sprintf(errctx, _("Summering line %s"), buf); - pushcontext(errctx); - asm_eol(); - popcontext(); - popcontext(); + if ((p = strchr(buf, '\n'))) { + *p = '\0'; } - nestedinc--; + parse_line(buf); + sprintf(errctx, _("Summering line %s"), buf); + pushcontext(errctx); + asm_eol(); popcontext(); popcontext(); - fclose(f); - return 0; + } + nestedinc--; + popcontext(); + popcontext(); + fclose(f); + return 0; } /* Fonction d'api pour assembler un fichier d'entrée */ void assemble_file(char *iname, char *oname) { - FILE *f; - - fprintf(stderr, _("Assembling file %s...\n"), iname); - pushcontext(_("Opening output file")); - if (!(f = fopen(oname, "wb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error writing output file")); - } - popcontext(); - - if (process_file(iname)) { - exception(1, _("Error reading file")); - } - pushcontext(_("Writing output file")); - fprintf(stderr, _("Generating output file %s...\n"), oname); - asm_eof(f); - popcontext(); + FILE *f; + + fprintf(stderr, _("Assembling file %s...\n"), iname); + pushcontext(_("Opening output file")); + if (!(f = fopen(oname, "wb"))) { + pushcontext(strerror(errno)); + exception(1, _("Error writing output file")); + } + popcontext(); + + if (process_file(iname)) { + exception(1, _("Error reading file")); + } + pushcontext(_("Writing output file")); + fprintf(stderr, _("Generating output file %s...\n"), oname); + asm_eof(f); + popcontext(); } diff --git a/lib/exceptions.c b/lib/exceptions.c index b86fb9f..adef5ee 100644 --- a/lib/exceptions.c +++ b/lib/exceptions.c @@ -19,63 +19,63 @@ int clevel = 0; char *Estrdup(char *o) { - char *r; + char *r; - if (o) { - if (!(r = strdup(o))) { - exception(1, _("Out of memory.")); - } - } else { - return NULL; + if (o) { + if (!(r = strdup(o))) { + exception(1, _("Out of memory.")); } - return r; + } else { + return NULL; + } + return r; } void *Emalloc(size_t s) { - void *r; + void *r; - if (s) { - if (!(r = malloc(s))) { - exception(1, _("Out of memory.")); - } - } else { - return NULL; + if (s) { + if (!(r = malloc(s))) { + exception(1, _("Out of memory.")); } - return r; + } else { + return NULL; + } + return r; } void pushcontext(char *c) { - if (clevel == 128) { - exception(1, _("Too much error contexts during pushcontext().")); - } - contexts[clevel++] = Estrdup(c); + if (clevel == 128) { + exception(1, _("Too much error contexts during pushcontext().")); + } + contexts[clevel++] = Estrdup(c); } void popcontext(void) { - if (clevel == 0) { - exception(1, _("Error context empty, but popcontext() called.")); - } - free(contexts[--clevel]); + if (clevel == 0) { + exception(1, _("Error context empty, but popcontext() called.")); + } + free(contexts[--clevel]); } void flushcontext(void) { - while (clevel) { - popcontext(); - } + while (clevel) { + popcontext(); + } } void exception(int level, char *msg) { - int i; + int i; - fprintf(stderr, "Error detected. Showing context.\n"); - for (i = 0; i < clevel; i++) { - fprintf(stderr, " (%i) - %s\n", i, contexts[i]); - } - fprintf(stderr, " Error description: %s\n", msg); - exit(level); + fprintf(stderr, "Error detected. Showing context.\n"); + for (i = 0; i < clevel; i++) { + fprintf(stderr, " (%i) - %s\n", i, contexts[i]); + } + fprintf(stderr, " Error description: %s\n", msg); + exit(level); } diff --git a/lib/fpu.c b/lib/fpu.c index f478ecd..cfac5ba 100644 --- a/lib/fpu.c +++ b/lib/fpu.c @@ -11,5 +11,5 @@ void fpu(Uint32 opcode) { - exception(1, _("FPU not implemented")); + exception(1, _("FPU not implemented")); } diff --git a/lib/hash.c b/lib/hash.c index 1def58f..c0f5bc5 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -4,200 +4,205 @@ #include "hash.h" #include "exceptions.h" #include "config.h" -static char *CHAINEHACHAGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; +static char *CHAINEHACHAGE = + + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; static int FonctionHachage(char *clef) { - unsigned int i; + unsigned int i; - if (!clef) { - exception(1, _("Internal error into hashing")); - } + if (!clef) { + exception(1, _("Internal error into hashing")); + } - for (i = 0; i < strlen(CHAINEHACHAGE); i++) { - if (clef[0] == CHAINEHACHAGE[i]) { - return (i); - } + for (i = 0; i < strlen(CHAINEHACHAGE); i++) { + if (clef[0] == CHAINEHACHAGE[i]) { + return (i); } - return strlen(CHAINEHACHAGE); + } + return strlen(CHAINEHACHAGE); } _Element CreerElement(char *Nom, _TypeVariable Var) { - _Element e; + _Element e; - e.NomVar = Estrdup(Nom); + e.NomVar = Estrdup(Nom); - e.Variable = Var; - return (e); + e.Variable = Var; + return (e); } static _ListeChaine InserTete(_ListeChaine l, _Element e) { - _ListeChaine aux; - unsigned int i; - - 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]; - } - aux->Elem.Variable = e.Variable; - aux->Suivant = l; - return (aux); + _ListeChaine aux; + unsigned int i; + + 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]; + } + aux->Elem.Variable = e.Variable; + aux->Suivant = l; + return (aux); } static int EgaliteChaine(char *ch1, char *ch2) { - unsigned int i; - - if (strlen(ch1) != strlen(ch2)) { - return (0); + unsigned int i; + + if (strlen(ch1) != strlen(ch2)) { + return (0); + } + for (i = 0; i < strlen(ch1); i++) { + if (ch1[i] != ch2[i]) { + return (0); } - for (i = 0; i < strlen(ch1); i++) { - if (ch1[i] != ch2[i]) { - return (0); - } - } - return (1); - /* return (1-strcmp(ch1,ch2)); */ + } + return (1); + /* return (1-strcmp(ch1,ch2)); */ } static void Supprimer(_ListeChaine * l, char *Nom) { - _ListeChaine l_aux = NULL; - - if ((*l) != NULL) { - if (EgaliteChaine((*l)->Elem.NomVar, Nom)) { - l_aux = *l; - *l = (*l)->Suivant; - free(l_aux->Elem.NomVar); - free(l_aux); - } else { - Supprimer(&((*l)->Suivant), Nom); - } + _ListeChaine l_aux = NULL; + + if ((*l) != NULL) { + if (EgaliteChaine((*l)->Elem.NomVar, Nom)) { + l_aux = *l; + *l = (*l)->Suivant; + free(l_aux->Elem.NomVar); + free(l_aux); + } else { + Supprimer(&((*l)->Suivant), Nom); } + } } static void Detruit(_ListeChaine * l) { - _ListeChaine l_aux = NULL; - - while (*l) { - l_aux = (*l)->Suivant; - free((*l)->Elem.NomVar); - free(*l); - *l = l_aux; - } + _ListeChaine l_aux = NULL; + + while (*l) { + l_aux = (*l)->Suivant; + free((*l)->Elem.NomVar); + free(*l); + *l = l_aux; + } } char SupprimerDansTab(_TableauVariable * t, char *Nom) { - int index = FonctionHachage(Nom); - - if (0 <= index && index <= strlen(CHAINEHACHAGE)) { - Supprimer(&((*t)[index]), Nom); - } else { - return (0); - } - return (1); + int index = FonctionHachage(Nom); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + Supprimer(&((*t)[index]), Nom); + } else { + return (0); + } + return (1); } char InsererVarDansTab(_TableauVariable * t, _Element e) { - int index = FonctionHachage(e.NomVar); - - if (0 <= index && index <= strlen(CHAINEHACHAGE)) { - (*t)[index] = InserTete((*t)[index], e); - } else { - return (0); - } - return (1); + int index = FonctionHachage(e.NomVar); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + (*t)[index] = InserTete((*t)[index], e); + } else { + return (0); + } + return (1); } static _TypeVariable NomVarToVarListe(char *Nom, _ListeChaine l, char *trouve) { - *trouve = 0; - while (l != NULL) { - if (EgaliteChaine(Nom, (l->Elem).NomVar)) { - *trouve = 1; - return (l->Elem.Variable); - } - l = l->Suivant; + *trouve = 0; + while (l != NULL) { + if (EgaliteChaine(Nom, (l->Elem).NomVar)) { + *trouve = 1; + return (l->Elem.Variable); } + l = l->Suivant; + } #ifdef HAVE_CONFIG_H - return (NULL); + return (NULL); #else - return 0; + return 0; #endif } _TypeVariable NomVarToVar(char *Nom, _TableauVariable t, char *trouve) { - return (NomVarToVarListe(Nom, t[FonctionHachage(Nom)], trouve)); + return (NomVarToVarListe(Nom, t[FonctionHachage(Nom)], trouve)); } void AfficheListe(_ListeChaine l) { - while (l != NULL) { - fprintf(stderr, "%s\n", l->Elem.NomVar); - l = l->Suivant; - } + while (l != NULL) { + fprintf(stderr, "%s\n", l->Elem.NomVar); + l = l->Suivant; + } } void AfficheTableau(_TableauVariable t) { - unsigned int i; + unsigned int i; - for (i = 0; i < TAILLECHAINEHACHAGE; i++) { - AfficheListe(t[i]); - } + for (i = 0; i < TAILLECHAINEHACHAGE; i++) { + AfficheListe(t[i]); + } } int Initialise(_TableauVariable * t) { - unsigned int i; + unsigned int i; - (*t) = (_TableauVariable) Emalloc(sizeof(_ListeChaine) * (strlen(CHAINEHACHAGE) + 1)); - for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { - (*t)[i] = NULL; - } - return (i); + + (*t) = + (_TableauVariable) Emalloc(sizeof(_ListeChaine) * + (strlen(CHAINEHACHAGE) + 1)); + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + (*t)[i] = NULL; + } + return (i); } void DetruitTab(_TableauVariable * t) { - int i; + int i; - for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { - Detruit(&((*t)[i])); - } + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + Detruit(&((*t)[i])); + } - free(*t); - *t = NULL; + free(*t); + *t = NULL; } #ifndef HAVE_CONFIG_H int main(void) { - int c; - _TableauVariable t; - - Initialise(&t); - InsererVarDansTab(&t, CreerElement("yves", 14)); - InsererVarDansTab(&t, CreerElement("vomitif", 8)); - InsererVarDansTab(&t, CreerElement("vomi", 2)); - InsererVarDansTab(&t, CreerElement("Vomi", 25)); - InsererVarDansTab(&t, CreerElement("_vomi", 20)); - AfficheTableau(t); - printf("\n"); - SupprimerDansTab(&t, "Vomi"); - AfficheTableau(t); - - /* c'est a cause du ouindause qui ferme tout de suite l'exec, - mais moi je veux ce qu'il se passe */ - c = getc(stdin); - return (c); + int c; + _TableauVariable t; + + Initialise(&t); + InsererVarDansTab(&t, CreerElement("yves", 14)); + InsererVarDansTab(&t, CreerElement("vomitif", 8)); + InsererVarDansTab(&t, CreerElement("vomi", 2)); + InsererVarDansTab(&t, CreerElement("Vomi", 25)); + InsererVarDansTab(&t, CreerElement("_vomi", 20)); + AfficheTableau(t); + printf("\n"); + SupprimerDansTab(&t, "Vomi"); + AfficheTableau(t); + + /* c'est a cause du ouindause qui ferme tout de suite l'exec, + mais moi je veux ce qu'il se passe */ + c = getc(stdin); + return (c); } #endif diff --git a/lib/interne.c b/lib/interne.c index a8e1db5..9e59944 100644 --- a/lib/interne.c +++ b/lib/interne.c @@ -10,85 +10,85 @@ void Reset(Uint32 * i) { /* met tous les bits d'un mot à zéro */ - *i &= 0; + *i &= 0; } /* Ok */ void Set(Uint32 * i) { /* met le bit de poids faible à 1 */ - *i &= 1; + *i &= 1; } /* Ok */ /* Met le bit 'position' à zéro */ void ResetBit(Uint32 * i, int position) { - if (position < 0 || position > 31) { - exception(1, _("ResetBit: Incorrect Value")); - } else { - Uint32 aux = VAL_MAX - (1 << position); + if (position < 0 || position > 31) { + exception(1, _("ResetBit: Incorrect Value")); + } else { + Uint32 aux = VAL_MAX - (1 << position); - *i &= aux; - } + *i &= aux; + } } /* Ok */ /* Met le bit 'position' à un */ void SetBit(Uint32 * i, int position) { - if (position < 0 || position > 31) { - exception(1, _("SetBit: Incorrect Value")); - } else { - Uint32 aux = 1 << position; + if (position < 0 || position > 31) { + exception(1, _("SetBit: Incorrect Value")); + } else { + Uint32 aux = 1 << position; - *i |= aux; - } + *i |= aux; + } } /* Ok */ /* Donne la valeur du bit 'position' */ int ValeurBit(Uint32 nombre, int position) { - if (position < 0 || position > 31) { - exception(1, _("ValeurBit: Incorrect Value")); - return (-1); - } - return ((nombre >> position) & 1); + if (position < 0 || position > 31) { + exception(1, _("ValeurBit: Incorrect Value")); + return (-1); + } + return ((nombre >> position) & 1); } /* Ok */ /* Affiche tous les bits d'un mot */ void Uint32toBin(Uint32 i) { - int k; + int k; - for (k = 31; k >= 0; k--) - printf("%d", ValeurBit(i, k)); - printf("\n"); + for (k = 31; k >= 0; k--) + printf("%d", ValeurBit(i, k)); + printf("\n"); } /* Ok */ /* Extrait un champ dans un mot */ Uint32 champ(Uint32 nombre, int taille) { - return (nombre & (taille - 1)); + return (nombre & (taille - 1)); } /* Ok */ Uint32 Opcode(Uint32 ins) { - return (champ(ins >> 0, 256)); + return (champ(ins >> 0, 256)); } /* Ok */ Uint32 Extension(Uint32 ins) { - return (champ(ins >> 8, 64)); + return (champ(ins >> 8, 64)); } /* Ok */ Uint32 Champ1(Uint32 ins) { - return (champ(ins >> 14, 64)); + return (champ(ins >> 14, 64)); } /* Ok */ Uint32 Champ2(Uint32 ins) { - return (champ(ins >> 20, 64)); + return (champ(ins >> 20, 64)); } /* Ok */ Uint32 Champ3(Uint32 ins) { - return (champ(ins >> 26, 64)); + return (champ(ins >> 26, 64)); } /* Ok */ diff --git a/lib/linker.c b/lib/linker.c index e748a2a..c66017c 100644 --- a/lib/linker.c +++ b/lib/linker.c @@ -13,15 +13,15 @@ /* 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; - int objindex; - Uint32 offset; - int type; - struct symbol_t *next; + char *name; + int objindex; + Uint32 offset; + int type; + struct symbol_t *next; } symbol_t; /* Et les variables globales */ @@ -38,322 +38,342 @@ _TableauVariable symbs; static FILE *openfilewriting(char *name) { - FILE *f; + FILE *f; - if (!(f = fopen(name, "wb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error writing file")); - } - return f; + if (!(f = fopen(name, "wb"))) { + pushcontext(strerror(errno)); + exception(1, _("Error writing file")); + } + return f; } static FILE *openfilereading(char *name) { - FILE *f; + FILE *f; - if (!(f = fopen(name, "rb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error reading file")); - } - return f; + if (!(f = fopen(name, "rb"))) { + pushcontext(strerror(errno)); + exception(1, _("Error reading file")); + } + return f; } static void writeword(Uint32 a, FILE * f) { - if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) { - if (ferror(f)) { - pushcontext(strerror(errno)); - } - exception(1, _("Error writing file")); + if (fwrite(&a, sizeof(unsigned long int), 1, f) != 1) { + if (ferror(f)) { + pushcontext(strerror(errno)); } + exception(1, _("Error writing file")); + } } static Uint32 readword(FILE * f) { - Uint32 a; + Uint32 a; - if (fread(&a, sizeof(a), 1, f) != 1) { - exception(1, _("premature end of file")); - } - return a; + if (fread(&a, sizeof(a), 1, f) != 1) { + exception(1, _("premature end of file")); + } + return a; } static char *readstring(FILE * f) { - Uint32 s; - char *r; - int i; - - s = readword(f); - r = (char *) Emalloc(s + 1); - for (i = 0; i < s; i++) { - r[i] = readword(f); - } - r[i] = '\0'; - return r; + Uint32 s; + char *r; + int i; + + s = readword(f); + r = (char *) Emalloc(s + 1); + for (i = 0; i < s; i++) { + r[i] = readword(f); + } + r[i] = '\0'; + return r; } /* Rajoute un symbole dans la pile */ 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 { - InsererVarDansTab(&symbs, CreerElement(name, newsymbol)); - } + 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 { + InsererVarDansTab(&symbs, CreerElement(name, newsymbol)); + } } /* Rajoute un fichier dans les structures */ void addfile(char *nom) { - FILE *f; - Uint32 start, nbsymbols, type, offset; - int i; - char *snom, errctx[BUFSIZ]; - - f = openfilereading(nom); - sprintf(errctx, _("Processing file %s"), nom); - pushcontext(errctx); - - if (readword(f) != 0x4f424e4e) { - exception(1, _("Bad signature")); - } - - readword(f); /* Taille du fichier */ - start = readword(f); - if ((startpoint != -1) && (start != -1)) { - exception(1, _("Startpoint already defined.")); - } - startpoint = start + textsize; - - 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 */ - nbsymbols = readword(f); - - pushcontext(_("Reading symbols")); - for (i = 0; i < nbsymbols; i++) { - type = readword(f); - offset = readword(f); - snom = readstring(f); - addsymbol(snom, offset, type); - } - popcontext(); - - objects[objindex]->textstart = textsize; - objects[objindex]->datastart = datasize; - objects[objindex]->bssstart = bsssize; - - objects[objindex]->text = (Uint32 *) Emalloc(objects[objindex]->s_text * sizeof(Uint32)); - objects[objindex]->data = (Uint32 *) Emalloc(objects[objindex]->s_data * sizeof(Uint32)); - - pushcontext(_("Reading text and data segments")); - for (i = 0; i < objects[objindex]->s_text; i++) { - objects[objindex]->text[i] = readword(f); - } - - for (i = 0; i < objects[objindex]->s_data; i++) { - objects[objindex]->data[i] = readword(f); - } - popcontext(); - fclose(f); - - textsize += objects[objindex]->s_text; - datasize += objects[objindex]->s_data; - bsssize += objects[objindex]->s_bss; - - objindex++; - popcontext(); + FILE *f; + Uint32 start, nbsymbols, type, offset; + int i; + char *snom, errctx[BUFSIZ]; + + f = openfilereading(nom); + sprintf(errctx, _("Processing file %s"), nom); + pushcontext(errctx); + + if (readword(f) != 0x4f424e4e) { + exception(1, _("Bad signature")); + } + + readword(f); /* Taille du fichier */ + start = readword(f); + if ((startpoint != -1) && (start != -1)) { + exception(1, _("Startpoint already defined.")); + } + startpoint = start + textsize; + + 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 */ + nbsymbols = readword(f); + + pushcontext(_("Reading symbols")); + for (i = 0; i < nbsymbols; i++) { + type = readword(f); + offset = readword(f); + snom = readstring(f); + addsymbol(snom, offset, type); + } + popcontext(); + + objects[objindex]->textstart = textsize; + objects[objindex]->datastart = datasize; + objects[objindex]->bssstart = bsssize; + + objects[objindex]->text = + (Uint32 *) Emalloc(objects[objindex]->s_text * sizeof(Uint32)); + objects[objindex]->data = + (Uint32 *) Emalloc(objects[objindex]->s_data * sizeof(Uint32)); + + pushcontext(_("Reading text and data segments")); + for (i = 0; i < objects[objindex]->s_text; i++) { + objects[objindex]->text[i] = readword(f); + } + + for (i = 0; i < objects[objindex]->s_data; i++) { + objects[objindex]->data[i] = readword(f); + } + popcontext(); + fclose(f); + + textsize += objects[objindex]->s_text; + datasize += objects[objindex]->s_data; + bsssize += objects[objindex]->s_bss; + + objindex++; + popcontext(); } /* Simplification de vie... */ static void dumptab(Uint32 * tab, int s, FILE * f) { - int i; + int i; - for (i = 0; i < s; i++) { - writeword(tab[i], f); - } + for (i = 0; i < s; i++) { + writeword(tab[i], f); + } } /* Nous dumpons la mémoire dans le fichier */ static void dumptext(object_t * obj, FILE * f) { - dumptab(obj->text, obj->s_text, f); + dumptab(obj->text, obj->s_text, f); } static void dumpdata(object_t * obj, FILE * f) { - dumptab(obj->data, obj->s_data, 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; - char trouve, err[BUFSIZ]; - Uint32 decal; - - for (s = s->next; s; s = s->next) { - if (s->type & 1) { - t = (symbol_t *) NomVarToVar(s->name, symbs, &trouve); - if (!trouve) { - sprintf(err, _("Symbol %s not found"), s->name); - exception(1, err); - } - switch (s->type) { - case 1: /* text */ - switch (t->type) { - case 0: - decal = objects[t->objindex]->textstart + t->offset; - break; - case 2: - decal = textsize + objects[t->objindex]->datastart + t->offset; - break; - case 4: - decal = textsize + datasize + objects[t->objindex]->bssstart + t->offset; - break; - default: - exception(1, _("Internal error")); - break; - } + symbol_t *s = symbols, *t; + char trouve, err[BUFSIZ]; + Uint32 decal; + + for (s = s->next; s; s = s->next) { + if (s->type & 1) { + t = (symbol_t *) NomVarToVar(s->name, symbs, &trouve); + if (!trouve) { + sprintf(err, _("Symbol %s not found"), s->name); + exception(1, err); + } + switch (s->type) { + case 1: /* text */ + switch (t->type) { + case 0: + decal = objects[t->objindex]->textstart + t->offset; + break; + case 2: + decal = + textsize + objects[t->objindex]->datastart + t->offset; + break; + case 4: + decal = + textsize + datasize + + objects[t->objindex]->bssstart + t->offset; + break; + default: + exception(1, _("Internal error")); + break; + } #ifdef DEBUG - fprintf(stderr, "Relogement effectué sur %i (text), de %i octets pour le symbole %s\n", s->offset, decal, s->name); + fprintf(stderr, + "Relogement effectué sur %i (text), de %i octets pour le symbole %s\n", + s->offset, decal, s->name); #endif - objects[s->objindex]->text[s->offset] += decal; - writeword(objects[s->objindex]->textstart + s->offset, f); - break; - case 3: /* data */ - switch (t->type) { - case 0: - decal = objects[t->objindex]->textstart + t->offset; - break; - case 2: - decal = textsize + objects[t->objindex]->datastart + t->offset; - break; - case 4: - decal = textsize + datasize + objects[t->objindex]->bssstart + t->offset; - break; - default: - exception(1, _("Internal error")); - break; - } + objects[s->objindex]->text[s->offset] += decal; + writeword(objects[s->objindex]->textstart + s->offset, f); + break; + case 3: /* data */ + switch (t->type) { + case 0: + decal = objects[t->objindex]->textstart + t->offset; + break; + case 2: + decal = + textsize + objects[t->objindex]->datastart + t->offset; + break; + case 4: + decal = + textsize + datasize + + objects[t->objindex]->bssstart + t->offset; + break; + default: + exception(1, _("Internal error")); + break; + } #ifdef DEBUG - fprintf(stderr, "Relogement effectué sur %i (data), de %i octets pour le symbole %s\n", s->offset, decal, s->name); + fprintf(stderr, + "Relogement effectué sur %i (data), de %i octets pour le symbole %s\n", + s->offset, decal, s->name); #endif - objects[s->objindex]->data[s->offset] += decal; - writeword(textsize + objects[s->objindex]->datastart + s->offset, f); - break; - default: - exception(1, _("Internal error")); - break; - } - } + objects[s->objindex]->data[s->offset] += decal; + writeword(textsize + + objects[s->objindex]->datastart + s->offset, f); + break; + default: + exception(1, _("Internal error")); + break; + } } + } } /* Cette fonction sert à écrire le fichier de sortie. */ 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); - writeword(startpoint, f); - writeword(textsize, f); - writeword(datasize, f); - writeword(bsssize, f); - writeword(nbrsymbs, f); - popcontext(); - pushcontext(_("Writing relocating informations")); - dumprelog(f); - popcontext(); - pushcontext(_("Writing text segments")); - for (i = 0; i < objindex; i++) { - dumptext(objects[i], f); - } - popcontext(); - pushcontext(_("Writing data segments")); - for (i = 0; i < objindex; i++) { - 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, _("Output file size: %i words containing %i relocating offsets.\n"), ftell(f), nbrsymbs); - fclose(f); + 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); + writeword(startpoint, f); + writeword(textsize, f); + writeword(datasize, f); + writeword(bsssize, f); + writeword(nbrsymbs, f); + popcontext(); + pushcontext(_("Writing relocating informations")); + dumprelog(f); + popcontext(); + pushcontext(_("Writing text segments")); + for (i = 0; i < objindex; i++) { + dumptext(objects[i], f); + } + popcontext(); + pushcontext(_("Writing data segments")); + for (i = 0; i < objindex; i++) { + 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, + _ + ("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) { - int i; + int i; - Initialise(&symbs); + Initialise(&symbs); - objects = (object_t **) Emalloc(n * sizeof(object_t *)); - psymbols = symbols = (symbol_t *) Emalloc(sizeof(symbol_t)); + 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]->text = objects[i]->data = NULL; - } + 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]->text = objects[i]->data = NULL; + } - symbols->next = NULL; + symbols->next = NULL; } void free_symbol(symbol_t * s) { - if (s->next) - free_symbol(s); + if (s->next) + free_symbol(s); - free(s->name); - free(s); + free(s->name); + free(s); } 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); - free(objects[i]); - } - free(objects); + 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); + free(objects[i]); + } + free(objects); } diff --git a/lib/memoire.c b/lib/memoire.c index e38f0d7..067f8f8 100644 --- a/lib/memoire.c +++ b/lib/memoire.c @@ -25,113 +25,113 @@ Uint32 *memoire_principale; void InitMemoire(void) { - memoire_principale = (Uint32 *) Emalloc(TAILLE_MEMOIRE * sizeof(Uint32)); + memoire_principale = (Uint32 *) Emalloc(TAILLE_MEMOIRE * sizeof(Uint32)); } void FlushMemoire(void) { - free(memoire_principale); + free(memoire_principale); } /* Lit le mot qui se trouve à l'offset 'offset' en mémoire */ Uint32 LD(Uint32 offset) { - Uint32 i; + Uint32 i; - switch (offset) { - case 0xffffff00: - initterm(); - i = !feof(input); - clearterm(); - return (i); - case 0xffffff01: - initterm(); - i = fgetc(input); - clearterm(); - break; - case 0xffffff05: - fscanf(input, "%i", &i); - break; - default: - if (offset >= TAILLE_MEMOIRE) { - exception(1, _("Invalid Memory Adress")); - } else { - i = memoire_principale[offset]; - } - break; + switch (offset) { + case 0xffffff00: + initterm(); + i = !feof(input); + clearterm(); + return (i); + case 0xffffff01: + initterm(); + i = fgetc(input); + clearterm(); + break; + case 0xffffff05: + fscanf(input, "%i", &i); + break; + default: + if (offset >= TAILLE_MEMOIRE) { + exception(1, _("Invalid Memory Adress")); + } else { + i = memoire_principale[offset]; } - return i; + break; + } + return i; } static void copychaine(Uint32 * t, char *s) { - while (*s) { - *(t++) = *(s++); - } + while (*s) { + *(t++) = *(s++); + } } static void litchaine(char *s, Uint32 * t) { - while (*t) { - *(s++) = *(t++); - } - *s = 0; + while (*t) { + *(s++) = *(t++); + } + *s = 0; } static void AfficheBinaire(Uint32 valeur) { - if (valeur) { - AfficheBinaire(valeur >> 1); - fprintf(stdout, "%i", valeur & 1); - } + if (valeur) { + AfficheBinaire(valeur >> 1); + fprintf(stdout, "%i", valeur & 1); + } } /* Ecrit le mot 'valeur' à l'offset 'offset' en mémoire */ void ST(Uint32 offset, Uint32 valeur) { - Uint32 oldOC; + Uint32 oldOC; - switch (offset) { - case 0xffffff02: - fgets(temp, BUFSIZ, stdin); - copychaine(&memoire_principale[valeur], temp); - break; - case 0xffffff03: - fprintf(stdout, "%c", valeur); - break; - case 0xffffff04: - litchaine(temp, &memoire_principale[valeur]); - fprintf(stdout, "%s", temp); - break; - case 0xffffff06: - fprintf(stdout, "%i", valeur); - break; - case 0xffffff07: - fprintf(stdout, "%x", valeur); - break; - case 0xffffff08: - fprintf(stdout, "%o", valeur); - break; - case 0xffffff09: - if (valeur) { - AfficheBinaire(valeur); - } else { - fprintf(stdout, "0"); - } - break; - case 0xffffff0a: - litchaine(temp, &memoire_principale[valeur]); - oldOC = LireRegistrePC(); - ChargeBinaire(temp); - EcrireRegistrePC(oldOC); - HasToRun = 1; - break; - default: - if (offset < 0 || offset >= TAILLE_MEMOIRE) - exception(1, _("Invalid Memory Adress")); - else - memoire_principale[offset] = valeur; - break; + switch (offset) { + case 0xffffff02: + fgets(temp, BUFSIZ, stdin); + copychaine(&memoire_principale[valeur], temp); + break; + case 0xffffff03: + fprintf(stdout, "%c", valeur); + break; + case 0xffffff04: + litchaine(temp, &memoire_principale[valeur]); + fprintf(stdout, "%s", temp); + break; + case 0xffffff06: + fprintf(stdout, "%i", valeur); + break; + case 0xffffff07: + fprintf(stdout, "%x", valeur); + break; + case 0xffffff08: + fprintf(stdout, "%o", valeur); + break; + case 0xffffff09: + if (valeur) { + AfficheBinaire(valeur); + } else { + fprintf(stdout, "0"); } - fflush(stdout); + break; + case 0xffffff0a: + litchaine(temp, &memoire_principale[valeur]); + oldOC = LireRegistrePC(); + ChargeBinaire(temp); + EcrireRegistrePC(oldOC); + HasToRun = 1; + break; + default: + if (offset < 0 || offset >= TAILLE_MEMOIRE) + exception(1, _("Invalid Memory Adress")); + else + memoire_principale[offset] = valeur; + break; + } + fflush(stdout); } diff --git a/lib/meta.c b/lib/meta.c index eb227d3..4f71ad7 100644 --- a/lib/meta.c +++ b/lib/meta.c @@ -19,8 +19,12 @@ \*************************/ static char meta_ops[] = ":.;(){}[=, "; -static char meta_firsts[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; -static char meta_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"; +static char meta_firsts[] = + + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; +static char meta_chars[] = + + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"; phon_t *phons = NULL; field_t *fields = NULL; @@ -31,30 +35,31 @@ instruct_t *instructs = NULL; static int isident(char *word) { - if (!strchr(meta_firsts, *word)) { - return 0; - } - while (*word) { - if (!strchr(meta_chars, *(word++))) { - return 0; - } + if (!strchr(meta_firsts, *word)) { + return 0; + } + while (*word) { + if (!strchr(meta_chars, *(word++))) { + return 0; } - return 1; + } + return 1; } /* La fonction getword pour le meta parser. Beaucoup plus simple que le parser normal. */ static char *getword(char *line, char *p) { - char o; + char o; - do { - o = *(p++) = *line; - line++; - } while ((*line) && (!strchr(meta_ops, o)) && (!strchr(meta_ops, *line))); - *p = '\0'; + do { + o = *(p++) = *line; + line++; + } while ((*line) && (!strchr(meta_ops, o)) + && (!strchr(meta_ops, *line))); + *p = '\0'; - return line; + return line; } /* La terrible fonction meta_parse_line. Elle lit une chaîne correspondant à une ligne du fichier texte d'entrée. @@ -63,481 +68,510 @@ d void meta_parse_line(char *line) { - char buffer[BUFSIZ], *first = NULL, m = 0, *p1 = NULL, *p2 = NULL, *Fnames[MAXF], *Fimplicits[MAXF], *Snames[MAXF], *Sinames[MAXF], gotname = 0, goteoi = 0, errbuff[BUFSIZ]; - phon_t *phon = NULL; - field_t *field = NULL; - pattern_t *pattern = NULL; - instruct_t *instruct = NULL; - metaexpr_t *metaexpr = NULL, *tmetaexpr, *tabmetaexpr[MAXM]; - int Fsizes[MAXF], Fvalues[MAXF], Itypes[MAXF], Etypes[MAXF], nbfields = 0, valid, i, nbimplicit = 0; - - if (*line == '#') { - return; - } - - sprintf(errbuff, _("Read line '%s'"), line); + char buffer[BUFSIZ], *first = NULL, m = 0, *p1 = NULL, *p2 = + NULL, *Fnames[MAXF], *Fimplicits[MAXF], *Snames[MAXF], + *Sinames[MAXF], gotname = 0, goteoi = 0, errbuff[BUFSIZ]; + phon_t *phon = NULL; + field_t *field = NULL; + pattern_t *pattern = NULL; + instruct_t *instruct = NULL; + metaexpr_t *metaexpr = NULL, *tmetaexpr, *tabmetaexpr[MAXM]; + int Fsizes[MAXF], Fvalues[MAXF], Itypes[MAXF], Etypes[MAXF], nbfields = + 0, valid, i, nbimplicit = 0; + + if (*line == '#') { + return; + } + + sprintf(errbuff, _("Read line '%s'"), line); + pushcontext(errbuff); + + while (*line) { + line = getword(line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); pushcontext(errbuff); + if (!m) { + /* Premier mot lut de la chaîne */ + if (*line != ':') { + exception(1, _("Missing operator ':'")); + } + line++; + switch (buffer[0]) { + case 'F': + first = Estrdup(&buffer[1]); + m = 1; + break; + case 'P': + first = Estrdup(&buffer[1]); + pattern = (pattern_t *) + Emalloc(sizeof(pattern_t)); + pattern->name = first; + pattern->next = NULL; + m = 2; + break; + case 'p': + m = 3; + break; + case 'I': + m = 4; + break; + } + } else { + /* Nous avons lut le premier mot, nous savons ce qu'il faut faire. */ + switch (m) { + case 1: /* Champ */ + if (gotname) { + Fsizes[nbfields++] = char_to_number(buffer, &valid); + if (!valid) { + exception(1, _("Invalid number.")); + } + if (*line) { + if (*line != ';') { + exception(1, + _("Expecting ';' for field separator.")); + } + line++; + gotname = 0; + } else { + field = (field_t *) + Emalloc(sizeof(field_t)); + field->next = fields->next; + fields->next = field; + field->name = first; + field->names = (char **) + Emalloc(nbfields * sizeof(char *)); + + field->sizes = (int *) Emalloc(nbfields * sizeof(int)); + + for (i = 0; i < nbfields; i++) { + field->names[i] = Fnames[i]; + field->sizes[i] = Fsizes[i]; + } + field->nbr = nbfields; + } + } else { + if (*line != ',') { + exception(1, _("Expecting ',' for field separator.")); + } + line++; + Fnames[nbfields] = Estrdup(buffer); + gotname = 1; + } + break; + case 2: /* Pattern */ + if (*buffer == '[') { + tmetaexpr = (metaexpr_t *) + Emalloc(sizeof(metaexpr_t)); + tmetaexpr->name = NULL; + tmetaexpr->string = NULL; + tmetaexpr->left = metaexpr; + metaexpr = tmetaexpr; + } else { + tmetaexpr = (metaexpr_t *) + Emalloc(sizeof(metaexpr_t)); + tmetaexpr->left = tmetaexpr->right = NULL; + tmetaexpr->type = 0; + if (*buffer == '.') { + tmetaexpr->type = 1; + popcontext(); + line = getword(line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + } + if (!isident(buffer)) { + exception(1, _("Identifier incorrect.")); + } + tmetaexpr->name = Estrdup(buffer); + if (*line == '=') { + if (*(++line) != '.') { + exception(1, _("Error: Expecting a . after a =")); + } + popcontext(); + line = getword(++line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + if (!isident(buffer)) { + exception(1, _("Identifier incorrect.")); + } + tmetaexpr->string = Estrdup(buffer); + } else { + tmetaexpr->string = NULL; + } + + if (metaexpr) { + metaexpr->right = tmetaexpr; + } else { + metaexpr = tmetaexpr; + } + if (*line == ';' || !*line) { + tabmetaexpr[nbfields++] = metaexpr; + if (*line) + line++; + metaexpr = NULL; + } + if (!*line) { + pattern = (pattern_t *) + Emalloc(sizeof(pattern_t)); + pattern->next = patterns->next; + patterns->next = pattern; + pattern->name = first; + pattern->expr = (metaexpr_t **) + Emalloc(nbfields * sizeof(metaexpr_t *)); + for (i = 0; i < nbfields; i++) { + pattern->expr[i] = tabmetaexpr[i]; + } + pattern->nbr = nbfields; + } + } + break; + case 3: /* Phonème */ + if (p1) { + if (p2) { + exception(1, _("Extra parameters for field 'p'.")); + } else { + if (isident(buffer)) { + p2 = Estrdup(buffer); + } else { + exception(1, _("Identifier incorrect.")); + } + if (*line) { + exception(1, _("Unexpected char at end of line.")); + } - while (*line) { - line = getword(line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - if (!m) { - /* Premier mot lut de la chaîne */ - if (*line != ':') { - exception(1, _("Missing operator ':'")); + phon = (phon_t *) + Emalloc(sizeof(phon_t)); + phon->next = phons->next; + phons->next = phon; + phon->p1 = p1; + phon->p2 = p2; + } + } else { + if (isident(buffer)) { + p1 = Estrdup(buffer); + if (*line != '=') { + exception(1, + _ + ("Expecting operator '=' for field 'p'.")); } line++; - switch (buffer[0]) { - case 'F': - first = Estrdup(&buffer[1]); - m = 1; - break; - case 'P': - first = Estrdup(&buffer[1]); - pattern = (pattern_t *) Emalloc(sizeof(pattern_t)); - pattern->name = first; - pattern->next = NULL; - m = 2; - break; - case 'p': - m = 3; - break; - case 'I': - m = 4; - break; + } else { + exception(1, _("Identifier incorrect.")); + } + } + break; + case 4: /* Instruction */ + if (!goteoi) { + if (isident(buffer) || (*buffer == '.')) { + Etypes[nbfields] = 0; + if (*buffer == '.') { + Etypes[nbfields] = 1; + popcontext(); + line = getword(line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + } + Fnames[nbfields] = Estrdup(buffer); + if (*line == '=') { + if (*(++line) != '.') { + exception(1, _("Error: character . expected.")); + } + popcontext(); + line = getword(++line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + if (!isident(buffer)) { + exception(1, _("Identifier incorrect.")); + } + Snames[nbfields] = Estrdup(buffer); + } else { + Snames[nbfields] = NULL; + } + nbfields++; + if (*line == ';') { + goteoi = 1; + line++; } + } } else { - /* Nous avons lut le premier mot, nous savons ce qu'il faut faire. */ - switch (m) { - case 1: /* Champ */ - if (gotname) { - Fsizes[nbfields++] = char_to_number(buffer, &valid); - if (!valid) { - exception(1, _("Invalid number.")); - } - if (*line) { - if (*line != ';') { - exception(1, _("Expecting ';' for field separator.")); - } - line++; - gotname = 0; - } else { - field = (field_t *) Emalloc(sizeof(field_t)); - field->next = fields->next; - fields->next = field; - field->name = first; - field->names = (char **) Emalloc(nbfields * sizeof(char *)); - field->sizes = (int *) Emalloc(nbfields * sizeof(int)); - - for (i = 0; i < nbfields; i++) { - field->names[i] = Fnames[i]; - field->sizes[i] = Fsizes[i]; - } - field->nbr = nbfields; - } - } else { - if (*line != ',') { - exception(1, _("Expecting ',' for field separator.")); - } - line++; - Fnames[nbfields] = Estrdup(buffer); - gotname = 1; - } - break; - case 2: /* Pattern */ - if (*buffer == '[') { - tmetaexpr = (metaexpr_t *) Emalloc(sizeof(metaexpr_t)); - tmetaexpr->name = NULL; - tmetaexpr->string = NULL; - tmetaexpr->left = metaexpr; - metaexpr = tmetaexpr; - } else { - tmetaexpr = (metaexpr_t *) Emalloc(sizeof(metaexpr_t)); - tmetaexpr->left = tmetaexpr->right = NULL; - tmetaexpr->type = 0; - if (*buffer == '.') { - tmetaexpr->type = 1; - popcontext(); - line = getword(line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - } - if (!isident(buffer)) { - exception(1, _("Identifier incorrect.")); - } - tmetaexpr->name = Estrdup(buffer); - if (*line == '=') { - if (*(++line) != '.') { - exception(1, _("Error: Expecting a . after a =")); - } - popcontext(); - line = getword(++line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - if (!isident(buffer)) { - exception(1, _("Identifier incorrect.")); - } - tmetaexpr->string = Estrdup(buffer); - } else { - tmetaexpr->string = NULL; - } - - if (metaexpr) { - metaexpr->right = tmetaexpr; - } else { - metaexpr = tmetaexpr; - } - if (*line == ';' || !*line) { - tabmetaexpr[nbfields++] = metaexpr; - if (*line) - line++; - metaexpr = NULL; - } - if (!*line) { - pattern = (pattern_t *) Emalloc(sizeof(pattern_t)); - pattern->next = patterns->next; - patterns->next = pattern; - pattern->name = first; - pattern->expr = (metaexpr_t **) Emalloc(nbfields * sizeof(metaexpr_t *)); - for (i = 0; i < nbfields; i++) { - pattern->expr[i] = tabmetaexpr[i]; - } - pattern->nbr = nbfields; - } - } - break; - case 3: /* Phonème */ - if (p1) { - if (p2) { - exception(1, _("Extra parameters for field 'p'.")); - } else { - if (isident(buffer)) { - p2 = Estrdup(buffer); - } else { - exception(1, _("Identifier incorrect.")); - } - if (*line) { - exception(1, _("Unexpected char at end of line.")); - } - - phon = (phon_t *) Emalloc(sizeof(phon_t)); - phon->next = phons->next; - phons->next = phon; - phon->p1 = p1; - phon->p2 = p2; - } - } else { - if (isident(buffer)) { - p1 = Estrdup(buffer); - if (*line != '=') { - exception(1, _("Expecting operator '=' for field 'p'.")); - } - line++; - } else { - exception(1, _("Identifier incorrect.")); - } - } - break; - case 4: /* Instruction */ - if (!goteoi) { - if (isident(buffer) || (*buffer == '.')) { - Etypes[nbfields] = 0; - if (*buffer == '.') { - Etypes[nbfields] = 1; - popcontext(); - line = getword(line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - } - Fnames[nbfields] = Estrdup(buffer); - if (*line == '=') { - if (*(++line) != '.') { - exception(1, _("Error: character . expected.")); - } - popcontext(); - line = getword(++line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - if (!isident(buffer)) { - exception(1, _("Identifier incorrect.")); - } - Snames[nbfields] = Estrdup(buffer); - } else { - Snames[nbfields] = NULL; - } - nbfields++; - if (*line == ';') { - goteoi = 1; - line++; - } - } - } else { - if (!isident(buffer)) { - exception(1, _("Identifier incorrect.")); - } - Fimplicits[nbimplicit] = Estrdup(buffer); - if (*line != '=') { - exception(1, _("= expected after an implicit name")); - } - popcontext(); - line = getword(++line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - - if (*buffer == '.') { - Itypes[nbimplicit] = 1; - popcontext(); - line = getword(line, buffer); - sprintf(errbuff, _("Analysing word '%s'"), buffer); - pushcontext(errbuff); - if (!isident(buffer)) { - exception(1, _("Identifier incorrect")); - } - Sinames[nbimplicit] = Estrdup(buffer); - } else { - Sinames[nbimplicit] = NULL; - Itypes[nbimplicit] = 2; - Fvalues[nbimplicit] = char_to_number(buffer, &valid); - if (!valid) { - if (!isident(buffer)) { - exception(1, _("Identifier incorrect")); - } - Sinames[nbimplicit] = Estrdup(buffer); - Itypes[nbimplicit] = 0; - } - } - nbimplicit++; - - if (*line) { - if (*line != ';') { - exception(1, _("expecting ; as field separator")); - } - line++; - } else { - instruct = (instruct_t *) Emalloc(sizeof(instruct_t)); - instruct->next = instructs->next; - instructs->next = instruct; - instruct->names = (char **) Emalloc(nbfields * sizeof(char *)); - instruct->strings = (char **) Emalloc(nbfields * sizeof(char *)); - instruct->etypes = (int *) Emalloc(nbfields * sizeof(int)); - instruct->implicits = (char **) Emalloc(nbimplicit * sizeof(char *)); - instruct->ivalues = (int *) Emalloc(nbimplicit * sizeof(int)); - instruct->istrings = (char **) Emalloc(nbimplicit * sizeof(char *)); - instruct->itypes = (int *) Emalloc(nbimplicit * sizeof(int)); - - for (i = 0; i < nbfields; i++) { - instruct->names[i] = Fnames[i]; - instruct->strings[i] = Snames[i]; - instruct->etypes[i] = Etypes[i]; - } - instruct->nbexplicit = nbfields; - for (i = 0; i < nbimplicit; i++) { - instruct->implicits[i] = Fimplicits[i]; - instruct->ivalues[i] = Fvalues[i]; - instruct->istrings[i] = Sinames[i]; - instruct->itypes[i] = Itypes[i]; - } - instruct->nbimplicit = nbimplicit; - goteoi = 0; - } - } - break; + if (!isident(buffer)) { + exception(1, _("Identifier incorrect.")); + } + Fimplicits[nbimplicit] = Estrdup(buffer); + if (*line != '=') { + exception(1, _("= expected after an implicit name")); + } + popcontext(); + line = getword(++line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + + if (*buffer == '.') { + Itypes[nbimplicit] = 1; + popcontext(); + line = getword(line, buffer); + sprintf(errbuff, _("Analysing word '%s'"), buffer); + pushcontext(errbuff); + if (!isident(buffer)) { + exception(1, _("Identifier incorrect")); } + Sinames[nbimplicit] = Estrdup(buffer); + } else { + Sinames[nbimplicit] = NULL; + Itypes[nbimplicit] = 2; + Fvalues[nbimplicit] = char_to_number(buffer, &valid); + if (!valid) { + if (!isident(buffer)) { + exception(1, _("Identifier incorrect")); + } + Sinames[nbimplicit] = Estrdup(buffer); + Itypes[nbimplicit] = 0; + } + } + nbimplicit++; + + if (*line) { + if (*line != ';') { + exception(1, _("expecting ; as field separator")); + } + line++; + } else { + instruct = (instruct_t *) + Emalloc(sizeof(instruct_t)); + instruct->next = instructs->next; + instructs->next = instruct; + instruct->names = (char **) + Emalloc(nbfields * sizeof(char *)); + + instruct->strings = (char **) + Emalloc(nbfields * sizeof(char *)); + + instruct->etypes = + + (int *) Emalloc(nbfields * sizeof(int)); + instruct->implicits = (char **) + Emalloc(nbimplicit * sizeof(char *)); + + instruct->ivalues = (int *) + Emalloc(nbimplicit * sizeof(int)); + + instruct->istrings = (char **) + Emalloc(nbimplicit * sizeof(char *)); + + instruct->itypes = (int *) + Emalloc(nbimplicit * sizeof(int)); + + for (i = 0; i < nbfields; i++) { + instruct->names[i] = Fnames[i]; + instruct->strings[i] = Snames[i]; + instruct->etypes[i] = Etypes[i]; + } + instruct->nbexplicit = nbfields; + for (i = 0; i < nbimplicit; i++) { + instruct->implicits[i] = Fimplicits[i]; + instruct->ivalues[i] = Fvalues[i]; + instruct->istrings[i] = Sinames[i]; + instruct->itypes[i] = Itypes[i]; + } + instruct->nbimplicit = nbimplicit; + goteoi = 0; + } } - popcontext(); + break; + } } - popcontext(); + } + + popcontext(); } /* Initialiseur et destructeur du meta parser */ int meta_init(void) { - if (!(phons = (phon_t *) malloc(sizeof(phon_t)))) { - return -1; - } - phons->p1 = phons->p2 = NULL; - phons->next = NULL; - - if (!(fields = (field_t *) malloc(sizeof(field_t)))) { - return -1; - } - fields->name = NULL; - fields->names = NULL; - fields->sizes = NULL; - fields->next = NULL; - - if (!(patterns = (pattern_t *) malloc(sizeof(pattern_t)))) { - return -1; - } - patterns->name = NULL; - patterns->expr = NULL; - patterns->next = NULL; - - if (!(instructs = (instruct_t *) malloc(sizeof(instruct_t)))) { - return -1; - } - instructs->names = NULL; - instructs->strings = NULL; - instructs->etypes = NULL; - instructs->implicits = NULL; - instructs->ivalues = NULL; - instructs->istrings = NULL; - instructs->itypes = NULL; - - return 0; + if (!(phons = (phon_t *) malloc(sizeof(phon_t)))) { + return -1; + } + phons->p1 = phons->p2 = NULL; + phons->next = NULL; + + if (!(fields = (field_t *) malloc(sizeof(field_t)))) { + return -1; + } + fields->name = NULL; + fields->names = NULL; + fields->sizes = NULL; + fields->next = NULL; + + if (!(patterns = (pattern_t *) malloc(sizeof(pattern_t)))) { + return -1; + } + patterns->name = NULL; + patterns->expr = NULL; + patterns->next = NULL; + + if (!(instructs = (instruct_t *) malloc(sizeof(instruct_t)))) { + return -1; + } + instructs->names = NULL; + instructs->strings = NULL; + instructs->etypes = NULL; + instructs->implicits = NULL; + instructs->ivalues = NULL; + instructs->istrings = NULL; + instructs->itypes = NULL; + + return 0; } static void recurs_free_phon(phon_t * phon) { - if (phon->next) { - recurs_free_phon(phon->next); - } + if (phon->next) { + recurs_free_phon(phon->next); + } - free(phon->p1); - free(phon->p2); - free(phon); + free(phon->p1); + free(phon->p2); + free(phon); } static void recurs_free_field(field_t * field) { - int i; - - if (field->next) { - recurs_free_field(field->next); - } - - free(field->name); - for (i = 0; i < field->nbr; i++) { - free(field->names[i]); - } - free(field->names); - free(field->sizes); - free(field); + int i; + + if (field->next) { + recurs_free_field(field->next); + } + + free(field->name); + for (i = 0; i < field->nbr; i++) { + free(field->names[i]); + } + free(field->names); + free(field->sizes); + free(field); } static void recurs_free_metaexpr(metaexpr_t * metaexpr) { - if (metaexpr->left) { - recurs_free_metaexpr(metaexpr->left); - } + if (metaexpr->left) { + recurs_free_metaexpr(metaexpr->left); + } - if (metaexpr->right) { - recurs_free_metaexpr(metaexpr->right); - } + if (metaexpr->right) { + recurs_free_metaexpr(metaexpr->right); + } - if (metaexpr->name) { - free(metaexpr->name); - } + if (metaexpr->name) { + free(metaexpr->name); + } - if (metaexpr->string) { - free(metaexpr->string); - } + if (metaexpr->string) { + free(metaexpr->string); + } - free(metaexpr); + free(metaexpr); } static void recurs_free_pattern(pattern_t * pattern) { - int i; - - if (pattern->next) { - recurs_free_pattern(pattern->next); - } - - free(pattern->name); - for (i = 0; i < pattern->nbr; i++) { - recurs_free_metaexpr(pattern->expr[i]); - } - free(pattern->expr); - free(pattern); + int i; + + if (pattern->next) { + recurs_free_pattern(pattern->next); + } + + free(pattern->name); + for (i = 0; i < pattern->nbr; i++) { + recurs_free_metaexpr(pattern->expr[i]); + } + free(pattern->expr); + free(pattern); } static void recurs_free_instruct(instruct_t * instruct) { - int i; - - if (instruct->next) { - recurs_free_instruct(instruct->next); - } + int i; - for (i = 0; i < instruct->nbexplicit; i++) { - free(instruct->names[i]); - if (instruct->strings[i]) { - free(instruct->strings[i]); - } - } + if (instruct->next) { + recurs_free_instruct(instruct->next); + } - for (i = 0; i < instruct->nbimplicit; i++) { - free(instruct->implicits[i]); - free(instruct->istrings[i]); + for (i = 0; i < instruct->nbexplicit; i++) { + free(instruct->names[i]); + if (instruct->strings[i]) { + free(instruct->strings[i]); } - - free(instruct->names); - free(instruct->strings); - free(instruct->etypes); - free(instruct->implicits); - free(instruct->istrings); - free(instruct->itypes); - free(instruct); + } + + for (i = 0; i < instruct->nbimplicit; i++) { + free(instruct->implicits[i]); + free(instruct->istrings[i]); + } + + free(instruct->names); + free(instruct->strings); + free(instruct->etypes); + free(instruct->implicits); + free(instruct->istrings); + free(instruct->itypes); + free(instruct); } void meta_flush(void) { - if (phons->next) - recurs_free_phon(phons->next); - if (fields->next) - recurs_free_field(fields->next); - if (patterns->next) - recurs_free_pattern(patterns->next); - if (instructs->next) - recurs_free_instruct(instructs->next); - free(phons); - free(fields); - free(patterns); - free(instructs); - phons = NULL; - fields = NULL; - patterns = NULL; - instructs = NULL; + if (phons->next) + recurs_free_phon(phons->next); + if (fields->next) + recurs_free_field(fields->next); + if (patterns->next) + recurs_free_pattern(patterns->next); + if (instructs->next) + recurs_free_instruct(instructs->next); + free(phons); + free(fields); + free(patterns); + free(instructs); + phons = NULL; + fields = NULL; + patterns = NULL; + instructs = NULL; } int meta_load(char *n) { - FILE *f; - char buf[BUFSIZ], errctx[BUFSIZ], *p; - int i = 0; + FILE *f; + char buf[BUFSIZ], errctx[BUFSIZ], *p; + int i = 0; - pushcontext(_("Loading meta file")); - sprintf(errctx, _("Opening file '%s'"), n); - pushcontext(errctx); + pushcontext(_("Loading meta file")); + sprintf(errctx, _("Opening file '%s'"), n); + pushcontext(errctx); - if (!(f = fopen(n, "r"))) { - pushcontext(strerror(errno)); - return 1; + if (!(f = fopen(n, "r"))) { + pushcontext(strerror(errno)); + return 1; + } + popcontext(); + pushcontext(_("Reading file")); + while (fgets(buf, BUFSIZ, f)) { + sprintf(errctx, _("Reading line %i"), ++i); + pushcontext(errctx); + if ((p = strchr(buf, '\r'))) { + *p = '\0'; } - popcontext(); - pushcontext(_("Reading file")); - while (fgets(buf, BUFSIZ, f)) { - sprintf(errctx, _("Reading line %i"), ++i); - pushcontext(errctx); - if ((p = strchr(buf, '\r'))) { - *p = '\0'; - } - if ((p = strchr(buf, '\n'))) { - *p = '\0'; - } - meta_parse_line(buf); - popcontext(); + if ((p = strchr(buf, '\n'))) { + *p = '\0'; } + meta_parse_line(buf); popcontext(); - popcontext(); + } + popcontext(); + popcontext(); - fclose(f); - return 0; + fclose(f); + return 0; } #ifndef HAVE_CONFIG_H @@ -546,74 +580,104 @@ int meta_load(char *n) void main(void) { - phon_t *phon; - field_t *field; - pattern_t *pattern; - instruct_t *instruct; - int i; - - if (meta_init()) - exception(1, _("Meta parser init failed.")); - - meta_load("instructions.txt"); - - fprintf(stderr, "\nListe des phonèmes:\n"); - for (phon = phons->next; phon; phon = phon->next) { - fprintf(stderr, " o %s <===> %s\n", phon->p1, phon->p2); + phon_t *phon; + field_t *field; + pattern_t *pattern; + instruct_t *instruct; + int i; + + if (meta_init()) + exception(1, _("Meta parser init failed.")); + + meta_load("instructions.txt"); + + fprintf(stderr, "\nListe des phonèmes:\n"); + for (phon = phons->next; phon; phon = phon->next) { + fprintf(stderr, " o %s <===> %s\n", phon->p1, phon->p2); + } + + fprintf(stderr, "\nListe des champs:\n"); + for (field = fields->next; field; field = field->next) { + fprintf(stderr, " o Champ nommé %s, contenant %i parties:\n", + field->name, field->nbr); + for (i = 0; i < field->nbr; i++) { + fprintf(stderr, " + %s (%i bits)\n", field->names[i], + field->sizes[i]); } + } - fprintf(stderr, "\nListe des champs:\n"); - for (field = fields->next; field; field = field->next) { - fprintf(stderr, " o Champ nommé %s, contenant %i parties:\n", field->name, field->nbr); - for (i = 0; i < field->nbr; i++) { - fprintf(stderr, " + %s (%i bits)\n", field->names[i], field->sizes[i]); - } + fprintf(stderr, "\nListe des patterns:\n"); + for (pattern = patterns->next; pattern; pattern = pattern->next) { + fprintf(stderr, + " o Pattern nommée %s, contenant %i metaexpressions:\n", + pattern->name, pattern->nbr); + for (i = 0; i < pattern->nbr; i++) { + 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"); + 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->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->type ? "Constante prédéfinie" : "Feuille"); + } } - - fprintf(stderr, "\nListe des patterns:\n"); - for (pattern = patterns->next; pattern; pattern = pattern->next) { - fprintf(stderr, " o Pattern nommée %s, contenant %i metaexpressions:\n", pattern->name, pattern->nbr); - for (i = 0; i < pattern->nbr; i++) { - 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"); - 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->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->type ? "Constante prédéfinie" : "Feuille"); - } - } + } + + fprintf(stderr, "\nListe des instructions:\n"); + for (instruct = instructs->next; instruct; instruct = instruct->next) { + fprintf(stderr, + " o Instruction contenant %i champs explicites et %i champs implicites.\n", + instruct->nbexplicit, instruct->nbimplicit); + fprintf(stderr, " => Champs explicites.\n"); + for (i = 0; i < instruct->nbexplicit; i++) { + fprintf(stderr, " + %s <= %s (type %s)\n", + instruct->names[i], + instruct-> + strings[i] ? instruct->strings[i] : + "Pas de chaîne associée", + instruct->etypes[i] ? "prédéfinit" : "direct"); } - - fprintf(stderr, "\nListe des instructions:\n"); - for (instruct = instructs->next; instruct; instruct = instruct->next) { - fprintf(stderr, " o Instruction contenant %i champs explicites et %i champs implicites.\n", instruct->nbexplicit, instruct->nbimplicit); - fprintf(stderr, " => Champs explicites.\n"); - for (i = 0; i < instruct->nbexplicit; i++) { - fprintf(stderr, " + %s <= %s (type %s)\n", - instruct->names[i], instruct->strings[i] ? instruct->strings[i] : "Pas de chaîne associée", instruct->etypes[i] ? "prédéfinit" : "direct"); - } - fprintf(stderr, " => Champs implicites.\n"); - for (i = 0; i < instruct->nbimplicit; i++) { - switch (instruct->itypes[i]) { - case 0: - fprintf(stderr, " + %s <= %s (type direct)\n", instruct->implicits[i], instruct->istrings[i]); - break; - case 1: - fprintf(stderr, " + %s <= %s (type prédéfinit)\n", instruct->implicits[i], instruct->istrings[i]); - break; - case 2: - fprintf(stderr, " + %s <= %i (type valeur)\n", instruct->implicits[i], instruct->ivalues[i]); - break; - } - } + fprintf(stderr, " => Champs implicites.\n"); + for (i = 0; i < instruct->nbimplicit; i++) { + switch (instruct->itypes[i]) { + case 0: + fprintf(stderr, + " + %s <= %s (type direct)\n", + instruct->implicits[i], instruct->istrings[i]); + break; + case 1: + fprintf(stderr, + " + %s <= %s (type prédéfinit)\n", + instruct->implicits[i], instruct->istrings[i]); + break; + case 2: + fprintf(stderr, + " + %s <= %i (type valeur)\n", + instruct->implicits[i], instruct->ivalues[i]); + break; + } } + } } #endif diff --git a/lib/numbers.c b/lib/numbers.c index 9f032b5..02cb536 100644 --- a/lib/numbers.c +++ b/lib/numbers.c @@ -6,57 +6,58 @@ en octal pr int char_to_number(char *st, int *valid) { - int whattype = 0, result = 0; + int whattype = 0, result = 0; - *valid = 0; + *valid = 0; - if (*st == '0') { - st++; - if (*st == 'x') { - whattype = 1; - st++; - } else if (*st) { - whattype = 2; - } else { - *valid = 1; - return 0; - } + if (*st == '0') { + st++; + if (*st == 'x') { + whattype = 1; + st++; + } else if (*st) { + whattype = 2; + } else { + *valid = 1; + return 0; } + } - while (*st) { - switch (whattype) { - case 0: - if ((*st < '0') || (*st > '9')) { - return 0; - } - result *= 10; - result += *st - '0'; - break; - case 1: - if (((*st < '0') || (*st > '9')) && ((*st < 'A') || (*st > 'F')) - && ((*st < 'a') || (*st > 'f'))) { - return 0; - } - result *= 16; - if ((*st >= '0') && (*st <= '9')) { - result += *st - '0'; - } else if ((*st >= 'A') && (*st <= 'F')) { - result += *st - 'A' + 10; - } else { - result += *st - 'a' + 10; - } - break; - case 2: - if ((*st < '0') || (*st > '7')) { - return 0; - } - result *= 8; - result += *st - '0'; - break; - } - st++; + while (*st) { + switch (whattype) { + case 0: + if ((*st < '0') || (*st > '9')) { + return 0; + } + result *= 10; + result += *st - '0'; + break; + case 1: + if (((*st < '0') || (*st > '9')) + && ((*st < 'A') || (*st > 'F')) + && ((*st < 'a') || (*st > 'f'))) { + return 0; + } + result *= 16; + if ((*st >= '0') && (*st <= '9')) { + result += *st - '0'; + } else if ((*st >= 'A') && (*st <= 'F')) { + result += *st - 'A' + 10; + } else { + result += *st - 'a' + 10; + } + break; + case 2: + if ((*st < '0') || (*st > '7')) { + return 0; + } + result *= 8; + result += *st - '0'; + break; } + st++; + } - *valid = 1; - return result; + *valid = 1; + return result; } diff --git a/lib/parser.c b/lib/parser.c index 7cc242d..cf2eab3 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -18,8 +18,8 @@ typedef unsigned char op_t; typedef struct operator_t { - op_t op; - int pri, func; + op_t op; + int pri, func; } operator_t; static op_t pile_operators[PILEOP_MAX]; @@ -30,258 +30,268 @@ static int pileop_pos = 0, pilecall_pos = 0; /* La liste des opérateurs reconnus par le parser */ static operator_t operators[] = { - {',', 0, OP_NEST}, - {'+', 2, OP_PLUS}, - {'-', 2, OP_MOINS}, - {'*', 3, OP_MUL}, - {'/', 3, OP_DIV}, - {'%', 3, OP_MOD}, - {'+' + 128, 4, OP_PLUS_UNARY}, - {'-' + 128, 4, OP_MOINS_UNARY}, - {'(', 5, OP_FUNC_CALL}, - {'(' + 128, 5, OP_LPAREN}, - {'[', 5, OP_DECAL}, - {'[' + 128, 5, OP_DIRECT}, - {255, -1, -1} + {',', 0, OP_NEST}, + {'+', 2, OP_PLUS}, + {'-', 2, OP_MOINS}, + {'*', 3, OP_MUL}, + {'/', 3, OP_DIV}, + {'%', 3, OP_MOD}, + {'+' + 128, 4, OP_PLUS_UNARY}, + {'-' + 128, 4, OP_MOINS_UNARY}, + {'(', 5, OP_FUNC_CALL}, + {'(' + 128, 5, OP_LPAREN}, + {'[', 5, OP_DECAL}, + {'[' + 128, 5, OP_DIRECT}, + {255, -1, -1} }; /* Fonction interne: convertit un operateur en sa structure */ static operator_t get_op(op_t op) { - int i; + int i; - for (i = 0; operators[i].op != 255; i++) { - if (operators[i].op == op) - return operators[i]; - } + for (i = 0; operators[i].op != 255; i++) { + if (operators[i].op == op) + return operators[i]; + } - return operators[i]; + return operators[i]; } /* Fonctions internes de lectures sur la structure */ static int get_pri(op_t op) { - return get_op(op).pri; + return get_op(op).pri; } static int get_func(op_t op) { - return get_op(op).func; + return get_op(op).func; } /* Focntions internes d'empilement / dépilement */ static op_t get_last_op(void) { - if (pileop_pos) - return pile_operators[pileop_pos - 1]; - else - return -1; + if (pileop_pos) + return pile_operators[pileop_pos - 1]; + else + return -1; } static op_t pop_op(void) { - if (pileop_pos) - return pile_operators[--pileop_pos]; - return -1; + if (pileop_pos) + return pile_operators[--pileop_pos]; + return -1; } static void push_op(op_t op) { - if (pileop_pos != PILEOP_MAX) - pile_operators[pileop_pos++] = op; - else - exception(-1, _("Too many nested operators in expression.\n")); + if (pileop_pos != PILEOP_MAX) + pile_operators[pileop_pos++] = op; + else + exception(-1, _("Too many nested operators in expression.\n")); } static int pop_call(void) { - if (pilecall_pos) - return pile_nestedcall[--pilecall_pos]; - return -1; + if (pilecall_pos) + return pile_nestedcall[--pilecall_pos]; + return -1; } static void increment_call(void) { - if (pilecall_pos) { - if (pile_nestedcall[pilecall_pos - 1] != -1) - pile_nestedcall[pilecall_pos - 1]++; - } + if (pilecall_pos) { + if (pile_nestedcall[pilecall_pos - 1] != -1) + pile_nestedcall[pilecall_pos - 1]++; + } } static int get_last_call(void) { - if (pilecall_pos) - return pile_nestedcall[pilecall_pos - 1]; - return -1; + if (pilecall_pos) + return pile_nestedcall[pilecall_pos - 1]; + return -1; } static void push_call(int call) { - if (pilecall_pos != PILECALL_MAX) - pile_nestedcall[pilecall_pos++] = call; - else - exception(-1, _("Too many nested functions calls in expression.\n")); + if (pilecall_pos != PILECALL_MAX) + pile_nestedcall[pilecall_pos++] = call; + else + exception(-1, _("Too many nested functions calls in expression.\n")); } /* Cette fonction lit un "mot" sur la chaine line et renvoit le nouveau pointeur */ static char *getword(char *line, char *p) { - char o = 0, *d = line, instring = 0, gotbslash = 0; - - do { - if (instring) { - o = *(p++) = *line; - if (!gotbslash) { - switch (instring) { - case 1: - if (*line == '\'') { - instring = 0; - } - break; - case 2: - if (*line == '"') { - instring = 0; - } - break; - } - if (*line == '\\') - gotbslash = 1; - } else { - gotbslash = 0; - } - } else { - if (*(line) == '\'') { - o = *(p++) = *line; - instring = 1; - } else if (*(line) == '"') { - o = *(p++) = *line; - instring = 2; - } else { - if (*(line) != ' ' && *(line) != '\t') { - o = *(p++) = *line; - } else if (d != line) { - *p = '\0'; - return line; - } - } + char o = 0, *d = line, instring = 0, gotbslash = 0; + + do { + if (instring) { + o = *(p++) = *line; + if (!gotbslash) { + switch (instring) { + case 1: + if (*line == '\'') { + instring = 0; + } + break; + case 2: + if (*line == '"') { + instring = 0; + } + break; + } + if (*line == '\\') + gotbslash = 1; + } else { + gotbslash = 0; + } + } else { + if (*(line) == '\'') { + o = *(p++) = *line; + instring = 1; + } else if (*(line) == '"') { + o = *(p++) = *line; + instring = 2; + } else { + if (*(line) != ' ' && *(line) != '\t') { + o = *(p++) = *line; + } else if (d != line) { + *p = '\0'; + return line; } - line++; - } while (((*line) && (*line != ')') && (*line != ']') && (*line != ';') && (get_func(*line) == -1) - && (get_func(o) == -1)) || (instring)); - *p = '\0'; - return line; + } + } + line++; + } + while (((*line) && (*line != ')') && (*line != ']') + && (*line != ';') && (get_func(*line) == -1) + && (get_func(o) == -1)) || (instring)); + *p = '\0'; + return line; } /* Cette fonction va parcourire une chaine afin d'appeler les fonction push_pule() et act_pile() */ void parse_line(char *line) { - char buffer[BUFSIZ], imm[BUFSIZ], *d = line; - op_t op; - int got_unary = 128, nbrargs; - - sprintf(buffer, "Read line '%s'", line); - pushcontext(buffer); - - while (*line) { - line = getword(line, buffer); - sprintf(imm, "Analysing word '%s' at position %i", buffer, line - d); - pushcontext(imm); - if (get_func(buffer[0]) != -1) { - /* Le mot lut est un operateur, on agit sur la pile */ - buffer[0] += got_unary; - if (got_unary) { - } - if (get_pri(buffer[0]) == -1) { - if (got_unary) { - exception(-1, _("Invalid unary operator")); - } else { - exception(-1, _("Invalid binary operator")); - } - } - while (get_pri(get_last_op()) >= get_pri(buffer[0]) - && (((get_last_op() & 127) != '(') && ((get_last_op() & 127) != '['))) { - act_pile(get_func(pop_op())); - got_unary = 0; - } - if (buffer[0] == '(') { - push_call(0); - } - if (buffer[0] == ',') { - increment_call(); - } else - push_op(buffer[0]); - got_unary = 128; - } else if ((buffer[0] == ';') || (buffer[0] == ')') || (buffer[0] == ']')) { - /* Le mot lut est un opérateur spécial, on vide la pile */ - switch (buffer[0]) { - case ';': - /* Equivalent a fin de ligne */ - while (pileop_pos) { - op = pop_op(); - if (op == '(') - exception(-1, _("Parse error: too much left parenthesis")); - act_pile(get_func(op)); - } - popcontext(); - popcontext(); - return; - case ')': - /* Fin de parenthese (Appel de fonction ou expression mathématique) */ - while (1) { - if (!pileop_pos) - exception(-1, _("Parse error: too much right parenthesis")); - op = pop_op(); - if (((op & 127) == '(')) - break; - if (((op & 127) == '[')) - exception(-1, _("Parse error: enclosure mismatch")); - act_pile(get_func(op)); - } - if (op == '(') { - nbrargs = pop_call(); - sprintf(imm, "%i", nbrargs); - push_pile(imm); - act_pile(get_func(op)); - } - got_unary = 0; - break; - case ']': - /* Fin d'opérateur de décalage */ - while (1) { - if (!pileop_pos) - exception(-1, _("Parse error: too much right parenthesis")); - op = pop_op(); - if (((op & 127) == '[')) - break; - if (((op & 127) == '(')) - exception(-1, _("Parse error: enclosure mismatch")); - act_pile(get_func(op)); - } - act_pile(get_func(op)); - got_unary = 0; - break; - } - } else if (((buffer[0] >= 'A') && (buffer[0] <= 'Z')) || ((buffer[0] >= 'a') && (buffer[0] <= 'z')) - || ((buffer[0] >= '0') && (buffer[0] <= '9')) || (buffer[0] == '_') || (buffer[0] == '"') - || (buffer[0] == '\'') || (buffer[0] == '.') || (buffer[0] == '#') || (buffer[0] == '?')) { - /* Dans tous les autres cas, on a reçu un symbole, on le pose sur la pile */ - push_pile(buffer); - got_unary = 0; - if (!get_last_call()) - increment_call(); - } else if (buffer[0]) { - exception(-1, _("Invalid character")); + char buffer[BUFSIZ], imm[BUFSIZ], *d = line; + op_t op; + int got_unary = 128, nbrargs; + + sprintf(buffer, "Read line '%s'", line); + pushcontext(buffer); + + while (*line) { + line = getword(line, buffer); + sprintf(imm, "Analysing word '%s' at position %i", buffer, line - d); + pushcontext(imm); + if (get_func(buffer[0]) != -1) { + /* Le mot lut est un operateur, on agit sur la pile */ + buffer[0] += got_unary; + if (got_unary) { + } + if (get_pri(buffer[0]) == -1) { + if (got_unary) { + exception(-1, _("Invalid unary operator")); + } else { + exception(-1, _("Invalid binary operator")); } + } + while (get_pri(get_last_op()) >= get_pri(buffer[0]) + && (((get_last_op() & 127) != '(') + && ((get_last_op() & 127) != '['))) { + act_pile(get_func(pop_op())); + got_unary = 0; + } + if (buffer[0] == '(') { + push_call(0); + } + if (buffer[0] == ',') { + increment_call(); + } else + push_op(buffer[0]); + got_unary = 128; + } else if ((buffer[0] == ';') || (buffer[0] == ')') + || (buffer[0] == ']')) { + /* Le mot lut est un opérateur spécial, on vide la pile */ + switch (buffer[0]) { + case ';': + /* Equivalent a fin de ligne */ + while (pileop_pos) { + op = pop_op(); + if (op == '(') + exception(-1, + _("Parse error: too much left parenthesis")); + act_pile(get_func(op)); + } + popcontext(); popcontext(); + return; + case ')': + /* Fin de parenthese (Appel de fonction ou expression mathématique) */ + while (1) { + if (!pileop_pos) + exception(-1, + _("Parse error: too much right parenthesis")); + op = pop_op(); + if (((op & 127) == '(')) + break; + if (((op & 127) == '[')) + exception(-1, _("Parse error: enclosure mismatch")); + act_pile(get_func(op)); + } + if (op == '(') { + nbrargs = pop_call(); + sprintf(imm, "%i", nbrargs); + push_pile(imm); + act_pile(get_func(op)); + } + got_unary = 0; + break; + case ']': + /* Fin d'opérateur de décalage */ + while (1) { + if (!pileop_pos) + exception(-1, + _("Parse error: too much right parenthesis")); + op = pop_op(); + if (((op & 127) == '[')) + break; + if (((op & 127) == '(')) + exception(-1, _("Parse error: enclosure mismatch")); + act_pile(get_func(op)); + } + act_pile(get_func(op)); + got_unary = 0; + break; + } + } else if (((buffer[0] >= 'A') && (buffer[0] <= 'Z')) + || ((buffer[0] >= 'a') && (buffer[0] <= 'z')) + || ((buffer[0] >= '0') && (buffer[0] <= '9')) + || (buffer[0] == '_') || (buffer[0] == '"') + || (buffer[0] == '\'') || (buffer[0] == '.') + || (buffer[0] == '#') || (buffer[0] == '?')) { + /* Dans tous les autres cas, on a reçu un symbole, on le pose sur la pile */ + push_pile(buffer); + got_unary = 0; + if (!get_last_call()) + increment_call(); + } else if (buffer[0]) { + exception(-1, _("Invalid character")); } - popcontext(); + } + + popcontext(); } #ifndef HAVE_CONFIG_H @@ -290,62 +300,63 @@ void parse_line(char *line) void push_pile(char *word) { - printf("%s ", word); + printf("%s ", word); } void act_pile(int func) { - char op; - - switch (func) { - case OP_PLUS: - op = '+'; - break; - case OP_MOINS: - op = '-'; - break; - case OP_DIV: - op = '/'; - break; - case OP_MUL: - op = '*'; - break; - case OP_PLUS_UNARY: - op = '\0'; - break; - case OP_MOINS_UNARY: - op = '|'; - break; - case OP_FUNC_CALL: - op = '@'; - break; - case OP_DECAL: - op = '['; - break; - case OP_DIRECT: - op = '{'; - break; - } - printf("%c ", op); + char op; + + switch (func) { + case OP_PLUS: + op = '+'; + break; + case OP_MOINS: + op = '-'; + break; + case OP_DIV: + op = '/'; + break; + case OP_MUL: + op = '*'; + break; + case OP_PLUS_UNARY: + op = '\0'; + break; + case OP_MOINS_UNARY: + op = '|'; + break; + case OP_FUNC_CALL: + op = '@'; + break; + case OP_DECAL: + op = '['; + break; + case OP_DIRECT: + op = '{'; + break; + } + printf("%c ", op); } void main(void) { - parse_line("ADD R18, R20, 39;"); - printf("\n\n"); - fflush(stdout); - parse_line("MOV R31, Bidule[48 + R12];"); - printf("\n\n"); - fflush(stdout); - parse_line("MOV R12, [R3];"); - printf("\n\n"); - fflush(stdout); - parse_line("MOV R22, Truc[(3+2)*8];"); - printf("\n\n"); - fflush(stdout); - parse_line("Trucmuche DB \"Test de chaîne complète avec des opérateurs comme le + et le - ...\""); - printf("\n\n"); - fflush(stdout); + parse_line("ADD R18, R20, 39;"); + printf("\n\n"); + fflush(stdout); + parse_line("MOV R31, Bidule[48 + R12];"); + printf("\n\n"); + fflush(stdout); + parse_line("MOV R12, [R3];"); + printf("\n\n"); + fflush(stdout); + parse_line("MOV R22, Truc[(3+2)*8];"); + printf("\n\n"); + fflush(stdout); + parse_line + ("Trucmuche DB \"Test de chaîne complète avec des opérateurs comme le + et le - ...\""); + printf("\n\n"); + fflush(stdout); } #endif diff --git a/lib/registre.c b/lib/registre.c index a11a88c..82ecfdd 100644 --- a/lib/registre.c +++ b/lib/registre.c @@ -18,166 +18,166 @@ Uint32 registre[64]; Uint32 LireRegistreRG(void) { - return (registre[REG_RG]); + return (registre[REG_RG]); } Uint32 LireRegistreRD(void) { - return (registre[REG_RD]); + return (registre[REG_RD]); } Uint32 LireRegistrePC(void) { - return (registre[REG_PC]); + return (registre[REG_PC]); } Uint32 LireRegistreFLAG(void) { - return (registre[REG_FLAG]); + return (registre[REG_FLAG]); } Uint32 LireRegistreSP(void) { - return (registre[REG_STACKPTR]); + return (registre[REG_STACKPTR]); } void EcrireRegistreRG(Uint32 val) { - registre[REG_RG] = val; + registre[REG_RG] = val; } void EcrireRegistreRD(Uint32 val) { - registre[REG_RD] = val; + registre[REG_RD] = val; } void EcrireRegistrePC(Uint32 val) { - registre[REG_PC] = val; + registre[REG_PC] = val; } void EcrireRegistreFLAG(Uint32 val) { - registre[REG_FLAG] = val; + registre[REG_FLAG] = val; } void EcrireRegistreSP(Uint32 val) { - registre[REG_STACKPTR] = val; + registre[REG_STACKPTR] = val; } /* Lit le mot qui se trouve dans le registre 'numero_registre' */ Uint32 LireRegistre(Uint32 champ_registre) { - Uint32 i; - - 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 */ - return (i); - } - i = registre[champ_registre]; /* Registre classique */ - return (i); - } else /* Registre spécial */ - switch (champ(champ_registre, 4)) { - case 0: - return (LireRegistreRG()); - case 1: - return (LireRegistreRD()); - case 2: - return (LireRegistrePC()); - case 3: - return (LireRegistreFLAG()); - default:{ - exception(1, _("Invalid Register Descriptor")); - return (0); - } - } + Uint32 i; + + 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 */ + return (i); + } + i = registre[champ_registre]; /* Registre classique */ + return (i); + } else /* Registre spécial */ + switch (champ(champ_registre, 4)) { + case 0: + return (LireRegistreRG()); + case 1: + return (LireRegistreRD()); + case 2: + return (LireRegistrePC()); + case 3: + return (LireRegistreFLAG()); + default:{ + exception(1, _("Invalid Register Descriptor")); + return (0); + } + } } /* Ecrit le mot 'valeur' dans le registre 'numero_registre' */ void EcrireRegistre(Uint32 champ_registre, Uint32 valeur) { - Uint32 i; - - 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 */ - else - registre[champ_registre] = valeur; /* Registre classique */ - } else /* Registre spécial */ - switch (champ(champ_registre, 4)) { - case 0: - EcrireRegistreRG(valeur); - case 1: - EcrireRegistreRD(valeur); - case 2: - EcrireRegistrePC(valeur); - case 3: - EcrireRegistreFLAG(valeur); - default: - exception(1, _("Invalid Register Descriptor")); - } + Uint32 i; + + 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 */ + else + registre[champ_registre] = valeur; /* Registre classique */ + } else /* Registre spécial */ + switch (champ(champ_registre, 4)) { + case 0: + EcrireRegistreRG(valeur); + case 1: + EcrireRegistreRD(valeur); + case 2: + EcrireRegistrePC(valeur); + case 3: + EcrireRegistreFLAG(valeur); + default: + exception(1, _("Invalid Register Descriptor")); + } } int Overflow(void) { - return (ValeurBit(LireRegistreFLAG(), 0)); + return (ValeurBit(LireRegistreFLAG(), 0)); } int Zero(void) { - return (ValeurBit(LireRegistreFLAG(), 1)); + return (ValeurBit(LireRegistreFLAG(), 1)); } int Sign(void) { - return (ValeurBit(LireRegistreFLAG(), 2)); + return (ValeurBit(LireRegistreFLAG(), 2)); } int Parity(void) { - return (ValeurBit(LireRegistreFLAG(), 3)); + return (ValeurBit(LireRegistreFLAG(), 3)); } void SetOverflow(void) { - registre[REG_FLAG] |= 1; + registre[REG_FLAG] |= 1; } void SetZero(void) { - registre[REG_FLAG] |= 2; + registre[REG_FLAG] |= 2; } void SetSign(void) { - registre[REG_FLAG] |= 4; + registre[REG_FLAG] |= 4; } void SetParity(void) { - registre[REG_FLAG] |= 8; + registre[REG_FLAG] |= 8; } void ResetOverflow(void) { - registre[REG_FLAG] &= (VAL_MAX - 1); + registre[REG_FLAG] &= (VAL_MAX - 1); } void ResetZero(void) { - registre[REG_FLAG] &= (VAL_MAX - 2); + registre[REG_FLAG] &= (VAL_MAX - 2); } void ResetSign(void) { - registre[REG_FLAG] &= (VAL_MAX - 4); + registre[REG_FLAG] &= (VAL_MAX - 4); } void ResetParity(void) { - registre[REG_FLAG] &= (VAL_MAX - 8); + registre[REG_FLAG] &= (VAL_MAX - 8); } void ResetRegistres(void) { - int i; + int i; - for (i = 0; i < NB_REGISTRES_PHYSIQUES; i++) - registre[i] = 0; + for (i = 0; i < NB_REGISTRES_PHYSIQUES; i++) + registre[i] = 0; } diff --git a/lib/simulator.c b/lib/simulator.c index 661ccd3..1bfdcd2 100644 --- a/lib/simulator.c +++ b/lib/simulator.c @@ -20,499 +20,531 @@ Uint32 base_addr = 0; Uint32 LireInstruction(void) { - return (LD(LireRegistrePC())); + return (LD(LireRegistrePC())); } void IncrementeCompteurOrdinal(void) { - Uint32 of = LireRegistreFLAG(); + Uint32 of = LireRegistreFLAG(); - EcrireRegistrePC(AdditionNonSigne(LireRegistrePC(), 1)); - EcrireRegistreFLAG(of); + EcrireRegistrePC(AdditionNonSigne(LireRegistrePC(), 1)); + EcrireRegistreFLAG(of); } static FILE *openfilereading(char *name) { - FILE *f; + FILE *f; - if (!(f = fopen(name, "rb"))) { - pushcontext(strerror(errno)); - exception(1, _("Error reading file")); - } - return f; + if (!(f = fopen(name, "rb"))) { + pushcontext(strerror(errno)); + exception(1, _("Error reading file")); + } + return f; } static Uint32 readword(FILE * f) { - Uint32 a; + Uint32 a; - if (fread(&a, sizeof(a), 1, f) != 1) { - exception(1, _("premature end of file")); - } - return a; + if (fread(&a, sizeof(a), 1, f) != 1) { + exception(1, _("premature end of file")); + } + return a; } Uint32 Adresse(Uint32 u, Uint32 instruction) { - Uint32 tmp; - - switch (champ(u, 4)) { - case 0: - exception(1, _("Adresse: Call With Invalid r/m Field State ( r/m=00 )")); - return (0); - case 1: - tmp = LireInstruction(); - IncrementeCompteurOrdinal(); - return (tmp); - case 2: - return (LireRegistre(Champ3(instruction))); /* Adresse dans registre */ - case 3: - tmp = LireRegistre(Champ3(instruction)) + LireInstruction(); /* Adresse dans registre + decalage de nouvelle instruction */ - IncrementeCompteurOrdinal(); - return (tmp); - default: - exception(1, _("Adresse: Unmatched Addr Field")); - return (0); - } + Uint32 tmp; + + switch (champ(u, 4)) { + case 0: + exception(1, + _("Adresse: Call With Invalid r/m Field State ( r/m=00 )")); + return (0); + case 1: + tmp = LireInstruction(); + IncrementeCompteurOrdinal(); + return (tmp); + case 2: + return (LireRegistre(Champ3(instruction))); /* Adresse dans registre */ + case 3: + tmp = LireRegistre(Champ3(instruction)) + LireInstruction(); /* Adresse dans registre + decalage de nouvelle instruction */ + IncrementeCompteurOrdinal(); + return (tmp); + default: + exception(1, _("Adresse: Unmatched Addr Field")); + return (0); + } } void Initialisation(void) { - int i; + int i; - InitMemoire(); + InitMemoire(); - for (i = 0; i < TAILLE_MEMOIRE; i++) - Reset(&memoire_principale[i]); + for (i = 0; i < TAILLE_MEMOIRE; i++) + Reset(&memoire_principale[i]); - EcrireRegistre(0, 0); + EcrireRegistre(0, 0); - EcrireRegistreSP(ADD_SP); /* initialisation du stack pointer */ + EcrireRegistreSP(ADD_SP); /* initialisation du stack pointer */ } void Flush(void) { - FlushMemoire(); + FlushMemoire(); } void DecodeExec(Uint32 instruction) { - Uint32 champ_registre_resultat, val1, val2, resultat; - int test1, test2; - Uint32 val, of; /* valeur qui va etre stockée */ + Uint32 champ_registre_resultat, val1, val2, resultat; + int test1, test2; + Uint32 val, of; /* valeur qui va etre stockée */ - if (Opcode(instruction) & 0x80) { - fpu(Opcode(instruction)); - } else { + if (Opcode(instruction) & 0x80) { + fpu(Opcode(instruction)); + } else { + switch (Opcode(instruction)) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + 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 (Opcode(instruction) < 6) { + 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: - /* 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 (Opcode(instruction) < 6) { - 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; - } - } - if ((Opcode(instruction) & 2) && !(Opcode(instruction & 3))) { - EcrireRegistreRG(resultat); - EcrireRegistreRD(SecondResult); - } else { - 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 { - val = Adresse(Champ1(instruction), instruction); - ST(val, LireInstruction()); - IncrementeCompteurOrdinal(); - } - } - } else { /* mov arg2, arg1 */ - 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: - - 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(); - if (Opcode(instruction) & 1) { - tmp += LireRegistrePC(); - } - EcrireRegistrePC(tmp); - } else { - IncrementeCompteurOrdinal(); - } - break; - - case 12: /* JMP */ - case 13: - if (ValeurBit(Extension(instruction), 0) == 0) { - /* RET */ - of = LireRegistreFLAG(); - EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), Champ1(instruction))); - EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); - EcrireRegistrePC(LD(LireRegistreSP())); - EcrireRegistreFLAG(of); - - } else if (ValeurBit(Extension(instruction), 1) == 0) { /* JMP */ - if (ValeurBit(Extension(instruction), 2) != 0) { - EcrireRegistrePC(LireRegistre(Champ1(instruction))); - } else { - EcrireRegistrePC(LireInstruction()); - } - } else { /* CALL */ - if (ValeurBit(Extension(instruction), 2) != 0) { - ST(LireRegistreSP(), LireRegistrePC()); - of = LireRegistreFLAG(); - EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); - EcrireRegistreFLAG(of); - EcrireRegistrePC(LireRegistre(Champ1(instruction))); - } else { - ST(LireRegistreSP(), LireRegistrePC() + 1); - of = LireRegistreFLAG(); - EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); - EcrireRegistreFLAG(of); - EcrireRegistrePC(LireInstruction()); - } - } - break; - case 14: /* PUSH */ - - if (ValeurBit(Extension(instruction), 0) == 0) - val = LireRegistre(Champ1(instruction)); - else { - val = LireInstruction(); - IncrementeCompteurOrdinal(); - } - ST(LireRegistreSP(), val); - of = LireRegistreFLAG(); - EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); - EcrireRegistreFLAG(of); - break; - case 15: /* POP */ - of = LireRegistreFLAG(); - EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); - EcrireRegistre(Champ1(instruction), LD(LireRegistreSP())); - EcrireRegistreFLAG(of); - break; - case 127: /* HALT-RESET */ - if (ValeurBit(Extension(instruction), 0) == 0) { - HasToRun = 0; /* Halt */ - } else { - HasToReset = 1; /* Reset */ - ResetRegistres(); - } - break; - default: - exception(1, _("DecodeExec: Invalid Opcode")); + resultat = SHR(val1); + break; + } + } + if ((Opcode(instruction) & 2) + && !(Opcode(instruction & 3))) { + EcrireRegistreRG(resultat); + EcrireRegistreRD(SecondResult); + } else { + 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 { + val = Adresse(Champ1(instruction), instruction); + ST(val, LireInstruction()); + IncrementeCompteurOrdinal(); + } + } + } else { /* mov arg2, arg1 */ + 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: + + 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(); + if (Opcode(instruction) & 1) { + tmp += LireRegistrePC(); + } + EcrireRegistrePC(tmp); + } else { + IncrementeCompteurOrdinal(); + } + break; + + case 12: /* JMP */ + case 13: + if (ValeurBit(Extension(instruction), 0) == 0) { + /* RET */ + of = LireRegistreFLAG(); + EcrireRegistreSP(AdditionNonSigne + (LireRegistreSP(), Champ1(instruction))); + EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); + EcrireRegistrePC(LD(LireRegistreSP())); + EcrireRegistreFLAG(of); + + } else if (ValeurBit(Extension(instruction), 1) == 0) { /* JMP */ + if (ValeurBit(Extension(instruction), 2) != 0) { + EcrireRegistrePC(LireRegistre(Champ1(instruction))); + } else { + EcrireRegistrePC(LireInstruction()); + } + } else { /* CALL */ + if (ValeurBit(Extension(instruction), 2) != 0) { + ST(LireRegistreSP(), LireRegistrePC()); + of = LireRegistreFLAG(); + EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); + EcrireRegistreFLAG(of); + EcrireRegistrePC(LireRegistre(Champ1(instruction))); + } else { + ST(LireRegistreSP(), LireRegistrePC() + 1); + of = LireRegistreFLAG(); + EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); + EcrireRegistreFLAG(of); + EcrireRegistrePC(LireInstruction()); + } + } + break; + case 14: /* PUSH */ + + if (ValeurBit(Extension(instruction), 0) == 0) + val = LireRegistre(Champ1(instruction)); + else { + val = LireInstruction(); + IncrementeCompteurOrdinal(); + } + ST(LireRegistreSP(), val); + of = LireRegistreFLAG(); + EcrireRegistreSP(SoustractionNonSigne(LireRegistreSP(), 1)); + EcrireRegistreFLAG(of); + break; + case 15: /* POP */ + of = LireRegistreFLAG(); + EcrireRegistreSP(AdditionNonSigne(LireRegistreSP(), 1)); + EcrireRegistre(Champ1(instruction), LD(LireRegistreSP())); + EcrireRegistreFLAG(of); + break; + case 127: /* HALT-RESET */ + if (ValeurBit(Extension(instruction), 0) == 0) { + HasToRun = 0; /* Halt */ + } else { + HasToReset = 1; /* Reset */ + ResetRegistres(); + } + break; + default: + exception(1, _("DecodeExec: Invalid Opcode")); } + } } void AfficheReg(void) // affiche reg { - int i, j; + int i, j; - for (i = 0; i <= 3; i++) { - for (j = 0; j < 8; j++) { - fprintf(stderr, " R%02d ", (i * 8 + j)); - } - fprintf(stderr, "\n"); - for (j = 0; j < 8; j++) { - fprintf(stderr, "%08lX ", (registre[i * 8 + j])); - } - fprintf(stderr, "\n"); + for (i = 0; i <= 3; i++) { + for (j = 0; j < 8; j++) { + fprintf(stderr, " R%02d ", (i * 8 + j)); } - fprintf(stderr, "Rg: %08lX | Rd: %08lX | Flag: %08lX | PC: %08lX\n", LireRegistreRG(), LireRegistreRD(), LireRegistreFLAG(), LireRegistrePC()); + fprintf(stderr, "\n"); + for (j = 0; j < 8; j++) { + fprintf(stderr, "%08lX ", (registre[i * 8 + j])); + } + fprintf(stderr, "\n"); + } + fprintf(stderr, "Rg: %08lX | Rd: %08lX | Flag: %08lX | PC: %08lX\n", + LireRegistreRG(), LireRegistreRD(), LireRegistreFLAG(), + LireRegistrePC()); } void Debogueur(void) { - int out = 0; - - Uint32 instruction = LireInstruction(); - - while (!out) { - AfficheReg(); - fprintf(stderr, "Opcode: %02X, extension: %02X, champ1: %02X, champ2: %02X, champ3: %02X\n", - Opcode(instruction), Extension(instruction), Champ1(instruction), Champ2(instruction), Champ3(instruction)); - fprintf(stderr, "%08lX:%08lX - %08lX - %08lX > ", LireRegistrePC(), instruction, LD(LireRegistrePC() + 1), LD(LireRegistrePC() + 2)); - - switch (fgetc(input)) { - case 'G': - case 'g': - fprintf(stderr, "Go\n\n"); - debug = 0; - out = 1; - break; - case 'p': - case 'P': - fprintf(stderr, "Proceed\n\n"); - out = 1; - break; - case 'R': - case 'r': - break; - case 'Q': - case 'q': - clearterm(); - exception(1, _("Shutdown requested")); - default: - fprintf(stderr, _("Help:\nG: go\nP: Proceed\nR: display registers\nQ: quit\n")); - } + int out = 0; + + Uint32 instruction = LireInstruction(); + + while (!out) { + AfficheReg(); + fprintf(stderr, + "Opcode: %02X, extension: %02X, champ1: %02X, champ2: %02X, champ3: %02X\n", + Opcode(instruction), Extension(instruction), + Champ1(instruction), Champ2(instruction), Champ3(instruction)); + fprintf(stderr, "%08lX:%08lX - %08lX - %08lX > ", + LireRegistrePC(), instruction, LD(LireRegistrePC() + 1), + LD(LireRegistrePC() + 2)); + + switch (fgetc(input)) { + case 'G': + case 'g': + fprintf(stderr, "Go\n\n"); + debug = 0; + out = 1; + break; + case 'p': + case 'P': + fprintf(stderr, "Proceed\n\n"); + out = 1; + break; + case 'R': + case 'r': + break; + case 'Q': + case 'q': + clearterm(); + exception(1, _("Shutdown requested")); + default: + fprintf(stderr, + _ + ("Help:\nG: go\nP: Proceed\nR: display registers\nQ: quit\n")); } + } } void Traitement(Uint32 entrypoint) { - Uint32 instruction; - - while (HasToRun) { - EcrireRegistrePC(entrypoint); - HasToRun = 1; - HasToReset = 0; - while ((HasToRun) && (!HasToReset)) { - if (debug) { - initterm(); - Debogueur(); - clearterm(); - } - instruction = LireInstruction(); - IncrementeCompteurOrdinal(); - DecodeExec(instruction); - } + Uint32 instruction; + + while (HasToRun) { + EcrireRegistrePC(entrypoint); + HasToRun = 1; + HasToReset = 0; + while ((HasToRun) && (!HasToReset)) { + if (debug) { + initterm(); + Debogueur(); + clearterm(); + } + instruction = LireInstruction(); + IncrementeCompteurOrdinal(); + DecodeExec(instruction); } + } } void ChargeBinaire(char *filename) { - FILE *file; - char message[BUFSIZ]; - Uint32 entrypoint, nb, ns, nbss, nr, *relocation_table; - int i; - - sprintf(message, _("Opening file %s"), filename); - pushcontext(message); - file = openfilereading(filename); - if (readword(file) != 0x58454e4e) { /* verification de la signature */ - exception(1, _("Invalid Signature")); - } - popcontext(); - - sprintf(message, _("Loading file %s"), filename); - pushcontext(message); - - readword(file); - - entrypoint = readword(file); /* point d'entrée */ - nb = readword(file); /* taille du segment text */ - ns = readword(file); /* taille des donnes statiques */ - nbss = readword(file); /* taille des donnees non init */ - nr = readword(file); /* taille de la table de relogement */ - - relocation_table = (Uint32 *) Emalloc(nr * sizeof(Uint32)); - for (i = 0; i < nr; i++) { - relocation_table[i] = readword(file); - } - - for (i = base_addr; i < (base_addr + nb + ns); i++) { /*chargement en ram de .text et .data */ - ST(i, readword(file)); - } - - for (i = 0; i < nr; i++) { /* relogement */ - ST(base_addr + relocation_table[i], LD(base_addr + relocation_table[i]) + base_addr); - } - - free(relocation_table); - - entrypoint += base_addr; - - EcrireRegistre(28, base_addr + nb); - EcrireRegistre(30, base_addr + nb + ns + nbss); - - base_addr += nb + ns + nbss; - fclose(file); - popcontext(); - sprintf(message, _("Executing file %s"), filename); - pushcontext(message); - Traitement(entrypoint); - popcontext(); - base_addr -= nb + ns + nbss; + FILE *file; + char message[BUFSIZ]; + Uint32 entrypoint, nb, ns, nbss, nr, *relocation_table; + int i; + + sprintf(message, _("Opening file %s"), filename); + pushcontext(message); + file = openfilereading(filename); + if (readword(file) != 0x58454e4e) { /* verification de la signature */ + exception(1, _("Invalid Signature")); + } + popcontext(); + + sprintf(message, _("Loading file %s"), filename); + pushcontext(message); + + readword(file); + + entrypoint = readword(file); /* point d'entrée */ + nb = readword(file); /* taille du segment text */ + ns = readword(file); /* taille des donnes statiques */ + nbss = readword(file); /* taille des donnees non init */ + nr = readword(file); /* taille de la table de relogement */ + + relocation_table = (Uint32 *) Emalloc(nr * sizeof(Uint32)); + for (i = 0; i < nr; i++) { + relocation_table[i] = readword(file); + } + + for (i = base_addr; i < (base_addr + nb + ns); i++) { /*chargement en ram de .text et .data */ + ST(i, readword(file)); + } + + for (i = 0; i < nr; i++) { /* relogement */ + ST(base_addr + relocation_table[i], + LD(base_addr + relocation_table[i]) + base_addr); + } + + free(relocation_table); + + entrypoint += base_addr; + + EcrireRegistre(28, base_addr + nb); + EcrireRegistre(30, base_addr + nb + ns + nbss); + + base_addr += nb + ns + nbss; + fclose(file); + popcontext(); + sprintf(message, _("Executing file %s"), filename); + pushcontext(message); + Traitement(entrypoint); + popcontext(); + base_addr -= nb + ns + nbss; } diff --git a/lib/terminal.c b/lib/terminal.c index adc2a49..528eae0 100644 --- a/lib/terminal.c +++ b/lib/terminal.c @@ -11,28 +11,28 @@ struct termios initial_settings, new_settings; void initterm(void) { - tcgetattr(fileno(input), &initial_settings); - new_settings = initial_settings; - new_settings.c_lflag &= ~ICANON; - new_settings.c_lflag &= ~ECHO; - new_settings.c_cc[VMIN] = 1; - new_settings.c_cc[VTIME] = 0; - new_settings.c_lflag &= ~ISIG; + tcgetattr(fileno(input), &initial_settings); + new_settings = initial_settings; + new_settings.c_lflag &= ~ICANON; + new_settings.c_lflag &= ~ECHO; + new_settings.c_cc[VMIN] = 1; + new_settings.c_cc[VTIME] = 0; + new_settings.c_lflag &= ~ISIG; - if (tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) { - exception(1, _("could not set terminal attributes")); - } + if (tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) { + exception(1, _("could not set terminal attributes")); + } } void clearterm(void) { - tcsetattr(fileno(input), TCSANOW, &initial_settings); + tcsetattr(fileno(input), TCSANOW, &initial_settings); } void openterm(void) { - if (!(input = fopen("/dev/tty", "r"))) { - exception(1, _("could not open terminal")); - } + if (!(input = fopen("/dev/tty", "r"))) { + exception(1, _("could not open terminal")); + } } -- cgit v1.2.3