diff --git a/Makefile b/Makefile index 9aa8f02..a0fbc42 100644 --- a/Makefile +++ b/Makefile @@ -110,4 +110,7 @@ clean: debug: $(TARGET) $(DEBUG) $< < gdb_load -.PHONY: clean debug elf +style: + astyle --style=bsd -s4 --indent-preprocessor --add-brackets --recursive *.h *.c* + +.PHONY: clean debug elf style diff --git a/inc/log.h b/inc/log.h index 68fb89d..ca90ba6 100644 --- a/inc/log.h +++ b/inc/log.h @@ -1,13 +1,14 @@ #ifndef _LOG_H_ # define _LOG_H_ -typedef enum { - LOG_INIT = 1, // les messages écrits pendant l'init - LOG_VM = 2, // les messages écrits par le lapin - LOG_SIMUNET = 4, // les logs de simulation réseau - LOG_SIMUAUDIO = 8, // les logs de simulation audio - LOG_SIMULEDS = 16, // les logs de simulation des leds - LOG_SIMUMOTORS = 32 // les messages écrits pendant l'init +typedef enum +{ + LOG_INIT = 1, // les messages écrits pendant l'init + LOG_VM = 2, // les messages écrits par le lapin + LOG_SIMUNET = 4, // les logs de simulation réseau + LOG_SIMUAUDIO = 8, // les logs de simulation audio + LOG_SIMULEDS = 16, // les logs de simulation des leds + LOG_SIMUMOTORS = 32 // les messages écrits pendant l'init } e_logtypes; void my_printf(e_logtypes t, const char *fmt, ...); diff --git a/inc/simu/win/simuaudio.h b/inc/simu/win/simuaudio.h index 17dc6a3..a8e06a6 100644 --- a/inc/simu/win/simuaudio.h +++ b/inc/simu/win/simuaudio.h @@ -13,28 +13,28 @@ // Global variables & type defs typedef struct WaveOutBuffer { - WORD Rate; - BYTE nChannels; - LONG dwDataSize; - BYTE Resolution; - HWND hwnd; - HGLOBAL hWaveHdr; - LPWAVEHDR lpWaveHdr; - HANDLE hData; // handle of waveform data memory - HPSTR lpData; // pointer to waveform data memory + WORD Rate; + BYTE nChannels; + LONG dwDataSize; + BYTE Resolution; + HWND hwnd; + HGLOBAL hWaveHdr; + LPWAVEHDR lpWaveHdr; + HANDLE hData; // handle of waveform data memory + HPSTR lpData; // pointer to waveform data memory } WaveOutBuffer; typedef struct WaveInBuffer { - WORD Rate; - BYTE nChannels; - LONG dwDataSize; - BYTE Resolution; - HWND hwnd; - HGLOBAL hWaveHdr; - LPWAVEHDR lpWaveHdr; - HANDLE hData; // handle of waveform data memory - HPSTR lpData; // pointer to waveform data memory + WORD Rate; + BYTE nChannels; + LONG dwDataSize; + BYTE Resolution; + HWND hwnd; + HGLOBAL hWaveHdr; + LPWAVEHDR lpWaveHdr; + HANDLE hData; // handle of waveform data memory + HPSTR lpData; // pointer to waveform data memory } WaveInBuffer; // audio diff --git a/inc/vcomp/compiler.h b/inc/vcomp/compiler.h index 3f5fc4f..b03260a 100644 --- a/inc/vcomp/compiler.h +++ b/inc/vcomp/compiler.h @@ -18,161 +18,161 @@ class Prodbuffer; class Compiler { private: - Memory* m; - int systempackage; - intptr_t* stdtypes; - - Prodbuffer* bc; // structure de production de bytecode - - Prodbuffer* outputbuf; // structure de production des messages de type - - - Parser* parser; - intptr_t* newpackage; - intptr_t* newref; - int globals; - int locals; - int nblocals; - - int ifuns; - - // typage - intptr_t createnodetypecore(const char* name); - intptr_t createnodetypecore(int name); - intptr_t createnodetype(int type); - intptr_t createnodetuple(int size); - intptr_t createnodetupleval(int size); - - intptr_t* actualtype(intptr_t* p); - - int parsegraph(Parser* p,int env,int mono,int rec,int labels,int newvars,intptr_t* rnode); - intptr_t parse_rnode(intptr_t* p); - - intptr_t creategraph(Parser* p,int env,int mono); - intptr_t creategraph(Parser* p,int env,int mono,int labels); - intptr_t creategraph(const char* src,int env,int mono); - - int recechograph(Prodbuffer *output,intptr_t* p,int rec,int labels); - - intptr_t reccopytype(intptr_t* p); - intptr_t recresetcopy(intptr_t* p); - - intptr_t recgoweak(intptr_t* p); - - int restoreactual(intptr_t* t,intptr_t* s,int vt,int vs,int k); - intptr_t recunif(intptr_t* x,intptr_t* y); - intptr_t unif(intptr_t* x,intptr_t* y); - intptr_t unif_argfun(); - - intptr_t unifbigger(intptr_t* x,intptr_t* y); - - intptr_t* argsfromfun(intptr_t *f); - - void echonode(int code,intptr_t* p); - // packages - int hash(const char* name); - intptr_t createpackage(const char* name,int loghach); - void addreftopackage(intptr_t* ref,intptr_t* package); - intptr_t* searchtype(int env,const char* name); - intptr_t* searchemptytype(int env,const char* name); - - void dumppackage(int env); - - intptr_t searchbytype(int env,int type); - - // liste de labels - intptr_t addlabel(intptr_t base,const char* name,int val,intptr_t ref); - int nblabels(intptr_t base); - void removenlabels(intptr_t base,int n); - int searchlabel_byname(intptr_t base,const char* name,int* val,intptr_t* ref); - int searchlabel_byval(intptr_t base,int val, char** name); - intptr_t* tuplefromlabels(intptr_t base); - - // compilation - int parsefile(int ifdef); - int parsevar(); - int parseconst(); - int parseproto(); - int parseifdef(int ifndef); - int skipifdef(); - intptr_t fillproto(int env,intptr_t* fun); - intptr_t findproto(int env,intptr_t* fun); - int parsetype(); - int parsestruct(); - int parsesum(); - - int parsefun(); - int parseprogram(); - int parseexpression(); - int parsearithm(); - int parsea1(); - int parsea2(); - int parsea3(); - int parsea4(); - int parsea5(); - int parsea6(); - int parseterm(); - int parseref(); - int parseif(); - intptr_t parselet(); - int parseset(); - int parsewhile(); - int parsefor(); - int parsepntfun(); - int parselink(); - int parsecall(); - int parselocals(); - int parsestring(); - int parsefields(intptr_t* p); - int parsematch(); - int parsematchcons(intptr_t* end); - - int parsegetpoint(); - int parsesetpoint(int local,int ind,int* opstore); - - int parseupdate(); - int parseupdatevals(); - - // parsing variables - int parseval(); - int parseval3(); - int parseval4(); - int parseval5(); - int parseval6(); - int parseval7(); - - // production bytecode - void bc_byte_or_int(int val,int opbyte,int opint); - void bcint_byte_or_int(int val); - // autres - intptr_t addstdlibcore(); - int addstdlibstr(); - int addstdlibbuf(); - int addstdlibfiles(); - - int recglobal(int val,Prodbuffer *b); - int recglobals(int vlab,Prodbuffer *b); - int recbc(int vref,Prodbuffer *b,Prodbuffer *btab,int offset); + Memory* m; + int systempackage; + intptr_t* stdtypes; + + Prodbuffer* bc; // structure de production de bytecode + + Prodbuffer* outputbuf; // structure de production des messages de type + + + Parser* parser; + intptr_t* newpackage; + intptr_t* newref; + int globals; + int locals; + int nblocals; + + int ifuns; + + // typage + intptr_t createnodetypecore(const char* name); + intptr_t createnodetypecore(int name); + intptr_t createnodetype(int type); + intptr_t createnodetuple(int size); + intptr_t createnodetupleval(int size); + + intptr_t* actualtype(intptr_t* p); + + int parsegraph(Parser* p,int env,int mono,int rec,int labels,int newvars,intptr_t* rnode); + intptr_t parse_rnode(intptr_t* p); + + intptr_t creategraph(Parser* p,int env,int mono); + intptr_t creategraph(Parser* p,int env,int mono,int labels); + intptr_t creategraph(const char* src,int env,int mono); + + int recechograph(Prodbuffer *output,intptr_t* p,int rec,int labels); + + intptr_t reccopytype(intptr_t* p); + intptr_t recresetcopy(intptr_t* p); + + intptr_t recgoweak(intptr_t* p); + + int restoreactual(intptr_t* t,intptr_t* s,int vt,int vs,int k); + intptr_t recunif(intptr_t* x,intptr_t* y); + intptr_t unif(intptr_t* x,intptr_t* y); + intptr_t unif_argfun(); + + intptr_t unifbigger(intptr_t* x,intptr_t* y); + + intptr_t* argsfromfun(intptr_t *f); + + void echonode(int code,intptr_t* p); + // packages + int hash(const char* name); + intptr_t createpackage(const char* name,int loghach); + void addreftopackage(intptr_t* ref,intptr_t* package); + intptr_t* searchtype(int env,const char* name); + intptr_t* searchemptytype(int env,const char* name); + + void dumppackage(int env); + + intptr_t searchbytype(int env,int type); + + // liste de labels + intptr_t addlabel(intptr_t base,const char* name,int val,intptr_t ref); + int nblabels(intptr_t base); + void removenlabels(intptr_t base,int n); + int searchlabel_byname(intptr_t base,const char* name,int* val,intptr_t* ref); + int searchlabel_byval(intptr_t base,int val, char** name); + intptr_t* tuplefromlabels(intptr_t base); + + // compilation + int parsefile(int ifdef); + int parsevar(); + int parseconst(); + int parseproto(); + int parseifdef(int ifndef); + int skipifdef(); + intptr_t fillproto(int env,intptr_t* fun); + intptr_t findproto(int env,intptr_t* fun); + int parsetype(); + int parsestruct(); + int parsesum(); + + int parsefun(); + int parseprogram(); + int parseexpression(); + int parsearithm(); + int parsea1(); + int parsea2(); + int parsea3(); + int parsea4(); + int parsea5(); + int parsea6(); + int parseterm(); + int parseref(); + int parseif(); + intptr_t parselet(); + int parseset(); + int parsewhile(); + int parsefor(); + int parsepntfun(); + int parselink(); + int parsecall(); + int parselocals(); + int parsestring(); + int parsefields(intptr_t* p); + int parsematch(); + int parsematchcons(intptr_t* end); + + int parsegetpoint(); + int parsesetpoint(int local,int ind,int* opstore); + + int parseupdate(); + int parseupdatevals(); + + // parsing variables + int parseval(); + int parseval3(); + int parseval4(); + int parseval5(); + int parseval6(); + int parseval7(); + + // production bytecode + void bc_byte_or_int(int val,int opbyte,int opint); + void bcint_byte_or_int(int val); + // autres + intptr_t addstdlibcore(); + int addstdlibstr(); + int addstdlibbuf(); + int addstdlibfiles(); + + int recglobal(int val,Prodbuffer *b); + int recglobals(int vlab,Prodbuffer *b); + int recbc(int vref,Prodbuffer *b,Prodbuffer *btab,int offset); public: - Prodbuffer* brelease; - Compiler(Memory* mem); - ~Compiler(); - int start(); - void stop(); - intptr_t addnative(int nref, const char** nameref, int* valref - , int* coderef, const char** typeref,void* arg); + Prodbuffer* brelease; + Compiler(Memory* mem); + ~Compiler(); + int start(); + void stop(); + intptr_t addnative(int nref, const char** nameref, int* valref + , int* coderef, const char** typeref,void* arg); - int gocompile(int type); // [filename/src packages] -> [packages] - int getsystempackage(); + int gocompile(int type); // [filename/src packages] -> [packages] + int getsystempackage(); - intptr_t* searchref(int env,char* name); - intptr_t* searchref_nosetused(int env,char* name); + intptr_t* searchref(int env,char* name); + intptr_t* searchref_nosetused(int env,char* name); - intptr_t echograph(Prodbuffer *output,intptr_t* p); - intptr_t copytype(intptr_t* p); - intptr_t recunifbigger(intptr_t* x,intptr_t* y); + intptr_t echograph(Prodbuffer *output,intptr_t* p); + intptr_t copytype(intptr_t* p); + intptr_t recunifbigger(intptr_t* x,intptr_t* y); }; diff --git a/inc/vcomp/file.h b/inc/vcomp/file.h index fb4d07f..e19f928 100644 --- a/inc/vcomp/file.h +++ b/inc/vcomp/file.h @@ -19,21 +19,21 @@ class FileSystem; class File { private : - FileSystem *filesystem; - FILE *file; - char* internalbuffer; - int sizeinternalbuffer; + FileSystem *filesystem; + FILE *file; + char* internalbuffer; + int sizeinternalbuffer; public: - File(FileSystem *fs); - ~File(); + File(FileSystem *fs); + ~File(); - int openread(const char* name); - char* getcontent(int *size); + int openread(const char* name); + char* getcontent(int *size); - int openwrite(const char* name); - int write(const char* outbuf,int size); + int openwrite(const char* name); + int write(const char* outbuf,int size); - int close(); + int close(); }; #endif diff --git a/inc/vcomp/interpreter.h b/inc/vcomp/interpreter.h index 6c794b2..1462ebc 100644 --- a/inc/vcomp/interpreter.h +++ b/inc/vcomp/interpreter.h @@ -17,91 +17,91 @@ class Prodbuffer; class Interpreter { private: - Memory* m; - int callstack; - char *pc; // pointeur vers le prochain bytecode - char *bc; // pointeur vers le début du bytecode - int locals; // pointeur vers les variable locales dans la pile - int* globals; // pointeur vers les variable globales + Memory* m; + int callstack; + char *pc; // pointeur vers le prochain bytecode + char *bc; // pointeur vers le début du bytecode + int locals; // pointeur vers les variable locales dans la pile + int* globals; // pointeur vers les variable globales public: - Prodbuffer* output; + Prodbuffer* output; - Interpreter(Memory* memory); - ~Interpreter(); - int start(); - void stop(); + Interpreter(Memory* memory); + ~Interpreter(); + int start(); + void stop(); - int go(); + int go(); - int TFCtest(char* p); + int TFCtest(char* p); -int BCexec(); -int BCret(); -int BCdrop(); -int BCdup(); -int BCnil(); -int BCgetlocal(); -int BCsetlocal(); -int BCgetglobal(); -int BCgetglobalb(); -int BCint(); -int BCgoto(); -int BCelse(); -int BCfetch(); -int BCstore(); -int BCdeftab(); -int BCnop(); -int BCadd(); -int BCsub(); -int BCmul(); -int BCdiv(); -int BCneg(); -int BCand(); -int BCor(); -int BCeor(); -int BCnot(); -int BCshl(); -int BCshr(); -int BCeq(); -int BCne(); -int BClt(); -int BCgt(); -int BCle(); -int BCge(); -int BCnon(); -int BCsearch(); -int BCfloat(); -int BCcall(); -int BCaddf(); -int BCsubf(); -int BCmulf(); -int BCdivf(); -int BCnegf(); -int BCltf(); -int BCgtf(); -int BClef(); -int BCgef(); -int BCsetlocal2(); -int BCsetglobal2(); -int BCexecnative(); -int BCgetlocalb(); -int BCfetchb(); -int BCdeftabb(); -int BCsetlocalb(); -int BCfirst(); -int BCmod(); -int BCmktab(); -int BCmktabb(); -int BCsetstruct(); -int BCsetstructb(); -int BCcallr(); -int BCcallrb(); -int BCgetglobalval(); -int BCgetglobalvalb(); -int BCupdate(); -int BCupdateb(); -int BCintb(); + int BCexec(); + int BCret(); + int BCdrop(); + int BCdup(); + int BCnil(); + int BCgetlocal(); + int BCsetlocal(); + int BCgetglobal(); + int BCgetglobalb(); + int BCint(); + int BCgoto(); + int BCelse(); + int BCfetch(); + int BCstore(); + int BCdeftab(); + int BCnop(); + int BCadd(); + int BCsub(); + int BCmul(); + int BCdiv(); + int BCneg(); + int BCand(); + int BCor(); + int BCeor(); + int BCnot(); + int BCshl(); + int BCshr(); + int BCeq(); + int BCne(); + int BClt(); + int BCgt(); + int BCle(); + int BCge(); + int BCnon(); + int BCsearch(); + int BCfloat(); + int BCcall(); + int BCaddf(); + int BCsubf(); + int BCmulf(); + int BCdivf(); + int BCnegf(); + int BCltf(); + int BCgtf(); + int BClef(); + int BCgef(); + int BCsetlocal2(); + int BCsetglobal2(); + int BCexecnative(); + int BCgetlocalb(); + int BCfetchb(); + int BCdeftabb(); + int BCsetlocalb(); + int BCfirst(); + int BCmod(); + int BCmktab(); + int BCmktabb(); + int BCsetstruct(); + int BCsetstructb(); + int BCcallr(); + int BCcallrb(); + int BCgetglobalval(); + int BCgetglobalvalb(); + int BCupdate(); + int BCupdateb(); + int BCintb(); }; diff --git a/inc/vcomp/memory.h b/inc/vcomp/memory.h index 5720849..351f005 100644 --- a/inc/vcomp/memory.h +++ b/inc/vcomp/memory.h @@ -37,15 +37,15 @@ class Stack { private: public: - Stack(); - ~Stack(); - intptr_t size; // taille du bloc alloué - intptr_t* base; // début de la pile - intptr_t* pp; // pointeur de pile - - void initialize(int s); - int bigger(Memory* m); - void dump(FILE* f); + Stack(); + ~Stack(); + intptr_t size; // taille du bloc alloué + intptr_t* base; // début de la pile + intptr_t* pp; // pointeur de pile + + void initialize(int s); + int bigger(Memory* m); + void dump(FILE* f); }; typedef void (*FORGET)(Memory*, void*); @@ -55,54 +55,54 @@ class Memory { private: - // gestion du GC - int root; // bloc racine (initialisé avec le premier bloc alloué) + // gestion du GC + int root; // bloc racine (initialisé avec le premier bloc alloué) - int size0; + int size0; public: - int gcincperiod; - int abort; - // stack courante - Stack stack; - Terminal* term; - FileSystem* filesystem; - Util *util; - Winutil *winutil; - Memory(int size,Terminal *t,FileSystem *fs); - ~Memory(); - - int start(); - void stop(); - - intptr_t addroot(intptr_t* p); - void removeroot(intptr_t *p); - - intptr_t* malloc(int size,int type); // allocation (type vaut TYPE_BINARY ou TYPE_TAB) - intptr_t* mallocClear(int size); // allocation de type TYPE_TAB avec initialisation à NIL - intptr_t* mallocExternal(void* pnt,FORGET fun); // allocation de type TYPE_EXT (la fonction fun(pnt) sera appelée lors de l'oubli du bloc) - intptr_t pushmalloc(int size,int type); // allocation (type vaut TYPE_BINARY ou TYPE_TAB) - intptr_t pushmallocClear(int size); // allocation de type TYPE_TAB avec initialisation à NIL - intptr_t pushExternal(void* pnt,FORGET fun); - - intptr_t push(int val); - void tabset(intptr_t* p,int i,int val); - void stackset(intptr_t* p,int val); - - - intptr_t* storestring(const char* src); - intptr_t* storebinary(const char* src,int size); - intptr_t* storenosrc(int size); - intptr_t pushstring(const char* src); - intptr_t pushbinary(char* src,int size); - intptr_t pushnosrc(int size); - intptr_t pushpnt(void* pnt); - - int deftab(int size); - - void listing(); - const char* errorname(int err); - void dump(); + int gcincperiod; + int abort; + // stack courante + Stack stack; + Terminal* term; + FileSystem* filesystem; + Util *util; + Winutil *winutil; + Memory(int size,Terminal *t,FileSystem *fs); + ~Memory(); + + int start(); + void stop(); + + intptr_t addroot(intptr_t* p); + void removeroot(intptr_t *p); + + intptr_t* malloc(int size,int type); // allocation (type vaut TYPE_BINARY ou TYPE_TAB) + intptr_t* mallocClear(int size); // allocation de type TYPE_TAB avec initialisation à NIL + intptr_t* mallocExternal(void* pnt,FORGET fun); // allocation de type TYPE_EXT (la fonction fun(pnt) sera appelée lors de l'oubli du bloc) + intptr_t pushmalloc(int size,int type); // allocation (type vaut TYPE_BINARY ou TYPE_TAB) + intptr_t pushmallocClear(int size); // allocation de type TYPE_TAB avec initialisation à NIL + intptr_t pushExternal(void* pnt,FORGET fun); + + intptr_t push(int val); + void tabset(intptr_t* p,int i,int val); + void stackset(intptr_t* p,int val); + + + intptr_t* storestring(const char* src); + intptr_t* storebinary(const char* src,int size); + intptr_t* storenosrc(int size); + intptr_t pushstring(const char* src); + intptr_t pushbinary(char* src,int size); + intptr_t pushnosrc(int size); + intptr_t pushpnt(void* pnt); + + int deftab(int size); + + void listing(); + const char* errorname(int err); + void dump(); }; // structures en mémoire @@ -120,14 +120,14 @@ class Memory //// les champs suivants ne sont - pour l'instant - remplis que pour //// les CODE_VAR et les CODE_CONST #define REF_USED 7 // 0 au debut, 1 de plus à chaque fois que la - // référence est utilisée +// référence est utilisée #define REF_SET 8 // pour les var. 0 si la var ne reçoit jamais de - // valeur, 1 si elle a seulement une valeur - // donnée à la déclaration, et 2 si elle est - // 'set'-ée dans le programme +// valeur, 1 si elle a seulement une valeur +// donnée à la déclaration, et 2 si elle est +// 'set'-ée dans le programme #define REF_USED_IN_IFDEF 9 // 0 au début, 1 de plus à chaque fois que - // la ref est utlisée comme condition dans - // un ifdef +// la ref est utlisée comme condition dans +// un ifdef #define CODE_TYPE -1 // type #define CODE_VAR -2 // variable @@ -266,61 +266,89 @@ void AbortMetal(Memory* m,int donotstop); inline intptr_t Memory::push(int val) { - *(++stack.pp)=val; - if ((stack.pp-stack.base)>=stack.size-1) - { - intptr_t k; if ((k=stack.bigger(this))) return k; + *(++stack.pp)=val; + if ((stack.pp-stack.base)>=stack.size-1) + { + intptr_t k; + if ((k=stack.bigger(this))) + { + return k; + } // term->printf(LOG_DEVCORE,"## bigger stack %d\n",stack.size); // stackprint(0); - } - return 0; + } + return 0; } inline void Memory::tabset(intptr_t* p,int i,int val) { - p[HEADER_LENGTH+i]=val; // règle une valeur d'une table + p[HEADER_LENGTH+i]=val; // règle une valeur d'une table } inline void Memory::stackset(intptr_t* p,int val) { - *p=val; + *p=val; } inline intptr_t Memory::pushmalloc(int size,int type) { - intptr_t* p=malloc(size,type); if (!p) return MTLERR_OM; - return push(PNTTOVAL(p)); + intptr_t* p=malloc(size,type); + if (!p) + { + return MTLERR_OM; + } + return push(PNTTOVAL(p)); } inline intptr_t Memory::pushmallocClear(int size) { - intptr_t* p=mallocClear(size); if (!p) return MTLERR_OM; - return push(PNTTOVAL(p)); + intptr_t* p=mallocClear(size); + if (!p) + { + return MTLERR_OM; + } + return push(PNTTOVAL(p)); } inline intptr_t Memory::pushstring(const char* src) { - intptr_t* p=storestring(src); if (!p) return MTLERR_OM; - return push(PNTTOVAL(p)); + intptr_t* p=storestring(src); + if (!p) + { + return MTLERR_OM; + } + return push(PNTTOVAL(p)); } inline intptr_t Memory::pushbinary(char* src,int size) { - intptr_t* p=storebinary(src,size); if (!p) return MTLERR_OM; - return push(PNTTOVAL(p)); + intptr_t* p=storebinary(src,size); + if (!p) + { + return MTLERR_OM; + } + return push(PNTTOVAL(p)); } inline intptr_t Memory::pushnosrc(int size) { - intptr_t* p=storenosrc(size); if (!p) return MTLERR_OM; - return push(PNTTOVAL(p)); + intptr_t* p=storenosrc(size); + if (!p) + { + return MTLERR_OM; + } + return push(PNTTOVAL(p)); } inline intptr_t Memory::pushpnt(void* pnt) { - intptr_t* p=malloc(1,TYPE_BINARY); if (!p) return MTLERR_OM; - *(BINSTART(p))=((intptr_t)pnt); - return push(PNTTOVAL(p)); + intptr_t* p=malloc(1,TYPE_BINARY); + if (!p) + { + return MTLERR_OM; + } + *(BINSTART(p))=((intptr_t)pnt); + return push(PNTTOVAL(p)); } #endif diff --git a/inc/vcomp/parser.h b/inc/vcomp/parser.h index f083532..0dfd4b0 100644 --- a/inc/vcomp/parser.h +++ b/inc/vcomp/parser.h @@ -20,43 +20,43 @@ class Memory; class Parser { private : - Terminal* term; - char* srcname; // nom de la source - char* src; // buffer source - int index; // index de lecture + Terminal* term; + char* srcname; // nom de la source + char* src; // buffer source + int index; // index de lecture - char savedchar; // caractère sauvegardé (momentanément remplacé par \0) - int indexsavedchar; //index du caractère sauvegardé + char savedchar; // caractère sauvegardé (momentanément remplacé par \0) + int indexsavedchar; //index du caractère sauvegardé - int again; // token a relire - int lign; // ligne courante - int offligne; // offset caractere debut ligne + int again; // token a relire + int lign; // ligne courante + int offligne; // offset caractere debut ligne - int lign0; // ligne courante (DERNIER TOKEN LU) - int offligne0; // offset caractere debut ligne (DERNIER TOKEN LU) - int offchar0; // offset caractere courant (DERNIER TOKEN LU) + int lign0; // ligne courante (DERNIER TOKEN LU) + int offligne0; // offset caractere debut ligne (DERNIER TOKEN LU) + int offchar0; // offset caractere courant (DERNIER TOKEN LU) - void savechar(int i); - void restorechar(); - void againchar(); - int nextchar(); - int gettoken(int filter); + void savechar(int i); + void restorechar(); + void againchar(); + int nextchar(); + int gettoken(int filter); public : - char* token; - Parser(Terminal* t,FileSystem* fs,char *filename); - Parser(Terminal* t,const char *buffer); - ~Parser(); + char* token; + Parser(Terminal* t,FileSystem* fs,char *filename); + Parser(Terminal* t,const char *buffer); + ~Parser(); - char* next(int filter); - void giveback(); + char* next(int filter); + void giveback(); - void echoposition(); + void echoposition(); - int parsekeyword(const char* keyword); - int parsekeyword(const char* keyword,int filter); - int getstring(Memory* m,char separator); + int parsekeyword(const char* keyword); + int parsekeyword(const char* keyword,int filter); + int getstring(Memory* m,char separator); }; diff --git a/inc/vcomp/prodbuffer.h b/inc/vcomp/prodbuffer.h index 92d7221..6770d6c 100644 --- a/inc/vcomp/prodbuffer.h +++ b/inc/vcomp/prodbuffer.h @@ -13,28 +13,28 @@ class Prodbuffer { //public: private: - int size; // taille du buffer de bytecode - char* buffer; // buffer de bytecode - int index; // index du buffer de bytecode + int size; // taille du buffer de bytecode + char* buffer; // buffer de bytecode + int index; // index du buffer de bytecode - void bigger(); // augmentation de la taille du buffer + void bigger(); // augmentation de la taille du buffer public: - // buffer bytecode - Prodbuffer(); - ~Prodbuffer(); - void reinit(); - void addchar(char c); - void addint(int i); - void addshort(int i); - void setchar(int index,char c); - void setint(int index,int i); - void setshort(int index,int i); - void addstr(char *src,int len); - void printf(const char *format, ...); - void cut(int len); - int getsize(); - char* getstart(); + // buffer bytecode + Prodbuffer(); + ~Prodbuffer(); + void reinit(); + void addchar(char c); + void addint(int i); + void addshort(int i); + void setchar(int index,char c); + void setint(int index,int i); + void setshort(int index,int i); + void addstr(char *src,int len); + void printf(const char *format, ...); + void cut(int len); + int getsize(); + char* getstart(); }; #define PRODBUFFER_FIRSTSIZE 128 diff --git a/inc/vcomp/resource.h b/inc/vcomp/resource.h index a22469d..31ce625 100644 --- a/inc/vcomp/resource.h +++ b/inc/vcomp/resource.h @@ -6,7 +6,7 @@ #define IDI_ICON2 103 // Next default values for new objects -// +// #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 104 diff --git a/inc/vcomp/terminal.h b/inc/vcomp/terminal.h index d8c5a62..fe9acde 100644 --- a/inc/vcomp/terminal.h +++ b/inc/vcomp/terminal.h @@ -15,16 +15,16 @@ class Memory; class Terminal { private: - char* internalbuffer; - int sizeinternalbuffer; + char* internalbuffer; + int sizeinternalbuffer; public: - Memory* m; - Terminal(); - ~Terminal(); + Memory* m; + Terminal(); + ~Terminal(); - void echo(int mask,char *s); - void echo(int mask,char *s,int size); - void printf(int mask,const char *format, ...); + void echo(int mask,char *s); + void echo(int mask,char *s,int size); + void printf(int mask,const char *format, ...); }; #endif diff --git a/inc/vcomp/util.h b/inc/vcomp/util.h index 8533f1d..67987ac 100644 --- a/inc/vcomp/util.h +++ b/inc/vcomp/util.h @@ -18,14 +18,14 @@ class Interpreter; class Util { private : - Memory* m; + Memory* m; public: - Util(Memory* mem); - ~Util(); - int start(); - void stop(); + Util(Memory* mem); + ~Util(); + int start(); + void stop(); - Compiler* compiler; - Interpreter* interpreter; + Compiler* compiler; + Interpreter* interpreter; }; #endif diff --git a/inc/vm/vbc_str.h b/inc/vm/vbc_str.h index 9d2aecf..91f085f 100644 --- a/inc/vm/vbc_str.h +++ b/inc/vm/vbc_str.h @@ -8,159 +8,159 @@ #define MaxOpcode 153 const char* strbytecod[MaxOpcode]= { -"exec", -"ret", -"intb", -"int", -"nil", -"drop", -"dup", -"getlocalb", -"getlocal", -"add", -"sub", -"mul", -"div", -"mod", -"and", -"or", -"eor", -"shl", -"shr", -"neg", -"not", -"non", -"eq", -"ne", -"lt", -"gt", -"le", -"ge", -"goto", -"else", -"mktabb", -"mktab", -"deftabb", -"deftab", -"fetchb", -"fetch", -"getglobalb", -"getglobal", -"Secho", -"Iecho", -"setlocalb", -"setlocal", -"setglobal", -"setstructb", -"setstruct", -"hd", -"tl", -"setlocal2", -"store", -"call", -"callrb", -"callr", -"first", -"time_ms", -"tabnew", -"fixarg", -"abs", -"max", -"min", -"rand", -"srand", -"time", -"strnew", -"strset", -"strcpy", -"vstrcmp", -"strfind", -"strfindrev", -"strlen", -"strget", -"strsub", -"strcat", -"tablen", -"strcatlist", -"led", -"motorset", -"motorget", -"button2", -"button3", -"playStart", -"playFeed", -"playStop", -"load", -"udpStart", -"udpCb", -"udpStop", -"udpSend", -"gc", -"tcpOpen", -"tcpClose", -"tcpSend", -"tcpCb", -"save", -"bytecode", -"loopcb", -"Iecholn", -"Secholn", -"tcpListen", -"envget", -"envset", -"sndVol", -"rfidGet", -"playTime", -"netCb", -"netSend", -"netState", -"netMac", -"netChk", -"netSetmode", -"netScan", -"netAuth", -"recStart", -"recStop", -"recVol", -"netSeqAdd", -"strgetword", -"strputword", -"atoi", -"htoi", -"itoa", -"ctoa", -"itoh", -"ctoh", -"itobin2", -"listswitch", -"listswitchstr", -"sndRefresh", -"sndWrite", -"sndRead", -"sndFeed", -"sndAmpli", -"corePP", -"corePush", -"corePull", -"coreBit0", -"tcpEnable", -"reboot", -"strcmp", -"adp2wav", -"wav2adp", -"alaw2wav", -"wav2alaw", -"netPmk", -"flashFirmware", -"crypt", -"uncrypt", -"netRssi", -"rfidGetList", -"rfidRead", -"rfidWrite", -"i2cRead", -"i2cWrite", -"strright" + "exec", + "ret", + "intb", + "int", + "nil", + "drop", + "dup", + "getlocalb", + "getlocal", + "add", + "sub", + "mul", + "div", + "mod", + "and", + "or", + "eor", + "shl", + "shr", + "neg", + "not", + "non", + "eq", + "ne", + "lt", + "gt", + "le", + "ge", + "goto", + "else", + "mktabb", + "mktab", + "deftabb", + "deftab", + "fetchb", + "fetch", + "getglobalb", + "getglobal", + "Secho", + "Iecho", + "setlocalb", + "setlocal", + "setglobal", + "setstructb", + "setstruct", + "hd", + "tl", + "setlocal2", + "store", + "call", + "callrb", + "callr", + "first", + "time_ms", + "tabnew", + "fixarg", + "abs", + "max", + "min", + "rand", + "srand", + "time", + "strnew", + "strset", + "strcpy", + "vstrcmp", + "strfind", + "strfindrev", + "strlen", + "strget", + "strsub", + "strcat", + "tablen", + "strcatlist", + "led", + "motorset", + "motorget", + "button2", + "button3", + "playStart", + "playFeed", + "playStop", + "load", + "udpStart", + "udpCb", + "udpStop", + "udpSend", + "gc", + "tcpOpen", + "tcpClose", + "tcpSend", + "tcpCb", + "save", + "bytecode", + "loopcb", + "Iecholn", + "Secholn", + "tcpListen", + "envget", + "envset", + "sndVol", + "rfidGet", + "playTime", + "netCb", + "netSend", + "netState", + "netMac", + "netChk", + "netSetmode", + "netScan", + "netAuth", + "recStart", + "recStop", + "recVol", + "netSeqAdd", + "strgetword", + "strputword", + "atoi", + "htoi", + "itoa", + "ctoa", + "itoh", + "ctoh", + "itobin2", + "listswitch", + "listswitchstr", + "sndRefresh", + "sndWrite", + "sndRead", + "sndFeed", + "sndAmpli", + "corePP", + "corePush", + "corePull", + "coreBit0", + "tcpEnable", + "reboot", + "strcmp", + "adp2wav", + "wav2adp", + "alaw2wav", + "wav2alaw", + "netPmk", + "flashFirmware", + "crypt", + "uncrypt", + "netRssi", + "rfidGetList", + "rfidRead", + "rfidWrite", + "i2cRead", + "i2cWrite", + "strright" }; #endif // ! VBC_STR_H_ diff --git a/src/comp/main.cpp b/src/comp/main.cpp index 47f503a..ad04b47 100644 --- a/src/comp/main.cpp +++ b/src/comp/main.cpp @@ -14,58 +14,70 @@ extern "C" { #include"vinterp.h" #include"properties.h" -char srcbytecode[65536]; + char srcbytecode[65536]; -void dump(uchar *src,int len) -{ - int i,j; - char buffer[64]; - printf("\n"); - for(i=0;i=32)&&(src[i+j]<128))?src[i+j]:'.')); - printf("\n"); + int i,j; + char buffer[64]; + printf("\n"); + for(i=0; i=32)&&(src[i+j]<128))?src[i+j]:'.')); + } + printf("\n"); // DelayMs(100); - } -} + } + } -void loadbytecode(const char *src) -{ - FILE *f; - int i,n; - f=fopen(src,"rb"); - if (!f) - { - printf("file %s not found\n",src); - return; - } - n=fread(srcbytecode,1,65536,f); - fclose(f); + void loadbytecode(const char *src) + { + FILE *f; + int i,n; + f=fopen(src,"rb"); + if (!f) + { + printf("file %s not found\n",src); + return; + } + n=fread(srcbytecode,1,65536,f); + fclose(f); #ifdef DUMPBC - f=fopen("dumpbc.c","wb"); - if (f) - { - fprintf(f,"const unsigned char dumpbc[%d]={\n",n); - for(i=0;i=32)&&(src[i+j]<128))?src[i+j]:'.')); + int i,j; + char buffer[64]; printf("\n"); + for(i=0; i=32)&&(src[i+j]<128))?src[i+j]:'.')); + } + printf("\n"); // DelayMs(100); - } + } } void loadbytecode(const char *src) { - FILE *f; - int i,n; - f=fopen(src,"rb"); - if (!f) - { - printf("file %s not found\n",src); - return; - } - n=fread(srcbytecode,1,MAXSIZE_BYTECODE,f); - fclose(f); + FILE *f; + int i,n; + f=fopen(src,"rb"); + if (!f) + { + printf("file %s not found\n",src); + return; + } + n=fread(srcbytecode,1,MAXSIZE_BYTECODE,f); + fclose(f); #ifdef DUMPBC - f=fopen("dumpbc.c","wb"); - if (f) - { - fprintf(f,"const unsigned char dumpbc[%d]={\n",n); - for(i=0;i='0' && *s<='9')) s++; - return (*s == '\0'); + while ('\0'!=*s && (*s>='0' && *s<='9')) + { + s++; + } + return (*s == '\0'); } /** @@ -154,52 +174,99 @@ static int my_is_number(const char *s) */ int handle_options(int argc, char **argv) { - int i=1; - int res=1; - - while (i=argc) { usage(); return 0; } - PropSet("MAC", argv[i]); - } else if (!strcmp(argv[i], "--boot")) { - PropSet("BOOT", "firmware"); - } else if (!strcmp(argv[i], "--source")) { - ++i; - if (i>=argc) { usage(); return 0; } - PropSet("SOURCE", argv[i]); - } else if (!strcmp(argv[i], "--logs")) { - ++i; - if (i>=argc) { usage(); return 0; } - my_printf_set_options(argv[i]); - } else if (!strcmp(argv[i], "--logfile")) { - ++i; - if (i>=argc) { usage(); return 0; } - if (my_printf_set_logfile(argv[i])) - { res=0; break; } - } else if (!strcmp(argv[i], "--maxlogsize")) { - ++i; - if (i>=argc || !my_is_number(argv[i])) { usage(); return 0; } - my_printf_set_max_log_size(atoi(argv[i])); - } else if (!strcmp(argv[i], "--maxlogtime")) { - ++i; - if (i>=argc || !my_is_number(argv[i])) { usage(); return 0; } - my_printf_set_max_log_time(atoi(argv[i])); - } else if (!strcmp(argv[i], "--dologtime")) { - my_printf_set_do_log_time(1); - } else { - usage(); - res=0; - break; - } - ++i; - } - - return res; + int i=1; + int res=1; + + while (i=argc) + { + usage(); + return 0; + } + PropSet("MAC", argv[i]); + } + else if (!strcmp(argv[i], "--boot")) + { + PropSet("BOOT", "firmware"); + } + else if (!strcmp(argv[i], "--source")) + { + ++i; + if (i>=argc) + { + usage(); + return 0; + } + PropSet("SOURCE", argv[i]); + } + else if (!strcmp(argv[i], "--logs")) + { + ++i; + if (i>=argc) + { + usage(); + return 0; + } + my_printf_set_options(argv[i]); + } + else if (!strcmp(argv[i], "--logfile")) + { + ++i; + if (i>=argc) + { + usage(); + return 0; + } + if (my_printf_set_logfile(argv[i])) + { + res=0; + break; + } + } + else if (!strcmp(argv[i], "--maxlogsize")) + { + ++i; + if (i>=argc || !my_is_number(argv[i])) + { + usage(); + return 0; + } + my_printf_set_max_log_size(atoi(argv[i])); + } + else if (!strcmp(argv[i], "--maxlogtime")) + { + ++i; + if (i>=argc || !my_is_number(argv[i])) + { + usage(); + return 0; + } + my_printf_set_max_log_time(atoi(argv[i])); + } + else if (!strcmp(argv[i], "--dologtime")) + { + my_printf_set_do_log_time(1); + } + else + { + usage(); + res=0; + break; + } + ++i; + } + + return res; } /** @@ -207,15 +274,15 @@ int handle_options(int argc, char **argv) */ void usage() { - printf("Usage: mtl_simy [--help] [--mac MACADDR] [--boot] [--source SOURCE]\n" \ - " --help: affiche cette aide et quitte\n" \ - " --mac MACADDR: permet de preciser une adresse mac pour le lapin\n" \ - " --boot: comme si on mettait \"BOOT firmware\" dans config.txt\n" \ - " --source SOURCE: compile et charge le bytecode present dans le fichier SOURCE\n" \ - " --logs ,...,: specifie les types de logs qu'on veut afficher, entre init,vm,simunet,simuleds et simuaudio, separes par des virgules\n" \ - " --logfile : specifie le fichier dans lequel ecrire les logs du lapin\n" \ - " --maxlogsize : specifie qu'un fichier de log ne peut depasser octets\n" \ - " --maxlogtime : specifie qu'il doit y avoir une rotation du fichier de log au moins toutes les secondes\n" \ - " --dologtime: specifie qu'on doit afficher sur chaque ligne de log le nombre de secondes ecoulees depuis le lancement du simulateur\n" \ - "Toutes les options sont prioritaires sur les valeurs contenues dans config.txt\n"); + printf("Usage: mtl_simy [--help] [--mac MACADDR] [--boot] [--source SOURCE]\n" \ + " --help: affiche cette aide et quitte\n" \ + " --mac MACADDR: permet de preciser une adresse mac pour le lapin\n" \ + " --boot: comme si on mettait \"BOOT firmware\" dans config.txt\n" \ + " --source SOURCE: compile et charge le bytecode present dans le fichier SOURCE\n" \ + " --logs ,...,: specifie les types de logs qu'on veut afficher, entre init,vm,simunet,simuleds et simuaudio, separes par des virgules\n" \ + " --logfile : specifie le fichier dans lequel ecrire les logs du lapin\n" \ + " --maxlogsize : specifie qu'un fichier de log ne peut depasser octets\n" \ + " --maxlogtime : specifie qu'il doit y avoir une rotation du fichier de log au moins toutes les secondes\n" \ + " --dologtime: specifie qu'on doit afficher sur chaque ligne de log le nombre de secondes ecoulees depuis le lancement du simulateur\n" \ + "Toutes les options sont prioritaires sur les valeurs contenues dans config.txt\n"); } diff --git a/src/simu/linux/simu.c b/src/simu/linux/simu.c index caa871c..d917936 100644 --- a/src/simu/linux/simu.c +++ b/src/simu/linux/simu.c @@ -36,10 +36,16 @@ void checkAllEvents(void); #endif #ifdef VMICROCHIP -int simuInit(){ return 0;} -int simuDoLoop(){ return 0;} -void simuSetLed(vub i,vub val){} -void simuSetMotor(vub i,vub val){} +int simuInit() +{ + return 0; +} +int simuDoLoop() +{ + return 0; +} +void simuSetLed(vub i,vub val) {} +void simuSetMotor(vub i,vub val) {} #else #include @@ -77,17 +83,17 @@ uchar colormul[256*256]; // initialisation des tables de couleur void colortabInit() { - int i,j,k; + int i,j,k; - for(i=0;i<256;i++) - for(j=0;j<256;j++) - { - k=i+j; - coloradd[i+(j<<8)]=(k<=255)?k:255; + for(i=0; i<256; i++) + for(j=0; j<256; j++) + { + k=i+j; + coloradd[i+(j<<8)]=(k<=255)?k:255; - k=i*j/255; - colormul[i+(j<<8)]=k; - } + k=i*j/255; + colormul[i+(j<<8)]=k; + } } // définition physique des leds @@ -111,70 +117,87 @@ int lastmovex=0; int lastmovey=0; #ifdef PROTORABBIT -int diodex[NBLED]={80,80,80,40,40,40,80,80,80,120,120,120,80,80,80}; -int diodey[NBLED]={40,40,40,90,90,90,90,90,90,90,90,90,130,130,130}; +int diodex[NBLED]= {80,80,80,40,40,40,80,80,80,120,120,120,80,80,80}; +int diodey[NBLED]= {40,40,40,90,90,90,90,90,90,90,90,90,130,130,130}; int diodergb[NBLED]= -{0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000 +{ + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000 }; #endif #ifdef PROTODAL -int diodex[NBLED]={40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120}; -int diodey[NBLED]={40,40,40,40,40,40,40,40,40,80,80,80,80,80,80,80,80,80,120,120,120,120,120,120,120,120,120}; +int diodex[NBLED]= {40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120}; +int diodey[NBLED]= {40,40,40,40,40,40,40,40,40,80,80,80,80,80,80,80,80,80,120,120,120,120,120,120,120,120,120}; int diodergb[NBLED]= -{0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000 +{ + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000 }; #endif // TODO -int getButton() { return 0; } +int getButton() +{ + return 0; +} // fonction d'affichage des diodes int simuDisplay(int* intensity) { - // TODO afficher l'état du lapin - return 0; + // TODO afficher l'état du lapin + return 0; } // initialisation du simulateur vsd simuInit() { - int i; - colortabInit(); - - for(i=0;i=NBHOLES-MASKEDHOLES) motorwheel[i]=1; - } + for(i=0; i=NBHOLES-MASKEDHOLES) + { + motorwheel[i]=1; + } + } #endif // setButton(1); - simuaudioinit(); - simunetinit(); - return 0; + simuaudioinit(); + simunetinit(); + return 0; } @@ -183,23 +206,35 @@ vsd simuInit() vsd simuDoLoop() { #ifdef VL_MOTORS - int i,last; + int i,last; #endif - checkAllEvents(); + checkAllEvents(); #ifdef VL_MOTORS - for(i=0;i=MAXMOTORVAL) motorval[i]-=MAXMOTORVAL; - if (last=MAXMOTORVAL) + { + motorval[i]-=MAXMOTORVAL; + } + if (last=NBLED)) return; - diodeval[i]=(val>>16)&255; - diodeval[i+1]=(val>>8)&255; - diodeval[i+2]=(val)&255; - - my_printf(LOG_SIMULEDS, "Setting led %d: [%d %d %d]\n", i/3, diodeval[i], diodeval[i+1], diodeval[i+2]); - my_printf(LOG_SIMULEDS, "All leds state:", i/3, diodeval[i], diodeval[i+1], diodeval[i+2]); - for (i=0; i=NBLED)) + { + return; + } + diodeval[i]=(val>>16)&255; + diodeval[i+1]=(val>>8)&255; + diodeval[i+2]=(val)&255; + + my_printf(LOG_SIMULEDS, "Setting led %d: [%d %d %d]\n", i/3, diodeval[i], diodeval[i+1], diodeval[i+2]); + my_printf(LOG_SIMULEDS, "All leds state:", i/3, diodeval[i], diodeval[i+1], diodeval[i+2]); + for (i=0; i>>>>>>>\n", res); - - // rendre la chaine imprimable - char printable[2048]; - if (res>2048) - res = 2048; - memcpy(printable, buf, 2048); - int i; - for (i=0; i>>>>>>>\n", res); + + // rendre la chaine imprimable + char printable[2048]; + if (res>2048) + { + res = 2048; + } + memcpy(printable, buf, 2048); + int i; + for (i=0; i maxval) - maxval = tcp_sock[i]; - } - } - - int nbevts = select(maxval+1, &fdset_r, NULL /* &fdset_w */, &fdset_err, &timeout); - if (nbevts < 0) - { - my_printf(LOG_SIMUNET, "Sockets : Tcp select failed (%s)\n", strerror(errno)); - return -1; - } - - /*** write events ***/ - for (i=0; i 0) - { - for (i=0; i<=maxval; ++i) - { - if (FD_ISSET(i, &fdset_w)) - { - tcpEventWrite(i); - } - if (FD_ISSET(i, &fdset_r)) - { - tcpEventRead(i); - } - if (FD_ISSET(i, &fdset_err)) - { - // TODO - } - } - } - - return 0; + fd_set fdset_r, fdset_w, fdset_err; + int nfds = 0; + int maxval = 0; // doit pouvoir être dispo directement + struct timeval timeout = {0, 0} ; + int i; + + FD_ZERO(&fdset_r); + FD_ZERO(&fdset_w); + FD_ZERO(&fdset_err); + + for (i=0; i maxval) + { + maxval = tcp_sock[i]; + } + } + } + + int nbevts = select(maxval+1, &fdset_r, NULL /* &fdset_w */, &fdset_err, &timeout); + if (nbevts < 0) + { + my_printf(LOG_SIMUNET, "Sockets : Tcp select failed (%s)\n", strerror(errno)); + return -1; + } + + /*** write events ***/ + for (i=0; i 0) + { + for (i=0; i<=maxval; ++i) + { + if (FD_ISSET(i, &fdset_w)) + { + tcpEventWrite(i); + } + if (FD_ISSET(i, &fdset_r)) + { + tcpEventRead(i); + } + if (FD_ISSET(i, &fdset_err)) + { + // TODO + } + } + } + + return 0; } /** @@ -189,90 +200,109 @@ int checkTcpEvents(void) */ int tcpEventRead(int fd) { - /* soit une donnée à lire, soit un accept, soit un close */ - /* accept si on l'attend, close si on lit 0 data, read sinon */ - int idx = tcpbysock(fd); - - if (idx < 0) - { - // TODO gérer l'erreur - return 0; - } - - if (tcp_listen[idx]) - { - // accept - struct sockaddr_in cor; - int ns; - int sizecor; - int ni,ip,port; - char buf[16]; - - tcp_listen[idx] = 0; - - sizecor=sizeof(cor); - ns=accept(fd,(struct sockaddr*)&cor,&sizecor); - if (ns==-1) return 1; - - ni=tcpgetfree(); - if (ni<0) - { - close(ns); - return 1; - } - - ip=cor.sin_addr.s_addr; - port=ntohs(cor.sin_port); - - tcp_sock[ni]=ns; - - my_printf(LOG_SIMUNET, "Sockets : accept Tcp from %x:%d (socket=%d)\n",ip,port,ns); - VPUSH(INTTOVAL(ni)); - VPUSH(INTTOVAL(2)); - sprintf(buf,"%d",idx); - VPUSH(PNTTOVAL(VMALLOCSTR(buf,strlen(buf)))); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; - } - // donnée ou close - else - { - char buf[2048]; - int res; - if (!tcp_enable[idx]) - { + /* soit une donnée à lire, soit un accept, soit un close */ + /* accept si on l'attend, close si on lit 0 data, read sinon */ + int idx = tcpbysock(fd); + + if (idx < 0) + { + // TODO gérer l'erreur + return 0; + } + + if (tcp_listen[idx]) + { + // accept + struct sockaddr_in cor; + int ns; + int sizecor; + int ni,ip,port; + char buf[16]; + + tcp_listen[idx] = 0; + + sizecor=sizeof(cor); + ns=accept(fd,(struct sockaddr*)&cor,&sizecor); + if (ns==-1) + { + return 1; + } + + ni=tcpgetfree(); + if (ni<0) + { + close(ns); + return 1; + } + + ip=cor.sin_addr.s_addr; + port=ntohs(cor.sin_port); + + tcp_sock[ni]=ns; + + my_printf(LOG_SIMUNET, "Sockets : accept Tcp from %x:%d (socket=%d)\n",ip,port,ns); + VPUSH(INTTOVAL(ni)); + VPUSH(INTTOVAL(2)); + sprintf(buf,"%d",idx); + VPUSH(PNTTOVAL(VMALLOCSTR(buf,strlen(buf)))); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; + } + // donnée ou close + else + { + char buf[2048]; + int res; + if (!tcp_enable[idx]) + { // Sleep(10); // recv(sock,buf,0,0); // printf("disabled\n"); - return 1; - } - - my_printf(LOG_SIMUNET, "Sockets : Read event on %d\n",fd); - res=recv(fd,buf,2048,0); - - helper_write_buffer(buf, res); - - VPUSH(INTTOVAL(idx)); - if (res>0) - { - VPUSH(INTTOVAL(1)); - VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); - } - else - { - VPUSH(INTTOVAL(-1)); - VPUSH(NIL); - } - - VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; - } + return 1; + } + + my_printf(LOG_SIMUNET, "Sockets : Read event on %d\n",fd); + res=recv(fd,buf,2048,0); + + helper_write_buffer(buf, res); + + VPUSH(INTTOVAL(idx)); + if (res>0) + { + VPUSH(INTTOVAL(1)); + VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); + } + else + { + VPUSH(INTTOVAL(-1)); + VPUSH(NIL); + } + + VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; + } } /** @@ -280,23 +310,32 @@ int tcpEventRead(int fd) */ int tcpEventWrite(int fd) { - int idx = tcpbysock(fd); - if (idx<0) { - my_printf(LOG_SIMUNET, "Sockets : idx < 0\n"); - return 0; - } - - tcp_writeEventToNotify[idx] = 0; - - my_printf(LOG_SIMUNET, "Sockets : Write event on %d\n",idx); - VPUSH(INTTOVAL(idx)); - VPUSH(INTTOVAL(0)); - VPUSH(NIL); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; + int idx = tcpbysock(fd); + if (idx<0) + { + my_printf(LOG_SIMUNET, "Sockets : idx < 0\n"); + return 0; + } + + tcp_writeEventToNotify[idx] = 0; + + my_printf(LOG_SIMUNET, "Sockets : Write event on %d\n",idx); + VPUSH(INTTOVAL(idx)); + VPUSH(INTTOVAL(0)); + VPUSH(NIL); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; } /** @@ -304,9 +343,12 @@ int tcpEventWrite(int fd) */ int tcpbysock(int s) { - int i; - for(i=0;i=0)&&(i=0)&&(i=0)&&(i=0)&&(i=0)&&(i=0)&&(i=0) - { - close(udp_sock[i]); - udp_port[i]=0; - } - return i; + int i=udpbyport(port); + if (i>=0) + { + close(udp_sock[i]); + udp_port[i]=0; + } + return i; } int udpsend(int localport,char* dstip,int dstport,char* msg, int len) { - struct sockaddr_in ina; - int i; - - i=udpbyport(localport); - if (i<0) i=udpcreate(localport); - if (i>=0) - { - ina.sin_family = AF_INET; - ina.sin_port = htons((unsigned short)dstport); - ina.sin_addr.s_addr = inet_addr_bin(dstip); - - // hack pour dns. dans le bytecode de simu, on considère que - // 192.168.1.1 est le dns. Si on essaie d'envoyer un paquet à - // cette adresse, il faut translater vers le "vrai" dns. - in_addr_t ip_192_168_1_1 = inet_addr("192.168.1.1"); - if (!memcmp(&(ina.sin_addr),&ip_192_168_1_1, sizeof(ip_192_168_1_1))) { - my_printf(LOG_SIMUNET, "Sockets : (hack) converting 192.168.1.1 to real dns ip\n"); - res_init(); - if (_res.nscount <= 0) { my_printf(LOG_SIMUNET, "Fatal error: no DNS available. Should abort\n"); return 0; }; - ina.sin_addr = _res.nsaddr_list[0].sin_addr; - } - - my_printf(LOG_SIMUNET, "Sockets : send %d bytes on chn %d\n",len,i); - sendto(udp_sock[i],msg,len,0,(struct sockaddr *)&ina,sizeof(ina)); - return 0; - } - return -1; + struct sockaddr_in ina; + int i; + + i=udpbyport(localport); + if (i<0) + { + i=udpcreate(localport); + } + if (i>=0) + { + ina.sin_family = AF_INET; + ina.sin_port = htons((unsigned short)dstport); + ina.sin_addr.s_addr = inet_addr_bin(dstip); + + // hack pour dns. dans le bytecode de simu, on considère que + // 192.168.1.1 est le dns. Si on essaie d'envoyer un paquet à + // cette adresse, il faut translater vers le "vrai" dns. + in_addr_t ip_192_168_1_1 = inet_addr("192.168.1.1"); + if (!memcmp(&(ina.sin_addr),&ip_192_168_1_1, sizeof(ip_192_168_1_1))) + { + my_printf(LOG_SIMUNET, "Sockets : (hack) converting 192.168.1.1 to real dns ip\n"); + res_init(); + if (_res.nscount <= 0) + { + my_printf(LOG_SIMUNET, "Fatal error: no DNS available. Should abort\n"); + return 0; + }; + ina.sin_addr = _res.nsaddr_list[0].sin_addr; + } + + my_printf(LOG_SIMUNET, "Sockets : send %d bytes on chn %d\n",len,i); + sendto(udp_sock[i],msg,len,0,(struct sockaddr *)&ina,sizeof(ina)); + return 0; + } + return -1; } int checkUdpEvents(void) { - fd_set fdset_r, fdset_w, fdset_err; - int nfds = 0; - int maxval = 0; // doit pouvoir être dispo directement - struct timeval timeout = {0, 0} ; - int i; - - FD_ZERO(&fdset_r); - FD_ZERO(&fdset_w); - FD_ZERO(&fdset_err); - - for (i=0; i maxval) - maxval = udp_sock[i]; - } - } - - int nbevts = select(maxval+1, &fdset_r, &fdset_w, &fdset_err, &timeout); - - if (nbevts < 0) - { - my_printf(LOG_SIMUNET, "Sockets : Udp select failed (%s)\n", strerror(errno)); - return -1; - } - - if (nbevts > 0) - { - for (i=0; i<=maxval; ++i) - { - if (FD_ISSET(i, &fdset_r)) - { - udpEventRead(i); - } - } - } - - return 0; + fd_set fdset_r, fdset_w, fdset_err; + int nfds = 0; + int maxval = 0; // doit pouvoir être dispo directement + struct timeval timeout = {0, 0} ; + int i; + + FD_ZERO(&fdset_r); + FD_ZERO(&fdset_w); + FD_ZERO(&fdset_err); + + for (i=0; i maxval) + { + maxval = udp_sock[i]; + } + } + } + + int nbevts = select(maxval+1, &fdset_r, &fdset_w, &fdset_err, &timeout); + + if (nbevts < 0) + { + my_printf(LOG_SIMUNET, "Sockets : Udp select failed (%s)\n", strerror(errno)); + return -1; + } + + if (nbevts > 0) + { + for (i=0; i<=maxval; ++i) + { + if (FD_ISSET(i, &fdset_r)) + { + udpEventRead(i); + } + } + } + + return 0; } int udpEventRead(int fd) { - char buf[4096]; - struct sockaddr_in add; - int i=udpbysock(fd); - int l=sizeof(add); - int res=recvfrom(fd,buf,4096,0,(struct sockaddr *)&add,&l); - if (res<0) return 1; - - my_printf(LOG_SIMUNET, "Sockets : UDP Read %d bytes on :%d from %s:%d\n",res,udp_port[i],inet_ntoa(add.sin_addr),ntohs(add.sin_port)); - VPUSH(INTTOVAL(i)); - VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); - VPUSH(PNTTOVAL(VMALLOCSTR((char*)&add.sin_addr.s_addr,4))); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBUDP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; + char buf[4096]; + struct sockaddr_in add; + int i=udpbysock(fd); + int l=sizeof(add); + int res=recvfrom(fd,buf,4096,0,(struct sockaddr *)&add,&l); + if (res<0) + { + return 1; + } + + my_printf(LOG_SIMUNET, "Sockets : UDP Read %d bytes on :%d from %s:%d\n",res,udp_port[i],inet_ntoa(add.sin_addr),ntohs(add.sin_port)); + VPUSH(INTTOVAL(i)); + VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); + VPUSH(PNTTOVAL(VMALLOCSTR((char*)&add.sin_addr.s_addr,4))); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBUDP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; } diff --git a/src/simu/log.c b/src/simu/log.c index 7d8b96c..066d544 100644 --- a/src/simu/log.c +++ b/src/simu/log.c @@ -37,23 +37,30 @@ static void my_printf_check_log_roll(void); */ static const char *get_prefix(e_logtypes t) { - const char *res = NULL; - int i; - switch (t) { - case LOG_INIT: - res = "init"; break; - case LOG_VM: - res = "vm"; break; - case LOG_SIMUNET: - res = "simunet"; break; - case LOG_SIMUAUDIO: - res = "simuaudio"; break; - case LOG_SIMULEDS: - res = "simuleds"; break; - case LOG_SIMUMOTORS: - res = "simumotors"; break; - } - return res; + const char *res = NULL; + int i; + switch (t) + { + case LOG_INIT: + res = "init"; + break; + case LOG_VM: + res = "vm"; + break; + case LOG_SIMUNET: + res = "simunet"; + break; + case LOG_SIMUAUDIO: + res = "simuaudio"; + break; + case LOG_SIMULEDS: + res = "simuleds"; + break; + case LOG_SIMUMOTORS: + res = "simumotors"; + break; + } + return res; } /** @@ -63,32 +70,39 @@ static const char *get_prefix(e_logtypes t) */ static int print_line_by_line(e_logtypes t, char *s, int force) { - char *tmp; - int nbwritten=0; - char pfx[256]; - if (do_log_time) - snprintf(pfx,255, "%06d %s", time(NULL) - time_first_log, get_prefix(t)); - else - strncpy(pfx, get_prefix(t), 255); - while (*s!='\0') - { - tmp = strchr(s, '\n'); - if (NULL!=tmp) { - *tmp='\0'; - current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s); - *tmp='\n'; - nbwritten+=(tmp-s)+1; - s=tmp+1; - } - else { - if (force) { - current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s); - nbwritten+=strlen(s); - } - break; - } - } - return nbwritten; + char *tmp; + int nbwritten=0; + char pfx[256]; + if (do_log_time) + { + snprintf(pfx,255, "%06d %s", time(NULL) - time_first_log, get_prefix(t)); + } + else + { + strncpy(pfx, get_prefix(t), 255); + } + while (*s!='\0') + { + tmp = strchr(s, '\n'); + if (NULL!=tmp) + { + *tmp='\0'; + current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s); + *tmp='\n'; + nbwritten+=(tmp-s)+1; + s=tmp+1; + } + else + { + if (force) + { + current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s); + nbwritten+=strlen(s); + } + break; + } + } + return nbwritten; } /** @@ -98,55 +112,61 @@ static int print_line_by_line(e_logtypes t, char *s, int force) */ void my_printf(e_logtypes t, const char *fmt, ...) { - if (NULL==outstream) - outstream = stdout; - - my_printf_check_log_roll(); - - if (0==time_first_log && do_log_time) - { - char tmp[256]; - time_first_log = time(NULL); - snprintf(tmp, 255, "First log time: %s", ctime(&time_first_log)); - print_line_by_line(LOG_INIT, tmp, 0); - } - - int len, nbwritten; - - if (0==((int)t & my_msk)) - return; - - if (siml && siml_t!=t) - { - // il faut "flusher" ce qu'il reste dans le buffer - print_line_by_line(siml_t, tmpbuf, 1); - siml=0; - siml_idx=0; - } - - va_list args; - va_start(args, fmt); - len=vsnprintf(tmpbuf+siml_idx, BUFSIZE-1-siml_idx, fmt , args); - va_end(args); - - // make_writeable(tmpbuf); ? s'il y a des caractères non imprimables ? - nbwritten=print_line_by_line(t, tmpbuf, 0); - - len += siml_idx; - if(nbwritten en .0 - my_printf_rotate_log_n(-1); - char *tmp; - tmp=strdup(outfilename); - my_printf_set_logfile(tmp); - free(tmp); + // décaler tous les logs existants et en .0 + my_printf_rotate_log_n(-1); + char *tmp; + tmp=strdup(outfilename); + my_printf_set_logfile(tmp); + free(tmp); } /** @@ -247,19 +281,25 @@ static int my_printf_rotate_log(void) */ static int my_printf_rotate_log_n(int n) { - char tmpn[255]; - char tmpnplusun[255]; - struct stat ststat; - - if (n<0) - strcpy(tmpn, outfilename); - else - snprintf(tmpn, 255, "%s.%d", outfilename, n); - snprintf(tmpnplusun, 255, "%s.%d", outfilename, n+1); - - if (!stat(tmpnplusun, &ststat)) - my_printf_rotate_log_n(n+1); - rename(tmpn, tmpnplusun); + char tmpn[255]; + char tmpnplusun[255]; + struct stat ststat; + + if (n<0) + { + strcpy(tmpn, outfilename); + } + else + { + snprintf(tmpn, 255, "%s.%d", outfilename, n); + } + snprintf(tmpnplusun, 255, "%s.%d", outfilename, n+1); + + if (!stat(tmpnplusun, &ststat)) + { + my_printf_rotate_log_n(n+1); + } + rename(tmpn, tmpnplusun); } /** @@ -267,30 +307,34 @@ static int my_printf_rotate_log_n(int n) */ static void my_printf_check_log_roll(void) { - int need_roll=0; - - if (0!=max_log_size) - need_roll += (current_log_size > max_log_size); - if (0!=max_log_time) - { - need_roll += (((int)time(NULL) - last_log_roll) > max_log_time); - } - - if (need_roll) - my_printf_rotate_log(); + int need_roll=0; + + if (0!=max_log_size) + { + need_roll += (current_log_size > max_log_size); + } + if (0!=max_log_time) + { + need_roll += (((int)time(NULL) - last_log_roll) > max_log_time); + } + + if (need_roll) + { + my_printf_rotate_log(); + } } void my_printf_set_max_log_size(int n) { - max_log_size=n; + max_log_size=n; } void my_printf_set_max_log_time(int n) { - max_log_time=n; + max_log_time=n; } void my_printf_set_do_log_time(int n) { - do_log_time=n; + do_log_time=n; } diff --git a/src/simu/properties.c b/src/simu/properties.c index 23361ad..824ea80 100644 --- a/src/simu/properties.c +++ b/src/simu/properties.c @@ -11,9 +11,9 @@ struct Prop { - char* name; - char* val; - struct Prop* nxt; + char* name; + char* val; + struct Prop* nxt; }; typedef struct Prop* prop; @@ -24,65 +24,89 @@ struct Prop *Properties; // il ne faudra pas oublier de désalloué le buffer, par un appel à fileReleaseBuffer char* fileLoadL(const char *filename,int *length) { - FILE* file; - int size; - char *buffer; - - file=fopen(filename,"rb"); - if (!file) return NULL; - - fseek(file,0,SEEK_END); - size=ftell(file); - fseek(file,0,SEEK_SET); - - buffer=(char*)malloc(size+1); - size=fread((void*)buffer,1,size,file); - buffer[size]=0; - fclose(file); - if (length) *length=size; - return buffer; + FILE* file; + int size; + char *buffer; + + file=fopen(filename,"rb"); + if (!file) + { + return NULL; + } + + fseek(file,0,SEEK_END); + size=ftell(file); + fseek(file,0,SEEK_SET); + + buffer=(char*)malloc(size+1); + size=fread((void*)buffer,1,size,file); + buffer[size]=0; + fclose(file); + if (length) + { + *length=size; + } + return buffer; } char* fileLoad(const char *filename) { - return fileLoadL(filename,NULL); + return fileLoadL(filename,NULL); } // libère le buffer retourné par fileLoad void fileReleaseBuffer(char* buffer) { - if (buffer) free((void*)buffer); + if (buffer) + { + free((void*)buffer); + } } // lecture d'une chaîne hexadécimale (s'arrête au premier caractère incorrect) int htoi(const char* src) { - int x,c; - x=0; - while(c=*src++) - { - if ((c>='0')&&(c<='9')) x=(x<<4)+c-'0'; - else if ((c>='A')&&(c<='F')) x=(x<<4)+c-'A'+10; - else if ((c>='a')&&(c<='f')) x=(x<<4)+c-'a'+10; - else return x; - } - return x; + int x,c; + x=0; + while(c=*src++) + { + if ((c>='0')&&(c<='9')) + { + x=(x<<4)+c-'0'; + } + else if ((c>='A')&&(c<='F')) + { + x=(x<<4)+c-'A'+10; + } + else if ((c>='a')&&(c<='f')) + { + x=(x<<4)+c-'a'+10; + } + else + { + return x; + } + } + return x; } // alloue une chaîne char* stringStore(const char *src,int len) { - char* s=(char*)malloc(len+1); - memcpy(s,src,len); - s[len]=0; - return s; + char* s=(char*)malloc(len+1); + memcpy(s,src,len); + s[len]=0; + return s; } // désalloue une chaîne void stringRelease(char *src) { - if (src) free((void*)src); + if (src) + { + free((void*)src); + } } @@ -90,204 +114,255 @@ void stringRelease(char *src) // ignore les espaces initiaux, et s'arrête au premier espace ou caractère spécial rencontré void suppspace(char* begin,int len, char **wordbegin, int *wordlen) { - int i=0; - int j; - - while((i0)&&(begin[i]<=32)) i++; // suppression des espaces - *wordbegin=begin+i; - - j=i; - while((j32)||(begin[j]<0))) j++; // aller jusqu'au bout du tag - *wordlen=j-i; + int i=0; + int j; + + while((i0)&&(begin[i]<=32)) + { + i++; // suppression des espaces + } + *wordbegin=begin+i; + + j=i; + while((j32)||(begin[j]<0))) + { + j++; // aller jusqu'au bout du tag + } + *wordlen=j-i; } // allocation de mémoire pour la structure prop prop propAlloc() { - prop pnt=(prop)malloc(sizeof(struct Prop)); - return pnt; + prop pnt=(prop)malloc(sizeof(struct Prop)); + return pnt; } // désallocation de mémoire pour la structure prop void propFree(prop pnt) { - if (pnt) free((void*)pnt); + if (pnt) + { + free((void*)pnt); + } } // détruit une prop, avec tous ses champs void propRelease(prop pnt) { - if (pnt) - { - propRelease(pnt->nxt); - stringRelease(pnt->name); - stringRelease(pnt->val); - propFree(pnt); - } + if (pnt) + { + propRelease(pnt->nxt); + stringRelease(pnt->name); + stringRelease(pnt->val); + propFree(pnt); + } } // ajout d'un champ à une structure prop 'nxt' prop propAdd(const char *name,int namelen,const char *val,int vallen,prop nxt) { - char *sname,*sval; + char *sname,*sval; - if (name[0] == ';') - return nxt; + if (name[0] == ';') + { + return nxt; + } - prop pnt=propAlloc(); - sname=stringStore(name,namelen); - sval=stringStore(val,vallen); + prop pnt=propAlloc(); + sname=stringStore(name,namelen); + sval=stringStore(val,vallen); - pnt->name=sname; - pnt->val=sval; - pnt->nxt=nxt; - return pnt; + pnt->name=sname; + pnt->val=sval; + pnt->nxt=nxt; + return pnt; } // inverse la liste de propriétés prop propReverse(prop pnt) { - prop last=NULL; - prop nxt=NULL; - while(pnt) - { - nxt=pnt->nxt; - pnt->nxt=last; - if (!nxt) return pnt; - last=pnt; - pnt=nxt; - } - return NULL; + prop last=NULL; + prop nxt=NULL; + while(pnt) + { + nxt=pnt->nxt; + pnt->nxt=last; + if (!nxt) + { + return pnt; + } + last=pnt; + pnt=nxt; + } + return NULL; } // parsing d'un buffer rempli avec le contenu d'un fichier de prop prop propParseInv(char *src) { - prop pnt=NULL; // pointeur vers la property en construction - int i,name_b,name_e,val_b,val_e; - - i=0; - while(src[i]) - { - while((src[i])&&(src[i]>0)&&(src[i]<=32)) i++; - if (!src[i]) return pnt; - name_b=i; - while(src[i]>32) i++; - if (!src[i]) return pnt; - name_e=i; - while((src[i])&&((src[i]==32)||(src[i]==9))) i++; - if (!src[i]) return pnt; - if (src[i]>32) // il y a bien un argument - { - val_b=i; - while((src[i]>=32)||(src[i]<0)) i++; - val_e=i; - pnt=propAdd(src+name_b,name_e-name_b,src+val_b,val_e-val_b,pnt); - } - else pnt=propAdd(src+name_b,name_e-name_b,src,0,pnt); - } - return pnt; + prop pnt=NULL; // pointeur vers la property en construction + int i,name_b,name_e,val_b,val_e; + + i=0; + while(src[i]) + { + while((src[i])&&(src[i]>0)&&(src[i]<=32)) + { + i++; + } + if (!src[i]) + { + return pnt; + } + name_b=i; + while(src[i]>32) + { + i++; + } + if (!src[i]) + { + return pnt; + } + name_e=i; + while((src[i])&&((src[i]==32)||(src[i]==9))) + { + i++; + } + if (!src[i]) + { + return pnt; + } + if (src[i]>32) // il y a bien un argument + { + val_b=i; + while((src[i]>=32)||(src[i]<0)) + { + i++; + } + val_e=i; + pnt=propAdd(src+name_b,name_e-name_b,src+val_b,val_e-val_b,pnt); + } + else + { + pnt=propAdd(src+name_b,name_e-name_b,src,0,pnt); + } + } + return pnt; } // parsing d'un buffer rempli avec le contenu d'un fichier de prop prop propParse(char *src) { - return propReverse(propParseInv(src)); + return propReverse(propParseInv(src)); } // retourne le premier élément prop propFirst(prop pnt) { - return pnt; + return pnt; } // retourne l'élément suivant prop propNext(prop pnt) { - return pnt->nxt; + return pnt->nxt; } // charge un fichier de prop // retourne un pointeur vers la structure prop du dernier champ prop propLoad(const char *file) { - prop pnt=NULL; // pointeur vers la property en construction - char* src=fileLoad(file); - - if (!src) return NULL; - pnt=propParse(src); - fileReleaseBuffer(src); - return pnt; + prop pnt=NULL; // pointeur vers la property en construction + char* src=fileLoad(file); + + if (!src) + { + return NULL; + } + pnt=propParse(src); + fileReleaseBuffer(src); + return pnt; } // dump d'une prop sur la sortie standard void propDump(prop pnt) { - while(pnt) - { - my_printf(LOG_INIT, "%s := %s\n",pnt->name,pnt->val); - pnt=pnt->nxt; - } + while(pnt) + { + my_printf(LOG_INIT, "%s := %s\n",pnt->name,pnt->val); + pnt=pnt->nxt; + } } // retourne la valeur d'un champ char* propGet(prop pnt,const char *field) { - while(pnt) - { - if (!strcmp(pnt->name,field)) return pnt->val; - pnt=pnt->nxt; - } - return emptystring; + while(pnt) + { + if (!strcmp(pnt->name,field)) + { + return pnt->val; + } + pnt=pnt->nxt; + } + return emptystring; } // retourne la valeur entière d'un champ int propGetInt(prop pnt,const char *field) { - char *p=propGet(pnt,field); - if ((p[0]=='0')&&((p[1]=='x')||(p[1]=='X'))) return htoi(p+2); - return atoi(p); + char *p=propGet(pnt,field); + if ((p[0]=='0')&&((p[1]=='x')||(p[1]=='X'))) + { + return htoi(p+2); + } + return atoi(p); } int PropLoad(const char* file) { - Properties=propLoad(file); - return 0; + Properties=propLoad(file); + return 0; } // retourne la valeur d'un champ char* PropGet(const char *field) { - return propGet(Properties,field); + return propGet(Properties,field); } // retourne la valeur entière d'un champ int PropGetInt(const char *field) { - return propGetInt(Properties,field); + return propGetInt(Properties,field); } // efface la valeur d'un champ s'il est présent, chaine void PropSet(const char *field, const char *val) { - prop tmp = Properties; - - while (tmp && strcmp(tmp->name, field)) - tmp = tmp->nxt; - if (!tmp) - Properties = propAdd(field,strlen(field),val,strlen(val),Properties); - else - { - stringRelease(tmp->val); - tmp->val = stringStore(val, strlen(val)); - } + prop tmp = Properties; + + while (tmp && strcmp(tmp->name, field)) + { + tmp = tmp->nxt; + } + if (!tmp) + { + Properties = propAdd(field,strlen(field),val,strlen(val),Properties); + } + else + { + stringRelease(tmp->val); + tmp->val = stringStore(val, strlen(val)); + } } // affiche toutes les prop sur la sortie standard void PropDump() { - propDump(Properties); + propDump(Properties); } diff --git a/src/simu/win/mp3/common.c b/src/simu/win/mp3/common.c index 52c280c..d5c5cad 100644 --- a/src/simu/win/mp3/common.c +++ b/src/simu/win/mp3/common.c @@ -9,21 +9,25 @@ #include "mpg123.h" -struct parameter param = { 1 , 1 , 0 , 0 }; +struct parameter param = { 1, 1, 0, 0 }; -int tabsel_123[2][3][16] = { - { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, - {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, - {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, - - { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, - {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, - {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } +int tabsel_123[2][3][16] = +{ + { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, + {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, + {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} + }, + + { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, + {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, + {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} + } }; long freqs[9] = { 44100, 48000, 32000, - 22050, 24000, 16000 , - 11025 , 12000 , 8000 }; + 22050, 24000, 16000, + 11025, 12000, 8000 + }; int bitindex; unsigned char *wordpointer; @@ -34,27 +38,36 @@ int pcm_point = 0; #if 0 static void get_II_stuff(struct frame *fr) { - static int translate[3][2][16] = - { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } , - { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } , - { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } , - { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } , - { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } , - { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } }; - - int table,sblim; - static struct al_table *tables[5] = - { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 }; - static int sblims[5] = { 27 , 30 , 8, 12 , 30 }; - - if(fr->lsf) - table = 4; - else - table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; - sblim = sblims[table]; - - fr->alloc = tables[table]; - fr->II_sblimit = sblim; + static int translate[3][2][16] = + { + { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 }, + { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } + }, + { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 }, + { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } + }, + { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 }, + { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } + } + }; + + int table,sblim; + static struct al_table *tables[5] = + { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 }; + static int sblims[5] = { 27, 30, 8, 12, 30 }; + + if(fr->lsf) + { + table = 4; + } + else + { + table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; + } + sblim = sblims[table]; + + fr->alloc = tables[table]; + fr->II_sblimit = sblim; } #endif @@ -64,13 +77,21 @@ static void get_II_stuff(struct frame *fr) int head_check(unsigned long head) { if( (head & 0xffe00000) != 0xffe00000) - return FALSE; + { + return FALSE; + } if(!((head>>17)&3)) - return FALSE; + { + return FALSE; + } if( ((head>>12)&0xf) == 0xf) - return FALSE; + { + return FALSE; + } if( ((head>>10)&0x3) == 0x3 ) - return FALSE; + { + return FALSE; + } return TRUE; } #endif @@ -83,30 +104,38 @@ int decode_header(struct frame *fr,unsigned long newhead) { - if( newhead & (1<<20) ) { - fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; - fr->mpeg25 = 0; + if( newhead & (1<<20) ) + { + fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; + fr->mpeg25 = 0; } - else { - fr->lsf = 1; - fr->mpeg25 = 1; + else + { + fr->lsf = 1; + fr->mpeg25 = 1; } - + fr->lay = 4-((newhead>>17)&3); - if( ((newhead>>10)&0x3) == 0x3) { - fprintf(stderr,"Stream error\n"); + if( ((newhead>>10)&0x3) == 0x3) + { + fprintf(stderr,"Stream error\n"); // MMechostr(1,"MP3 Stream error\n"); - return -1; + return -1; } - if(fr->mpeg25) { - fr->sampling_frequency = 6 + ((newhead>>10)&0x3); + if(fr->mpeg25) + { + fr->sampling_frequency = 6 + ((newhead>>10)&0x3); } else - fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); + { + fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); + } fr->error_protection = ((newhead>>16)&0x1)^0x1; if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */ - fr->bitrate_index = ((newhead>>12)&0xf); + { + fr->bitrate_index = ((newhead>>12)&0xf); + } fr->bitrate_index = ((newhead>>12)&0xf); fr->padding = ((newhead>>9)&0x1); @@ -121,18 +150,18 @@ int decode_header(struct frame *fr,unsigned long newhead) if(!fr->bitrate_index) { - fprintf(stderr,"Free format not supported.\n"); - return (0); + fprintf(stderr,"Free format not supported.\n"); + return (0); } //fprintf(stderr,"newhead = %x\n",newhead); //fprintf(stderr,"layer = %d\n",fr->lay); switch(fr->lay) { - case 1: + case 1: #if 0 - fr->do_layer = do_layer1; - fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? - (fr->mode_ext<<2)+4 : 32; + fr->do_layer = do_layer1; + fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? + (fr->mode_ext<<2)+4 : 32; fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000; fr->framesize /= freqs[fr->sampling_frequency]; fr->framesize = ((fr->framesize+fr->padding)<<2)-4; @@ -140,12 +169,12 @@ int decode_header(struct frame *fr,unsigned long newhead) fprintf(stderr,"Not supported!\n"); #endif break; - case 2: + case 2: #if 0 - fr->do_layer = do_layer2; + fr->do_layer = do_layer2; get_II_stuff(fr); fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? - (fr->mode_ext<<2)+4 : fr->II_sblimit; + (fr->mode_ext<<2)+4 : fr->II_sblimit; fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000; fr->framesize /= freqs[fr->sampling_frequency]; fr->framesize += fr->padding - 4; @@ -153,25 +182,31 @@ int decode_header(struct frame *fr,unsigned long newhead) fprintf(stderr,"Not supported!\n"); #endif break; - case 3: + case 3: #if 0 fr->do_layer = do_layer3; if(fr->lsf) - ssize = (fr->stereo == 1) ? 9 : 17; + { + ssize = (fr->stereo == 1) ? 9 : 17; + } else - ssize = (fr->stereo == 1) ? 17 : 32; + { + ssize = (fr->stereo == 1) ? 17 : 32; + } #endif #if 0 if(fr->error_protection) - ssize += 2; + { + ssize += 2; + } #endif - fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000; - fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); - fr->framesize = fr->framesize + fr->padding - 4; - break; - default: - fprintf(stderr,"Sorry, unknown layer type.\n"); + fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000; + fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); + fr->framesize = fr->framesize + fr->padding - 4; + break; + default: + fprintf(stderr,"Sorry, unknown layer type.\n"); return (0); } @@ -184,79 +219,81 @@ int decode_header(struct frame *fr,unsigned long newhead) #if 1 void print_header(struct frame *fr) { - static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; - static char *layers[4] = { "Unknown" , "I", "II", "III" }; - - fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", - fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), - layers[fr->lay],freqs[fr->sampling_frequency], - modes[fr->mode],fr->mode_ext,fr->framesize+4); - fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n", - fr->stereo,fr->copyright?"Yes":"No", - fr->original?"Yes":"No",fr->error_protection?"Yes":"No", - fr->emphasis); - fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n", - tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension); + static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; + static char *layers[4] = { "Unknown", "I", "II", "III" }; + + fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", + fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), + layers[fr->lay],freqs[fr->sampling_frequency], + modes[fr->mode],fr->mode_ext,fr->framesize+4); + fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n", + fr->stereo,fr->copyright?"Yes":"No", + fr->original?"Yes":"No",fr->error_protection?"Yes":"No", + fr->emphasis); + fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n", + tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension); } void print_header_compact(struct frame *fr) { - static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" }; - static char *layers[4] = { "Unknown" , "I", "II", "III" }; - - fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n", - fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), - layers[fr->lay], - tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index], - freqs[fr->sampling_frequency], modes[fr->mode]); + static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" }; + static char *layers[4] = { "Unknown", "I", "II", "III" }; + + fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n", + fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), + layers[fr->lay], + tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index], + freqs[fr->sampling_frequency], modes[fr->mode]); } #endif unsigned int getbits(int number_of_bits) { - unsigned long rval; + unsigned long rval; - if(!number_of_bits) - return 0; + if(!number_of_bits) + { + return 0; + } - { - rval = wordpointer[0]; - rval <<= 8; - rval |= wordpointer[1]; - rval <<= 8; - rval |= wordpointer[2]; - rval <<= bitindex; - rval &= 0xffffff; + { + rval = wordpointer[0]; + rval <<= 8; + rval |= wordpointer[1]; + rval <<= 8; + rval |= wordpointer[2]; + rval <<= bitindex; + rval &= 0xffffff; - bitindex += number_of_bits; + bitindex += number_of_bits; - rval >>= (24-number_of_bits); + rval >>= (24-number_of_bits); - wordpointer += (bitindex>>3); - bitindex &= 7; - } - return rval; + wordpointer += (bitindex>>3); + bitindex &= 7; + } + return rval; } unsigned int getbits_fast(int number_of_bits) { - unsigned long rval; - - { - rval = wordpointer[0]; - rval <<= 8; - rval |= wordpointer[1]; - rval <<= bitindex; - rval &= 0xffff; - bitindex += number_of_bits; - - rval >>= (16-number_of_bits); - - wordpointer += (bitindex>>3); - bitindex &= 7; - } - return rval; + unsigned long rval; + + { + rval = wordpointer[0]; + rval <<= 8; + rval |= wordpointer[1]; + rval <<= bitindex; + rval &= 0xffff; + bitindex += number_of_bits; + + rval >>= (16-number_of_bits); + + wordpointer += (bitindex>>3); + bitindex &= 7; + } + return rval; } diff --git a/src/simu/win/mp3/dct64_i386.c b/src/simu/win/mp3/dct64_i386.c index b737957..5181821 100644 --- a/src/simu/win/mp3/dct64_i386.c +++ b/src/simu/win/mp3/dct64_i386.c @@ -1,7 +1,7 @@ //#ifdef HAVEGTK /* * Discrete Cosine Tansform (DCT) for subband synthesis - * optimized for machines with no auto-increment. + * optimized for machines with no auto-increment. * The performance is highly compiler dependend. Maybe * the dct64.c version for 'normal' processor may be faster * even for Intel processors. @@ -12,295 +12,295 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) { - { - register real *costab = pnts[0]; - - b1[0x00] = samples[0x00] + samples[0x1F]; - b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0]; - - b1[0x01] = samples[0x01] + samples[0x1E]; - b1[0x1E] = (samples[0x01] - samples[0x1E]) * costab[0x1]; - - b1[0x02] = samples[0x02] + samples[0x1D]; - b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2]; - - b1[0x03] = samples[0x03] + samples[0x1C]; - b1[0x1C] = (samples[0x03] - samples[0x1C]) * costab[0x3]; - - b1[0x04] = samples[0x04] + samples[0x1B]; - b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4]; - - b1[0x05] = samples[0x05] + samples[0x1A]; - b1[0x1A] = (samples[0x05] - samples[0x1A]) * costab[0x5]; - - b1[0x06] = samples[0x06] + samples[0x19]; - b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6]; - - b1[0x07] = samples[0x07] + samples[0x18]; - b1[0x18] = (samples[0x07] - samples[0x18]) * costab[0x7]; - - b1[0x08] = samples[0x08] + samples[0x17]; - b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8]; - - b1[0x09] = samples[0x09] + samples[0x16]; - b1[0x16] = (samples[0x09] - samples[0x16]) * costab[0x9]; - - b1[0x0A] = samples[0x0A] + samples[0x15]; - b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA]; - - b1[0x0B] = samples[0x0B] + samples[0x14]; - b1[0x14] = (samples[0x0B] - samples[0x14]) * costab[0xB]; - - b1[0x0C] = samples[0x0C] + samples[0x13]; - b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC]; - - b1[0x0D] = samples[0x0D] + samples[0x12]; - b1[0x12] = (samples[0x0D] - samples[0x12]) * costab[0xD]; - - b1[0x0E] = samples[0x0E] + samples[0x11]; - b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE]; - - b1[0x0F] = samples[0x0F] + samples[0x10]; - b1[0x10] = (samples[0x0F] - samples[0x10]) * costab[0xF]; - } - - - { - register real *costab = pnts[1]; - - b2[0x00] = b1[0x00] + b1[0x0F]; - b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0]; - b2[0x01] = b1[0x01] + b1[0x0E]; - b2[0x0E] = (b1[0x01] - b1[0x0E]) * costab[1]; - b2[0x02] = b1[0x02] + b1[0x0D]; - b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2]; - b2[0x03] = b1[0x03] + b1[0x0C]; - b2[0x0C] = (b1[0x03] - b1[0x0C]) * costab[3]; - b2[0x04] = b1[0x04] + b1[0x0B]; - b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4]; - b2[0x05] = b1[0x05] + b1[0x0A]; - b2[0x0A] = (b1[0x05] - b1[0x0A]) * costab[5]; - b2[0x06] = b1[0x06] + b1[0x09]; - b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6]; - b2[0x07] = b1[0x07] + b1[0x08]; - b2[0x08] = (b1[0x07] - b1[0x08]) * costab[7]; - - b2[0x10] = b1[0x10] + b1[0x1F]; - b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0]; - b2[0x11] = b1[0x11] + b1[0x1E]; - b2[0x1E] = (b1[0x1E] - b1[0x11]) * costab[1]; - b2[0x12] = b1[0x12] + b1[0x1D]; - b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2]; - b2[0x13] = b1[0x13] + b1[0x1C]; - b2[0x1C] = (b1[0x1C] - b1[0x13]) * costab[3]; - b2[0x14] = b1[0x14] + b1[0x1B]; - b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4]; - b2[0x15] = b1[0x15] + b1[0x1A]; - b2[0x1A] = (b1[0x1A] - b1[0x15]) * costab[5]; - b2[0x16] = b1[0x16] + b1[0x19]; - b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6]; - b2[0x17] = b1[0x17] + b1[0x18]; - b2[0x18] = (b1[0x18] - b1[0x17]) * costab[7]; - } - - { - register real *costab = pnts[2]; - - b1[0x00] = b2[0x00] + b2[0x07]; - b1[0x07] = (b2[0x00] - b2[0x07]) * costab[0]; - b1[0x01] = b2[0x01] + b2[0x06]; - b1[0x06] = (b2[0x01] - b2[0x06]) * costab[1]; - b1[0x02] = b2[0x02] + b2[0x05]; - b1[0x05] = (b2[0x02] - b2[0x05]) * costab[2]; - b1[0x03] = b2[0x03] + b2[0x04]; - b1[0x04] = (b2[0x03] - b2[0x04]) * costab[3]; - - b1[0x08] = b2[0x08] + b2[0x0F]; - b1[0x0F] = (b2[0x0F] - b2[0x08]) * costab[0]; - b1[0x09] = b2[0x09] + b2[0x0E]; - b1[0x0E] = (b2[0x0E] - b2[0x09]) * costab[1]; - b1[0x0A] = b2[0x0A] + b2[0x0D]; - b1[0x0D] = (b2[0x0D] - b2[0x0A]) * costab[2]; - b1[0x0B] = b2[0x0B] + b2[0x0C]; - b1[0x0C] = (b2[0x0C] - b2[0x0B]) * costab[3]; - - b1[0x10] = b2[0x10] + b2[0x17]; - b1[0x17] = (b2[0x10] - b2[0x17]) * costab[0]; - b1[0x11] = b2[0x11] + b2[0x16]; - b1[0x16] = (b2[0x11] - b2[0x16]) * costab[1]; - b1[0x12] = b2[0x12] + b2[0x15]; - b1[0x15] = (b2[0x12] - b2[0x15]) * costab[2]; - b1[0x13] = b2[0x13] + b2[0x14]; - b1[0x14] = (b2[0x13] - b2[0x14]) * costab[3]; - - b1[0x18] = b2[0x18] + b2[0x1F]; - b1[0x1F] = (b2[0x1F] - b2[0x18]) * costab[0]; - b1[0x19] = b2[0x19] + b2[0x1E]; - b1[0x1E] = (b2[0x1E] - b2[0x19]) * costab[1]; - b1[0x1A] = b2[0x1A] + b2[0x1D]; - b1[0x1D] = (b2[0x1D] - b2[0x1A]) * costab[2]; - b1[0x1B] = b2[0x1B] + b2[0x1C]; - b1[0x1C] = (b2[0x1C] - b2[0x1B]) * costab[3]; - } - - { - register real const cos0 = pnts[3][0]; - register real const cos1 = pnts[3][1]; - - b2[0x00] = b1[0x00] + b1[0x03]; - b2[0x03] = (b1[0x00] - b1[0x03]) * cos0; - b2[0x01] = b1[0x01] + b1[0x02]; - b2[0x02] = (b1[0x01] - b1[0x02]) * cos1; - - b2[0x04] = b1[0x04] + b1[0x07]; - b2[0x07] = (b1[0x07] - b1[0x04]) * cos0; - b2[0x05] = b1[0x05] + b1[0x06]; - b2[0x06] = (b1[0x06] - b1[0x05]) * cos1; - - b2[0x08] = b1[0x08] + b1[0x0B]; - b2[0x0B] = (b1[0x08] - b1[0x0B]) * cos0; - b2[0x09] = b1[0x09] + b1[0x0A]; - b2[0x0A] = (b1[0x09] - b1[0x0A]) * cos1; - - b2[0x0C] = b1[0x0C] + b1[0x0F]; - b2[0x0F] = (b1[0x0F] - b1[0x0C]) * cos0; - b2[0x0D] = b1[0x0D] + b1[0x0E]; - b2[0x0E] = (b1[0x0E] - b1[0x0D]) * cos1; - - b2[0x10] = b1[0x10] + b1[0x13]; - b2[0x13] = (b1[0x10] - b1[0x13]) * cos0; - b2[0x11] = b1[0x11] + b1[0x12]; - b2[0x12] = (b1[0x11] - b1[0x12]) * cos1; - - b2[0x14] = b1[0x14] + b1[0x17]; - b2[0x17] = (b1[0x17] - b1[0x14]) * cos0; - b2[0x15] = b1[0x15] + b1[0x16]; - b2[0x16] = (b1[0x16] - b1[0x15]) * cos1; - - b2[0x18] = b1[0x18] + b1[0x1B]; - b2[0x1B] = (b1[0x18] - b1[0x1B]) * cos0; - b2[0x19] = b1[0x19] + b1[0x1A]; - b2[0x1A] = (b1[0x19] - b1[0x1A]) * cos1; - - b2[0x1C] = b1[0x1C] + b1[0x1F]; - b2[0x1F] = (b1[0x1F] - b1[0x1C]) * cos0; - b2[0x1D] = b1[0x1D] + b1[0x1E]; - b2[0x1E] = (b1[0x1E] - b1[0x1D]) * cos1; - } - - { - register real const cos0 = pnts[4][0]; - - b1[0x00] = b2[0x00] + b2[0x01]; - b1[0x01] = (b2[0x00] - b2[0x01]) * cos0; - b1[0x02] = b2[0x02] + b2[0x03]; - b1[0x03] = (b2[0x03] - b2[0x02]) * cos0; - b1[0x02] += b1[0x03]; - - b1[0x04] = b2[0x04] + b2[0x05]; - b1[0x05] = (b2[0x04] - b2[0x05]) * cos0; - b1[0x06] = b2[0x06] + b2[0x07]; - b1[0x07] = (b2[0x07] - b2[0x06]) * cos0; - b1[0x06] += b1[0x07]; - b1[0x04] += b1[0x06]; - b1[0x06] += b1[0x05]; - b1[0x05] += b1[0x07]; - - b1[0x08] = b2[0x08] + b2[0x09]; - b1[0x09] = (b2[0x08] - b2[0x09]) * cos0; - b1[0x0A] = b2[0x0A] + b2[0x0B]; - b1[0x0B] = (b2[0x0B] - b2[0x0A]) * cos0; - b1[0x0A] += b1[0x0B]; - - b1[0x0C] = b2[0x0C] + b2[0x0D]; - b1[0x0D] = (b2[0x0C] - b2[0x0D]) * cos0; - b1[0x0E] = b2[0x0E] + b2[0x0F]; - b1[0x0F] = (b2[0x0F] - b2[0x0E]) * cos0; - b1[0x0E] += b1[0x0F]; - b1[0x0C] += b1[0x0E]; - b1[0x0E] += b1[0x0D]; - b1[0x0D] += b1[0x0F]; - - b1[0x10] = b2[0x10] + b2[0x11]; - b1[0x11] = (b2[0x10] - b2[0x11]) * cos0; - b1[0x12] = b2[0x12] + b2[0x13]; - b1[0x13] = (b2[0x13] - b2[0x12]) * cos0; - b1[0x12] += b1[0x13]; - - b1[0x14] = b2[0x14] + b2[0x15]; - b1[0x15] = (b2[0x14] - b2[0x15]) * cos0; - b1[0x16] = b2[0x16] + b2[0x17]; - b1[0x17] = (b2[0x17] - b2[0x16]) * cos0; - b1[0x16] += b1[0x17]; - b1[0x14] += b1[0x16]; - b1[0x16] += b1[0x15]; - b1[0x15] += b1[0x17]; - - b1[0x18] = b2[0x18] + b2[0x19]; - b1[0x19] = (b2[0x18] - b2[0x19]) * cos0; - b1[0x1A] = b2[0x1A] + b2[0x1B]; - b1[0x1B] = (b2[0x1B] - b2[0x1A]) * cos0; - b1[0x1A] += b1[0x1B]; - - b1[0x1C] = b2[0x1C] + b2[0x1D]; - b1[0x1D] = (b2[0x1C] - b2[0x1D]) * cos0; - b1[0x1E] = b2[0x1E] + b2[0x1F]; - b1[0x1F] = (b2[0x1F] - b2[0x1E]) * cos0; - b1[0x1E] += b1[0x1F]; - b1[0x1C] += b1[0x1E]; - b1[0x1E] += b1[0x1D]; - b1[0x1D] += b1[0x1F]; - } - - out0[0x10*16] = b1[0x00]; - out0[0x10*12] = b1[0x04]; - out0[0x10* 8] = b1[0x02]; - out0[0x10* 4] = b1[0x06]; - out0[0x10* 0] = b1[0x01]; - out1[0x10* 0] = b1[0x01]; - out1[0x10* 4] = b1[0x05]; - out1[0x10* 8] = b1[0x03]; - out1[0x10*12] = b1[0x07]; - - b1[0x08] += b1[0x0C]; - out0[0x10*14] = b1[0x08]; - b1[0x0C] += b1[0x0a]; - out0[0x10*10] = b1[0x0C]; - b1[0x0A] += b1[0x0E]; - out0[0x10* 6] = b1[0x0A]; - b1[0x0E] += b1[0x09]; - out0[0x10* 2] = b1[0x0E]; - b1[0x09] += b1[0x0D]; - out1[0x10* 2] = b1[0x09]; - b1[0x0D] += b1[0x0B]; - out1[0x10* 6] = b1[0x0D]; - b1[0x0B] += b1[0x0F]; - out1[0x10*10] = b1[0x0B]; - out1[0x10*14] = b1[0x0F]; - - b1[0x18] += b1[0x1C]; - out0[0x10*15] = b1[0x10] + b1[0x18]; - out0[0x10*13] = b1[0x18] + b1[0x14]; - b1[0x1C] += b1[0x1a]; - out0[0x10*11] = b1[0x14] + b1[0x1C]; - out0[0x10* 9] = b1[0x1C] + b1[0x12]; - b1[0x1A] += b1[0x1E]; - out0[0x10* 7] = b1[0x12] + b1[0x1A]; - out0[0x10* 5] = b1[0x1A] + b1[0x16]; - b1[0x1E] += b1[0x19]; - out0[0x10* 3] = b1[0x16] + b1[0x1E]; - out0[0x10* 1] = b1[0x1E] + b1[0x11]; - b1[0x19] += b1[0x1D]; - out1[0x10* 1] = b1[0x11] + b1[0x19]; - out1[0x10* 3] = b1[0x19] + b1[0x15]; - b1[0x1D] += b1[0x1B]; - out1[0x10* 5] = b1[0x15] + b1[0x1D]; - out1[0x10* 7] = b1[0x1D] + b1[0x13]; - b1[0x1B] += b1[0x1F]; - out1[0x10* 9] = b1[0x13] + b1[0x1B]; - out1[0x10*11] = b1[0x1B] + b1[0x17]; - out1[0x10*13] = b1[0x17] + b1[0x1F]; - out1[0x10*15] = b1[0x1F]; + { + register real *costab = pnts[0]; + + b1[0x00] = samples[0x00] + samples[0x1F]; + b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0]; + + b1[0x01] = samples[0x01] + samples[0x1E]; + b1[0x1E] = (samples[0x01] - samples[0x1E]) * costab[0x1]; + + b1[0x02] = samples[0x02] + samples[0x1D]; + b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2]; + + b1[0x03] = samples[0x03] + samples[0x1C]; + b1[0x1C] = (samples[0x03] - samples[0x1C]) * costab[0x3]; + + b1[0x04] = samples[0x04] + samples[0x1B]; + b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4]; + + b1[0x05] = samples[0x05] + samples[0x1A]; + b1[0x1A] = (samples[0x05] - samples[0x1A]) * costab[0x5]; + + b1[0x06] = samples[0x06] + samples[0x19]; + b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6]; + + b1[0x07] = samples[0x07] + samples[0x18]; + b1[0x18] = (samples[0x07] - samples[0x18]) * costab[0x7]; + + b1[0x08] = samples[0x08] + samples[0x17]; + b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8]; + + b1[0x09] = samples[0x09] + samples[0x16]; + b1[0x16] = (samples[0x09] - samples[0x16]) * costab[0x9]; + + b1[0x0A] = samples[0x0A] + samples[0x15]; + b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA]; + + b1[0x0B] = samples[0x0B] + samples[0x14]; + b1[0x14] = (samples[0x0B] - samples[0x14]) * costab[0xB]; + + b1[0x0C] = samples[0x0C] + samples[0x13]; + b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC]; + + b1[0x0D] = samples[0x0D] + samples[0x12]; + b1[0x12] = (samples[0x0D] - samples[0x12]) * costab[0xD]; + + b1[0x0E] = samples[0x0E] + samples[0x11]; + b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE]; + + b1[0x0F] = samples[0x0F] + samples[0x10]; + b1[0x10] = (samples[0x0F] - samples[0x10]) * costab[0xF]; + } + + + { + register real *costab = pnts[1]; + + b2[0x00] = b1[0x00] + b1[0x0F]; + b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0]; + b2[0x01] = b1[0x01] + b1[0x0E]; + b2[0x0E] = (b1[0x01] - b1[0x0E]) * costab[1]; + b2[0x02] = b1[0x02] + b1[0x0D]; + b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2]; + b2[0x03] = b1[0x03] + b1[0x0C]; + b2[0x0C] = (b1[0x03] - b1[0x0C]) * costab[3]; + b2[0x04] = b1[0x04] + b1[0x0B]; + b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4]; + b2[0x05] = b1[0x05] + b1[0x0A]; + b2[0x0A] = (b1[0x05] - b1[0x0A]) * costab[5]; + b2[0x06] = b1[0x06] + b1[0x09]; + b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6]; + b2[0x07] = b1[0x07] + b1[0x08]; + b2[0x08] = (b1[0x07] - b1[0x08]) * costab[7]; + + b2[0x10] = b1[0x10] + b1[0x1F]; + b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0]; + b2[0x11] = b1[0x11] + b1[0x1E]; + b2[0x1E] = (b1[0x1E] - b1[0x11]) * costab[1]; + b2[0x12] = b1[0x12] + b1[0x1D]; + b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2]; + b2[0x13] = b1[0x13] + b1[0x1C]; + b2[0x1C] = (b1[0x1C] - b1[0x13]) * costab[3]; + b2[0x14] = b1[0x14] + b1[0x1B]; + b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4]; + b2[0x15] = b1[0x15] + b1[0x1A]; + b2[0x1A] = (b1[0x1A] - b1[0x15]) * costab[5]; + b2[0x16] = b1[0x16] + b1[0x19]; + b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6]; + b2[0x17] = b1[0x17] + b1[0x18]; + b2[0x18] = (b1[0x18] - b1[0x17]) * costab[7]; + } + + { + register real *costab = pnts[2]; + + b1[0x00] = b2[0x00] + b2[0x07]; + b1[0x07] = (b2[0x00] - b2[0x07]) * costab[0]; + b1[0x01] = b2[0x01] + b2[0x06]; + b1[0x06] = (b2[0x01] - b2[0x06]) * costab[1]; + b1[0x02] = b2[0x02] + b2[0x05]; + b1[0x05] = (b2[0x02] - b2[0x05]) * costab[2]; + b1[0x03] = b2[0x03] + b2[0x04]; + b1[0x04] = (b2[0x03] - b2[0x04]) * costab[3]; + + b1[0x08] = b2[0x08] + b2[0x0F]; + b1[0x0F] = (b2[0x0F] - b2[0x08]) * costab[0]; + b1[0x09] = b2[0x09] + b2[0x0E]; + b1[0x0E] = (b2[0x0E] - b2[0x09]) * costab[1]; + b1[0x0A] = b2[0x0A] + b2[0x0D]; + b1[0x0D] = (b2[0x0D] - b2[0x0A]) * costab[2]; + b1[0x0B] = b2[0x0B] + b2[0x0C]; + b1[0x0C] = (b2[0x0C] - b2[0x0B]) * costab[3]; + + b1[0x10] = b2[0x10] + b2[0x17]; + b1[0x17] = (b2[0x10] - b2[0x17]) * costab[0]; + b1[0x11] = b2[0x11] + b2[0x16]; + b1[0x16] = (b2[0x11] - b2[0x16]) * costab[1]; + b1[0x12] = b2[0x12] + b2[0x15]; + b1[0x15] = (b2[0x12] - b2[0x15]) * costab[2]; + b1[0x13] = b2[0x13] + b2[0x14]; + b1[0x14] = (b2[0x13] - b2[0x14]) * costab[3]; + + b1[0x18] = b2[0x18] + b2[0x1F]; + b1[0x1F] = (b2[0x1F] - b2[0x18]) * costab[0]; + b1[0x19] = b2[0x19] + b2[0x1E]; + b1[0x1E] = (b2[0x1E] - b2[0x19]) * costab[1]; + b1[0x1A] = b2[0x1A] + b2[0x1D]; + b1[0x1D] = (b2[0x1D] - b2[0x1A]) * costab[2]; + b1[0x1B] = b2[0x1B] + b2[0x1C]; + b1[0x1C] = (b2[0x1C] - b2[0x1B]) * costab[3]; + } + + { + register real const cos0 = pnts[3][0]; + register real const cos1 = pnts[3][1]; + + b2[0x00] = b1[0x00] + b1[0x03]; + b2[0x03] = (b1[0x00] - b1[0x03]) * cos0; + b2[0x01] = b1[0x01] + b1[0x02]; + b2[0x02] = (b1[0x01] - b1[0x02]) * cos1; + + b2[0x04] = b1[0x04] + b1[0x07]; + b2[0x07] = (b1[0x07] - b1[0x04]) * cos0; + b2[0x05] = b1[0x05] + b1[0x06]; + b2[0x06] = (b1[0x06] - b1[0x05]) * cos1; + + b2[0x08] = b1[0x08] + b1[0x0B]; + b2[0x0B] = (b1[0x08] - b1[0x0B]) * cos0; + b2[0x09] = b1[0x09] + b1[0x0A]; + b2[0x0A] = (b1[0x09] - b1[0x0A]) * cos1; + + b2[0x0C] = b1[0x0C] + b1[0x0F]; + b2[0x0F] = (b1[0x0F] - b1[0x0C]) * cos0; + b2[0x0D] = b1[0x0D] + b1[0x0E]; + b2[0x0E] = (b1[0x0E] - b1[0x0D]) * cos1; + + b2[0x10] = b1[0x10] + b1[0x13]; + b2[0x13] = (b1[0x10] - b1[0x13]) * cos0; + b2[0x11] = b1[0x11] + b1[0x12]; + b2[0x12] = (b1[0x11] - b1[0x12]) * cos1; + + b2[0x14] = b1[0x14] + b1[0x17]; + b2[0x17] = (b1[0x17] - b1[0x14]) * cos0; + b2[0x15] = b1[0x15] + b1[0x16]; + b2[0x16] = (b1[0x16] - b1[0x15]) * cos1; + + b2[0x18] = b1[0x18] + b1[0x1B]; + b2[0x1B] = (b1[0x18] - b1[0x1B]) * cos0; + b2[0x19] = b1[0x19] + b1[0x1A]; + b2[0x1A] = (b1[0x19] - b1[0x1A]) * cos1; + + b2[0x1C] = b1[0x1C] + b1[0x1F]; + b2[0x1F] = (b1[0x1F] - b1[0x1C]) * cos0; + b2[0x1D] = b1[0x1D] + b1[0x1E]; + b2[0x1E] = (b1[0x1E] - b1[0x1D]) * cos1; + } + + { + register real const cos0 = pnts[4][0]; + + b1[0x00] = b2[0x00] + b2[0x01]; + b1[0x01] = (b2[0x00] - b2[0x01]) * cos0; + b1[0x02] = b2[0x02] + b2[0x03]; + b1[0x03] = (b2[0x03] - b2[0x02]) * cos0; + b1[0x02] += b1[0x03]; + + b1[0x04] = b2[0x04] + b2[0x05]; + b1[0x05] = (b2[0x04] - b2[0x05]) * cos0; + b1[0x06] = b2[0x06] + b2[0x07]; + b1[0x07] = (b2[0x07] - b2[0x06]) * cos0; + b1[0x06] += b1[0x07]; + b1[0x04] += b1[0x06]; + b1[0x06] += b1[0x05]; + b1[0x05] += b1[0x07]; + + b1[0x08] = b2[0x08] + b2[0x09]; + b1[0x09] = (b2[0x08] - b2[0x09]) * cos0; + b1[0x0A] = b2[0x0A] + b2[0x0B]; + b1[0x0B] = (b2[0x0B] - b2[0x0A]) * cos0; + b1[0x0A] += b1[0x0B]; + + b1[0x0C] = b2[0x0C] + b2[0x0D]; + b1[0x0D] = (b2[0x0C] - b2[0x0D]) * cos0; + b1[0x0E] = b2[0x0E] + b2[0x0F]; + b1[0x0F] = (b2[0x0F] - b2[0x0E]) * cos0; + b1[0x0E] += b1[0x0F]; + b1[0x0C] += b1[0x0E]; + b1[0x0E] += b1[0x0D]; + b1[0x0D] += b1[0x0F]; + + b1[0x10] = b2[0x10] + b2[0x11]; + b1[0x11] = (b2[0x10] - b2[0x11]) * cos0; + b1[0x12] = b2[0x12] + b2[0x13]; + b1[0x13] = (b2[0x13] - b2[0x12]) * cos0; + b1[0x12] += b1[0x13]; + + b1[0x14] = b2[0x14] + b2[0x15]; + b1[0x15] = (b2[0x14] - b2[0x15]) * cos0; + b1[0x16] = b2[0x16] + b2[0x17]; + b1[0x17] = (b2[0x17] - b2[0x16]) * cos0; + b1[0x16] += b1[0x17]; + b1[0x14] += b1[0x16]; + b1[0x16] += b1[0x15]; + b1[0x15] += b1[0x17]; + + b1[0x18] = b2[0x18] + b2[0x19]; + b1[0x19] = (b2[0x18] - b2[0x19]) * cos0; + b1[0x1A] = b2[0x1A] + b2[0x1B]; + b1[0x1B] = (b2[0x1B] - b2[0x1A]) * cos0; + b1[0x1A] += b1[0x1B]; + + b1[0x1C] = b2[0x1C] + b2[0x1D]; + b1[0x1D] = (b2[0x1C] - b2[0x1D]) * cos0; + b1[0x1E] = b2[0x1E] + b2[0x1F]; + b1[0x1F] = (b2[0x1F] - b2[0x1E]) * cos0; + b1[0x1E] += b1[0x1F]; + b1[0x1C] += b1[0x1E]; + b1[0x1E] += b1[0x1D]; + b1[0x1D] += b1[0x1F]; + } + + out0[0x10*16] = b1[0x00]; + out0[0x10*12] = b1[0x04]; + out0[0x10* 8] = b1[0x02]; + out0[0x10* 4] = b1[0x06]; + out0[0x10* 0] = b1[0x01]; + out1[0x10* 0] = b1[0x01]; + out1[0x10* 4] = b1[0x05]; + out1[0x10* 8] = b1[0x03]; + out1[0x10*12] = b1[0x07]; + + b1[0x08] += b1[0x0C]; + out0[0x10*14] = b1[0x08]; + b1[0x0C] += b1[0x0a]; + out0[0x10*10] = b1[0x0C]; + b1[0x0A] += b1[0x0E]; + out0[0x10* 6] = b1[0x0A]; + b1[0x0E] += b1[0x09]; + out0[0x10* 2] = b1[0x0E]; + b1[0x09] += b1[0x0D]; + out1[0x10* 2] = b1[0x09]; + b1[0x0D] += b1[0x0B]; + out1[0x10* 6] = b1[0x0D]; + b1[0x0B] += b1[0x0F]; + out1[0x10*10] = b1[0x0B]; + out1[0x10*14] = b1[0x0F]; + + b1[0x18] += b1[0x1C]; + out0[0x10*15] = b1[0x10] + b1[0x18]; + out0[0x10*13] = b1[0x18] + b1[0x14]; + b1[0x1C] += b1[0x1a]; + out0[0x10*11] = b1[0x14] + b1[0x1C]; + out0[0x10* 9] = b1[0x1C] + b1[0x12]; + b1[0x1A] += b1[0x1E]; + out0[0x10* 7] = b1[0x12] + b1[0x1A]; + out0[0x10* 5] = b1[0x1A] + b1[0x16]; + b1[0x1E] += b1[0x19]; + out0[0x10* 3] = b1[0x16] + b1[0x1E]; + out0[0x10* 1] = b1[0x1E] + b1[0x11]; + b1[0x19] += b1[0x1D]; + out1[0x10* 1] = b1[0x11] + b1[0x19]; + out1[0x10* 3] = b1[0x19] + b1[0x15]; + b1[0x1D] += b1[0x1B]; + out1[0x10* 5] = b1[0x15] + b1[0x1D]; + out1[0x10* 7] = b1[0x1D] + b1[0x13]; + b1[0x1B] += b1[0x1F]; + out1[0x10* 9] = b1[0x13] + b1[0x1B]; + out1[0x10*11] = b1[0x1B] + b1[0x17]; + out1[0x10*13] = b1[0x17] + b1[0x1F]; + out1[0x10*15] = b1[0x1F]; } /* @@ -309,8 +309,8 @@ static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples) */ void dct64(real *a,real *b,real *c) { - real bufs[0x40]; - dct64_1(a,b,bufs,bufs+0x20,c); + real bufs[0x40]; + dct64_1(a,b,bufs,bufs+0x20,c); } //#endif diff --git a/src/simu/win/mp3/decode_i386.c b/src/simu/win/mp3/decode_i386.c index 6ddf679..eb4e8ed 100644 --- a/src/simu/win/mp3/decode_i386.c +++ b/src/simu/win/mp3/decode_i386.c @@ -1,6 +1,6 @@ //#ifdef HAVEGTK -/* - * Mpeg Layer-1,2,3 audio decoder +/* + * Mpeg Layer-1,2,3 audio decoder * ------------------------------ * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved. * See also 'README' @@ -20,136 +20,141 @@ extern struct mpstr *gmp; - /* old WRITE_SAMPLE */ +/* old WRITE_SAMPLE */ #define WRITE_SAMPLE(samples,sum,clip) \ - if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \ - else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \ - else { *(samples) = sum; } + if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \ + else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \ + else { *(samples) = sum; } int synth_1to1_mono(real *bandPtr,unsigned char *samples,int *pnt) { - short samples_tmp[64]; - short *tmp1 = samples_tmp; - int i,ret; - int pnt1 = 0; - - ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1); - samples += *pnt; - - for(i=0;i<32;i++) { - *( (short *) samples) = *tmp1; - samples += 2; - tmp1 += 2; - } - *pnt += 64; - - return ret; + short samples_tmp[64]; + short *tmp1 = samples_tmp; + int i,ret; + int pnt1 = 0; + + ret = synth_1to1(bandPtr,0,(unsigned char *) samples_tmp,&pnt1); + samples += *pnt; + + for(i=0; i<32; i++) + { + *( (short *) samples) = *tmp1; + samples += 2; + tmp1 += 2; + } + *pnt += 64; + + return ret; } int synth_1to1(real *bandPtr,int channel,unsigned char *out,int *pnt) { - static const int step = 2; - int bo; - short *samples = (short *) (out + *pnt); - - real *b0,(*buf)[0x110]; - int clip = 0; - int bo1; - - bo = gmp->synth_bo; - - if(!channel) { - bo--; - bo &= 0xf; - buf = gmp->synth_buffs[0]; - } - else { - samples++; - buf = gmp->synth_buffs[1]; - } - - if(bo & 0x1) { - b0 = buf[0]; - bo1 = bo; - dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr); - } - else { - b0 = buf[1]; - bo1 = bo+1; - dct64(buf[0]+bo,buf[1]+bo+1,bandPtr); - } - - gmp->synth_bo = bo; - - { - register int j; - real *window = decwin + 16 - bo1; - - for (j=16;j;j--,b0+=0x10,window+=0x20,samples+=step) + static const int step = 2; + int bo; + short *samples = (short *) (out + *pnt); + + real *b0,(*buf)[0x110]; + int clip = 0; + int bo1; + + bo = gmp->synth_bo; + + if(!channel) + { + bo--; + bo &= 0xf; + buf = gmp->synth_buffs[0]; + } + else { - real sum; - sum = window[0x0] * b0[0x0]; - sum -= window[0x1] * b0[0x1]; - sum += window[0x2] * b0[0x2]; - sum -= window[0x3] * b0[0x3]; - sum += window[0x4] * b0[0x4]; - sum -= window[0x5] * b0[0x5]; - sum += window[0x6] * b0[0x6]; - sum -= window[0x7] * b0[0x7]; - sum += window[0x8] * b0[0x8]; - sum -= window[0x9] * b0[0x9]; - sum += window[0xA] * b0[0xA]; - sum -= window[0xB] * b0[0xB]; - sum += window[0xC] * b0[0xC]; - sum -= window[0xD] * b0[0xD]; - sum += window[0xE] * b0[0xE]; - sum -= window[0xF] * b0[0xF]; - - WRITE_SAMPLE(samples,sum,clip); + samples++; + buf = gmp->synth_buffs[1]; } + if(bo & 0x1) { - real sum; - sum = window[0x0] * b0[0x0]; - sum += window[0x2] * b0[0x2]; - sum += window[0x4] * b0[0x4]; - sum += window[0x6] * b0[0x6]; - sum += window[0x8] * b0[0x8]; - sum += window[0xA] * b0[0xA]; - sum += window[0xC] * b0[0xC]; - sum += window[0xE] * b0[0xE]; - WRITE_SAMPLE(samples,sum,clip); - b0-=0x10,window-=0x20,samples+=step; + b0 = buf[0]; + bo1 = bo; + dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr); } - window += bo1<<1; + else + { + b0 = buf[1]; + bo1 = bo+1; + dct64(buf[0]+bo,buf[1]+bo+1,bandPtr); + } + + gmp->synth_bo = bo; - for (j=15;j;j--,b0-=0x10,window-=0x20,samples+=step) { - real sum; - sum = -window[-0x1] * b0[0x0]; - sum -= window[-0x2] * b0[0x1]; - sum -= window[-0x3] * b0[0x2]; - sum -= window[-0x4] * b0[0x3]; - sum -= window[-0x5] * b0[0x4]; - sum -= window[-0x6] * b0[0x5]; - sum -= window[-0x7] * b0[0x6]; - sum -= window[-0x8] * b0[0x7]; - sum -= window[-0x9] * b0[0x8]; - sum -= window[-0xA] * b0[0x9]; - sum -= window[-0xB] * b0[0xA]; - sum -= window[-0xC] * b0[0xB]; - sum -= window[-0xD] * b0[0xC]; - sum -= window[-0xE] * b0[0xD]; - sum -= window[-0xF] * b0[0xE]; - sum -= window[-0x0] * b0[0xF]; - - WRITE_SAMPLE(samples,sum,clip); + register int j; + real *window = decwin + 16 - bo1; + + for (j=16; j; j--,b0+=0x10,window+=0x20,samples+=step) + { + real sum; + sum = window[0x0] * b0[0x0]; + sum -= window[0x1] * b0[0x1]; + sum += window[0x2] * b0[0x2]; + sum -= window[0x3] * b0[0x3]; + sum += window[0x4] * b0[0x4]; + sum -= window[0x5] * b0[0x5]; + sum += window[0x6] * b0[0x6]; + sum -= window[0x7] * b0[0x7]; + sum += window[0x8] * b0[0x8]; + sum -= window[0x9] * b0[0x9]; + sum += window[0xA] * b0[0xA]; + sum -= window[0xB] * b0[0xB]; + sum += window[0xC] * b0[0xC]; + sum -= window[0xD] * b0[0xD]; + sum += window[0xE] * b0[0xE]; + sum -= window[0xF] * b0[0xF]; + + WRITE_SAMPLE(samples,sum,clip); + } + + { + real sum; + sum = window[0x0] * b0[0x0]; + sum += window[0x2] * b0[0x2]; + sum += window[0x4] * b0[0x4]; + sum += window[0x6] * b0[0x6]; + sum += window[0x8] * b0[0x8]; + sum += window[0xA] * b0[0xA]; + sum += window[0xC] * b0[0xC]; + sum += window[0xE] * b0[0xE]; + WRITE_SAMPLE(samples,sum,clip); + b0-=0x10,window-=0x20,samples+=step; + } + window += bo1<<1; + + for (j=15; j; j--,b0-=0x10,window-=0x20,samples+=step) + { + real sum; + sum = -window[-0x1] * b0[0x0]; + sum -= window[-0x2] * b0[0x1]; + sum -= window[-0x3] * b0[0x2]; + sum -= window[-0x4] * b0[0x3]; + sum -= window[-0x5] * b0[0x4]; + sum -= window[-0x6] * b0[0x5]; + sum -= window[-0x7] * b0[0x6]; + sum -= window[-0x8] * b0[0x7]; + sum -= window[-0x9] * b0[0x8]; + sum -= window[-0xA] * b0[0x9]; + sum -= window[-0xB] * b0[0xA]; + sum -= window[-0xC] * b0[0xB]; + sum -= window[-0xD] * b0[0xC]; + sum -= window[-0xE] * b0[0xD]; + sum -= window[-0xF] * b0[0xE]; + sum -= window[-0x0] * b0[0xF]; + + WRITE_SAMPLE(samples,sum,clip); + } } - } - *pnt += 128; + *pnt += 128; - return clip; + return clip; } //#endif diff --git a/src/simu/win/mp3/huffman.h b/src/simu/win/mp3/huffman.h index 7bfb58c..16b4a63 100644 --- a/src/simu/win/mp3/huffman.h +++ b/src/simu/win/mp3/huffman.h @@ -1,332 +1,332 @@ /* * huffman tables ... recalcualted to work with my optimzed * decoder scheme (MH) - * - * probably we could save a few bytes of memory, because the + * + * probably we could save a few bytes of memory, because the * smaller tables are often the part of a bigger table */ -struct newhuff +struct newhuff { - unsigned int linbits; - short *table; + unsigned int linbits; + short *table; }; -static short tab0[] = -{ - 0 +static short tab0[] = +{ + 0 }; static short tab1[] = { - -5, -3, -1, 17, 1, 16, 0 + -5, -3, -1, 17, 1, 16, 0 }; static short tab2[] = { - -15, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 17, -1, 1, - 16, 0 + -15, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 17, -1, 1, + 16, 0 }; static short tab3[] = { - -13, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 16, 17, -1, - 1, 0 + -13, -11, -9, -5, -3, -1, 34, 2, 18, -1, 33, 32, 16, 17, -1, + 1, 0 }; static short tab5[] = { - -29, -25, -23, -15, -7, -5, -3, -1, 51, 35, 50, 49, -3, -1, 19, - 3, -1, 48, 34, -3, -1, 18, 33, -1, 2, 32, 17, -1, 1, 16, - 0 + -29, -25, -23, -15, -7, -5, -3, -1, 51, 35, 50, 49, -3, -1, 19, + 3, -1, 48, 34, -3, -1, 18, 33, -1, 2, 32, 17, -1, 1, 16, + 0 }; static short tab6[] = { - -25, -19, -13, -9, -5, -3, -1, 51, 3, 35, -1, 50, 48, -1, 19, - 49, -3, -1, 34, 2, 18, -3, -1, 33, 32, 1, -1, 17, -1, 16, - 0 + -25, -19, -13, -9, -5, -3, -1, 51, 3, 35, -1, 50, 48, -1, 19, + 49, -3, -1, 34, 2, 18, -3, -1, 33, 32, 1, -1, 17, -1, 16, + 0 }; static short tab7[] = { - -69, -65, -57, -39, -29, -17, -11, -7, -3, -1, 85, 69, -1, 84, 83, - -1, 53, 68, -3, -1, 37, 82, 21, -5, -1, 81, -1, 5, 52, -1, - 80, -1, 67, 51, -5, -3, -1, 36, 66, 20, -1, 65, 64, -11, -7, - -3, -1, 4, 35, -1, 50, 3, -1, 19, 49, -3, -1, 48, 34, 18, - -5, -1, 33, -1, 2, 32, 17, -1, 1, 16, 0 + -69, -65, -57, -39, -29, -17, -11, -7, -3, -1, 85, 69, -1, 84, 83, + -1, 53, 68, -3, -1, 37, 82, 21, -5, -1, 81, -1, 5, 52, -1, + 80, -1, 67, 51, -5, -3, -1, 36, 66, 20, -1, 65, 64, -11, -7, + -3, -1, 4, 35, -1, 50, 3, -1, 19, 49, -3, -1, 48, 34, 18, + -5, -1, 33, -1, 2, 32, 17, -1, 1, 16, 0 }; static short tab8[] = { - -65, -63, -59, -45, -31, -19, -13, -7, -5, -3, -1, 85, 84, 69, 83, - -3, -1, 53, 68, 37, -3, -1, 82, 5, 21, -5, -1, 81, -1, 52, - 67, -3, -1, 80, 51, 36, -5, -3, -1, 66, 20, 65, -3, -1, 4, - 64, -1, 35, 50, -9, -7, -3, -1, 19, 49, -1, 3, 48, 34, -1, - 2, 32, -1, 18, 33, 17, -3, -1, 1, 16, 0 + -65, -63, -59, -45, -31, -19, -13, -7, -5, -3, -1, 85, 84, 69, 83, + -3, -1, 53, 68, 37, -3, -1, 82, 5, 21, -5, -1, 81, -1, 52, + 67, -3, -1, 80, 51, 36, -5, -3, -1, 66, 20, 65, -3, -1, 4, + 64, -1, 35, 50, -9, -7, -3, -1, 19, 49, -1, 3, 48, 34, -1, + 2, 32, -1, 18, 33, 17, -3, -1, 1, 16, 0 }; static short tab9[] = { - -63, -53, -41, -29, -19, -11, -5, -3, -1, 85, 69, 53, -1, 83, -1, - 84, 5, -3, -1, 68, 37, -1, 82, 21, -3, -1, 81, 52, -1, 67, - -1, 80, 4, -7, -3, -1, 36, 66, -1, 51, 64, -1, 20, 65, -5, - -3, -1, 35, 50, 19, -1, 49, -1, 3, 48, -5, -3, -1, 34, 2, - 18, -1, 33, 32, -3, -1, 17, 1, -1, 16, 0 + -63, -53, -41, -29, -19, -11, -5, -3, -1, 85, 69, 53, -1, 83, -1, + 84, 5, -3, -1, 68, 37, -1, 82, 21, -3, -1, 81, 52, -1, 67, + -1, 80, 4, -7, -3, -1, 36, 66, -1, 51, 64, -1, 20, 65, -5, + -3, -1, 35, 50, 19, -1, 49, -1, 3, 48, -5, -3, -1, 34, 2, + 18, -1, 33, 32, -3, -1, 17, 1, -1, 16, 0 }; static short tab10[] = { --125,-121,-111, -83, -55, -35, -21, -13, -7, -3, -1, 119, 103, -1, 118, - 87, -3, -1, 117, 102, 71, -3, -1, 116, 86, -1, 101, 55, -9, -3, - -1, 115, 70, -3, -1, 85, 84, 99, -1, 39, 114, -11, -5, -3, -1, - 100, 7, 112, -1, 98, -1, 69, 53, -5, -1, 6, -1, 83, 68, 23, - -17, -5, -1, 113, -1, 54, 38, -5, -3, -1, 37, 82, 21, -1, 81, - -1, 52, 67, -3, -1, 22, 97, -1, 96, -1, 5, 80, -19, -11, -7, - -3, -1, 36, 66, -1, 51, 4, -1, 20, 65, -3, -1, 64, 35, -1, - 50, 3, -3, -1, 19, 49, -1, 48, 34, -7, -3, -1, 18, 33, -1, - 2, 32, 17, -1, 1, 16, 0 + -125,-121,-111, -83, -55, -35, -21, -13, -7, -3, -1, 119, 103, -1, 118, + 87, -3, -1, 117, 102, 71, -3, -1, 116, 86, -1, 101, 55, -9, -3, + -1, 115, 70, -3, -1, 85, 84, 99, -1, 39, 114, -11, -5, -3, -1, + 100, 7, 112, -1, 98, -1, 69, 53, -5, -1, 6, -1, 83, 68, 23, + -17, -5, -1, 113, -1, 54, 38, -5, -3, -1, 37, 82, 21, -1, 81, + -1, 52, 67, -3, -1, 22, 97, -1, 96, -1, 5, 80, -19, -11, -7, + -3, -1, 36, 66, -1, 51, 4, -1, 20, 65, -3, -1, 64, 35, -1, + 50, 3, -3, -1, 19, 49, -1, 48, 34, -7, -3, -1, 18, 33, -1, + 2, 32, 17, -1, 1, 16, 0 }; static short tab11[] = { --121,-113, -89, -59, -43, -27, -17, -7, -3, -1, 119, 103, -1, 118, 117, - -3, -1, 102, 71, -1, 116, -1, 87, 85, -5, -3, -1, 86, 101, 55, - -1, 115, 70, -9, -7, -3, -1, 69, 84, -1, 53, 83, 39, -1, 114, - -1, 100, 7, -5, -1, 113, -1, 23, 112, -3, -1, 54, 99, -1, 96, - -1, 68, 37, -13, -7, -5, -3, -1, 82, 5, 21, 98, -3, -1, 38, - 6, 22, -5, -1, 97, -1, 81, 52, -5, -1, 80, -1, 67, 51, -1, - 36, 66, -15, -11, -7, -3, -1, 20, 65, -1, 4, 64, -1, 35, 50, - -1, 19, 49, -5, -3, -1, 3, 48, 34, 33, -5, -1, 18, -1, 2, - 32, 17, -3, -1, 1, 16, 0 + -121,-113, -89, -59, -43, -27, -17, -7, -3, -1, 119, 103, -1, 118, 117, + -3, -1, 102, 71, -1, 116, -1, 87, 85, -5, -3, -1, 86, 101, 55, + -1, 115, 70, -9, -7, -3, -1, 69, 84, -1, 53, 83, 39, -1, 114, + -1, 100, 7, -5, -1, 113, -1, 23, 112, -3, -1, 54, 99, -1, 96, + -1, 68, 37, -13, -7, -5, -3, -1, 82, 5, 21, 98, -3, -1, 38, + 6, 22, -5, -1, 97, -1, 81, 52, -5, -1, 80, -1, 67, 51, -1, + 36, 66, -15, -11, -7, -3, -1, 20, 65, -1, 4, 64, -1, 35, 50, + -1, 19, 49, -5, -3, -1, 3, 48, 34, 33, -5, -1, 18, -1, 2, + 32, 17, -3, -1, 1, 16, 0 }; static short tab12[] = { --115, -99, -73, -45, -27, -17, -9, -5, -3, -1, 119, 103, 118, -1, 87, - 117, -3, -1, 102, 71, -1, 116, 101, -3, -1, 86, 55, -3, -1, 115, - 85, 39, -7, -3, -1, 114, 70, -1, 100, 23, -5, -1, 113, -1, 7, - 112, -1, 54, 99, -13, -9, -3, -1, 69, 84, -1, 68, -1, 6, 5, - -1, 38, 98, -5, -1, 97, -1, 22, 96, -3, -1, 53, 83, -1, 37, - 82, -17, -7, -3, -1, 21, 81, -1, 52, 67, -5, -3, -1, 80, 4, - 36, -1, 66, 20, -3, -1, 51, 65, -1, 35, 50, -11, -7, -5, -3, - -1, 64, 3, 48, 19, -1, 49, 34, -1, 18, 33, -7, -5, -3, -1, - 2, 32, 0, 17, -1, 1, 16 + -115, -99, -73, -45, -27, -17, -9, -5, -3, -1, 119, 103, 118, -1, 87, + 117, -3, -1, 102, 71, -1, 116, 101, -3, -1, 86, 55, -3, -1, 115, + 85, 39, -7, -3, -1, 114, 70, -1, 100, 23, -5, -1, 113, -1, 7, + 112, -1, 54, 99, -13, -9, -3, -1, 69, 84, -1, 68, -1, 6, 5, + -1, 38, 98, -5, -1, 97, -1, 22, 96, -3, -1, 53, 83, -1, 37, + 82, -17, -7, -3, -1, 21, 81, -1, 52, 67, -5, -3, -1, 80, 4, + 36, -1, 66, 20, -3, -1, 51, 65, -1, 35, 50, -11, -7, -5, -3, + -1, 64, 3, 48, 19, -1, 49, 34, -1, 18, 33, -7, -5, -3, -1, + 2, 32, 0, 17, -1, 1, 16 }; static short tab13[] = { --509,-503,-475,-405,-333,-265,-205,-153,-115, -83, -53, -35, -21, -13, -9, - -7, -5, -3, -1, 254, 252, 253, 237, 255, -1, 239, 223, -3, -1, 238, - 207, -1, 222, 191, -9, -3, -1, 251, 206, -1, 220, -1, 175, 233, -1, - 236, 221, -9, -5, -3, -1, 250, 205, 190, -1, 235, 159, -3, -1, 249, - 234, -1, 189, 219, -17, -9, -3, -1, 143, 248, -1, 204, -1, 174, 158, - -5, -1, 142, -1, 127, 126, 247, -5, -1, 218, -1, 173, 188, -3, -1, - 203, 246, 111, -15, -7, -3, -1, 232, 95, -1, 157, 217, -3, -1, 245, - 231, -1, 172, 187, -9, -3, -1, 79, 244, -3, -1, 202, 230, 243, -1, - 63, -1, 141, 216, -21, -9, -3, -1, 47, 242, -3, -1, 110, 156, 15, - -5, -3, -1, 201, 94, 171, -3, -1, 125, 215, 78, -11, -5, -3, -1, - 200, 214, 62, -1, 185, -1, 155, 170, -1, 31, 241, -23, -13, -5, -1, - 240, -1, 186, 229, -3, -1, 228, 140, -1, 109, 227, -5, -1, 226, -1, - 46, 14, -1, 30, 225, -15, -7, -3, -1, 224, 93, -1, 213, 124, -3, - -1, 199, 77, -1, 139, 184, -7, -3, -1, 212, 154, -1, 169, 108, -1, - 198, 61, -37, -21, -9, -5, -3, -1, 211, 123, 45, -1, 210, 29, -5, - -1, 183, -1, 92, 197, -3, -1, 153, 122, 195, -7, -5, -3, -1, 167, - 151, 75, 209, -3, -1, 13, 208, -1, 138, 168, -11, -7, -3, -1, 76, - 196, -1, 107, 182, -1, 60, 44, -3, -1, 194, 91, -3, -1, 181, 137, - 28, -43, -23, -11, -5, -1, 193, -1, 152, 12, -1, 192, -1, 180, 106, - -5, -3, -1, 166, 121, 59, -1, 179, -1, 136, 90, -11, -5, -1, 43, - -1, 165, 105, -1, 164, -1, 120, 135, -5, -1, 148, -1, 119, 118, 178, - -11, -3, -1, 27, 177, -3, -1, 11, 176, -1, 150, 74, -7, -3, -1, - 58, 163, -1, 89, 149, -1, 42, 162, -47, -23, -9, -3, -1, 26, 161, - -3, -1, 10, 104, 160, -5, -3, -1, 134, 73, 147, -3, -1, 57, 88, - -1, 133, 103, -9, -3, -1, 41, 146, -3, -1, 87, 117, 56, -5, -1, - 131, -1, 102, 71, -3, -1, 116, 86, -1, 101, 115, -11, -3, -1, 25, - 145, -3, -1, 9, 144, -1, 72, 132, -7, -5, -1, 114, -1, 70, 100, - 40, -1, 130, 24, -41, -27, -11, -5, -3, -1, 55, 39, 23, -1, 113, - -1, 85, 7, -7, -3, -1, 112, 54, -1, 99, 69, -3, -1, 84, 38, - -1, 98, 53, -5, -1, 129, -1, 8, 128, -3, -1, 22, 97, -1, 6, - 96, -13, -9, -5, -3, -1, 83, 68, 37, -1, 82, 5, -1, 21, 81, - -7, -3, -1, 52, 67, -1, 80, 36, -3, -1, 66, 51, 20, -19, -11, - -5, -1, 65, -1, 4, 64, -3, -1, 35, 50, 19, -3, -1, 49, 3, - -1, 48, 34, -3, -1, 18, 33, -1, 2, 32, -3, -1, 17, 1, 16, - 0 + -509,-503,-475,-405,-333,-265,-205,-153,-115, -83, -53, -35, -21, -13, -9, + -7, -5, -3, -1, 254, 252, 253, 237, 255, -1, 239, 223, -3, -1, 238, + 207, -1, 222, 191, -9, -3, -1, 251, 206, -1, 220, -1, 175, 233, -1, + 236, 221, -9, -5, -3, -1, 250, 205, 190, -1, 235, 159, -3, -1, 249, + 234, -1, 189, 219, -17, -9, -3, -1, 143, 248, -1, 204, -1, 174, 158, + -5, -1, 142, -1, 127, 126, 247, -5, -1, 218, -1, 173, 188, -3, -1, + 203, 246, 111, -15, -7, -3, -1, 232, 95, -1, 157, 217, -3, -1, 245, + 231, -1, 172, 187, -9, -3, -1, 79, 244, -3, -1, 202, 230, 243, -1, + 63, -1, 141, 216, -21, -9, -3, -1, 47, 242, -3, -1, 110, 156, 15, + -5, -3, -1, 201, 94, 171, -3, -1, 125, 215, 78, -11, -5, -3, -1, + 200, 214, 62, -1, 185, -1, 155, 170, -1, 31, 241, -23, -13, -5, -1, + 240, -1, 186, 229, -3, -1, 228, 140, -1, 109, 227, -5, -1, 226, -1, + 46, 14, -1, 30, 225, -15, -7, -3, -1, 224, 93, -1, 213, 124, -3, + -1, 199, 77, -1, 139, 184, -7, -3, -1, 212, 154, -1, 169, 108, -1, + 198, 61, -37, -21, -9, -5, -3, -1, 211, 123, 45, -1, 210, 29, -5, + -1, 183, -1, 92, 197, -3, -1, 153, 122, 195, -7, -5, -3, -1, 167, + 151, 75, 209, -3, -1, 13, 208, -1, 138, 168, -11, -7, -3, -1, 76, + 196, -1, 107, 182, -1, 60, 44, -3, -1, 194, 91, -3, -1, 181, 137, + 28, -43, -23, -11, -5, -1, 193, -1, 152, 12, -1, 192, -1, 180, 106, + -5, -3, -1, 166, 121, 59, -1, 179, -1, 136, 90, -11, -5, -1, 43, + -1, 165, 105, -1, 164, -1, 120, 135, -5, -1, 148, -1, 119, 118, 178, + -11, -3, -1, 27, 177, -3, -1, 11, 176, -1, 150, 74, -7, -3, -1, + 58, 163, -1, 89, 149, -1, 42, 162, -47, -23, -9, -3, -1, 26, 161, + -3, -1, 10, 104, 160, -5, -3, -1, 134, 73, 147, -3, -1, 57, 88, + -1, 133, 103, -9, -3, -1, 41, 146, -3, -1, 87, 117, 56, -5, -1, + 131, -1, 102, 71, -3, -1, 116, 86, -1, 101, 115, -11, -3, -1, 25, + 145, -3, -1, 9, 144, -1, 72, 132, -7, -5, -1, 114, -1, 70, 100, + 40, -1, 130, 24, -41, -27, -11, -5, -3, -1, 55, 39, 23, -1, 113, + -1, 85, 7, -7, -3, -1, 112, 54, -1, 99, 69, -3, -1, 84, 38, + -1, 98, 53, -5, -1, 129, -1, 8, 128, -3, -1, 22, 97, -1, 6, + 96, -13, -9, -5, -3, -1, 83, 68, 37, -1, 82, 5, -1, 21, 81, + -7, -3, -1, 52, 67, -1, 80, 36, -3, -1, 66, 51, 20, -19, -11, + -5, -1, 65, -1, 4, 64, -3, -1, 35, 50, 19, -3, -1, 49, 3, + -1, 48, 34, -3, -1, 18, 33, -1, 2, 32, -3, -1, 17, 1, 16, + 0 }; static short tab15[] = { --495,-445,-355,-263,-183,-115, -77, -43, -27, -13, -7, -3, -1, 255, 239, - -1, 254, 223, -1, 238, -1, 253, 207, -7, -3, -1, 252, 222, -1, 237, - 191, -1, 251, -1, 206, 236, -7, -3, -1, 221, 175, -1, 250, 190, -3, - -1, 235, 205, -1, 220, 159, -15, -7, -3, -1, 249, 234, -1, 189, 219, - -3, -1, 143, 248, -1, 204, 158, -7, -3, -1, 233, 127, -1, 247, 173, - -3, -1, 218, 188, -1, 111, -1, 174, 15, -19, -11, -3, -1, 203, 246, - -3, -1, 142, 232, -1, 95, 157, -3, -1, 245, 126, -1, 231, 172, -9, - -3, -1, 202, 187, -3, -1, 217, 141, 79, -3, -1, 244, 63, -1, 243, - 216, -33, -17, -9, -3, -1, 230, 47, -1, 242, -1, 110, 240, -3, -1, - 31, 241, -1, 156, 201, -7, -3, -1, 94, 171, -1, 186, 229, -3, -1, - 125, 215, -1, 78, 228, -15, -7, -3, -1, 140, 200, -1, 62, 109, -3, - -1, 214, 227, -1, 155, 185, -7, -3, -1, 46, 170, -1, 226, 30, -5, - -1, 225, -1, 14, 224, -1, 93, 213, -45, -25, -13, -7, -3, -1, 124, - 199, -1, 77, 139, -1, 212, -1, 184, 154, -7, -3, -1, 169, 108, -1, - 198, 61, -1, 211, 210, -9, -5, -3, -1, 45, 13, 29, -1, 123, 183, - -5, -1, 209, -1, 92, 208, -1, 197, 138, -17, -7, -3, -1, 168, 76, - -1, 196, 107, -5, -1, 182, -1, 153, 12, -1, 60, 195, -9, -3, -1, - 122, 167, -1, 166, -1, 192, 11, -1, 194, -1, 44, 91, -55, -29, -15, - -7, -3, -1, 181, 28, -1, 137, 152, -3, -1, 193, 75, -1, 180, 106, - -5, -3, -1, 59, 121, 179, -3, -1, 151, 136, -1, 43, 90, -11, -5, - -1, 178, -1, 165, 27, -1, 177, -1, 176, 105, -7, -3, -1, 150, 74, - -1, 164, 120, -3, -1, 135, 58, 163, -17, -7, -3, -1, 89, 149, -1, - 42, 162, -3, -1, 26, 161, -3, -1, 10, 160, 104, -7, -3, -1, 134, - 73, -1, 148, 57, -5, -1, 147, -1, 119, 9, -1, 88, 133, -53, -29, - -13, -7, -3, -1, 41, 103, -1, 118, 146, -1, 145, -1, 25, 144, -7, - -3, -1, 72, 132, -1, 87, 117, -3, -1, 56, 131, -1, 102, 71, -7, - -3, -1, 40, 130, -1, 24, 129, -7, -3, -1, 116, 8, -1, 128, 86, - -3, -1, 101, 55, -1, 115, 70, -17, -7, -3, -1, 39, 114, -1, 100, - 23, -3, -1, 85, 113, -3, -1, 7, 112, 54, -7, -3, -1, 99, 69, - -1, 84, 38, -3, -1, 98, 22, -3, -1, 6, 96, 53, -33, -19, -9, - -5, -1, 97, -1, 83, 68, -1, 37, 82, -3, -1, 21, 81, -3, -1, - 5, 80, 52, -7, -3, -1, 67, 36, -1, 66, 51, -1, 65, -1, 20, - 4, -9, -3, -1, 35, 50, -3, -1, 64, 3, 19, -3, -1, 49, 48, - 34, -9, -7, -3, -1, 18, 33, -1, 2, 32, 17, -3, -1, 1, 16, - 0 + -495,-445,-355,-263,-183,-115, -77, -43, -27, -13, -7, -3, -1, 255, 239, + -1, 254, 223, -1, 238, -1, 253, 207, -7, -3, -1, 252, 222, -1, 237, + 191, -1, 251, -1, 206, 236, -7, -3, -1, 221, 175, -1, 250, 190, -3, + -1, 235, 205, -1, 220, 159, -15, -7, -3, -1, 249, 234, -1, 189, 219, + -3, -1, 143, 248, -1, 204, 158, -7, -3, -1, 233, 127, -1, 247, 173, + -3, -1, 218, 188, -1, 111, -1, 174, 15, -19, -11, -3, -1, 203, 246, + -3, -1, 142, 232, -1, 95, 157, -3, -1, 245, 126, -1, 231, 172, -9, + -3, -1, 202, 187, -3, -1, 217, 141, 79, -3, -1, 244, 63, -1, 243, + 216, -33, -17, -9, -3, -1, 230, 47, -1, 242, -1, 110, 240, -3, -1, + 31, 241, -1, 156, 201, -7, -3, -1, 94, 171, -1, 186, 229, -3, -1, + 125, 215, -1, 78, 228, -15, -7, -3, -1, 140, 200, -1, 62, 109, -3, + -1, 214, 227, -1, 155, 185, -7, -3, -1, 46, 170, -1, 226, 30, -5, + -1, 225, -1, 14, 224, -1, 93, 213, -45, -25, -13, -7, -3, -1, 124, + 199, -1, 77, 139, -1, 212, -1, 184, 154, -7, -3, -1, 169, 108, -1, + 198, 61, -1, 211, 210, -9, -5, -3, -1, 45, 13, 29, -1, 123, 183, + -5, -1, 209, -1, 92, 208, -1, 197, 138, -17, -7, -3, -1, 168, 76, + -1, 196, 107, -5, -1, 182, -1, 153, 12, -1, 60, 195, -9, -3, -1, + 122, 167, -1, 166, -1, 192, 11, -1, 194, -1, 44, 91, -55, -29, -15, + -7, -3, -1, 181, 28, -1, 137, 152, -3, -1, 193, 75, -1, 180, 106, + -5, -3, -1, 59, 121, 179, -3, -1, 151, 136, -1, 43, 90, -11, -5, + -1, 178, -1, 165, 27, -1, 177, -1, 176, 105, -7, -3, -1, 150, 74, + -1, 164, 120, -3, -1, 135, 58, 163, -17, -7, -3, -1, 89, 149, -1, + 42, 162, -3, -1, 26, 161, -3, -1, 10, 160, 104, -7, -3, -1, 134, + 73, -1, 148, 57, -5, -1, 147, -1, 119, 9, -1, 88, 133, -53, -29, + -13, -7, -3, -1, 41, 103, -1, 118, 146, -1, 145, -1, 25, 144, -7, + -3, -1, 72, 132, -1, 87, 117, -3, -1, 56, 131, -1, 102, 71, -7, + -3, -1, 40, 130, -1, 24, 129, -7, -3, -1, 116, 8, -1, 128, 86, + -3, -1, 101, 55, -1, 115, 70, -17, -7, -3, -1, 39, 114, -1, 100, + 23, -3, -1, 85, 113, -3, -1, 7, 112, 54, -7, -3, -1, 99, 69, + -1, 84, 38, -3, -1, 98, 22, -3, -1, 6, 96, 53, -33, -19, -9, + -5, -1, 97, -1, 83, 68, -1, 37, 82, -3, -1, 21, 81, -3, -1, + 5, 80, 52, -7, -3, -1, 67, 36, -1, 66, 51, -1, 65, -1, 20, + 4, -9, -3, -1, 35, 50, -3, -1, 64, 3, 19, -3, -1, 49, 48, + 34, -9, -7, -3, -1, 18, 33, -1, 2, 32, 17, -3, -1, 1, 16, + 0 }; static short tab16[] = { --509,-503,-461,-323,-103, -37, -27, -15, -7, -3, -1, 239, 254, -1, 223, - 253, -3, -1, 207, 252, -1, 191, 251, -5, -1, 175, -1, 250, 159, -3, - -1, 249, 248, 143, -7, -3, -1, 127, 247, -1, 111, 246, 255, -9, -5, - -3, -1, 95, 245, 79, -1, 244, 243, -53, -1, 240, -1, 63, -29, -19, - -13, -7, -5, -1, 206, -1, 236, 221, 222, -1, 233, -1, 234, 217, -1, - 238, -1, 237, 235, -3, -1, 190, 205, -3, -1, 220, 219, 174, -11, -5, - -1, 204, -1, 173, 218, -3, -1, 126, 172, 202, -5, -3, -1, 201, 125, - 94, 189, 242, -93, -5, -3, -1, 47, 15, 31, -1, 241, -49, -25, -13, - -5, -1, 158, -1, 188, 203, -3, -1, 142, 232, -1, 157, 231, -7, -3, - -1, 187, 141, -1, 216, 110, -1, 230, 156, -13, -7, -3, -1, 171, 186, - -1, 229, 215, -1, 78, -1, 228, 140, -3, -1, 200, 62, -1, 109, -1, - 214, 155, -19, -11, -5, -3, -1, 185, 170, 225, -1, 212, -1, 184, 169, - -5, -1, 123, -1, 183, 208, 227, -7, -3, -1, 14, 224, -1, 93, 213, - -3, -1, 124, 199, -1, 77, 139, -75, -45, -27, -13, -7, -3, -1, 154, - 108, -1, 198, 61, -3, -1, 92, 197, 13, -7, -3, -1, 138, 168, -1, - 153, 76, -3, -1, 182, 122, 60, -11, -5, -3, -1, 91, 137, 28, -1, - 192, -1, 152, 121, -1, 226, -1, 46, 30, -15, -7, -3, -1, 211, 45, - -1, 210, 209, -5, -1, 59, -1, 151, 136, 29, -7, -3, -1, 196, 107, - -1, 195, 167, -1, 44, -1, 194, 181, -23, -13, -7, -3, -1, 193, 12, - -1, 75, 180, -3, -1, 106, 166, 179, -5, -3, -1, 90, 165, 43, -1, - 178, 27, -13, -5, -1, 177, -1, 11, 176, -3, -1, 105, 150, -1, 74, - 164, -5, -3, -1, 120, 135, 163, -3, -1, 58, 89, 42, -97, -57, -33, - -19, -11, -5, -3, -1, 149, 104, 161, -3, -1, 134, 119, 148, -5, -3, - -1, 73, 87, 103, 162, -5, -1, 26, -1, 10, 160, -3, -1, 57, 147, - -1, 88, 133, -9, -3, -1, 41, 146, -3, -1, 118, 9, 25, -5, -1, - 145, -1, 144, 72, -3, -1, 132, 117, -1, 56, 131, -21, -11, -5, -3, - -1, 102, 40, 130, -3, -1, 71, 116, 24, -3, -1, 129, 128, -3, -1, - 8, 86, 55, -9, -5, -1, 115, -1, 101, 70, -1, 39, 114, -5, -3, - -1, 100, 85, 7, 23, -23, -13, -5, -1, 113, -1, 112, 54, -3, -1, - 99, 69, -1, 84, 38, -3, -1, 98, 22, -1, 97, -1, 6, 96, -9, - -5, -1, 83, -1, 53, 68, -1, 37, 82, -1, 81, -1, 21, 5, -33, - -23, -13, -7, -3, -1, 52, 67, -1, 80, 36, -3, -1, 66, 51, 20, - -5, -1, 65, -1, 4, 64, -1, 35, 50, -3, -1, 19, 49, -3, -1, - 3, 48, 34, -3, -1, 18, 33, -1, 2, 32, -3, -1, 17, 1, 16, - 0 + -509,-503,-461,-323,-103, -37, -27, -15, -7, -3, -1, 239, 254, -1, 223, + 253, -3, -1, 207, 252, -1, 191, 251, -5, -1, 175, -1, 250, 159, -3, + -1, 249, 248, 143, -7, -3, -1, 127, 247, -1, 111, 246, 255, -9, -5, + -3, -1, 95, 245, 79, -1, 244, 243, -53, -1, 240, -1, 63, -29, -19, + -13, -7, -5, -1, 206, -1, 236, 221, 222, -1, 233, -1, 234, 217, -1, + 238, -1, 237, 235, -3, -1, 190, 205, -3, -1, 220, 219, 174, -11, -5, + -1, 204, -1, 173, 218, -3, -1, 126, 172, 202, -5, -3, -1, 201, 125, + 94, 189, 242, -93, -5, -3, -1, 47, 15, 31, -1, 241, -49, -25, -13, + -5, -1, 158, -1, 188, 203, -3, -1, 142, 232, -1, 157, 231, -7, -3, + -1, 187, 141, -1, 216, 110, -1, 230, 156, -13, -7, -3, -1, 171, 186, + -1, 229, 215, -1, 78, -1, 228, 140, -3, -1, 200, 62, -1, 109, -1, + 214, 155, -19, -11, -5, -3, -1, 185, 170, 225, -1, 212, -1, 184, 169, + -5, -1, 123, -1, 183, 208, 227, -7, -3, -1, 14, 224, -1, 93, 213, + -3, -1, 124, 199, -1, 77, 139, -75, -45, -27, -13, -7, -3, -1, 154, + 108, -1, 198, 61, -3, -1, 92, 197, 13, -7, -3, -1, 138, 168, -1, + 153, 76, -3, -1, 182, 122, 60, -11, -5, -3, -1, 91, 137, 28, -1, + 192, -1, 152, 121, -1, 226, -1, 46, 30, -15, -7, -3, -1, 211, 45, + -1, 210, 209, -5, -1, 59, -1, 151, 136, 29, -7, -3, -1, 196, 107, + -1, 195, 167, -1, 44, -1, 194, 181, -23, -13, -7, -3, -1, 193, 12, + -1, 75, 180, -3, -1, 106, 166, 179, -5, -3, -1, 90, 165, 43, -1, + 178, 27, -13, -5, -1, 177, -1, 11, 176, -3, -1, 105, 150, -1, 74, + 164, -5, -3, -1, 120, 135, 163, -3, -1, 58, 89, 42, -97, -57, -33, + -19, -11, -5, -3, -1, 149, 104, 161, -3, -1, 134, 119, 148, -5, -3, + -1, 73, 87, 103, 162, -5, -1, 26, -1, 10, 160, -3, -1, 57, 147, + -1, 88, 133, -9, -3, -1, 41, 146, -3, -1, 118, 9, 25, -5, -1, + 145, -1, 144, 72, -3, -1, 132, 117, -1, 56, 131, -21, -11, -5, -3, + -1, 102, 40, 130, -3, -1, 71, 116, 24, -3, -1, 129, 128, -3, -1, + 8, 86, 55, -9, -5, -1, 115, -1, 101, 70, -1, 39, 114, -5, -3, + -1, 100, 85, 7, 23, -23, -13, -5, -1, 113, -1, 112, 54, -3, -1, + 99, 69, -1, 84, 38, -3, -1, 98, 22, -1, 97, -1, 6, 96, -9, + -5, -1, 83, -1, 53, 68, -1, 37, 82, -1, 81, -1, 21, 5, -33, + -23, -13, -7, -3, -1, 52, 67, -1, 80, 36, -3, -1, 66, 51, 20, + -5, -1, 65, -1, 4, 64, -1, 35, 50, -3, -1, 19, 49, -3, -1, + 3, 48, 34, -3, -1, 18, 33, -1, 2, 32, -3, -1, 17, 1, 16, + 0 }; static short tab24[] = { --451,-117, -43, -25, -15, -7, -3, -1, 239, 254, -1, 223, 253, -3, -1, - 207, 252, -1, 191, 251, -5, -1, 250, -1, 175, 159, -1, 249, 248, -9, - -5, -3, -1, 143, 127, 247, -1, 111, 246, -3, -1, 95, 245, -1, 79, - 244, -71, -7, -3, -1, 63, 243, -1, 47, 242, -5, -1, 241, -1, 31, - 240, -25, -9, -1, 15, -3, -1, 238, 222, -1, 237, 206, -7, -3, -1, - 236, 221, -1, 190, 235, -3, -1, 205, 220, -1, 174, 234, -15, -7, -3, - -1, 189, 219, -1, 204, 158, -3, -1, 233, 173, -1, 218, 188, -7, -3, - -1, 203, 142, -1, 232, 157, -3, -1, 217, 126, -1, 231, 172, 255,-235, --143, -77, -45, -25, -15, -7, -3, -1, 202, 187, -1, 141, 216, -5, -3, - -1, 14, 224, 13, 230, -5, -3, -1, 110, 156, 201, -1, 94, 186, -9, - -5, -1, 229, -1, 171, 125, -1, 215, 228, -3, -1, 140, 200, -3, -1, - 78, 46, 62, -15, -7, -3, -1, 109, 214, -1, 227, 155, -3, -1, 185, - 170, -1, 226, 30, -7, -3, -1, 225, 93, -1, 213, 124, -3, -1, 199, - 77, -1, 139, 184, -31, -15, -7, -3, -1, 212, 154, -1, 169, 108, -3, - -1, 198, 61, -1, 211, 45, -7, -3, -1, 210, 29, -1, 123, 183, -3, - -1, 209, 92, -1, 197, 138, -17, -7, -3, -1, 168, 153, -1, 76, 196, - -3, -1, 107, 182, -3, -1, 208, 12, 60, -7, -3, -1, 195, 122, -1, - 167, 44, -3, -1, 194, 91, -1, 181, 28, -57, -35, -19, -7, -3, -1, - 137, 152, -1, 193, 75, -5, -3, -1, 192, 11, 59, -3, -1, 176, 10, - 26, -5, -1, 180, -1, 106, 166, -3, -1, 121, 151, -3, -1, 160, 9, - 144, -9, -3, -1, 179, 136, -3, -1, 43, 90, 178, -7, -3, -1, 165, - 27, -1, 177, 105, -1, 150, 164, -17, -9, -5, -3, -1, 74, 120, 135, - -1, 58, 163, -3, -1, 89, 149, -1, 42, 162, -7, -3, -1, 161, 104, - -1, 134, 119, -3, -1, 73, 148, -1, 57, 147, -63, -31, -15, -7, -3, - -1, 88, 133, -1, 41, 103, -3, -1, 118, 146, -1, 25, 145, -7, -3, - -1, 72, 132, -1, 87, 117, -3, -1, 56, 131, -1, 102, 40, -17, -7, - -3, -1, 130, 24, -1, 71, 116, -5, -1, 129, -1, 8, 128, -1, 86, - 101, -7, -5, -1, 23, -1, 7, 112, 115, -3, -1, 55, 39, 114, -15, - -7, -3, -1, 70, 100, -1, 85, 113, -3, -1, 54, 99, -1, 69, 84, - -7, -3, -1, 38, 98, -1, 22, 97, -5, -3, -1, 6, 96, 53, -1, - 83, 68, -51, -37, -23, -15, -9, -3, -1, 37, 82, -1, 21, -1, 5, - 80, -1, 81, -1, 52, 67, -3, -1, 36, 66, -1, 51, 20, -9, -5, - -1, 65, -1, 4, 64, -1, 35, 50, -1, 19, 49, -7, -5, -3, -1, - 3, 48, 34, 18, -1, 33, -1, 2, 32, -3, -1, 17, 1, -1, 16, - 0 + -451,-117, -43, -25, -15, -7, -3, -1, 239, 254, -1, 223, 253, -3, -1, + 207, 252, -1, 191, 251, -5, -1, 250, -1, 175, 159, -1, 249, 248, -9, + -5, -3, -1, 143, 127, 247, -1, 111, 246, -3, -1, 95, 245, -1, 79, + 244, -71, -7, -3, -1, 63, 243, -1, 47, 242, -5, -1, 241, -1, 31, + 240, -25, -9, -1, 15, -3, -1, 238, 222, -1, 237, 206, -7, -3, -1, + 236, 221, -1, 190, 235, -3, -1, 205, 220, -1, 174, 234, -15, -7, -3, + -1, 189, 219, -1, 204, 158, -3, -1, 233, 173, -1, 218, 188, -7, -3, + -1, 203, 142, -1, 232, 157, -3, -1, 217, 126, -1, 231, 172, 255,-235, + -143, -77, -45, -25, -15, -7, -3, -1, 202, 187, -1, 141, 216, -5, -3, + -1, 14, 224, 13, 230, -5, -3, -1, 110, 156, 201, -1, 94, 186, -9, + -5, -1, 229, -1, 171, 125, -1, 215, 228, -3, -1, 140, 200, -3, -1, + 78, 46, 62, -15, -7, -3, -1, 109, 214, -1, 227, 155, -3, -1, 185, + 170, -1, 226, 30, -7, -3, -1, 225, 93, -1, 213, 124, -3, -1, 199, + 77, -1, 139, 184, -31, -15, -7, -3, -1, 212, 154, -1, 169, 108, -3, + -1, 198, 61, -1, 211, 45, -7, -3, -1, 210, 29, -1, 123, 183, -3, + -1, 209, 92, -1, 197, 138, -17, -7, -3, -1, 168, 153, -1, 76, 196, + -3, -1, 107, 182, -3, -1, 208, 12, 60, -7, -3, -1, 195, 122, -1, + 167, 44, -3, -1, 194, 91, -1, 181, 28, -57, -35, -19, -7, -3, -1, + 137, 152, -1, 193, 75, -5, -3, -1, 192, 11, 59, -3, -1, 176, 10, + 26, -5, -1, 180, -1, 106, 166, -3, -1, 121, 151, -3, -1, 160, 9, + 144, -9, -3, -1, 179, 136, -3, -1, 43, 90, 178, -7, -3, -1, 165, + 27, -1, 177, 105, -1, 150, 164, -17, -9, -5, -3, -1, 74, 120, 135, + -1, 58, 163, -3, -1, 89, 149, -1, 42, 162, -7, -3, -1, 161, 104, + -1, 134, 119, -3, -1, 73, 148, -1, 57, 147, -63, -31, -15, -7, -3, + -1, 88, 133, -1, 41, 103, -3, -1, 118, 146, -1, 25, 145, -7, -3, + -1, 72, 132, -1, 87, 117, -3, -1, 56, 131, -1, 102, 40, -17, -7, + -3, -1, 130, 24, -1, 71, 116, -5, -1, 129, -1, 8, 128, -1, 86, + 101, -7, -5, -1, 23, -1, 7, 112, 115, -3, -1, 55, 39, 114, -15, + -7, -3, -1, 70, 100, -1, 85, 113, -3, -1, 54, 99, -1, 69, 84, + -7, -3, -1, 38, 98, -1, 22, 97, -5, -3, -1, 6, 96, 53, -1, + 83, 68, -51, -37, -23, -15, -9, -3, -1, 37, 82, -1, 21, -1, 5, + 80, -1, 81, -1, 52, 67, -3, -1, 36, 66, -1, 51, 20, -9, -5, + -1, 65, -1, 4, 64, -1, 35, 50, -1, 19, 49, -7, -5, -3, -1, + 3, 48, 34, 18, -1, 33, -1, 2, 32, -3, -1, 17, 1, -1, 16, + 0 }; static short tab_c0[] = { - -29, -21, -13, -7, -3, -1, 11, 15, -1, 13, 14, -3, -1, 7, 5, - 9, -3, -1, 6, 3, -1, 10, 12, -3, -1, 2, 1, -1, 4, 8, - 0 + -29, -21, -13, -7, -3, -1, 11, 15, -1, 13, 14, -3, -1, 7, 5, + 9, -3, -1, 6, 3, -1, 10, 12, -3, -1, 2, 1, -1, 4, 8, + 0 }; static short tab_c1[] = { - -15, -7, -3, -1, 15, 14, -1, 13, 12, -3, -1, 11, 10, -1, 9, - 8, -7, -3, -1, 7, 6, -1, 5, 4, -3, -1, 3, 2, -1, 1, - 0 + -15, -7, -3, -1, 15, 14, -1, 13, 12, -3, -1, 11, 10, -1, 9, + 8, -7, -3, -1, 7, 6, -1, 5, 4, -3, -1, 3, 2, -1, 1, + 0 }; -static struct newhuff ht[] = +static struct newhuff ht[] = { - { /* 0 */ 0 , tab0 } , - { /* 2 */ 0 , tab1 } , - { /* 3 */ 0 , tab2 } , - { /* 3 */ 0 , tab3 } , - { /* 0 */ 0 , tab0 } , - { /* 4 */ 0 , tab5 } , - { /* 4 */ 0 , tab6 } , - { /* 6 */ 0 , tab7 } , - { /* 6 */ 0 , tab8 } , - { /* 6 */ 0 , tab9 } , - { /* 8 */ 0 , tab10 } , - { /* 8 */ 0 , tab11 } , - { /* 8 */ 0 , tab12 } , - { /* 16 */ 0 , tab13 } , - { /* 0 */ 0 , tab0 } , - { /* 16 */ 0 , tab15 } , + { /* 0 */ 0, tab0 }, + { /* 2 */ 0, tab1 }, + { /* 3 */ 0, tab2 }, + { /* 3 */ 0, tab3 }, + { /* 0 */ 0, tab0 }, + { /* 4 */ 0, tab5 }, + { /* 4 */ 0, tab6 }, + { /* 6 */ 0, tab7 }, + { /* 6 */ 0, tab8 }, + { /* 6 */ 0, tab9 }, + { /* 8 */ 0, tab10 }, + { /* 8 */ 0, tab11 }, + { /* 8 */ 0, tab12 }, + { /* 16 */ 0, tab13 }, + { /* 0 */ 0, tab0 }, + { /* 16 */ 0, tab15 }, - { /* 16 */ 1 , tab16 } , - { /* 16 */ 2 , tab16 } , - { /* 16 */ 3 , tab16 } , - { /* 16 */ 4 , tab16 } , - { /* 16 */ 6 , tab16 } , - { /* 16 */ 8 , tab16 } , - { /* 16 */ 10, tab16 } , - { /* 16 */ 13, tab16 } , - { /* 16 */ 4 , tab24 } , - { /* 16 */ 5 , tab24 } , - { /* 16 */ 6 , tab24 } , - { /* 16 */ 7 , tab24 } , - { /* 16 */ 8 , tab24 } , - { /* 16 */ 9 , tab24 } , - { /* 16 */ 11, tab24 } , - { /* 16 */ 13, tab24 } + { /* 16 */ 1, tab16 }, + { /* 16 */ 2, tab16 }, + { /* 16 */ 3, tab16 }, + { /* 16 */ 4, tab16 }, + { /* 16 */ 6, tab16 }, + { /* 16 */ 8, tab16 }, + { /* 16 */ 10, tab16 }, + { /* 16 */ 13, tab16 }, + { /* 16 */ 4, tab24 }, + { /* 16 */ 5, tab24 }, + { /* 16 */ 6, tab24 }, + { /* 16 */ 7, tab24 }, + { /* 16 */ 8, tab24 }, + { /* 16 */ 9, tab24 }, + { /* 16 */ 11, tab24 }, + { /* 16 */ 13, tab24 } }; -static struct newhuff htc[] = +static struct newhuff htc[] = { - { /* 1 , 1 , */ 0 , tab_c0 } , - { /* 1 , 1 , */ 0 , tab_c1 } + { /* 1 , 1 , */ 0, tab_c0 }, + { /* 1 , 1 , */ 0, tab_c1 } }; diff --git a/src/simu/win/mp3/interface.c b/src/simu/win/mp3/interface.c index 7ec3816..1d251da 100644 --- a/src/simu/win/mp3/interface.c +++ b/src/simu/win/mp3/interface.c @@ -10,246 +10,264 @@ struct mpstr *gmp; -BOOL InitMP3(struct mpstr *mp) +BOOL InitMP3(struct mpstr *mp) { - memset(mp,0,sizeof(struct mpstr)); + memset(mp,0,sizeof(struct mpstr)); - mp->framesize = 0; - mp->fsizeold = -1; - mp->bsize = 0; - mp->head = mp->tail = NULL; - mp->fr.single = -1; - mp->bsnum = 0; - mp->synth_bo = 1; + mp->framesize = 0; + mp->fsizeold = -1; + mp->bsize = 0; + mp->head = mp->tail = NULL; + mp->fr.single = -1; + mp->bsnum = 0; + mp->synth_bo = 1; - make_decode_tables(32767); - init_layer3(SBLIMIT); + make_decode_tables(32767); + init_layer3(SBLIMIT); - return !0; + return !0; } void ExitMP3(struct mpstr *mp) { - struct buf *b,*bn; - - b = mp->tail; - while(b) { - free(b->pnt); - bn = b->next; - free(b); - b = bn; - } + struct buf *b,*bn; + + b = mp->tail; + while(b) + { + free(b->pnt); + bn = b->next; + free(b); + b = bn; + } } static struct buf *addbuf(struct mpstr *mp,char *buf,int size) { - struct buf *nbuf; - - nbuf = malloc( sizeof(struct buf) ); - if(!nbuf) { - fprintf(stderr,"Out of memory!\n"); - return NULL; - } - nbuf->pnt = malloc(size); - if(!nbuf->pnt) { - free(nbuf); - return NULL; - } - nbuf->size = size; - memcpy(nbuf->pnt,buf,size); - nbuf->next = NULL; - nbuf->prev = mp->head; - nbuf->pos = 0; - - if(!mp->tail) { - mp->tail = nbuf; - } - else { - mp->head->next = nbuf; - } - - mp->head = nbuf; - mp->bsize += size; - - return nbuf; + struct buf *nbuf; + + nbuf = malloc( sizeof(struct buf) ); + if(!nbuf) + { + fprintf(stderr,"Out of memory!\n"); + return NULL; + } + nbuf->pnt = malloc(size); + if(!nbuf->pnt) + { + free(nbuf); + return NULL; + } + nbuf->size = size; + memcpy(nbuf->pnt,buf,size); + nbuf->next = NULL; + nbuf->prev = mp->head; + nbuf->pos = 0; + + if(!mp->tail) + { + mp->tail = nbuf; + } + else + { + mp->head->next = nbuf; + } + + mp->head = nbuf; + mp->bsize += size; + + return nbuf; } static void remove_buf(struct mpstr *mp) { - struct buf *buf = mp->tail; - - mp->tail = buf->next; - if(mp->tail) - mp->tail->prev = NULL; - else { - mp->tail = mp->head = NULL; - } - - free(buf->pnt); - free(buf); + struct buf *buf = mp->tail; + + mp->tail = buf->next; + if(mp->tail) + { + mp->tail->prev = NULL; + } + else + { + mp->tail = mp->head = NULL; + } + + free(buf->pnt); + free(buf); } static int read_buf_byte(struct mpstr *mp) { - unsigned int b; + unsigned int b; - int pos; + int pos; - pos = mp->tail->pos; - while(pos >= mp->tail->size) { - remove_buf(mp); - pos = mp->tail->pos; - if(!mp->tail) { - fprintf(stderr,"Fatal error!\n"); + pos = mp->tail->pos; + while(pos >= mp->tail->size) + { + remove_buf(mp); + pos = mp->tail->pos; + if(!mp->tail) + { + fprintf(stderr,"Fatal error!\n"); // exit(1); - return 0; - } - } + return 0; + } + } - b = mp->tail->pnt[pos]; - mp->bsize--; - mp->tail->pos++; - + b = mp->tail->pnt[pos]; + mp->bsize--; + mp->tail->pos++; - return b; + + return b; } static void read_head(struct mpstr *mp) { - unsigned long head; + unsigned long head; - mp->header<<=8; - head = read_buf_byte(mp); - mp->header+=(head&255); + mp->header<<=8; + head = read_buf_byte(mp); + mp->header+=(head&255); -/* head <<= 8; - head |= read_buf_byte(mp); - head <<= 8; - head |= read_buf_byte(mp); - head <<= 8; - head |= read_buf_byte(mp); + /* head <<= 8; + head |= read_buf_byte(mp); + head <<= 8; + head |= read_buf_byte(mp); + head <<= 8; + head |= read_buf_byte(mp); - mp->header = head; -*/ + mp->header = head; + */ } int checkmp3header(head) { - if ( - ((head & 0xffe00000) == 0xffe00000) - &&((head>>17)&3) - &&(((head>>12)&0xf) != 0xf) - &&(((head>>10)&0x3) != 0x3 ) - &&((4-((head>>17)&3)) == 3) - ) return 1; - return 0; + if ( + ((head & 0xffe00000) == 0xffe00000) + &&((head>>17)&3) + &&(((head>>12)&0xf) != 0xf) + &&(((head>>10)&0x3) != 0x3 ) + &&((4-((head>>17)&3)) == 3) + ) + { + return 1; + } + return 0; } int decodeMP3(struct mpstr *mp,char *in,int isize,char *out,int osize,int *done) { - int len; + int len; - gmp = mp; + gmp = mp; - if(osize < 4608) - { - fprintf(stderr,"To less out space\n"); - return MP3_ERR; - } + if(osize < 4608) + { + fprintf(stderr,"To less out space\n"); + return MP3_ERR; + } - if(in) - { - if(addbuf(mp,in,isize) == NULL) + if(in) { - return MP3_ERR; - } - } - - - /* First decode header */ - while(mp->framesize == 0) - { - if(mp->bsize < 1) - { - return MP3_NEED_MORE; - } - read_head(mp); - if ((checkmp3header(mp->header))&&(decode_header(&mp->fr,mp->header)==1)) - { - mp->framesize = mp->fr.framesize; - } - } -/* - if(mp->framesize == 0) - { - if(mp->bsize < 4) - { - return MP3_NEED_MORE; - } - read_head(mp); - decode_header(&mp->fr,mp->header); - mp->framesize = mp->fr.framesize; - } -*/ - /* printf(" fr.framesize = %i \n",mp->fr.framesize); - printf(" bsize = %i \n",mp->bsize); - */ - - if(mp->fr.framesize > mp->bsize) - { - return MP3_NEED_MORE; - } - wordpointer = mp->bsspace[mp->bsnum] + 512; - mp->bsnum = (mp->bsnum + 1) & 0x1; - bitindex = 0; - - len = 0; - while(len < mp->framesize) - { - int nlen; - int blen = mp->tail->size - mp->tail->pos; - if( (mp->framesize - len) <= blen) + if(addbuf(mp,in,isize) == NULL) + { + return MP3_ERR; + } + } + + + /* First decode header */ + while(mp->framesize == 0) { - nlen = mp->framesize-len; - } - else + if(mp->bsize < 1) + { + return MP3_NEED_MORE; + } + read_head(mp); + if ((checkmp3header(mp->header))&&(decode_header(&mp->fr,mp->header)==1)) + { + mp->framesize = mp->fr.framesize; + } + } + /* + if(mp->framesize == 0) + { + if(mp->bsize < 4) + { + return MP3_NEED_MORE; + } + read_head(mp); + decode_header(&mp->fr,mp->header); + mp->framesize = mp->fr.framesize; + } + */ + /* printf(" fr.framesize = %i \n",mp->fr.framesize); + printf(" bsize = %i \n",mp->bsize); + */ + + if(mp->fr.framesize > mp->bsize) { - nlen = blen; + return MP3_NEED_MORE; } - memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); - len += nlen; - mp->tail->pos += nlen; - mp->bsize -= nlen; - if(mp->tail->pos == mp->tail->size) + wordpointer = mp->bsspace[mp->bsnum] + 512; + mp->bsnum = (mp->bsnum + 1) & 0x1; + bitindex = 0; + + len = 0; + while(len < mp->framesize) { - remove_buf(mp); + int nlen; + int blen = mp->tail->size - mp->tail->pos; + if( (mp->framesize - len) <= blen) + { + nlen = mp->framesize-len; + } + else + { + nlen = blen; + } + memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); + len += nlen; + mp->tail->pos += nlen; + mp->bsize -= nlen; + if(mp->tail->pos == mp->tail->size) + { + remove_buf(mp); + } } - } - *done = 0; - if(mp->fr.error_protection) - getbits(16); - do_layer3(&mp->fr,(unsigned char *) out,done); + *done = 0; + if(mp->fr.error_protection) + { + getbits(16); + } + do_layer3(&mp->fr,(unsigned char *) out,done); - mp->fsizeold = mp->framesize; - mp->framesize = 0; - return MP3_OK; + mp->fsizeold = mp->framesize; + mp->framesize = 0; + return MP3_OK; } int set_pointer(long backstep) { - unsigned char *bsbufold; - if(gmp->fsizeold < 0 && backstep > 0) { - fprintf(stderr,"Can't step back %ld!\n",backstep); - return MP3_ERR; - } - bsbufold = gmp->bsspace[gmp->bsnum] + 512; - wordpointer -= backstep; - if (backstep) - memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,backstep); - bitindex = 0; - return MP3_OK; + unsigned char *bsbufold; + if(gmp->fsizeold < 0 && backstep > 0) + { + fprintf(stderr,"Can't step back %ld!\n",backstep); + return MP3_ERR; + } + bsbufold = gmp->bsspace[gmp->bsnum] + 512; + wordpointer -= backstep; + if (backstep) + { + memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,backstep); + } + bitindex = 0; + return MP3_OK; } //#endif diff --git a/src/simu/win/mp3/mpg123.h b/src/simu/win/mp3/mpg123.h index bf61cf4..2e75de1 100644 --- a/src/simu/win/mp3/mpg123.h +++ b/src/simu/win/mp3/mpg123.h @@ -59,7 +59,8 @@ extern int tabsel_123[2][3][16]; -struct frame { +struct frame +{ int stereo; int jsbound; int single; @@ -80,11 +81,12 @@ struct frame { int framesize; /* computed framesize */ }; -struct parameter { - int quiet; /* shut up! */ - int tryresync; /* resync stream after error */ - int verbose; /* verbose level */ - int checkrange; +struct parameter +{ + int quiet; /* shut up! */ + int tryresync; /* resync stream after error */ + int verbose; /* verbose level */ + int checkrange; }; /* extern unsigned int get1bit(void); */ @@ -101,34 +103,36 @@ extern int decode_header(struct frame *fr,unsigned long newhead); -struct gr_info_s { - int scfsi; - unsigned part2_3_length; - unsigned big_values; - unsigned scalefac_compress; - unsigned block_type; - unsigned mixed_block_flag; - unsigned table_select[3]; - unsigned subblock_gain[3]; - unsigned maxband[3]; - unsigned maxbandl; - unsigned maxb; - unsigned region1start; - unsigned region2start; - unsigned preflag; - unsigned scalefac_scale; - unsigned count1table_select; - real *full_gain[3]; - real *pow2gain; +struct gr_info_s +{ + int scfsi; + unsigned part2_3_length; + unsigned big_values; + unsigned scalefac_compress; + unsigned block_type; + unsigned mixed_block_flag; + unsigned table_select[3]; + unsigned subblock_gain[3]; + unsigned maxband[3]; + unsigned maxbandl; + unsigned maxb; + unsigned region1start; + unsigned region2start; + unsigned preflag; + unsigned scalefac_scale; + unsigned count1table_select; + real *full_gain[3]; + real *pow2gain; }; struct III_sideinfo { - unsigned main_data_begin; - unsigned private_bits; - struct { - struct gr_info_s gr[2]; - } ch[2]; + unsigned main_data_begin; + unsigned private_bits; + struct + { + struct gr_info_s gr[2]; + } ch[2]; }; extern int synth_1to1 (real *,int,unsigned char *,int *); diff --git a/src/simu/win/mp3/mpglib.h b/src/simu/win/mp3/mpglib.h index b3c051c..de96c95 100644 --- a/src/simu/win/mp3/mpglib.h +++ b/src/simu/win/mp3/mpglib.h @@ -1,33 +1,36 @@ -struct buf { - unsigned char *pnt; - long size; - long pos; - struct buf *next; - struct buf *prev; +struct buf +{ + unsigned char *pnt; + long size; + long pos; + struct buf *next; + struct buf *prev; }; -struct framebuf { - struct buf *buf; - long pos; - struct frame *next; - struct frame *prev; +struct framebuf +{ + struct buf *buf; + long pos; + struct frame *next; + struct frame *prev; }; -struct mpstr { - struct buf *head,*tail; - int bsize; - int framesize; - int fsizeold; - struct frame fr; - unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */ - real hybrid_block[2][2][SBLIMIT*SSLIMIT]; - int hybrid_blc[2]; - unsigned long header; - int bsnum; - real synth_buffs[2][2][0x110]; - int synth_bo; - +struct mpstr +{ + struct buf *head,*tail; + int bsize; + int framesize; + int fsizeold; + struct frame fr; + unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */ + real hybrid_block[2][2][SBLIMIT*SSLIMIT]; + int hybrid_blc[2]; + unsigned long header; + int bsnum; + real synth_buffs[2][2][0x110]; + int synth_bo; + }; #define BOOL int @@ -39,6 +42,6 @@ struct mpstr { BOOL InitMP3(struct mpstr *mp); int decodeMP3(struct mpstr *mp,char *inmemory,int inmemsize, - char *outmemory,int outmemsize,int *done); + char *outmemory,int outmemsize,int *done); void ExitMP3(struct mpstr *mp); diff --git a/src/simu/win/simu.c b/src/simu/win/simu.c index dfae497..cbf6b99 100644 --- a/src/simu/win/simu.c +++ b/src/simu/win/simu.c @@ -42,10 +42,16 @@ #endif #ifdef VMICROCHIP -int simuInit(){ return 0;} -int simuDoLoop(){ return 0;} -void simuSetLed(vub i,vub val){} -void simuSetMotor(vub i,vub val){} +int simuInit() +{ + return 0; +} +int simuDoLoop() +{ + return 0; +} +void simuSetLed(vub i,vub val) {} +void simuSetMotor(vub i,vub val) {} #else #include @@ -83,17 +89,17 @@ uchar colormul[256*256]; // initialisation des tables de couleur void colortabInit() { - int i,j,k; + int i,j,k; - for(i=0;i<256;i++) - for(j=0;j<256;j++) - { - k=i+j; - coloradd[i+(j<<8)]=(k<=255)?k:255; + for(i=0; i<256; i++) + for(j=0; j<256; j++) + { + k=i+j; + coloradd[i+(j<<8)]=(k<=255)?k:255; - k=i*j/255; - colormul[i+(j<<8)]=k; - } + k=i*j/255; + colormul[i+(j<<8)]=k; + } } // définition physique des leds @@ -114,14 +120,15 @@ int lastmovex=0; int lastmovey=0; #ifdef PROTORABBIT -int diodex[NBLED]={80,80,80,40,40,40,80,80,80,120,120,120,80,80,80}; -int diodey[NBLED]={40,40,40,90,90,90,90,90,90,90,90,90,130,130,130}; +int diodex[NBLED]= {80,80,80,40,40,40,80,80,80,120,120,120,80,80,80}; +int diodey[NBLED]= {40,40,40,90,90,90,90,90,90,90,90,90,130,130,130}; int diodergb[NBLED]= -{0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000 +{ + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000 }; HWND buttoncheck; HWND button3; @@ -132,18 +139,19 @@ int height=160*SCALE; #endif #ifdef PROTODAL -int diodex[NBLED]={40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120}; -int diodey[NBLED]={40,40,40,40,40,40,40,40,40,80,80,80,80,80,80,80,80,80,120,120,120,120,120,120,120,120,120}; +int diodex[NBLED]= {40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120,40,40,40,80,80,80,120,120,120}; +int diodey[NBLED]= {40,40,40,40,40,40,40,40,40,80,80,80,80,80,80,80,80,80,120,120,120,120,120,120,120,120,120}; int diodergb[NBLED]= -{0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000, -0x0000ff,0x00ff00,0xff0000 +{ + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000, + 0x0000ff,0x00ff00,0xff0000 }; HWND buttoncheck; #define SUPP_HIGH 20 @@ -167,445 +175,470 @@ DIBSECTION mainbmpinfo ; void winrepaint() { - - HDC Dcw , Dcb ; + + HDC Dcw, Dcb ; HBITMAP OldBitmap ; - PAINTSTRUCT ps; + PAINTSTRUCT ps; - Dcw=BeginPaint(mainwin,&ps); - Dcb=CreateCompatibleDC(Dcw); - OldBitmap=SelectObject(Dcb,mainbmp); - BitBlt(Dcw,0,0,width,height,Dcb,0,0,SRCCOPY); - SelectObject(Dcb,OldBitmap); - DeleteDC(Dcb); + Dcw=BeginPaint(mainwin,&ps); + Dcb=CreateCompatibleDC(Dcw); + OldBitmap=SelectObject(Dcb,mainbmp); + BitBlt(Dcw,0,0,width,height,Dcb,0,0,SRCCOPY); + SelectObject(Dcb,OldBitmap); + DeleteDC(Dcb); - EndPaint(mainwin,&ps); + EndPaint(mainwin,&ps); } void winupdate() { - InvalidateRect(mainwin,NULL,FALSE); + InvalidateRect(mainwin,NULL,FALSE); UpdateWindow(mainwin); } void mire() { - int i,j; - char *pnt,*pnt0; - int next; - int increment=4; - - pnt0=mainbmppnt; - next=mainbmpnextline; - - for(i=0;i=width)||(y>=height)) return; - pnt=mainbmppnt; - next=mainbmpnextline; - pnt+=y*mainbmpnextline+x*increment; - pnt[0]=r; - pnt[1]=g; - pnt[2]=b; + int r,g,b; + uchar *pnt; + int next; + int increment=4; + x*=SCALE; + y*=SCALE; + r=INTTOR(rgb); + g=INTTOG(rgb); + b=INTTOB(rgb); + if ((x<0)||(y<0)||(x>=width)||(y>=height)) + { + return; + } + pnt=mainbmppnt; + next=mainbmpnextline; + pnt+=y*mainbmpnextline+x*increment; + pnt[0]=r; + pnt[1]=g; + pnt[2]=b; } void diodedraw(int num,int val) { - int i,j,x,y,r,g,b,rgb; - uchar *pnt,*pnt0; - int next; - int increment=4; - - if ((num<0)||(num>=NBLED)) return; - x=diodex[num]*SCALE-DIODELEN/2; - y=diodey[num]*SCALE-DIODELEN/2; - rgb=diodergb[num]; - r=COLORMUL(INTTOR(rgb),(uchar)val); - g=COLORMUL(INTTOG(rgb),(uchar)val); - b=COLORMUL(INTTOB(rgb),(uchar)val); - - if ((x<0)||(y<0)||(x+DIODELEN>width)||(y+DIODELEN>height)) return; - - pnt0=mainbmppnt; - next=mainbmpnextline; - pnt0+=y*mainbmpnextline+x*increment; - - for(i=0;i=NBLED)) + { + return; + } + x=diodex[num]*SCALE-DIODELEN/2; + y=diodey[num]*SCALE-DIODELEN/2; + rgb=diodergb[num]; + r=COLORMUL(INTTOR(rgb),(uchar)val); + g=COLORMUL(INTTOG(rgb),(uchar)val); + b=COLORMUL(INTTOB(rgb),(uchar)val); + + if ((x<0)||(y<0)||(x+DIODELEN>width)||(y+DIODELEN>height)) + { + return; + } + + pnt0=mainbmppnt; + next=mainbmpnextline; + pnt0+=y*mainbmpnextline+x*increment; + + for(i=0; i>9)-MOTORLEN/2; - rgb=0xc0c0c0; - r=INTTOR(rgb); - g=INTTOG(rgb); - b=INTTOB(rgb); - - if ((x<0)||(y<0)||(x+MOTORLEN>width)||(y+MOTORLEN>height)) return; - - pnt0=mainbmppnt; - next=mainbmpnextline; - pnt0+=y*mainbmpnextline+x*increment; - - for(i=0;i>9)-MOTORLEN/2; + rgb=0xc0c0c0; + r=INTTOR(rgb); + g=INTTOG(rgb); + b=INTTOB(rgb); + + if ((x<0)||(y<0)||(x+MOTORLEN>width)||(y+MOTORLEN>height)) + { + return; + } + + pnt0=mainbmppnt; + next=mainbmpnextline; + pnt0+=y*mainbmpnextline+x*increment; + + for(i=0; i0)?d:-d; - d=HIWORD(lParam)-lastmovey; - movesimu+=(d>0)?d:-d; - lastmovex=LOWORD(lParam); - lastmovey=HIWORD(lParam); - if (rclicsimu) - { - xclicsimu=LOWORD(lParam)*1000/width; - yclicsimu=HIWORD(lParam)*1000/height; - d=-1; - if ((yclicsimu>=250)&&(yclicsimu<750)) - { - if (xclicsimu<150) d=0; - if (xclicsimu>850) d=1; - } - if (d>=0) - { - v=MAXMOTORVAL*(yclicsimu-250)/500; - if (vv;k--) - { - last=motorwheel[motorval[d]]; - motorval[d]--; - if (last0)?d:-d; + d=HIWORD(lParam)-lastmovey; + movesimu+=(d>0)?d:-d; + lastmovex=LOWORD(lParam); + lastmovey=HIWORD(lParam); + if (rclicsimu) + { + xclicsimu=LOWORD(lParam)*1000/width; + yclicsimu=HIWORD(lParam)*1000/height; + d=-1; + if ((yclicsimu>=250)&&(yclicsimu<750)) + { + if (xclicsimu<150) + { + d=0; + } + if (xclicsimu>850) + { + d=1; + } + } + if (d>=0) + { + v=MAXMOTORVAL*(yclicsimu-250)/500; + if (vv; k--) + { + last=motorwheel[motorval[d]]; + motorval[d]--; + if (last150)&&(xclicsimu<850)) - { - setButton(1); - } - break; + rclicsimu=1; + xclicsimu=LOWORD(lParam)*1000/width; + yclicsimu=HIWORD(lParam)*1000/height; + if ((xclicsimu>150)&&(xclicsimu<850)) + { + setButton(1); + } + break; case WM_LBUTTONUP : - rclicsimu=0; - setButton(0); - xclicsimu=-1; - yclicsimu=-1; - break; - case MM_WOM_DONE: - audioEventPlayData(msg,wParam,lParam); - break; - case MM_WIM_DATA: - audioEventRecData(msg,wParam,lParam); - break; - case UDPEVENT: - udpevent(msg,wParam,lParam); - break; - case TCPEVENT: - tcpevent(msg,wParam,lParam); - break; + rclicsimu=0; + setButton(0); + xclicsimu=-1; + yclicsimu=-1; + break; + case MM_WOM_DONE: + audioEventPlayData(msg,wParam,lParam); + break; + case MM_WIM_DATA: + audioEventRecData(msg,wParam,lParam); + break; + case UDPEVENT: + udpevent(msg,wParam,lParam); + break; + case TCPEVENT: + tcpevent(msg,wParam,lParam); + break; default: return( DefWindowProc( hwnd, msg, wParam, lParam ) ); } - - return 0; + + return 0; } int initWindow() { - char buf[256]; + char buf[256]; - RECT r; - r.bottom=height+SUPP_HIGH; - r.right=width; - r.left=0; - r.top=0; + RECT r; + r.bottom=height+SUPP_HIGH; + r.right=width; + r.left=0; + r.top=0; - AdjustWindowRect(&r,FlagMainWindow,FALSE); + AdjustWindowRect(&r,FlagMainWindow,FALSE); - mainwin = CreateWindow("GenericClass",TEXT("Vlisp - "VLISP_HARDWARE), FlagMainWindow, - 600,0,r.right-r.left,r.bottom-r.top, - NULL,NULL,NULL,NULL); + mainwin = CreateWindow("GenericClass",TEXT("Vlisp - "VLISP_HARDWARE), FlagMainWindow, + 600,0,r.right-r.left,r.bottom-r.top, + NULL,NULL,NULL,NULL); if (!mainwin) - { - wsprintf(buf,TEXT("%d"),GetLastError()); - MessageBox(NULL,TEXT("window error"),buf,MB_OK); - return -1; - } - + { + wsprintf(buf,TEXT("%d"),GetLastError()); + MessageBox(NULL,TEXT("window error"),buf,MB_OK); + return -1; + } + ShowWindow(mainwin,SW_SHOW); UpdateWindow(mainwin); #ifdef PROTORABBIT - crText("button=",mainwin,0,height,width/2,20); - buttoncheck=crCheck("",mainwin,width/2,height,width/2,20); - crText("b3=",mainwin,0,height+20,width/2,20); - button3=crEdit("0",mainwin,width/2,height+20,width/2,20); - crText("rfid=",mainwin,0,height+40,width/2,20); - buttonrfid=crEdit("",mainwin,width/2,height+40,width/2,20); + crText("button=",mainwin,0,height,width/2,20); + buttoncheck=crCheck("",mainwin,width/2,height,width/2,20); + crText("b3=",mainwin,0,height+20,width/2,20); + button3=crEdit("0",mainwin,width/2,height+20,width/2,20); + crText("rfid=",mainwin,0,height+40,width/2,20); + buttonrfid=crEdit("",mainwin,width/2,height+40,width/2,20); #endif #ifdef PROTODAL - crText("button=",mainwin,0,height,width/2,20); - buttoncheck=crCheck("",mainwin,width/2,height,width/2,20); + crText("button=",mainwin,0,height,width/2,20); + buttoncheck=crCheck("",mainwin,width/2,height,width/2,20); #endif - return 0; + return 0; } int initBitmap ( ) { - BITMAPINFO Bi ; - int tf ; - HDC dc ; - char buf[256]; - - dc = NULL ; - - Bi.bmiHeader.biSize = sizeof ( BITMAPINFOHEADER ) ; - Bi.bmiHeader.biWidth = width ; - - Bi.bmiHeader.biHeight = - height ; - - Bi.bmiHeader.biPlanes = 1 ; - Bi.bmiHeader.biBitCount = 32 ; - Bi.bmiHeader.biCompression = BI_RGB ; - Bi.bmiHeader.biSizeImage = 0 ; - Bi.bmiHeader.biXPelsPerMeter = 0 ; - Bi.bmiHeader.biYPelsPerMeter = 0 ; - Bi.bmiHeader.biClrUsed = 0 ; - tf = DIB_RGB_COLORS ; + BITMAPINFO Bi ; + int tf ; + HDC dc ; + char buf[256]; + + dc = NULL ; + + Bi.bmiHeader.biSize = sizeof ( BITMAPINFOHEADER ) ; + Bi.bmiHeader.biWidth = width ; + + Bi.bmiHeader.biHeight = - height ; + + Bi.bmiHeader.biPlanes = 1 ; + Bi.bmiHeader.biBitCount = 32 ; + Bi.bmiHeader.biCompression = BI_RGB ; + Bi.bmiHeader.biSizeImage = 0 ; + Bi.bmiHeader.biXPelsPerMeter = 0 ; + Bi.bmiHeader.biYPelsPerMeter = 0 ; + Bi.bmiHeader.biClrUsed = 0 ; + tf = DIB_RGB_COLORS ; Bi.bmiHeader.biClrImportant = Bi.bmiHeader.biClrUsed ; // Bi.bmiColors[0] = NULL ; - mainbmp = CreateDIBSection ( dc , (BITMAPINFO * ) & Bi , tf , & mainbmppnt , NULL , 0 ) ; - - if (mainbmp == NULL ) - { - wsprintf(buf,TEXT("%d"),GetLastError()); - MessageBox(NULL,TEXT("bitmap error"),buf,MB_OK); - return -1; - } - - GetObject ( mainbmp , sizeof( DIBSECTION ) , &mainbmpinfo ) ; + mainbmp = CreateDIBSection ( dc, (BITMAPINFO * ) & Bi, tf, & mainbmppnt, NULL, 0 ) ; + + if (mainbmp == NULL ) + { + wsprintf(buf,TEXT("%d"),GetLastError()); + MessageBox(NULL,TEXT("bitmap error"),buf,MB_OK); + return -1; + } + + GetObject ( mainbmp, sizeof( DIBSECTION ), &mainbmpinfo ) ; mainbmpnextline=mainbmpinfo.dsBm.bmWidthBytes ; - return 0 ; -} + return 0 ; +} static BOOL FirstInstance() { WNDCLASS wc; BOOL rc; - + wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (LPVOID) WindowProc; wc.cbClsExtra = 0; @@ -617,64 +650,82 @@ static BOOL FirstInstance() wc.lpszMenuName = 0; wc.lpszClassName = "GenericClass"; rc = RegisterClass( &wc ); - + return( rc ); } // fonction d'affichage des diodes int simuDisplay(int* intensity) { - int i; - erasebmp(); - for(i=0;i>9,0xffffff); - plot(2,(160*(128+256))>>9,0xffffff); - plot(160-2,(160*128)>>9,0xffffff); - plot(160-2,(160*(128+256))>>9,0xffffff); - motordraw(15,motorval[0]*256/MAXMOTORVAL); - motordraw(160-15,motorval[1]*256/MAXMOTORVAL); + plot(2,(160*128)>>9,0xffffff); + plot(2,(160*(128+256))>>9,0xffffff); + plot(160-2,(160*128)>>9,0xffffff); + plot(160-2,(160*(128+256))>>9,0xffffff); + motordraw(15,motorval[0]*256/MAXMOTORVAL); + motordraw(160-15,motorval[1]*256/MAXMOTORVAL); #endif - // VPRINT("led %d = %d\n",i,intensity[i]); - } + // VPRINT("led %d = %d\n",i,intensity[i]); + } - winupdate(); - return 0; + winupdate(); + return 0; } // initialisation du simulateur vsd simuInit() { - int i; - colortabInit(); + int i; + colortabInit(); - for(i=0;i=NBHOLES-MASKEDHOLES) motorwheel[i]=1; - } + for(i=0; i=NBHOLES-MASKEDHOLES) + { + motorwheel[i]=1; + } + } #endif - FirstInstance(); - if (initBitmap ( )) return FALSE; - mire(); - if (initWindow()) return FALSE; - winupdate(); + FirstInstance(); + if (initBitmap ( )) + { + return FALSE; + } + mire(); + if (initWindow()) + { + return FALSE; + } + winupdate(); // setButton(1); - simuaudioinit(); - simunetinit(); - return 0; + simuaudioinit(); + simunetinit(); + return 0; } @@ -682,69 +733,87 @@ vsd simuInit() // fonction à appeler régulièrement, pour traiter les messages de la fenêtre du simulateur vsd simuDoLoop() { - MSG msg; + MSG msg; #ifdef VL_MOTORS - int i,last; + int i,last; #endif - while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { - TranslateMessage( &msg ); - DispatchMessage( &msg ); - } + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage( &msg ); + DispatchMessage( &msg ); + } #ifdef VL_MOTORS - for(i=0;i=MAXMOTORVAL) motorval[i]-=MAXMOTORVAL; - if (last=MAXMOTORVAL) + { + motorval[i]-=MAXMOTORVAL; + } + if (last=NBLED)) return; - diodeval[i]=(val>>16)&255; - diodeval[i+1]=(val>>8)&255; - diodeval[i+2]=(val)&255; + i*=3; + if ((i<0)||(i>=NBLED)) + { + return; + } + diodeval[i]=(val>>16)&255; + diodeval[i+1]=(val>>8)&255; + diodeval[i+2]=(val)&255; } #ifdef VL_MOTORS // réglagle d'un moteur void set_motor_dir(int num_motor, int sens) { - motordir[num_motor?1:0]=(sens==0)?0:((sens==1)?1:-1); + motordir[num_motor?1:0]=(sens==0)?0:((sens==1)?1:-1); } int get_motor_val(int i) { - return motorcount[i?1:0]; + return motorcount[i?1:0]; } #else int get_motor_val(int i) { - return 128; + return 128; } #endif int get_button3() { - return getText(button3); + return getText(button3); } char buf_rfid[256]; char* get_rfid() { - getTextStr(buttonrfid,buf_rfid); - if (strlen(buf_rfid)) return buf_rfid; - return NULL; + getTextStr(buttonrfid,buf_rfid); + if (strlen(buf_rfid)) + { + return buf_rfid; + } + return NULL; } #endif diff --git a/src/simu/win/simuaudio.c b/src/simu/win/simuaudio.c index d9d330b..6a4848f 100644 --- a/src/simu/win/simuaudio.c +++ b/src/simu/win/simuaudio.c @@ -53,9 +53,9 @@ int bufplaystart; void mp3simuinit() { - InitMP3(&mp); - bufplayout_i=0; - bufplaystart=0; + InitMP3(&mp); + bufplayout_i=0; + bufplaystart=0; } char bufmp3[4608]; @@ -65,211 +65,281 @@ char bufmp3[4608]; void mp3scale(short *src,int len,short *dst,int freq,int lsf,int stereo,int dstlen) { - if (stereo==2) - { - if (len==dstlen) memcpy((char*)dst,(char*)src,len); - } - if (stereo==1) - { - int i; + if (stereo==2) + { + if (len==dstlen) + { + memcpy((char*)dst,(char*)src,len); + } + } + if (stereo==1) + { + int i; // int k=dstlen/len; - dstlen/=2; - len/=2; - for(i=0;i %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len); - dstlen=getmp3len(freqs[mp.fr.sampling_frequency]); - mp3scale((short*)bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen); - bufplayout_i+=dstlen; - } - else - { - len=audioPlayFetch(bufplayin+bufplaystart,BUFMP3IN_LENGTH-bufplaystart); - len+=bufplaystart; + dstlen=getmp3len(freqs[mp.fr.sampling_frequency]); + mp3scale((short*)bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen); + bufplayout_i+=dstlen; + } + else + { + len=audioPlayFetch(bufplayin+bufplaystart,BUFMP3IN_LENGTH-bufplaystart); + len+=bufplaystart; // printf("simuFetchWav get=%d\n",len); - if (len) - { - bufplaystart=0; - ret = decodeMP3(&mp,bufplayin,len,bufmp3,4608,&len); - if (ret==MP3_OK) - { + if (len) + { + bufplaystart=0; + ret = decodeMP3(&mp,bufplayin,len,bufmp3,4608,&len); + if (ret==MP3_OK) + { // printf("freq=%d lsf=%d stereo=%d -> %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len); - dstlen=getmp3len(freqs[mp.fr.sampling_frequency]); - mp3scale(bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen); - bufplayout_i+=dstlen; - } - } - else loop=0; - } - }; + dstlen=getmp3len(freqs[mp.fr.sampling_frequency]); + mp3scale(bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen); + bufplayout_i+=dstlen; + } + } + else + { + loop=0; + } + } + }; } void simuFetchWav(int ask) { - int len; - int loop=1; - int askwav=2+(((ask-bufplayout_i)/10)&(-2)); - - while((loop)&&(bufplaystart=askwav) len=askwav; - - wavscale(bufplayin,len,bufplayout+bufplayout_i); - bufplayout_i+=len*10; - if (bufplaystart!=len) - { - memcpy(bufplayin,bufplayin+len,bufplaystart-len); - bufplaystart-=len; - } - else bufplaystart=0; + int len; + int loop=1; + int askwav=2+(((ask-bufplayout_i)/10)&(-2)); + + while((loop)&&(bufplaystart=askwav) + { + len=askwav; + } + + wavscale(bufplayin,len,bufplayout+bufplayout_i); + bufplayout_i+=len*10; + if (bufplaystart!=len) + { + memcpy(bufplayin,bufplayin+len,bufplaystart-len); + bufplaystart-=len; + } + else + { + bufplaystart=0; + } } void simuFetchAdpcm(int ask) { - printf("simuFetchAdpcm %d\n",ask); - while(bufplayout_i=2)&&(bufplayin[0]=='I')&&(bufplayin[1]=='D')&&(bufplayin[2]=='3')) ||((bufplayin[0]&255)==255)) - { - int i=0; - int head=0; - PlayType=PLAYTYPE_MP3; - printf("detect MP3\n"); - PlayState=PLAYST_PLAY; - return; - -/* while((i20)&&(bufplayin[0]=='R')&&(bufplayin[1]=='I')&&(bufplayin[2]=='F')) - { - int i; - printf("detect wav bufplaystart=%d\n",bufplaystart); - PlayType=(bufplayin[20]==0x11)?PLAYTYPE_ADPCM:PLAYTYPE_WAV; - for(i=0;i=2)&&(bufplayin[0]=='I')&&(bufplayin[1]=='D')&&(bufplayin[2]=='3')) ||((bufplayin[0]&255)==255)) + { + int i=0; + int head=0; + PlayType=PLAYTYPE_MP3; + printf("detect MP3\n"); + PlayState=PLAYST_PLAY; + return; + + /* while((i20)&&(bufplayin[0]=='R')&&(bufplayin[1]=='I')&&(bufplayin[2]=='F')) + { + int i; + printf("detect wav bufplaystart=%d\n",bufplaystart); + PlayType=(bufplayin[20]==0x11)?PLAYTYPE_ADPCM:PLAYTYPE_WAV; + for(i=0; i0) memset(WaveOutBuf[i].lpData+k,0,len); + len=WaveOutBuf[i].dwDataSize-k; + if (len>0) + { + memset(WaveOutBuf[i].lpData+k,0,len); + } - return 0; + return 0; } int InFillBuffer(int i) { - char *p; + char *p; - if (WaveInBuf[i].lpData==NULL) return 0; + if (WaveInBuf[i].lpData==NULL) + { + return 0; + } - p=audioRecFeed_begin(256); - adpcmencode(WaveInBuf[i].lpData,p); -/* p=audioRecFeed_begin(WaveInBuf[i].dwDataSize); - memcpy(p,WaveInBuf[i].lpData,WaveInBuf[i].dwDataSize); -*/ audioRecFeed_end(); + p=audioRecFeed_begin(256); + adpcmencode(WaveInBuf[i].lpData,p); + /* p=audioRecFeed_begin(WaveInBuf[i].dwDataSize); + memcpy(p,WaveInBuf[i].lpData,WaveInBuf[i].dwDataSize); + */ audioRecFeed_end(); - return 0; + return 0; } void WaveOutFreeAll() { - int i; - for (i=0;ilpData = WaveOutBuf[i].lpData; - WaveOutBuf[i].lpWaveHdr->dwBufferLength = WaveOutBuf[i].dwDataSize; - WaveOutBuf[i].lpWaveHdr->dwUser = i; - WaveOutBuf[i].lpWaveHdr->dwFlags = 0L; - WaveOutBuf[i].lpWaveHdr->dwLoops = 1L; - wResult = waveOutPrepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR)); - if (wResult != 0) + for (i=0; ilpData = WaveOutBuf[i].lpData; + WaveOutBuf[i].lpWaveHdr->dwBufferLength = WaveOutBuf[i].dwDataSize; + WaveOutBuf[i].lpWaveHdr->dwUser = i; + WaveOutBuf[i].lpWaveHdr->dwFlags = 0L; + WaveOutBuf[i].lpWaveHdr->dwLoops = 1L; + wResult = waveOutPrepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR)); + if (wResult != 0) + { + wResult=waveOutClose(hWaveOut); + WaveOutFreeAll(); + return 0; + } + memset(WaveOutBuf[i].lpData,0,WaveOutBuf[i].dwDataSize); } - memset(WaveOutBuf[i].lpData,0,WaveOutBuf[i].dwDataSize); - } - for (i=0;i8) BitsPerSample=16; - pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM; - pcmWaveFormat.wf.nChannels=nChannels; - pcmWaveFormat.wf.nSamplesPerSec=rate; - pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8); - pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8; - pcmWaveFormat.wBitsPerSample=BitsPerSample; - - if (r=waveInOpen((LPHWAVEIN)&hWaveIn, WAVE_MAPPER, - (LPWAVEFORMATEX)&pcmWaveFormat, - (LONG)mainwin, 0L, CALLBACK_WINDOW)) - { -// MMechostr(MSKDEBUG,"Record: Unable to Open Device\n"); - switch (r) - { - case MMSYSERR_ALLOCATED : printf("Device allready allocated\n"); break; - case MMSYSERR_BADDEVICEID : printf("Bad device ID\n"); break; - case MMSYSERR_NODRIVER : printf("No driver\n"); break; - case WAVERR_BADFORMAT : printf("Bad Format\n"); break; - case MMSYSERR_NOMEM : printf("No memory\n"); break; - } - return 0; - } - - for (i=0;i8) { - WaveInFreeAll(); -// MMechostr(MSKDEBUG,"Record: Unable to allocate wave header\n"); - return 0; + BitsPerSample=16; } - - WaveInBuf[i].lpWaveHdr = (LPWAVEHDR) GlobalLock(WaveInBuf[i].hWaveHdr); - if (WaveInBuf[i].lpWaveHdr == NULL) + pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM; + pcmWaveFormat.wf.nChannels=nChannels; + pcmWaveFormat.wf.nSamplesPerSec=rate; + pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8); + pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8; + pcmWaveFormat.wBitsPerSample=BitsPerSample; + + if (r=waveInOpen((LPHWAVEIN)&hWaveIn, WAVE_MAPPER, + (LPWAVEFORMATEX)&pcmWaveFormat, + (LONG)mainwin, 0L, CALLBACK_WINDOW)) { - WaveInFreeAll(); -// MMechostr(MSKDEBUG,"Record: Unable to Lock wave header memory\n"); +// MMechostr(MSKDEBUG,"Record: Unable to Open Device\n"); + switch (r) + { + case MMSYSERR_ALLOCATED : + printf("Device allready allocated\n"); + break; + case MMSYSERR_BADDEVICEID : + printf("Bad device ID\n"); + break; + case MMSYSERR_NODRIVER : + printf("No driver\n"); + break; + case WAVERR_BADFORMAT : + printf("Bad Format\n"); + break; + case MMSYSERR_NOMEM : + printf("No memory\n"); + break; + } return 0; } - // After allocation, set up and prepare header. - - WaveInBuf[i].lpWaveHdr->lpData = WaveInBuf[i].lpData; - WaveInBuf[i].lpWaveHdr->dwBufferLength = WaveInBuf[i].dwDataSize; - WaveInBuf[i].lpWaveHdr->dwUser = i; - WaveInBuf[i].lpWaveHdr->dwFlags = 0L; - WaveInBuf[i].lpWaveHdr->dwLoops = 0L; - wResult=waveInPrepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR)); - if (wResult != 0) + for (i=0; ilpData = WaveInBuf[i].lpData; + WaveInBuf[i].lpWaveHdr->dwBufferLength = WaveInBuf[i].dwDataSize; + WaveInBuf[i].lpWaveHdr->dwUser = i; + WaveInBuf[i].lpWaveHdr->dwFlags = 0L; + WaveInBuf[i].lpWaveHdr->dwLoops = 0L; + wResult=waveInPrepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR)); + if (wResult != 0) + { + char buf[MAXERRORLENGTH]; + WaveInFreeAll(); // MMechostr(MSKDEBUG,"PrepareRecBuf: Unable to write block to the device\n"); - waveOutGetErrorText(wResult,buf,MAXERRORLENGTH); + waveOutGetErrorText(wResult,buf,MAXERRORLENGTH); // MMechostr(MSKDEBUG,buf); // MMechostr(MSKDEBUG,"\n"); - return 0; + return 0; + } + } - } - - for (i=0;idwUser); - return 0; + PlayData(((LPWAVEHDR)lParam)->dwUser); + return 0; } int audioEventRecData(int eventId, int wParam, int lParam) { // printf("###rec cb\n"); - RecData(((LPWAVEHDR)lParam)->dwUser); - return 0; + RecData(((LPWAVEHDR)lParam)->dwUser); + return 0; } void audioSetVolume(int vol) { - vol&=255; - waveOutSetVolume(hWaveOut,(vol<<24)+(vol<<8)); + vol&=255; + waveOutSetVolume(hWaveOut,(vol<<24)+(vol<<8)); } int simuaudioinit() { - nPlayBuffers=0; - PlayState=PLAYST_IDLE; - - nRecBuffers=0; - SendInBuffers=0; - StopRecord=FALSE; - RecordInitialised=FALSE; - - PlayInit(44100,10000 //22050 - ,8); - - return 0; + nPlayBuffers=0; + PlayState=PLAYST_IDLE; + + nRecBuffers=0; + SendInBuffers=0; + StopRecord=FALSE; + RecordInitialised=FALSE; + + PlayInit(44100,10000 //22050 + ,8); + + return 0; } diff --git a/src/simu/win/simunet.c b/src/simu/win/simunet.c index 50e9667..48c5189 100644 --- a/src/simu/win/simunet.c +++ b/src/simu/win/simunet.c @@ -22,379 +22,468 @@ int udp_port[UDPMAX]; // port=0 -> disponible int inet_addr_bin(char *ip) { - return *(int*)ip; + return *(int*)ip; } int simunetinit() { - int i; - WORD wVersionRequested; - WSADATA wsaData; - - wVersionRequested = MAKEWORD( 1, 1 ); - if (WSAStartup( wVersionRequested, &wsaData )) return -1; - for(i=0;i=0)&&(i=0)&&(i=0)&&(i=0)&&(i=0)&&(i=0)&&(i0) - { - VPUSH(INTTOVAL(1)); - VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); - } - else - { - VPUSH(INTTOVAL(-1)); - VPUSH(NIL); - } - VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; - } - return 0; + return 1; + } + + printf("Sockets : Read event on %d\n",sock); + res=recv(sock,buf,1024,0); + + VPUSH(INTTOVAL(i)); + if (res>0) + { + VPUSH(INTTOVAL(1)); + VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); + } + else + { + VPUSH(INTTOVAL(-1)); + VPUSH(NIL); + } + VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; + } + return 0; } int udpbyport(int p) { - int i; - for(i=0;i=0) - { - closesocket(udp_sock[i]); - udp_port[i]=0; - } - return i; + int i=udpbyport(port); + if (i>=0) + { + closesocket(udp_sock[i]); + udp_port[i]=0; + } + return i; } int udpsend(int localport,char* dstip,int dstport,char* msg, int len) { - SOCKADDR_IN ina; - int i; - - i=udpbyport(localport); - if (i<0) i=udpcreate(localport); - if (i>=0) - { - ina.sin_family = PF_INET; - ina.sin_port = htons((unsigned short)dstport); - ina.sin_addr.s_addr = inet_addr_bin(dstip); - printf("send %d bytes on chn %d\n",len,i); - sendto(udp_sock[i],msg,len,0,(struct sockaddr *)&ina,sizeof(ina)); - return 0; - } - return -1; + SOCKADDR_IN ina; + int i; + + i=udpbyport(localport); + if (i<0) + { + i=udpcreate(localport); + } + if (i>=0) + { + ina.sin_family = PF_INET; + ina.sin_port = htons((unsigned short)dstport); + ina.sin_addr.s_addr = inet_addr_bin(dstip); + printf("send %d bytes on chn %d\n",len,i); + sendto(udp_sock[i],msg,len,0,(struct sockaddr *)&ina,sizeof(ina)); + return 0; + } + return -1; } int udpevent(int eventId, int wParam, int lParam) { - int NetEvent,NetError; - SOCKET sock=(SOCKET)wParam; - int i=udpbysock(sock); - printf("Sockets : UDP event\n"); - - if (i<0) return 0; - - NetEvent=WSAGETSELECTEVENT(lParam); - NetError=WSAGETSELECTERROR(lParam); - if (NetError) - { - return 1; - } - if (NetEvent==FD_READ) - { - char buf[4096]; - char *ip; - SOCKADDR_IN add; - int l=sizeof(add); - int res=recvfrom(sock,buf,4096,0,(struct sockaddr *)&add,&l); - if (res<0) return 1; - - ip=inet_ntoa(add.sin_addr); - printf("Sockets : UDP Read %d bytes on :%d from %s:%d\n",res,udp_port[i],ip,ntohs(add.sin_port)); - VPUSH(INTTOVAL(i)); - VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); - VPUSH(PNTTOVAL(VMALLOCSTR((char*)&add.sin_addr.s_addr,4))); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBUDP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();VPULL();} - VPULL(); - return 1; - } - return 0; + int NetEvent,NetError; + SOCKET sock=(SOCKET)wParam; + int i=udpbysock(sock); + printf("Sockets : UDP event\n"); + + if (i<0) + { + return 0; + } + + NetEvent=WSAGETSELECTEVENT(lParam); + NetError=WSAGETSELECTERROR(lParam); + if (NetError) + { + return 1; + } + if (NetEvent==FD_READ) + { + char buf[4096]; + char *ip; + SOCKADDR_IN add; + int l=sizeof(add); + int res=recvfrom(sock,buf,4096,0,(struct sockaddr *)&add,&l); + if (res<0) + { + return 1; + } + + ip=inet_ntoa(add.sin_addr); + printf("Sockets : UDP Read %d bytes on :%d from %s:%d\n",res,udp_port[i],ip,ntohs(add.sin_port)); + VPUSH(INTTOVAL(i)); + VPUSH(PNTTOVAL(VMALLOCSTR(buf,res))); + VPUSH(PNTTOVAL(VMALLOCSTR((char*)&add.sin_addr.s_addr,4))); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBUDP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + VPULL(); + } + VPULL(); + return 1; + } + return 0; } diff --git a/src/vcomp/bootstrap.cpp b/src/vcomp/bootstrap.cpp index 332f168..3881172 100644 --- a/src/vcomp/bootstrap.cpp +++ b/src/vcomp/bootstrap.cpp @@ -33,58 +33,63 @@ void AbortMetal(Memory* m,int donotstop) { - if (m->abort) return; - PRINTF(m)(LOG_RUNTIME,"\n---------- end of execution\n"); - m->stop(); + if (m->abort) + { + return; + } + PRINTF(m)(LOG_RUNTIME,"\n---------- end of execution\n"); + m->stop(); } int StartMetal(const char *starter, const char* output, bool inSign) { - int k; + int k; // passe dans le répertoire de l'executable, en retenant le répertoire courant - Terminal* t=new Terminal(); - Memory* m=new Memory(32*1024,t,NULL); - t->m=m; - - if (k=m->start()) - { - t->printf(LOG_RUNTIME,"Launcher : erreur d'initialisation Memory\n"); - return -1; - } - STRPUSH(m, (char*) starter); - if (!(k=m->util->compiler->gocompile(COMPILE_FROMFILE))) - { - t->printf(LOG_RUNTIME,"\nCompiler : done !\n"); - File* f=new File(NULL); - f->openwrite((char*) output); - if (inSign) { - f->write("amber", 5); - char sizeStr[9]; - sprintf(sizeStr, "%.8x", m->util->compiler->brelease->getsize()); - f->write(sizeStr, 8); - } - f->write(m->util->compiler->brelease->getstart(),m->util->compiler->brelease->getsize()); - if (inSign) { - f->write("Mind", 4); - } - f->close(); - return 0; - } - else - { - t->printf(LOG_RUNTIME,"Launcher : %s\n\n",m->errorname(k)); - return -1; - } + Terminal* t=new Terminal(); + Memory* m=new Memory(32*1024,t,NULL); + t->m=m; + + if (k=m->start()) + { + t->printf(LOG_RUNTIME,"Launcher : erreur d'initialisation Memory\n"); + return -1; + } + STRPUSH(m, (char*) starter); + if (!(k=m->util->compiler->gocompile(COMPILE_FROMFILE))) + { + t->printf(LOG_RUNTIME,"\nCompiler : done !\n"); + File* f=new File(NULL); + f->openwrite((char*) output); + if (inSign) + { + f->write("amber", 5); + char sizeStr[9]; + sprintf(sizeStr, "%.8x", m->util->compiler->brelease->getsize()); + f->write(sizeStr, 8); + } + f->write(m->util->compiler->brelease->getstart(),m->util->compiler->brelease->getsize()); + if (inSign) + { + f->write("Mind", 4); + } + f->close(); + return 0; + } + else + { + t->printf(LOG_RUNTIME,"Launcher : %s\n\n",m->errorname(k)); + return -1; + } } int vcompDoit(char *starter) { - int k=StartMetal(starter, "foo.bin", false); - getchar(); - return k; + int k=StartMetal(starter, "foo.bin", false); + getchar(); + return k; } diff --git a/src/vcomp/compiler.cpp b/src/vcomp/compiler.cpp index 177002b..698d7ab 100644 --- a/src/vcomp/compiler.cpp +++ b/src/vcomp/compiler.cpp @@ -19,47 +19,80 @@ Compiler::Compiler(Memory* mem) { - m=mem; - bc=new Prodbuffer(); - outputbuf=new Prodbuffer(); - brelease=new Prodbuffer(); + m=mem; + bc=new Prodbuffer(); + outputbuf=new Prodbuffer(); + brelease=new Prodbuffer(); }; Compiler::~Compiler() { - delete bc; - delete outputbuf; - delete brelease; + delete bc; + delete outputbuf; + delete brelease; }; int Compiler::start() { - int k; - - // création du package systeme - if (k=createpackage("system",8)) return k; - systempackage=STACKGET(m,0); - m->addroot(VALTOPNT(systempackage)); // le package systeme devient une racine - if (k=addstdlibcore()) return k; - int pck=STACKREF(m); - - // création du tableau des types utilisés par le compilateur - if (k=creategraph("I",systempackage,0)) return k; - if (k=creategraph("F",systempackage,0)) return k; - if (k=creategraph("S",systempackage,0)) return k; - if (k=creategraph("Env",systempackage,0)) return k; - if (k=creategraph("Xml",systempackage,0)) return k; - - if (k=creategraph("fun[u0 list u0]list u0",systempackage,0)) return k; - if (k=creategraph("fun[tab u0 I]u0",systempackage,0)) return k; - if (k=creategraph("fun[fun u0 u1 u0]u1",systempackage,0)) return k; - if (k=DEFTAB(m,STDTYPE_LENGTH)) return k; - stdtypes=VALTOPNT(STACKPULL(m)); - m->addroot(stdtypes); + int k; + + // création du package systeme + if (k=createpackage("system",8)) + { + return k; + } + systempackage=STACKGET(m,0); + m->addroot(VALTOPNT(systempackage)); // le package systeme devient une racine + if (k=addstdlibcore()) + { + return k; + } + int pck=STACKREF(m); + + // création du tableau des types utilisés par le compilateur + if (k=creategraph("I",systempackage,0)) + { + return k; + } + if (k=creategraph("F",systempackage,0)) + { + return k; + } + if (k=creategraph("S",systempackage,0)) + { + return k; + } + if (k=creategraph("Env",systempackage,0)) + { + return k; + } + if (k=creategraph("Xml",systempackage,0)) + { + return k; + } + + if (k=creategraph("fun[u0 list u0]list u0",systempackage,0)) + { + return k; + } + if (k=creategraph("fun[tab u0 I]u0",systempackage,0)) + { + return k; + } + if (k=creategraph("fun[fun u0 u1 u0]u1",systempackage,0)) + { + return k; + } + if (k=DEFTAB(m,STDTYPE_LENGTH)) + { + return k; + } + stdtypes=VALTOPNT(STACKPULL(m)); + m->addroot(stdtypes); // dumppackage(systempackage); - return 0; + return 0; } void Compiler::stop() @@ -68,402 +101,487 @@ void Compiler::stop() int Compiler::getsystempackage() { - return systempackage; + return systempackage; } // crée et empile un package intptr_t Compiler::createpackage(const char* name,int loghach) { - intptr_t k; - if (k=PUSHMALLOCCLEAR(m,PACK_LENGTH)) return k; - intptr_t* p=VALTOPNT(STACKGET(m,0)); + intptr_t k; + if (k=PUSHMALLOCCLEAR(m,PACK_LENGTH)) + { + return k; + } + intptr_t* p=VALTOPNT(STACKGET(m,0)); - if (k=STRPUSH(m,name)) return k; - TABSET(m,p,PACK_NAME,STACKPULL(m)); + if (k=STRPUSH(m,name)) + { + return k; + } + TABSET(m,p,PACK_NAME,STACKPULL(m)); - if (k=PUSHMALLOCCLEAR(m,1+(1<>8); - i=(i+1)&7; - } - return v&255; + int v=0; + int i=0; + while(*name) + { + int c=(*name++)&255; + c=c<>8); + i=(i+1)&7; + } + return v&255; } // ajoute une référence à un package // [ref package] -> [package] void Compiler::addreftopackage(intptr_t* ref,intptr_t* package) { - int v; - if (TABGET(ref,REF_NAME)!=NIL) v=hash(STRSTART(VALTOPNT(TABGET(ref,REF_NAME)))); - else v=hash(STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1)))); + int v; + if (TABGET(ref,REF_NAME)!=NIL) + { + v=hash(STRSTART(VALTOPNT(TABGET(ref,REF_NAME)))); + } + else + { + v=hash(STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1)))); + } - intptr_t* p=VALTOPNT(TABGET(package,PACK_HACH)); + intptr_t* p=VALTOPNT(TABGET(package,PACK_HACH)); // TABSET(m,ref,REF_PACKAGE,PNTTOVAL(package)); - v&=TABLEN(p)-2; - TABSET(m,ref,REF_NEXT,TABGET(p,v)); - TABSET(m,p,v,PNTTOVAL(ref)); - v=TABLEN(p)-1; - TABSET(m,ref,REF_NEXTALL,TABGET(p,v)); - TABSET(m,p,v,PNTTOVAL(ref)); + v&=TABLEN(p)-2; + TABSET(m,ref,REF_NEXT,TABGET(p,v)); + TABSET(m,p,v,PNTTOVAL(ref)); + v=TABLEN(p)-1; + TABSET(m,ref,REF_NEXTALL,TABGET(p,v)); + TABSET(m,p,v,PNTTOVAL(ref)); } // recherche d'un type dans un environnement intptr_t* Compiler::searchtype(int env,const char* name) { - intptr_t v=hash(name); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((TABGET(ref,REF_NAME)==NIL) - &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) ) - return ref; - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NULL; + intptr_t v=hash(name); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((TABGET(ref,REF_NAME)==NIL) + &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) ) + { + return ref; + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NULL; } // recherche d'un type non défini dans un environnement intptr_t* Compiler::searchemptytype(int env,const char* name) { - intptr_t v=hash(name); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((TABGET(ref,REF_CODE)==INTTOVAL(CODE_EMPTYTYPE)) - &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) ) - return ref; - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NULL; + intptr_t v=hash(name); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((TABGET(ref,REF_CODE)==INTTOVAL(CODE_EMPTYTYPE)) + &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) ) + { + return ref; + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NULL; } void Compiler::dumppackage(int env) { - while(env!=NIL) - { - PRINTF(m)(LOG_DEVCORE,"package %s",STRSTART(VALTOPNT(TABGET(VALTOPNT(env),PACK_NAME)))); - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,TABLEN(p)-1); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - char* name=NULL; - if (TABGET(ref,REF_NAME)!=NIL) name=STRSTART(VALTOPNT(TABGET(ref,REF_NAME))); - if (!name) name=STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))); - if (name) PRINTF(m)(LOG_DEVCORE," %s",name); - vref=TABGET(ref,REF_NEXTALL); - } - PRINTF(m)(LOG_DEVCORE,"\n"); - env=TABGET(VALTOPNT(env),PACK_NEXT); - } + while(env!=NIL) + { + PRINTF(m)(LOG_DEVCORE,"package %s",STRSTART(VALTOPNT(TABGET(VALTOPNT(env),PACK_NAME)))); + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,TABLEN(p)-1); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + char* name=NULL; + if (TABGET(ref,REF_NAME)!=NIL) + { + name=STRSTART(VALTOPNT(TABGET(ref,REF_NAME))); + } + if (!name) + { + name=STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))); + } + if (name) + { + PRINTF(m)(LOG_DEVCORE," %s",name); + } + vref=TABGET(ref,REF_NEXTALL); + } + PRINTF(m)(LOG_DEVCORE,"\n"); + env=TABGET(VALTOPNT(env),PACK_NEXT); + } } intptr_t Compiler::searchbytype(int env,int type) { - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,TABLEN(p)-1); - while(vref!=NIL) - { - if (TABGET(VALTOPNT(vref),REF_TYPE)==type) return vref; - vref=TABGET(VALTOPNT(vref),REF_NEXTALL); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NIL; + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,TABLEN(p)-1); + while(vref!=NIL) + { + if (TABGET(VALTOPNT(vref),REF_TYPE)==type) + { + return vref; + } + vref=TABGET(VALTOPNT(vref),REF_NEXTALL); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NIL; } intptr_t Compiler::fillproto(int env,intptr_t* fun) { - intptr_t k; - intptr_t v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME)))); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions - { - if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) - &&(TABGET(ref,REF_VAL)==NIL) ) - { - if (k=unifbigger(VALTOPNT(TABGET(fun,REF_TYPE)),VALTOPNT(TABGET(ref,REF_TYPE)))) - { - PRINTF(m)(LOG_COMPILER,"Compiler : prototype does not match\n"); - return k; - } - TABSET(m,ref,REF_VAL,TABGET(fun,REF_VAL)); + intptr_t k; + intptr_t v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME)))); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions + { + if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) + &&(TABGET(ref,REF_VAL)==NIL) ) + { + if (k=unifbigger(VALTOPNT(TABGET(fun,REF_TYPE)),VALTOPNT(TABGET(ref,REF_TYPE)))) + { + PRINTF(m)(LOG_COMPILER,"Compiler : prototype does not match\n"); + return k; + } + TABSET(m,ref,REF_VAL,TABGET(fun,REF_VAL)); // TABSET(m,ref,REF_PACKAGE,TABGET(fun,REF_PACKAGE)); - } - } - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return 0; + } + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return 0; } intptr_t Compiler::findproto(int env,intptr_t* fun) { - intptr_t v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME)))); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions - { - if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) - &&(TABGET(ref,REF_VAL)==NIL) ) - { - return TABGET(ref,REF_PACKAGE); - } - } - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NIL; + intptr_t v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME)))); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions + { + if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) + &&(TABGET(ref,REF_VAL)==NIL) ) + { + return TABGET(ref,REF_PACKAGE); + } + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NIL; } // recherche d'une référence dans un environnement intptr_t* Compiler::searchref(int env,char* name) { - intptr_t v=hash(name); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((TABGET(ref,REF_NAME)!=NIL) - &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) ) - { - // à partir du moment où on recherche cette référence, c'est - // qu'elle est utlisé quelque part, donc... - TABSET(m,ref,REF_USED,INTTOVAL(1)); - - return ref; - } - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NULL; + intptr_t v=hash(name); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((TABGET(ref,REF_NAME)!=NIL) + &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) ) + { + // à partir du moment où on recherche cette référence, c'est + // qu'elle est utlisé quelque part, donc... + TABSET(m,ref,REF_USED,INTTOVAL(1)); + + return ref; + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NULL; } // recherche d'une référence dans un environnement, ne marque pas la référence comme utilisée intptr_t* Compiler::searchref_nosetused(int env,char* name) { - intptr_t v=hash(name); - while(env!=NIL) - { - intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); - intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); - while(vref!=NIL) - { - intptr_t* ref=VALTOPNT(vref); - if ((TABGET(ref,REF_NAME)!=NIL) - &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) ) - return ref; - vref=TABGET(ref,REF_NEXT); - } - env=TABGET(VALTOPNT(env),PACK_NEXT); - } - return NULL; + intptr_t v=hash(name); + while(env!=NIL) + { + intptr_t* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH)); + intptr_t vref=TABGET(p,v&(TABLEN(p)-2)); + while(vref!=NIL) + { + intptr_t* ref=VALTOPNT(vref); + if ((TABGET(ref,REF_NAME)!=NIL) + &&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) ) + { + return ref; + } + vref=TABGET(ref,REF_NEXT); + } + env=TABGET(VALTOPNT(env),PACK_NEXT); + } + return NULL; } // ajout d'un label dans une liste de labels intptr_t Compiler::addlabel(intptr_t base,const char* name,int val,intptr_t ref) { - intptr_t k; - intptr_t* p=MALLOCCLEAR(m,LABELLIST_LENGTH); - if (!p) return MTLERR_OM; - TABSET(m,p,LABELLIST_NEXT,STACKGETFROMREF(m,base,0)); - STACKSETFROMREF(m,base,0,PNTTOVAL(p)); - - if (k=STRPUSH(m,name)) return k; - TABSET(m,p,LABELLIST_NAME,STACKPULL(m)); - TABSET(m,p,LABELLIST_VAL,val); - TABSET(m,p,LABELLIST_REF,ref); - return 0; + intptr_t k; + intptr_t* p=MALLOCCLEAR(m,LABELLIST_LENGTH); + if (!p) + { + return MTLERR_OM; + } + TABSET(m,p,LABELLIST_NEXT,STACKGETFROMREF(m,base,0)); + STACKSETFROMREF(m,base,0,PNTTOVAL(p)); + + if (k=STRPUSH(m,name)) + { + return k; + } + TABSET(m,p,LABELLIST_NAME,STACKPULL(m)); + TABSET(m,p,LABELLIST_VAL,val); + TABSET(m,p,LABELLIST_REF,ref); + return 0; } // compte le nombre de labels int Compiler::nblabels(intptr_t base) { - int n=0; - intptr_t vlab=STACKGETFROMREF(m,base,0); - while(vlab!=NIL) - { - n++; - vlab=TABGET(VALTOPNT(vlab),LABELLIST_NEXT); - } - return n; + int n=0; + intptr_t vlab=STACKGETFROMREF(m,base,0); + while(vlab!=NIL) + { + n++; + vlab=TABGET(VALTOPNT(vlab),LABELLIST_NEXT); + } + return n; } void Compiler::removenlabels(intptr_t base,int n) { - while(n>0) - { - STACKSETFROMREF(m,base,0,TABGET(VALTOPNT(STACKGETFROMREF(m,base,0)),LABELLIST_NEXT)); - n--; - } + while(n>0) + { + STACKSETFROMREF(m,base,0,TABGET(VALTOPNT(STACKGETFROMREF(m,base,0)),LABELLIST_NEXT)); + n--; + } } // recherche d'un label dans une liste de labels int Compiler::searchlabel_byname(intptr_t base,const char* name,int* val,intptr_t* ref) { - intptr_t vlab=STACKGETFROMREF(m,base,0); - while(vlab!=NIL) - { - intptr_t* lab=VALTOPNT(vlab); - if (!strcmp(name,STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME)))) ) - { - if (val) *val=TABGET(lab,LABELLIST_VAL); - if (ref) *ref=TABGET(lab,LABELLIST_REF); - return 0; - } - vlab=TABGET(lab,LABELLIST_NEXT); - } - return -1; + intptr_t vlab=STACKGETFROMREF(m,base,0); + while(vlab!=NIL) + { + intptr_t* lab=VALTOPNT(vlab); + if (!strcmp(name,STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME)))) ) + { + if (val) + { + *val=TABGET(lab,LABELLIST_VAL); + } + if (ref) + { + *ref=TABGET(lab,LABELLIST_REF); + } + return 0; + } + vlab=TABGET(lab,LABELLIST_NEXT); + } + return -1; } // recherche d'un label dans une liste de labels int Compiler::searchlabel_byval(intptr_t base,int val, char** name) { - intptr_t vlab=STACKGETFROMREF(m,base,0); - while(vlab!=NIL) - { - intptr_t* lab=VALTOPNT(vlab); - if (val==TABGET(lab,LABELLIST_VAL)) - { - *name=STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME))); - return 0; - } - vlab=TABGET(lab,LABELLIST_NEXT); - } - return -1; + intptr_t vlab=STACKGETFROMREF(m,base,0); + while(vlab!=NIL) + { + intptr_t* lab=VALTOPNT(vlab); + if (val==TABGET(lab,LABELLIST_VAL)) + { + *name=STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME))); + return 0; + } + vlab=TABGET(lab,LABELLIST_NEXT); + } + return -1; } // création d'un tuple à partir d'une liste de labels intptr_t* Compiler::tuplefromlabels(intptr_t base) { - int n=nblabels(base); - intptr_t* t=MALLOC(m,n,TYPE_TAB); - if (!t) return t; - - intptr_t vlab=STACKGETFROMREF(m,base,0); - int i; for(i=n-1;i>=0;i--) - { - intptr_t* lab=VALTOPNT(vlab); - TABSET(m,t,i,TABGET(lab,LABELLIST_REF)); - vlab=TABGET(lab,LABELLIST_NEXT); - } - return t; + int n=nblabels(base); + intptr_t* t=MALLOC(m,n,TYPE_TAB); + if (!t) + { + return t; + } + + intptr_t vlab=STACKGETFROMREF(m,base,0); + int i; + for(i=n-1; i>=0; i--) + { + intptr_t* lab=VALTOPNT(vlab); + TABSET(m,t,i,TABGET(lab,LABELLIST_REF)); + vlab=TABGET(lab,LABELLIST_NEXT); + } + return t; } // ajoute des fonctions à un package intptr_t Compiler::addnative(int nref, const char** nameref, int* valref - , int* coderef, const char** typeref,void* arg) + , int* coderef, const char** typeref,void* arg) { - int i,k; + int i,k; - for(i=0;i=0) - { - if (k=creategraph(typeref[i],systempackage,0)) return k; - TABSET(m,p,REF_TYPE,STACKPULL(m)); + intptr_t* p=MALLOCCLEAR(m,REF_LENGTH); + if (!p) + { + return MTLERR_OM; + } + if (k=STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } + + if (nameref[i]) + { + if (k=STRPUSH(m,nameref[i])) + { + return k; + } + TABSET(m,p,REF_NAME,STACKPULL(m)); + } + + TABSET(m,p,REF_CODE,INTTOVAL(coderef[i])); + + if (coderef[i]>=0) + { + if (k=creategraph(typeref[i],systempackage,0)) + { + return k; + } + TABSET(m,p,REF_TYPE,STACKPULL(m)); // if (k=PUSHPNT(m,(int*)valref[i])) return k; - if (k=STACKPUSH(m,INTTOVAL(valref[i]))) return k; - - intptr_t* fun=MALLOCCLEAR(m,FUN_LENGTH); - if (!fun) return MTLERR_OM; - TABSET(m,p,REF_VAL,PNTTOVAL(fun)); - - TABSET(m,fun,FUN_NBARGS,INTTOVAL(coderef[i])); - TABSET(m,fun,FUN_NBLOCALS,NIL); + if (k=STACKPUSH(m,INTTOVAL(valref[i]))) + { + return k; + } + + intptr_t* fun=MALLOCCLEAR(m,FUN_LENGTH); + if (!fun) + { + return MTLERR_OM; + } + TABSET(m,p,REF_VAL,PNTTOVAL(fun)); + + TABSET(m,fun,FUN_NBARGS,INTTOVAL(coderef[i])); + TABSET(m,fun,FUN_NBLOCALS,NIL); // PRINTF(m)(1,"valref %s = %x\n",nameref[i],valref[i]); // TABSET(m,fun,FUN_BC,((int)valref[i])<<1); - TABSET(m,fun,FUN_BC,STACKPULL(m)); - TABSET(m,fun,FUN_REF,*((int*)arg)); - TABSET(m,fun,FUN_REFERENCE,PNTTOVAL(p)); - } - else if ((coderef[i]==CODE_VAR) - ||(coderef[i]==CODE_CONS)||(coderef[i]==CODE_CONS0)) - { - if (k=creategraph(typeref[i],systempackage,0)) return k; - TABSET(m,p,REF_TYPE,STACKPULL(m)); - TABSET(m,p,REF_VAL,(int)valref[i]); - } - else if (coderef[i]==CODE_FIELD) - { - if (k=creategraph(typeref[i],systempackage,0)) return k; - if (k=STACKPUSH(m,(int)valref[i])) return k; - int vtype=searchbytype(systempackage, - TABGET(argsfromfun(VALTOPNT(STACKGET(m,1))),TYPEHEADER_LENGTH)); - if (k=STACKPUSH(m,vtype)) return k; - if (k=DEFTAB(m,2)) return k; - TABSET(m,p,REF_VAL,STACKPULL(m)); - TABSET(m,p,REF_TYPE,STACKPULL(m)); - } - else if ((coderef[i]==CODE_TYPE)||(coderef[i]==CODE_SUM)||(coderef[i]==CODE_STRUCT)) - { - if (k=createnodetypecore(typeref[i])) return k; - TABSET(m,p,REF_TYPE,STACKPULL(m)); - TABSET(m,p,REF_VAL,(int)valref[i]); - } - addreftopackage(p,VALTOPNT(systempackage)); - STACKDROP(m); - } - return 0; + TABSET(m,fun,FUN_BC,STACKPULL(m)); + TABSET(m,fun,FUN_REF,*((int*)arg)); + TABSET(m,fun,FUN_REFERENCE,PNTTOVAL(p)); + } + else if ((coderef[i]==CODE_VAR) + ||(coderef[i]==CODE_CONS)||(coderef[i]==CODE_CONS0)) + { + if (k=creategraph(typeref[i],systempackage,0)) + { + return k; + } + TABSET(m,p,REF_TYPE,STACKPULL(m)); + TABSET(m,p,REF_VAL,(int)valref[i]); + } + else if (coderef[i]==CODE_FIELD) + { + if (k=creategraph(typeref[i],systempackage,0)) + { + return k; + } + if (k=STACKPUSH(m,(int)valref[i])) + { + return k; + } + int vtype=searchbytype(systempackage, + TABGET(argsfromfun(VALTOPNT(STACKGET(m,1))),TYPEHEADER_LENGTH)); + if (k=STACKPUSH(m,vtype)) + { + return k; + } + if (k=DEFTAB(m,2)) + { + return k; + } + TABSET(m,p,REF_VAL,STACKPULL(m)); + TABSET(m,p,REF_TYPE,STACKPULL(m)); + } + else if ((coderef[i]==CODE_TYPE)||(coderef[i]==CODE_SUM)||(coderef[i]==CODE_STRUCT)) + { + if (k=createnodetypecore(typeref[i])) + { + return k; + } + TABSET(m,p,REF_TYPE,STACKPULL(m)); + TABSET(m,p,REF_VAL,(int)valref[i]); + } + addreftopackage(p,VALTOPNT(systempackage)); + STACKDROP(m); + } + return 0; } diff --git a/src/vcomp/compiler_file.cpp b/src/vcomp/compiler_file.cpp index 2957ddb..1a31059 100644 --- a/src/vcomp/compiler_file.cpp +++ b/src/vcomp/compiler_file.cpp @@ -20,33 +20,36 @@ int Compiler::recglobal(int val,Prodbuffer *b) { - if (val==NIL) - { - b->addint(-1); + if (val==NIL) + { + b->addint(-1); // printf("nil\n"); - } - else if (ISVALPNT(val)) - { - intptr_t* p=VALTOPNT(val); - if (HEADER_TYPE(p)==TYPE_TAB) - { - b->addint((TABLEN(p)<<2)+3); + } + else if (ISVALPNT(val)) + { + intptr_t* p=VALTOPNT(val); + if (HEADER_TYPE(p)==TYPE_TAB) + { + b->addint((TABLEN(p)<<2)+3); // printf("tuple %d @ %x : %x\n",TABLEN(p),val,*p); - for(int i=0;iaddint((STRLEN(p)<<2)+1); - b->addstr(STRSTART(p),STRLEN(p)); + for(int i=0; iaddint((STRLEN(p)<<2)+1); + b->addstr(STRSTART(p),STRLEN(p)); // printf("'%s'\n",STRSTART(p)); - } - } - else - { - b->addint(val); + } + } + else + { + b->addint(val); // printf("%d\n",VALTOINT(val)); - } - return 0; + } + return 0; } @@ -56,63 +59,78 @@ int Compiler::recglobal(int val,Prodbuffer *b) */ int Compiler::recglobals(int vlab,Prodbuffer *b) { - if (vlab==NIL) return -1; + if (vlab==NIL) + { + return -1; + } - int n=1+recglobals(TABGET(VALTOPNT(vlab),LABELLIST_NEXT),b); - int val=TABGET(VALTOPNT(vlab),LABELLIST_REF); + int n=1+recglobals(TABGET(VALTOPNT(vlab),LABELLIST_NEXT),b); + int val=TABGET(VALTOPNT(vlab),LABELLIST_REF); // printf("%d:%s ",n,STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); - // avant de l'enregistrer, on verifie si tout est correct - intptr_t* p; - p = searchref_nosetused(PNTTOVAL(newpackage),STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); - if (NULL != p) - { - if (VALTOINT(TABGET(p,REF_USED)) == 0) - { - PRINTF(m)(LOG_WARNING,"%s is declared but never used\n", - STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); - } - else if (VALTOINT(TABGET(p,REF_CODE)) == CODE_VAR) - { - if (VALTOINT(TABGET(p,REF_SET)) == 0) - { - if (VALTOINT(TABGET(p,REF_USED)) > VALTOINT(TABGET(p,REF_USED_IN_IFDEF))) - PRINTF(m)(LOG_WARNING,"%s never gets a value\n", - STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); - } - else if (VALTOINT(TABGET(p,REF_SET)) == 1) - PRINTF(m)(LOG_WARNING,"%s is only set once, at declaration. It should be a const.\n", - STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); - } - } - - recglobal(val,b); - return n; + // avant de l'enregistrer, on verifie si tout est correct + intptr_t* p; + p = searchref_nosetused(PNTTOVAL(newpackage),STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); + if (NULL != p) + { + if (VALTOINT(TABGET(p,REF_USED)) == 0) + { + PRINTF(m)(LOG_WARNING,"%s is declared but never used\n", + STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); + } + else if (VALTOINT(TABGET(p,REF_CODE)) == CODE_VAR) + { + if (VALTOINT(TABGET(p,REF_SET)) == 0) + { + if (VALTOINT(TABGET(p,REF_USED)) > VALTOINT(TABGET(p,REF_USED_IN_IFDEF))) + PRINTF(m)(LOG_WARNING,"%s never gets a value\n", + STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); + } + else if (VALTOINT(TABGET(p,REF_SET)) == 1) + PRINTF(m)(LOG_WARNING,"%s is only set once, at declaration. It should be a const.\n", + STRSTART(VALTOPNT(TABGET(VALTOPNT(vlab),LABELLIST_NAME)))); + } + } + + recglobal(val,b); + return n; } int Compiler::recbc(int vref,Prodbuffer *b,Prodbuffer *btab,int offset) { - if (vref==NIL) return -1; - int f=recbc(TABGET(VALTOPNT(vref),REF_NEXTALL),b,btab,offset); - if (f<-1) return f; - if (VALTOINT(TABGET(VALTOPNT(vref),REF_CODE))<0) return f; - int nf=VALTOINT(TABGET(VALTOPNT(vref),REF_PACKAGE)); - if (nf<=f) return f; - if (TABGET(VALTOPNT(vref),REF_VAL)==NIL) - { - printf("%s is EMPTY !!!\n",STRSTART(VALTOPNT(TABGET(VALTOPNT(vref),REF_NAME)))); - return -2; - } - intptr_t *fun=VALTOPNT(TABGET(VALTOPNT(vref),REF_VAL)); - int nloc=VALTOINT(TABGET(fun,FUN_NBLOCALS))-VALTOINT(TABGET(fun,FUN_NBARGS)); - int nargs=VALTOINT(TABGET(fun,FUN_NBARGS)); - int ipc=b->getsize()-offset; - btab->addint(ipc); - b->addchar(nargs); - b->addshort(nloc); - b->addstr(STRSTART(VALTOPNT(TABGET(fun,FUN_BC))),STRLEN(VALTOPNT(TABGET(fun,FUN_BC)))); - printf("%d@%d:%s nargs=%d nlocals=%d / %d bytes\n",nf,ipc,STRSTART(VALTOPNT(TABGET(VALTOPNT(vref),REF_NAME))),nargs,nloc,STRLEN(VALTOPNT(TABGET(fun,FUN_BC)))); - return nf; + if (vref==NIL) + { + return -1; + } + int f=recbc(TABGET(VALTOPNT(vref),REF_NEXTALL),b,btab,offset); + if (f<-1) + { + return f; + } + if (VALTOINT(TABGET(VALTOPNT(vref),REF_CODE))<0) + { + return f; + } + int nf=VALTOINT(TABGET(VALTOPNT(vref),REF_PACKAGE)); + if (nf<=f) + { + return f; + } + if (TABGET(VALTOPNT(vref),REF_VAL)==NIL) + { + printf("%s is EMPTY !!!\n",STRSTART(VALTOPNT(TABGET(VALTOPNT(vref),REF_NAME)))); + return -2; + } + intptr_t *fun=VALTOPNT(TABGET(VALTOPNT(vref),REF_VAL)); + int nloc=VALTOINT(TABGET(fun,FUN_NBLOCALS))-VALTOINT(TABGET(fun,FUN_NBARGS)); + int nargs=VALTOINT(TABGET(fun,FUN_NBARGS)); + int ipc=b->getsize()-offset; + btab->addint(ipc); + b->addchar(nargs); + b->addshort(nloc); + b->addstr(STRSTART(VALTOPNT(TABGET(fun,FUN_BC))),STRLEN(VALTOPNT(TABGET(fun,FUN_BC)))); + printf("%d@%d:%s nargs=%d nlocals=%d / %d bytes\n",nf,ipc,STRSTART(VALTOPNT(TABGET(VALTOPNT(vref),REF_NAME))),nargs,nloc,STRLEN(VALTOPNT(TABGET(fun,FUN_BC)))); + return nf; } @@ -120,256 +138,349 @@ int Compiler::recbc(int vref,Prodbuffer *b,Prodbuffer *btab,int offset) // [filename/src packages] -> [packages] int Compiler::gocompile(int type) { - int k; - - if (STACKGET(m,0)==NIL) - { - STACKDROP(m); - return 0; - } - char* name=STRSTART(VALTOPNT(STACKGET(m,0))); - if (type==COMPILE_FROMFILE) - { - parser=new Parser(m->term,m->filesystem,name); - PRINTF(m)(LOG_COMPILER,"Compiler : compiling file '%s'\n",name); - } - else - { - parser=new Parser(m->term,name); - name="..."; - PRINTF(m)(LOG_COMPILER,"Compiler : compiling string buffer\n"); - } - - if (k=createpackage(name,4)) return k; // [package nom_fichier env] - TABSET(m,VALTOPNT(STACKGET(m,0)),PACK_NEXT,STACKGET(m,2)); - STACKSET(m,2,STACKGET(m,0)); // [newenv nom_fichier newenv] - STACKDROPN(m,2); // [newenv] - newpackage=VALTOPNT(STACKGET(m,0)); - // parsing + int k; + + if (STACKGET(m,0)==NIL) + { + STACKDROP(m); + return 0; + } + char* name=STRSTART(VALTOPNT(STACKGET(m,0))); + if (type==COMPILE_FROMFILE) + { + parser=new Parser(m->term,m->filesystem,name); + PRINTF(m)(LOG_COMPILER,"Compiler : compiling file '%s'\n",name); + } + else + { + parser=new Parser(m->term,name); + name="..."; + PRINTF(m)(LOG_COMPILER,"Compiler : compiling string buffer\n"); + } + + if (k=createpackage(name,4)) + { + return k; // [package nom_fichier env] + } + TABSET(m,VALTOPNT(STACKGET(m,0)),PACK_NEXT,STACKGET(m,2)); + STACKSET(m,2,STACKGET(m,0)); // [newenv nom_fichier newenv] + STACKDROPN(m,2); // [newenv] + newpackage=VALTOPNT(STACKGET(m,0)); + // parsing //### ici on doit pouvoir ajouter la liste des globales et la liste des fonctions //### c'est ici également qu'on va réinitialiser la structure bc - if (k=STACKPUSH(m,NIL)) return k; // GLOBALS - globals=STACKREF(m); - ifuns=0; + if (k=STACKPUSH(m,NIL)) + { + return k; // GLOBALS + } + globals=STACKREF(m); + ifuns=0; - k=parsefile(0); + k=parsefile(0); //### c'est fait, on a le bytecode, la liste des fonctions, et la liste des globales - if (!k) - { - Prodbuffer* btab=new Prodbuffer(); - - int n=nblabels(globals); - brelease->addint(0); - recglobals(STACKGETFROMREF(m,globals,0),brelease); - brelease->setint(0,brelease->getsize()); - - int sizebc=brelease->getsize(); - brelease->addint(0); // on prépare le champ pour la taille du bytecode - intptr_t* p=VALTOPNT(TABGET(newpackage,PACK_HACH)); - int vref=TABGET(p,TABLEN(p)-1); - int nfun=recbc(vref,brelease,btab,sizebc+4); - if (nfun<0) return nfun; - - brelease->setint(sizebc,brelease->getsize()-sizebc-4); - brelease->addshort(nfun+1); - brelease->addstr(btab->getstart(),btab->getsize()); - delete btab; - } - if (k) parser->echoposition(); - PRINTF(m)(LOG_COMPILER,"\n"); + if (!k) + { + Prodbuffer* btab=new Prodbuffer(); + + int n=nblabels(globals); + brelease->addint(0); + recglobals(STACKGETFROMREF(m,globals,0),brelease); + brelease->setint(0,brelease->getsize()); + + int sizebc=brelease->getsize(); + brelease->addint(0); // on prépare le champ pour la taille du bytecode + intptr_t* p=VALTOPNT(TABGET(newpackage,PACK_HACH)); + int vref=TABGET(p,TABLEN(p)-1); + int nfun=recbc(vref,brelease,btab,sizebc+4); + if (nfun<0) + { + return nfun; + } + + brelease->setint(sizebc,brelease->getsize()-sizebc-4); + brelease->addshort(nfun+1); + brelease->addstr(btab->getstart(),btab->getsize()); + delete btab; + } + if (k) + { + parser->echoposition(); + } + PRINTF(m)(LOG_COMPILER,"\n"); // dumppackage(STACKGET(m,0)); - delete parser; - return k; + delete parser; + return k; } int Compiler::parsefile(int ifdef) { - int k; - - while(parser->next(0)) - { - if (!strcmp(parser->token,"fun")) - { - if ((!parser->next(0))||(!islabel(parser->token))) - { - if (parser->token) PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found '%s')\n",parser->token); - else PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found EOF)\n"); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - intptr_t* s=VALTOPNT(STACKGET(m,0)); - if (k=parsefun()) - { - PRINTF(m)(LOG_COMPILER,"Compiler : error compiling function '%s'\n",STRSTART(s)); - return k; - } - } - else if (!strcmp(parser->token,"type")) - { - if ((!parser->next(0))||(!islabel(parser->token))) - { - if (parser->token) PRINTF(m)(LOG_COMPILER,"Compiler : name of type expected (found '%s')\n",parser->token); - else PRINTF(m)(LOG_COMPILER,"Compiler : name of type expected (found EOF)\n"); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - intptr_t* s=VALTOPNT(STACKGET(m,0)); - - if (k=parsetype()) - { - PRINTF(m)(LOG_COMPILER,"Compiler : error compiling type '%s'\n",STRSTART(s)); - return k; - } - } - else if (!strcmp(parser->token,"var")) - { - if ((!parser->next(0))||(!islabel(parser->token))) - { - if (parser->token) PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found '%s')\n",parser->token); - else PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found EOF)\n"); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - intptr_t* s=VALTOPNT(STACKGET(m,0)); - - if (k=parsevar()) - { - PRINTF(m)(LOG_COMPILER,"Compiler : error compiling var '%s'\n",STRSTART(s)); - return k; - } - } - else if (!strcmp(parser->token,"const")) - { - if ((!parser->next(0))||(!islabel(parser->token))) - { - if (parser->token) - PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found '%s')\n",parser->token); - else - PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found EOF)"); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - intptr_t *s=VALTOPNT(STACKGET(m,0)); - if (k=parseconst()) - { - PRINTF(m)(LOG_COMPILER,"Compiler : error compiling const '%s'\n",STRSTART(s)); - return k; - } - } - else if (!strcmp(parser->token,"proto")) - { - if ((!parser->next(0))||(!islabel(parser->token))) - { - if (parser->token) PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found '%s')\n",parser->token); - else PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found EOF)\n"); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - intptr_t* s=VALTOPNT(STACKGET(m,0)); - - if (k=parseproto()) - { - PRINTF(m)(LOG_COMPILER,"Compiler : error compiling proto '%s'\n",STRSTART(s)); - return k; - } - } - else if (!strcmp(parser->token,"ifdef")) - { - if (k=parseifdef(0)) return k; - } - else if (!strcmp(parser->token,"ifndef")) - { - if (k=parseifdef(1)) return k; - } - else if ((ifdef)&&(!strcmp(parser->token,"}"))) - { - parser->giveback(); - return 0; - } - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : unknown token %s\n",parser->token); - return MTLERR_SN; - } - } - return 0; + int k; + + while(parser->next(0)) + { + if (!strcmp(parser->token,"fun")) + { + if ((!parser->next(0))||(!islabel(parser->token))) + { + if (parser->token) + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found '%s')\n",parser->token); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found EOF)\n"); + } + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + intptr_t* s=VALTOPNT(STACKGET(m,0)); + if (k=parsefun()) + { + PRINTF(m)(LOG_COMPILER,"Compiler : error compiling function '%s'\n",STRSTART(s)); + return k; + } + } + else if (!strcmp(parser->token,"type")) + { + if ((!parser->next(0))||(!islabel(parser->token))) + { + if (parser->token) + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of type expected (found '%s')\n",parser->token); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of type expected (found EOF)\n"); + } + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + intptr_t* s=VALTOPNT(STACKGET(m,0)); + + if (k=parsetype()) + { + PRINTF(m)(LOG_COMPILER,"Compiler : error compiling type '%s'\n",STRSTART(s)); + return k; + } + } + else if (!strcmp(parser->token,"var")) + { + if ((!parser->next(0))||(!islabel(parser->token))) + { + if (parser->token) + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found '%s')\n",parser->token); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found EOF)\n"); + } + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + intptr_t* s=VALTOPNT(STACKGET(m,0)); + + if (k=parsevar()) + { + PRINTF(m)(LOG_COMPILER,"Compiler : error compiling var '%s'\n",STRSTART(s)); + return k; + } + } + else if (!strcmp(parser->token,"const")) + { + if ((!parser->next(0))||(!islabel(parser->token))) + { + if (parser->token) + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found '%s')\n",parser->token); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of reference expected (found EOF)"); + } + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + intptr_t *s=VALTOPNT(STACKGET(m,0)); + if (k=parseconst()) + { + PRINTF(m)(LOG_COMPILER,"Compiler : error compiling const '%s'\n",STRSTART(s)); + return k; + } + } + else if (!strcmp(parser->token,"proto")) + { + if ((!parser->next(0))||(!islabel(parser->token))) + { + if (parser->token) + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found '%s')\n",parser->token); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : name of function expected (found EOF)\n"); + } + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + intptr_t* s=VALTOPNT(STACKGET(m,0)); + + if (k=parseproto()) + { + PRINTF(m)(LOG_COMPILER,"Compiler : error compiling proto '%s'\n",STRSTART(s)); + return k; + } + } + else if (!strcmp(parser->token,"ifdef")) + { + if (k=parseifdef(0)) + { + return k; + } + } + else if (!strcmp(parser->token,"ifndef")) + { + if (k=parseifdef(1)) + { + return k; + } + } + else if ((ifdef)&&(!strcmp(parser->token,"}"))) + { + parser->giveback(); + return 0; + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : unknown token %s\n",parser->token); + return MTLERR_SN; + } + } + return 0; } int Compiler::skipifdef() { - int n=0; - while(parser->next(0)) - { - if (!strcmp(parser->token,"{")) n++; - else if (!strcmp(parser->token,"}")) - { - n--; - if (n<0) return 0; - } - } - PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); - return MTLERR_SN; + int n=0; + while(parser->next(0)) + { + if (!strcmp(parser->token,"{")) + { + n++; + } + else if (!strcmp(parser->token,"}")) + { + n--; + if (n<0) + { + return 0; + } + } + } + PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); + return MTLERR_SN; } int Compiler::parseifdef(int ifndef) { - int k; - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found EOF)\n"); - return MTLERR_SN; - } - if (!islabel(parser->token)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - int first=1; - - intptr_t - *ref, *type; - if ((!(ref = searchref(PNTTOVAL(newpackage),parser->token)))&&(!(type = searchtype(PNTTOVAL(newpackage),parser->token)))) - first=0; - - // si c'est une variable on note qu'elle est utlilisé dans un ifdef - // ça nous permet de savoir, plus tard, si elle est seulement - // utilisée dans un/des ifdef - dans ce cas elle n'a pas besoin de - // valeur - ou aussi autre part - dans ce cas elle a besoin d'être - // settée au moins une fois. - if (ref) - { - int curval = VALTOINT(TABGET(ref,REF_USED_IN_IFDEF)); - TABSET(m,ref,REF_USED_IN_IFDEF,INTTOVAL(curval+1)); - } - - if (ifndef) first=!first; - if (k=parser->parsekeyword("{")) return k; - if (first) - { - parsefile(1); - if (k=parser->parsekeyword("}")) return k; - } - else if (k=skipifdef()) return k; - if (!parser->next(0)) return 0; - if (strcmp(parser->token,"else")) - { - parser->giveback(); - return 0; - } - else - { - if (k=parser->parsekeyword("{")) return k; - if (!first) - { - parsefile(1); - if (k=parser->parsekeyword("}")) return k; - } - else if (k=skipifdef()) return k; - } - return 0; + int k; + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found EOF)\n"); + return MTLERR_SN; + } + if (!islabel(parser->token)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + int first=1; + + intptr_t + *ref, *type; + if ((!(ref = searchref(PNTTOVAL(newpackage),parser->token)))&&(!(type = searchtype(PNTTOVAL(newpackage),parser->token)))) + { + first=0; + } + + // si c'est une variable on note qu'elle est utlilisé dans un ifdef + // ça nous permet de savoir, plus tard, si elle est seulement + // utilisée dans un/des ifdef - dans ce cas elle n'a pas besoin de + // valeur - ou aussi autre part - dans ce cas elle a besoin d'être + // settée au moins une fois. + if (ref) + { + int curval = VALTOINT(TABGET(ref,REF_USED_IN_IFDEF)); + TABSET(m,ref,REF_USED_IN_IFDEF,INTTOVAL(curval+1)); + } + + if (ifndef) + { + first=!first; + } + if (k=parser->parsekeyword("{")) + { + return k; + } + if (first) + { + parsefile(1); + if (k=parser->parsekeyword("}")) + { + return k; + } + } + else if (k=skipifdef()) + { + return k; + } + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,"else")) + { + parser->giveback(); + return 0; + } + else + { + if (k=parser->parsekeyword("{")) + { + return k; + } + if (!first) + { + parsefile(1); + if (k=parser->parsekeyword("}")) + { + return k; + } + } + else if (k=skipifdef()) + { + return k; + } + } + return 0; } void displaybc(Memory* m,char* src); @@ -379,175 +490,241 @@ void displaybc(Memory* m,char* src); // [name] -> 0 int Compiler::parsefun() { - int k; - intptr_t* type_result; + int k; + intptr_t* type_result; // PRINTF(m)(LOG_DEVCORE,"fonction %s\n",STRSTART(VALTOPNT(STACKGET(m,0)))); - char* name=STRSTART(VALTOPNT(STACKGET(m,0))); - // création des variables de travail - if (k=STACKPUSH(m,NIL)) return k; // LOCALS - locals=STACKREF(m); - - if (k=createnodetype(TYPENAME_FUN)) return k; - - // recherche des arguments - int narg=0; - do - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : argument or '=' expected (found EOF)\n"); - return MTLERR_SN; - } - if (islabel(parser->token)) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - if (k=addlabel(locals,parser->token,INTTOVAL(narg++),STACKGET(m,0))) return k; - } - else if (strcmp(parser->token,"=")) - { - PRINTF(m)(LOG_COMPILER,"Compiler : argument or '=' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - } while(strcmp(parser->token,"=")); - // construction du type initial de la fonction - if (k=createnodetuple(narg)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun - STACKDROP(m); - - if (k=createnodetype(TYPENAME_UNDEF)) return k; // noeud résultat - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun - type_result=VALTOPNT(STACKPULL(m)); // on garde en mémoire le type du résultat - // ici : [type local global name] - - // on crée le bloc fonction - newref=MALLOCCLEAR(m,REF_LENGTH); - if (!newref) return MTLERR_OM; - TABSET(m,newref,REF_TYPE,STACKPULL(m)); - TABSET(m,newref,REF_NAME,STACKGET(m,1)); - TABSET(m,newref,REF_CODE,INTTOVAL(narg)); - - // vient d'être déclarée, pas encore utilisée - TABSET(m,newref,REF_USED,INTTOVAL(0)); - - k=findproto(PNTTOVAL(newpackage),newref); - - TABSET(m,newref,REF_PACKAGE,(k!=NIL)?k:INTTOVAL(ifuns++)); - - if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref local global name] - addreftopackage(newref,newpackage); - STACKDROP(m); - // [local global name] - - // on poursuit la compilation vers le corps de la fonction - nblocals=narg; - - bc->reinit(); // initialisation de la production de bytecode - - // [locals globals] - - // parsing - if (k=parseprogram()) return k; - - // [type locals globals] + char* name=STRSTART(VALTOPNT(STACKGET(m,0))); + // création des variables de travail + if (k=STACKPUSH(m,NIL)) + { + return k; // LOCALS + } + locals=STACKREF(m); + + if (k=createnodetype(TYPENAME_FUN)) + { + return k; + } + + // recherche des arguments + int narg=0; + do + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : argument or '=' expected (found EOF)\n"); + return MTLERR_SN; + } + if (islabel(parser->token)) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + if (k=addlabel(locals,parser->token,INTTOVAL(narg++),STACKGET(m,0))) + { + return k; + } + } + else if (strcmp(parser->token,"=")) + { + PRINTF(m)(LOG_COMPILER,"Compiler : argument or '=' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + } + while(strcmp(parser->token,"=")); + // construction du type initial de la fonction + if (k=createnodetuple(narg)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun + STACKDROP(m); + + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; // noeud résultat + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun + type_result=VALTOPNT(STACKPULL(m)); // on garde en mémoire le type du résultat + // ici : [type local global name] + + // on crée le bloc fonction + newref=MALLOCCLEAR(m,REF_LENGTH); + if (!newref) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_TYPE,STACKPULL(m)); + TABSET(m,newref,REF_NAME,STACKGET(m,1)); + TABSET(m,newref,REF_CODE,INTTOVAL(narg)); + + // vient d'être déclarée, pas encore utilisée + TABSET(m,newref,REF_USED,INTTOVAL(0)); + + k=findproto(PNTTOVAL(newpackage),newref); + + TABSET(m,newref,REF_PACKAGE,(k!=NIL)?k:INTTOVAL(ifuns++)); + + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return MTLERR_OM; // [newref local global name] + } + addreftopackage(newref,newpackage); + STACKDROP(m); + // [local global name] + + // on poursuit la compilation vers le corps de la fonction + nblocals=narg; + + bc->reinit(); // initialisation de la production de bytecode + + // [locals globals] + + // parsing + if (k=parseprogram()) + { + return k; + } + + // [type locals globals] // la pile contient le type du résultat de la fonction - if (k=parser->parsekeyword(";;")) return k; + if (k=parser->parsekeyword(";;")) + { + return k; + } - // unifier le type résultat - if (k=unif(type_result,VALTOPNT(STACKGET(m,0)))) return k; - STACKDROP(m); - // [locals globals name] - // créer le bloc programme - intptr_t* fun=MALLOCCLEAR(m,FUN_LENGTH); - if (!fun) return MTLERR_OM; - TABSET(m,newref,REF_VAL,PNTTOVAL(fun)); + // unifier le type résultat + if (k=unif(type_result,VALTOPNT(STACKGET(m,0)))) + { + return k; + } + STACKDROP(m); + // [locals globals name] + // créer le bloc programme + intptr_t* fun=MALLOCCLEAR(m,FUN_LENGTH); + if (!fun) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_VAL,PNTTOVAL(fun)); - TABSET(m,fun,FUN_NBARGS,INTTOVAL(narg)); - TABSET(m,fun,FUN_NBLOCALS,INTTOVAL(nblocals)); + TABSET(m,fun,FUN_NBARGS,INTTOVAL(narg)); + TABSET(m,fun,FUN_NBLOCALS,INTTOVAL(nblocals)); - // stocker le bytecode - bc->addchar(OPret); + // stocker le bytecode + bc->addchar(OPret); - if (k=STRPUSHBINARY(m,bc->getstart(),bc->getsize())) return k; - TABSET(m,fun,FUN_BC,STACKPULL(m)); + if (k=STRPUSHBINARY(m,bc->getstart(),bc->getsize())) + { + return k; + } + TABSET(m,fun,FUN_BC,STACKPULL(m)); - if (!strcmp(name,"awcConnect")) - displaybc(m,STRSTART(VALTOPNT(TABGET(fun,FUN_BC)))); + if (!strcmp(name,"awcConnect")) + { + displaybc(m,STRSTART(VALTOPNT(TABGET(fun,FUN_BC)))); + } - // construire le tuple des références globales + // construire le tuple des références globales // int* globalstuple=tuplefromlabels(globals); // if (!globalstuple) return MTLERR_OM; // TABSET(m,fun,FUN_REF,PNTTOVAL(globalstuple)); - TABSET(m,fun,FUN_REFERENCE,PNTTOVAL(newref)); + TABSET(m,fun,FUN_REFERENCE,PNTTOVAL(newref)); - STACKDROPN(m,2); + STACKDROPN(m,2); - // [] + // [] - // chercher d'éventuels prototypes - if (k=fillproto(PNTTOVAL(newpackage),newref)) return k; + // chercher d'éventuels prototypes + if (k=fillproto(PNTTOVAL(newpackage),newref)) + { + return k; + } - outputbuf->reinit(); - outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + outputbuf->reinit(); + outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - return 0; + return 0; } // compilation d'une variable // [name] -> 0 int Compiler::parsevar() { - int k; - int hasvalue = 0; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ';;' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"=")) - { - if (k=parseval()) return k; - hasvalue = 1; - } - else - { - parser->giveback(); - if (k=STACKPUSH(m,NIL)) return k; - if (k=createnodetype(TYPENAME_WEAK)) return k; - } - // [val type name] - if (k=parser->parsekeyword(";;")) return k; - - int val=INTTOVAL(nblabels(globals)); - if (k=addlabel(globals,STRSTART(VALTOPNT(STACKGET(m,2))),val,STACKGET(m,1))) return k; // enregistrement d'une nouvelle globale - - // on crée le bloc fonction - newref=MALLOCCLEAR(m,REF_LENGTH); - if (!newref) return MTLERR_OM; - TABSET(m,newref,REF_TYPE,STACKPULL(m)); - TABSET(m,newref,REF_VAL,STACKPULL(m)); - TABSET(m,newref,REF_NAME,STACKPULL(m)); - TABSET(m,newref,REF_CODE,INTTOVAL(CODE_VAR)); - - // la variable vient d'être créé, elle n'est donc ni utilisée ni modifiée encore - TABSET(m,newref,REF_USED,INTTOVAL(0)); - TABSET(m,newref,REF_SET,INTTOVAL(hasvalue)); - TABSET(m,newref,REF_USED_IN_IFDEF,INTTOVAL(0)); - - TABSET(m,newref,REF_PACKAGE,val); - - if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref] - addreftopackage(newref,newpackage); - STACKDROP(m); - - outputbuf->reinit(); - outputbuf->printf("Compiler : var %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - return 0; + int k; + int hasvalue = 0; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ';;' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"=")) + { + if (k=parseval()) + { + return k; + } + hasvalue = 1; + } + else + { + parser->giveback(); + if (k=STACKPUSH(m,NIL)) + { + return k; + } + if (k=createnodetype(TYPENAME_WEAK)) + { + return k; + } + } + // [val type name] + if (k=parser->parsekeyword(";;")) + { + return k; + } + + int val=INTTOVAL(nblabels(globals)); + if (k=addlabel(globals,STRSTART(VALTOPNT(STACKGET(m,2))),val,STACKGET(m,1))) + { + return k; // enregistrement d'une nouvelle globale + } + + // on crée le bloc fonction + newref=MALLOCCLEAR(m,REF_LENGTH); + if (!newref) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_TYPE,STACKPULL(m)); + TABSET(m,newref,REF_VAL,STACKPULL(m)); + TABSET(m,newref,REF_NAME,STACKPULL(m)); + TABSET(m,newref,REF_CODE,INTTOVAL(CODE_VAR)); + + // la variable vient d'être créé, elle n'est donc ni utilisée ni modifiée encore + TABSET(m,newref,REF_USED,INTTOVAL(0)); + TABSET(m,newref,REF_SET,INTTOVAL(hasvalue)); + TABSET(m,newref,REF_USED_IN_IFDEF,INTTOVAL(0)); + + TABSET(m,newref,REF_PACKAGE,val); + + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return MTLERR_OM; // [newref] + } + addreftopackage(newref,newpackage); + STACKDROP(m); + + outputbuf->reinit(); + outputbuf->printf("Compiler : var %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + return 0; } @@ -555,39 +732,57 @@ int Compiler::parsevar() // [name] -> 0 int Compiler::parseconst() { - int k; - - if (k=parser->parsekeyword("=")) return k; - if (k=parseval()) return k; - if (k=parser->parsekeyword(";;")) return k; - - int val=INTTOVAL(nblabels(globals)); - if (k=addlabel(globals,STRSTART(VALTOPNT(STACKGET(m,2))),val,STACKGET(m,1))) return k; // enregistrement d'une nouvelle globale - - // on crée le bloc fonction - newref=MALLOCCLEAR(m,REF_LENGTH); - if (!newref) return MTLERR_OM; - TABSET(m,newref,REF_TYPE,STACKPULL(m)); - TABSET(m,newref,REF_VAL,STACKPULL(m)); - TABSET(m,newref,REF_NAME,STACKPULL(m)); - TABSET(m,newref,REF_CODE,INTTOVAL(CODE_CONST)); - - // la constante vient d'être créée, elle n'est donc pas utilisée, mais elle a une valeur - TABSET(m,newref,REF_USED,INTTOVAL(0)); - TABSET(m,newref,REF_SET,INTTOVAL(1)); - TABSET(m,newref,REF_USED_IN_IFDEF,INTTOVAL(0)); - - TABSET(m,newref,REF_PACKAGE,val); - - if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref] - addreftopackage(newref,newpackage); - STACKDROP(m); - - outputbuf->reinit(); - outputbuf->printf("Compiler : const %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - return 0; + int k; + + if (k=parser->parsekeyword("=")) + { + return k; + } + if (k=parseval()) + { + return k; + } + if (k=parser->parsekeyword(";;")) + { + return k; + } + + int val=INTTOVAL(nblabels(globals)); + if (k=addlabel(globals,STRSTART(VALTOPNT(STACKGET(m,2))),val,STACKGET(m,1))) + { + return k; // enregistrement d'une nouvelle globale + } + + // on crée le bloc fonction + newref=MALLOCCLEAR(m,REF_LENGTH); + if (!newref) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_TYPE,STACKPULL(m)); + TABSET(m,newref,REF_VAL,STACKPULL(m)); + TABSET(m,newref,REF_NAME,STACKPULL(m)); + TABSET(m,newref,REF_CODE,INTTOVAL(CODE_CONST)); + + // la constante vient d'être créée, elle n'est donc pas utilisée, mais elle a une valeur + TABSET(m,newref,REF_USED,INTTOVAL(0)); + TABSET(m,newref,REF_SET,INTTOVAL(1)); + TABSET(m,newref,REF_USED_IN_IFDEF,INTTOVAL(0)); + + TABSET(m,newref,REF_PACKAGE,val); + + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return MTLERR_OM; // [newref] + } + addreftopackage(newref,newpackage); + STACKDROP(m); + + outputbuf->reinit(); + outputbuf->printf("Compiler : const %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + return 0; } @@ -595,169 +790,231 @@ int Compiler::parseconst() // [name] -> 0 int Compiler::parseproto() { - int k; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found EOF)\n"); - return MTLERR_SN; - } - int nbarg=-1; - if (!strcmp(parser->token,"=")) - { - if (k=creategraph(parser,PNTTOVAL(newpackage),0)) return k; - int vp=STACKGET(m,0); - if (vp!=NIL) - { - intptr_t* p=VALTOPNT(vp); - if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_FUN)) - { - vp=TABGET(p,TYPEHEADER_LENGTH); - if (vp!=NIL) - { - intptr_t* p=VALTOPNT(vp); - if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_TUPLE)) - { - nbarg=TABLEN(p)-TYPEHEADER_LENGTH; - } - } - } - } - if (nbarg<0) - { - PRINTF(m)(LOG_COMPILER,"Compiler : function type expected\n"); - return MTLERR_SN; - } - } - else if (isdecimal(parser->token)) - { - nbarg=mtl_atoi(parser->token); - - if (k=createnodetype(TYPENAME_FUN)) return k; - - int i;for(i=0;itoken); - return MTLERR_SN; - } - if (k=parser->parsekeyword(";;")) return k; - // on crée le bloc fonction - newref=MALLOCCLEAR(m,REF_LENGTH); - if (!newref) return MTLERR_OM; - TABSET(m,newref,REF_TYPE,STACKPULL(m)); - TABSET(m,newref,REF_NAME,STACKPULL(m)); - TABSET(m,newref,REF_CODE,INTTOVAL(nbarg)); - TABSET(m,newref,REF_PACKAGE,INTTOVAL(ifuns++)); - if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref] - addreftopackage(newref,newpackage); - STACKDROP(m); - - outputbuf->reinit(); - outputbuf->printf("Compiler : proto %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - return 0; + int k; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found EOF)\n"); + return MTLERR_SN; + } + int nbarg=-1; + if (!strcmp(parser->token,"=")) + { + if (k=creategraph(parser,PNTTOVAL(newpackage),0)) + { + return k; + } + int vp=STACKGET(m,0); + if (vp!=NIL) + { + intptr_t* p=VALTOPNT(vp); + if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_FUN)) + { + vp=TABGET(p,TYPEHEADER_LENGTH); + if (vp!=NIL) + { + intptr_t* p=VALTOPNT(vp); + if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_TUPLE)) + { + nbarg=TABLEN(p)-TYPEHEADER_LENGTH; + } + } + } + } + if (nbarg<0) + { + PRINTF(m)(LOG_COMPILER,"Compiler : function type expected\n"); + return MTLERR_SN; + } + } + else if (isdecimal(parser->token)) + { + nbarg=mtl_atoi(parser->token); + + if (k=createnodetype(TYPENAME_FUN)) + { + return k; + } + + int i; + for(i=0; itoken); + return MTLERR_SN; + } + if (k=parser->parsekeyword(";;")) + { + return k; + } + // on crée le bloc fonction + newref=MALLOCCLEAR(m,REF_LENGTH); + if (!newref) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_TYPE,STACKPULL(m)); + TABSET(m,newref,REF_NAME,STACKPULL(m)); + TABSET(m,newref,REF_CODE,INTTOVAL(nbarg)); + TABSET(m,newref,REF_PACKAGE,INTTOVAL(ifuns++)); + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return MTLERR_OM; // [newref] + } + addreftopackage(newref,newpackage); + STACKDROP(m); + + outputbuf->reinit(); + outputbuf->printf("Compiler : proto %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + return 0; } // compilation d'un type // [name] -> 0 int Compiler::parsetype() { - int k; + int k; // PRINTF(m)(LOG_DEVCORE,"type %s\n",STRSTART(VALTOPNT(STACKGET(m,0)))); - char* name=STRSTART(VALTOPNT(STACKGET(m,0))); - // création des variables de travail - if (k=STACKPUSH(m,NIL)) return k; // LOCALS - locals=STACKREF(m); - - newref=searchemptytype(PNTTOVAL(newpackage),name); - int mergetype=1; - if (newref) - { - if (k=createnodetypecore(TABGET(VALTOPNT(TABGET(newref,REF_TYPE)),TYPEHEADER_LENGTH+1))) return k; - } - else - { - mergetype=0; - if (k=createnodetypecore(STACKGET(m,1))) return k; - newref=MALLOCCLEAR(m,REF_LENGTH); - if (!newref) return MTLERR_OM; - TABSET(m,newref,REF_CODE,INTTOVAL(CODE_EMPTYTYPE)); - TABSET(m,newref,REF_TYPE,STACKGET(m,0)); - if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newtyp local name] - addreftopackage(newref,newpackage); - STACKDROP(m); - } - - int narg=0; - if (parser->next(0)) - { - if (strcmp(parser->token,"(")) parser->giveback(); - else - { - do - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : parameter or ')' expected (found EOF)\n"); - return MTLERR_SN; - } - if (islabel(parser->token)) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - if (k=addlabel(locals,parser->token,STACKGET(m,0),INTTOVAL(narg++))) return k; - } - else if (strcmp(parser->token,")")) - { - PRINTF(m)(LOG_COMPILER,"Compiler : parameter or ')' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - } while(strcmp(parser->token,")")); - if (k=DEFTAB(m,narg)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - STACKDROP(m); - } - } - if (!mergetype) STACKDROP(m); - else if (k=unif(VALTOPNT(STACKPULL(m)),VALTOPNT(TABGET(newref,REF_TYPE)))) return k; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '=' or ';;' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"=")) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : uncomplete type definition (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"[")) return parsestruct(); - parser->giveback(); - return parsesum(); - } - else if (!strcmp(parser->token,";;")) - { - STACKDROPN(m,2); - outputbuf->reinit(); - outputbuf->printf("Compiler : uncompleted type : "); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - return 0; - } - PRINTF(m)(LOG_COMPILER,"Compiler : '=' or ';;' expected (found '%s')\n",parser->token); - return MTLERR_SN; + char* name=STRSTART(VALTOPNT(STACKGET(m,0))); + // création des variables de travail + if (k=STACKPUSH(m,NIL)) + { + return k; // LOCALS + } + locals=STACKREF(m); + + newref=searchemptytype(PNTTOVAL(newpackage),name); + int mergetype=1; + if (newref) + { + if (k=createnodetypecore(TABGET(VALTOPNT(TABGET(newref,REF_TYPE)),TYPEHEADER_LENGTH+1))) + { + return k; + } + } + else + { + mergetype=0; + if (k=createnodetypecore(STACKGET(m,1))) + { + return k; + } + newref=MALLOCCLEAR(m,REF_LENGTH); + if (!newref) + { + return MTLERR_OM; + } + TABSET(m,newref,REF_CODE,INTTOVAL(CODE_EMPTYTYPE)); + TABSET(m,newref,REF_TYPE,STACKGET(m,0)); + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return MTLERR_OM; // [newtyp local name] + } + addreftopackage(newref,newpackage); + STACKDROP(m); + } + + int narg=0; + if (parser->next(0)) + { + if (strcmp(parser->token,"(")) + { + parser->giveback(); + } + else + { + do + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : parameter or ')' expected (found EOF)\n"); + return MTLERR_SN; + } + if (islabel(parser->token)) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + if (k=addlabel(locals,parser->token,STACKGET(m,0),INTTOVAL(narg++))) + { + return k; + } + } + else if (strcmp(parser->token,")")) + { + PRINTF(m)(LOG_COMPILER,"Compiler : parameter or ')' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + } + while(strcmp(parser->token,")")); + if (k=DEFTAB(m,narg)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + STACKDROP(m); + } + } + if (!mergetype) + { + STACKDROP(m); + } + else if (k=unif(VALTOPNT(STACKPULL(m)),VALTOPNT(TABGET(newref,REF_TYPE)))) + { + return k; + } + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '=' or ';;' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"=")) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : uncomplete type definition (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"[")) + { + return parsestruct(); + } + parser->giveback(); + return parsesum(); + } + else if (!strcmp(parser->token,";;")) + { + STACKDROPN(m,2); + outputbuf->reinit(); + outputbuf->printf("Compiler : uncompleted type : "); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + return 0; + } + PRINTF(m)(LOG_COMPILER,"Compiler : '=' or ';;' expected (found '%s')\n",parser->token); + return MTLERR_SN; } @@ -765,167 +1022,232 @@ int Compiler::parsetype() // [locals nom] int Compiler::parsestruct() { - int k; - - // on crée le bloc type - TABSET(m,newref,REF_VAL,INTTOVAL(0)); - TABSET(m,newref,REF_CODE,INTTOVAL(CODE_STRUCT)); - // [local name] - - int loop=1; - do - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (islabel(parser->token)) - { - if (k=STRPUSH(m,parser->token)) return k; - - // on crée le bloc champ - intptr_t* newfield=MALLOCCLEAR(m,REF_LENGTH); - if (!newfield) return MTLERR_OM; - TABSET(m,newfield,REF_NAME,STACKPULL(m)); - TABSET(m,newfield,REF_CODE,INTTOVAL(CODE_FIELD)); - if (k=STACKPUSH(m,PNTTOVAL(newfield))) return MTLERR_OM; // [newfield local name] - addreftopackage(newfield,newpackage); - STACKDROP(m); - // [local name] - if (k=STACKPUSH(m,TABGET(newref,REF_VAL))) return k; - if (k=STACKPUSH(m,PNTTOVAL(newref))) return k; - if (k=DEFTAB(m,FIELD_LENGTH)) return k; - TABSET(m,newfield,REF_VAL,STACKPULL(m)); - - TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation - - if (k=createnodetype(TYPENAME_FUN)) return k; - if (k=STACKPUSH(m,TABGET(newref,REF_TYPE))) return k; - if (k=createnodetuple(1)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun - STACKDROP(m); - - if ((!parser->next(0))||(strcmp(parser->token,":"))) - { - parser->giveback(); - k=createnodetype(TYPENAME_WEAK); - } - else k=creategraph(parser,PNTTOVAL(newpackage),1,locals); - if (k) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun - STACKDROP(m); - TABSET(m,newfield,REF_TYPE,STACKPULL(m)); - - outputbuf->reinit(); - outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newfield,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newfield,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - } - else if (!strcmp(parser->token,"]")) loop=0; - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - } while(loop); - if (k=parser->parsekeyword(";;")) return k; - - STACKDROPN(m,2); - - outputbuf->reinit(); - outputbuf->printf("Compiler : "); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - outputbuf->printf(" : struct (%d)\n",VALTOINT(TABGET(newref,REF_VAL))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - - return 0; + int k; + + // on crée le bloc type + TABSET(m,newref,REF_VAL,INTTOVAL(0)); + TABSET(m,newref,REF_CODE,INTTOVAL(CODE_STRUCT)); + // [local name] + + int loop=1; + do + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (islabel(parser->token)) + { + if (k=STRPUSH(m,parser->token)) + { + return k; + } + + // on crée le bloc champ + intptr_t* newfield=MALLOCCLEAR(m,REF_LENGTH); + if (!newfield) + { + return MTLERR_OM; + } + TABSET(m,newfield,REF_NAME,STACKPULL(m)); + TABSET(m,newfield,REF_CODE,INTTOVAL(CODE_FIELD)); + if (k=STACKPUSH(m,PNTTOVAL(newfield))) + { + return MTLERR_OM; // [newfield local name] + } + addreftopackage(newfield,newpackage); + STACKDROP(m); + // [local name] + if (k=STACKPUSH(m,TABGET(newref,REF_VAL))) + { + return k; + } + if (k=STACKPUSH(m,PNTTOVAL(newref))) + { + return k; + } + if (k=DEFTAB(m,FIELD_LENGTH)) + { + return k; + } + TABSET(m,newfield,REF_VAL,STACKPULL(m)); + + TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation + + if (k=createnodetype(TYPENAME_FUN)) + { + return k; + } + if (k=STACKPUSH(m,TABGET(newref,REF_TYPE))) + { + return k; + } + if (k=createnodetuple(1)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun + STACKDROP(m); + + if ((!parser->next(0))||(strcmp(parser->token,":"))) + { + parser->giveback(); + k=createnodetype(TYPENAME_WEAK); + } + else + { + k=creategraph(parser,PNTTOVAL(newpackage),1,locals); + } + if (k) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun + STACKDROP(m); + TABSET(m,newfield,REF_TYPE,STACKPULL(m)); + + outputbuf->reinit(); + outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newfield,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newfield,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + } + else if (!strcmp(parser->token,"]")) + { + loop=0; + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : field name or ']' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + } + while(loop); + if (k=parser->parsekeyword(";;")) + { + return k; + } + + STACKDROPN(m,2); + + outputbuf->reinit(); + outputbuf->printf("Compiler : "); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + outputbuf->printf(" : struct (%d)\n",VALTOINT(TABGET(newref,REF_VAL))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + + return 0; } // compilation d'une somme // [locals nom] int Compiler::parsesum() { - int k; - - // on crée le bloc type - TABSET(m,newref,REF_VAL,INTTOVAL(0)); - TABSET(m,newref,REF_CODE,INTTOVAL(CODE_SUM)); - // [local name] - - int loop=1; - do - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found EOF)\n"); - return MTLERR_SN; - } - if (islabel(parser->token)) - { - if (k=STRPUSH(m,parser->token)) return k; - - // on crée le bloc champ - intptr_t* newcons=MALLOCCLEAR(m,REF_LENGTH); - if (!newcons) return MTLERR_OM; - TABSET(m,newcons,REF_NAME,STACKPULL(m)); - if (k=STACKPUSH(m,PNTTOVAL(newcons))) return MTLERR_OM; // [newcons local name] - addreftopackage(newcons,newpackage); - STACKDROP(m); - // [local name] - TABSET(m,newcons,REF_VAL,TABGET(newref,REF_VAL)); - TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation - - if (k=createnodetype(TYPENAME_FUN)) return k; - - if ((parser->next(0))&&((!strcmp(parser->token,"|"))||(!strcmp(parser->token,";;")))) - { - parser->giveback(); - if (k=createnodetuple(0)) return k; - TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS0)); - } - else - { - parser->giveback(); - if (k=creategraph(parser,PNTTOVAL(newpackage),1,locals)) return k; - if (k=createnodetuple(1)) return k; - TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS)); - } - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun - STACKDROP(m); - TABSET(m,VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH+1,TABGET(newref,REF_TYPE)); // attachement du type resultat au noeud fun - TABSET(m,newcons,REF_TYPE,STACKPULL(m)); - - outputbuf->reinit(); - outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newcons,REF_NAME)))); - echograph(outputbuf,VALTOPNT(TABGET(newcons,REF_TYPE))); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - } - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,";;")) loop=0; - else if (strcmp(parser->token,"|")) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - } while(loop); - STACKDROPN(m,2); - - outputbuf->reinit(); - outputbuf->printf("Compiler : "); - echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); - outputbuf->printf(" : constructor\n"); - PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); - - return 0; + int k; + + // on crée le bloc type + TABSET(m,newref,REF_VAL,INTTOVAL(0)); + TABSET(m,newref,REF_CODE,INTTOVAL(CODE_SUM)); + // [local name] + + int loop=1; + do + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found EOF)\n"); + return MTLERR_SN; + } + if (islabel(parser->token)) + { + if (k=STRPUSH(m,parser->token)) + { + return k; + } + + // on crée le bloc champ + intptr_t* newcons=MALLOCCLEAR(m,REF_LENGTH); + if (!newcons) + { + return MTLERR_OM; + } + TABSET(m,newcons,REF_NAME,STACKPULL(m)); + if (k=STACKPUSH(m,PNTTOVAL(newcons))) + { + return MTLERR_OM; // [newcons local name] + } + addreftopackage(newcons,newpackage); + STACKDROP(m); + // [local name] + TABSET(m,newcons,REF_VAL,TABGET(newref,REF_VAL)); + TABSET(m,newref,REF_VAL,INTTOVAL(1+VALTOINT(TABGET(newref,REF_VAL)))); // incrémentation + + if (k=createnodetype(TYPENAME_FUN)) + { + return k; + } + + if ((parser->next(0))&&((!strcmp(parser->token,"|"))||(!strcmp(parser->token,";;")))) + { + parser->giveback(); + if (k=createnodetuple(0)) + { + return k; + } + TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS0)); + } + else + { + parser->giveback(); + if (k=creategraph(parser,PNTTOVAL(newpackage),1,locals)) + { + return k; + } + if (k=createnodetuple(1)) + { + return k; + } + TABSET(m,newcons,REF_CODE,INTTOVAL(CODE_CONS)); + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun + STACKDROP(m); + TABSET(m,VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH+1,TABGET(newref,REF_TYPE)); // attachement du type resultat au noeud fun + TABSET(m,newcons,REF_TYPE,STACKPULL(m)); + + outputbuf->reinit(); + outputbuf->printf("Compiler : %s : ",STRSTART(VALTOPNT(TABGET(newcons,REF_NAME)))); + echograph(outputbuf,VALTOPNT(TABGET(newcons,REF_TYPE))); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,";;")) + { + loop=0; + } + else if (strcmp(parser->token,"|")) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '|' or ';;' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + } + while(loop); + STACKDROPN(m,2); + + outputbuf->reinit(); + outputbuf->printf("Compiler : "); + echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); + outputbuf->printf(" : constructor\n"); + PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); + + return 0; } diff --git a/src/vcomp/compiler_prog.cpp b/src/vcomp/compiler_prog.cpp index b4113ae..6d3b6c6 100644 --- a/src/vcomp/compiler_prog.cpp +++ b/src/vcomp/compiler_prog.cpp @@ -20,233 +20,418 @@ int Compiler::parseprogram() { - int k; - - while(1) - { - if (k=parseexpression()) return k; - if (!parser->next(0)) return 0; - if (strcmp(parser->token,";")) - { - parser->giveback(); - return 0; - } - bc->addchar(OPdrop); - STACKDROP(m); // on supprime le type de l'expression - } + int k; + + while(1) + { + if (k=parseexpression()) + { + return k; + } + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,";")) + { + parser->giveback(); + return 0; + } + bc->addchar(OPdrop); + STACKDROP(m); // on supprime le type de l'expression + } } int Compiler::parseexpression() { - int k; - //~ asm volatile ("int3;"); - if (k=parsearithm()) return k; - //~ PRINTF(m)(LOG_COMPILER,"Compiler : parseexp arithm\n"); + int k; + //~ asm volatile ("int3;"); + if (k=parsearithm()) + { + return k; + } + //~ PRINTF(m)(LOG_COMPILER,"Compiler : parseexp arithm\n"); - if (!parser->next(0)) return 0; - if (strcmp(parser->token,"::")) + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,"::")) + { + parser->giveback(); + return 0; + } + if (k=parseexpression()) { - parser->giveback(); - return 0; + return k; // récursion } - if (k=parseexpression()) return k; // récursion - if (k=createnodetuple(2)) return k; - if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__u0_list_u0__list_u0)))) return k; - if (k=unif_argfun()) return k; + if (k=createnodetuple(2)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__u0_list_u0__list_u0)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } - bc->addchar(OPdeftabb); - bc->addchar(2); + bc->addchar(OPdeftabb); + bc->addchar(2); - return 0; + return 0; } int Compiler::parsearithm() { - int k; + int k; - if (k=parsea1()) return k; - while(1) + if (k=parsea1()) { - if (!parser->next(0)) return 0; - if ((strcmp(parser->token,"&&"))&&(strcmp(parser->token,"||"))) + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if ((strcmp(parser->token,"&&"))&&(strcmp(parser->token,"||"))) + { + parser->giveback(); + return 0; + } + bc->addchar(OPdup); + if (strcmp(parser->token,"&&")) + { + bc->addchar(OPnon); + } + bc->addchar(OPelse); + int bc_i=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut + bc->addchar(OPdrop); + if (k=parsea1()) { - parser->giveback(); - return 0; + return k; } - bc->addchar(OPdup); - if (strcmp(parser->token,"&&")) bc->addchar(OPnon); - bc->addchar(OPelse); - int bc_i=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut - bc->addchar(OPdrop); - if (k=parsea1()) return k; - bc->setshort(bc_i,bc->getsize()); // on règle le saut + bc->setshort(bc_i,bc->getsize()); // on règle le saut - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - STACKDROP(m); - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + STACKDROP(m); + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } } } int Compiler::parsea1() { - int k; + int k; - if ((parser->next(0))&&(!strcmp(parser->token,"!"))) + if ((parser->next(0))&&(!strcmp(parser->token,"!"))) { - if (k=parsea1()) return k; - bc->addchar(OPnon); - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - return 0; - } - parser->giveback(); - return parsea2(); + if (k=parsea1()) + { + return k; + } + bc->addchar(OPnon); + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + return 0; + } + parser->giveback(); + return parsea2(); } int Compiler::parsea2() { - int k,op,typ; - - if (k=parsea3()) return k; - while(1) - { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"==")) { op=OPeq; typ=0;} - else if (!strcmp(parser->token,"!=")) { op=OPne; typ=0;} - else if (!strcmp(parser->token,"<")) { op=OPlt; typ=1;} - else if (!strcmp(parser->token,">")) { op=OPgt; typ=1;} - else if (!strcmp(parser->token,"<=")) { op=OPle; typ=1;} - else if (!strcmp(parser->token,">=")) { op=OPge; typ=1;} - else - { - parser->giveback(); - return 0; - } - if (k=parsea3()) return k; - bc->addchar(op); - - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - if (typ==1) - { - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - } - STACKSET(m,0,TABGET(stdtypes,STDTYPE_I)); + int k,op,typ; + + if (k=parsea3()) + { + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"==")) + { + op=OPeq; + typ=0; + } + else if (!strcmp(parser->token,"!=")) + { + op=OPne; + typ=0; + } + else if (!strcmp(parser->token,"<")) + { + op=OPlt; + typ=1; + } + else if (!strcmp(parser->token,">")) + { + op=OPgt; + typ=1; + } + else if (!strcmp(parser->token,"<=")) + { + op=OPle; + typ=1; + } + else if (!strcmp(parser->token,">=")) + { + op=OPge; + typ=1; + } + else + { + parser->giveback(); + return 0; + } + if (k=parsea3()) + { + return k; + } + bc->addchar(op); + + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + if (typ==1) + { + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + } + STACKSET(m,0,TABGET(stdtypes,STDTYPE_I)); } } int Compiler::parsea3() { - int k,op,typ; - - if (k=parsea4()) return k; - while(1) - { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"+")) { op=OPadd; typ=1;} - else if (!strcmp(parser->token,"-")) { op=OPsub; typ=1;} - else - { - parser->giveback(); - return 0; - } - if (k=parsea4()) return k; - bc->addchar(op); - - intptr_t* ptyp; - if (typ==1) ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; - STACKDROP(m); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; + int k,op,typ; + + if (k=parsea4()) + { + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"+")) + { + op=OPadd; + typ=1; + } + else if (!strcmp(parser->token,"-")) + { + op=OPsub; + typ=1; + } + else + { + parser->giveback(); + return 0; + } + if (k=parsea4()) + { + return k; + } + bc->addchar(op); + + intptr_t* ptyp; + if (typ==1) + { + ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); + } + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } + STACKDROP(m); + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } } } int Compiler::parsea4() { - int k,op,typ; - - if (k=parsea5()) return k; - while(1) - { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"*")) { op=OPmul; typ=1;} - else if (!strcmp(parser->token,"/")) { op=OPdiv; typ=1;} - else if (!strcmp(parser->token,"%")) { op=OPmod; typ=1;} - else - { - parser->giveback(); - return 0; - } - if (k=parsea5()) return k; - bc->addchar(op); - - intptr_t* ptyp; - if (typ==1) ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; - STACKDROP(m); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; + int k,op,typ; + + if (k=parsea5()) + { + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"*")) + { + op=OPmul; + typ=1; + } + else if (!strcmp(parser->token,"/")) + { + op=OPdiv; + typ=1; + } + else if (!strcmp(parser->token,"%")) + { + op=OPmod; + typ=1; + } + else + { + parser->giveback(); + return 0; + } + if (k=parsea5()) + { + return k; + } + bc->addchar(op); + + intptr_t* ptyp; + if (typ==1) + { + ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); + } + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } + STACKDROP(m); + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } } } int Compiler::parsea5() { - int k,op; - - if (k=parsea6()) return k; - while(1) - { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"&")) op=OPand; - else if (!strcmp(parser->token,"|")) op=OPor; - else if (!strcmp(parser->token,"^")) op=OPeor; - else if (!strcmp(parser->token,"<<")) op=OPshl; - else if (!strcmp(parser->token,">>")) op=OPshr; - else - { - parser->giveback(); - return 0; - } - if (k=parsea6()) return k; - bc->addchar(op); - - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - STACKDROP(m); - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; + int k,op; + + if (k=parsea6()) + { + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"&")) + { + op=OPand; + } + else if (!strcmp(parser->token,"|")) + { + op=OPor; + } + else if (!strcmp(parser->token,"^")) + { + op=OPeor; + } + else if (!strcmp(parser->token,"<<")) + { + op=OPshl; + } + else if (!strcmp(parser->token,">>")) + { + op=OPshr; + } + else + { + parser->giveback(); + return 0; + } + if (k=parsea6()) + { + return k; + } + bc->addchar(op); + + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + STACKDROP(m); + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } } } int Compiler::parsea6() { - int k; - - if (!parser->next(0)) return parseterm(); - - if (!strcmp(parser->token,"-")) - { - if (!parser->next(0)) return parsea6(); - if (isdecimal(parser->token)) // gestion des entiers - { - int i=-mtl_atoi(parser->token); - bc->addchar(OPint); - bc->addint(i); - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - parser->giveback(); - if (k=parsea6()) return k; - bc->addchar(OPneg); - return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); - } - else if (!strcmp(parser->token,"~")) - { - if (k=parsea6()) return k; - bc->addchar(OPnot); - return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); - } - parser->giveback(); - return parseterm(); + int k; + + if (!parser->next(0)) + { + return parseterm(); + } + + if (!strcmp(parser->token,"-")) + { + if (!parser->next(0)) + { + return parsea6(); + } + if (isdecimal(parser->token)) // gestion des entiers + { + int i=-mtl_atoi(parser->token); + bc->addchar(OPint); + bc->addint(i); + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + parser->giveback(); + if (k=parsea6()) + { + return k; + } + bc->addchar(OPneg); + return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); + } + else if (!strcmp(parser->token,"~")) + { + if (k=parsea6()) + { + return k; + } + bc->addchar(OPnot); + return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); + } + parser->giveback(); + return parseterm(); } diff --git a/src/vcomp/compiler_term.cpp b/src/vcomp/compiler_term.cpp index 49a3746..4fd5a1d 100644 --- a/src/vcomp/compiler_term.cpp +++ b/src/vcomp/compiler_term.cpp @@ -20,611 +20,871 @@ // switch 8 / 32 bits void Compiler::bc_byte_or_int(int val,int opbyte,int opint) { - if ((val>=0)&&(val<=255)) - { - bc->addchar(opbyte); - bc->addchar(val); - } - else - { - bc->addchar(OPint); - bc->addint(val); - bc->addchar(opint); - } + if ((val>=0)&&(val<=255)) + { + bc->addchar(opbyte); + bc->addchar(val); + } + else + { + bc->addchar(OPint); + bc->addint(val); + bc->addchar(opint); + } } void Compiler::bcint_byte_or_int(int val) { - if ((val>=0)&&(val<=255)) - { - bc->addchar(OPintb); - bc->addchar(val); - } - else - { - bc->addchar(OPint); - bc->addint(val); - } + if ((val>=0)&&(val<=255)) + { + bc->addchar(OPintb); + bc->addchar(val); + } + else + { + bc->addchar(OPint); + bc->addint(val); + } } int Compiler::parseterm() { - int k; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"(")) // gestion des parenthèses - { - if (k=parseprogram()) return k; - return parser->parsekeyword(")"); - } - else if (!strcmp(parser->token,"[")) // gestion des tuples - { - if (parser->next(0)) - { - if (islabel(parser->token)) - { - intptr_t *p; - if ((p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) - return parsefields(p); - } - parser->giveback(); - } - int nval=0; - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"]")) - { - bc_byte_or_int(nval,OPdeftabb,OPdeftab); - return createnodetuple(nval); - } - parser->giveback(); - if (k=parseexpression()) return k; - nval++; - } - } - else if (!strcmp(parser->token,"{")) // gestion des tableaux - { - int nval=0; - if (k=createnodetype(TYPENAME_TAB)) return k; - if (k=createnodetype(TYPENAME_UNDEF)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - intptr_t* p=VALTOPNT(STACKPULL(m)); - - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"}")) - { - bc_byte_or_int(nval,OPdeftabb,OPdeftab); - return 0; - } - parser->giveback(); - if (k=parseexpression()) return k; - if (k=unif(VALTOPNT(STACKGET(m,0)),p)) return k; - STACKDROP(m); - nval++; - } - } - else if (!strcmp(parser->token,"if")) // gestion du if - return parseif(); - else if (!strcmp(parser->token,"let")) // gestion du let - return parselet(); - else if (!strcmp(parser->token,"set")) // gestion du set - return parseset(); - else if (!strcmp(parser->token,"while")) // gestion du while - return parsewhile(); - else if (!strcmp(parser->token,"for")) // gestion du for - return parsefor(); - else if (!strcmp(parser->token,"match")) // gestion du match - return parsematch(); - else if (!strcmp(parser->token,"call")) // gestion du while - return parsecall(); - else if (!strcmp(parser->token,"update")) // gestion du update - return parseupdate(); - else if (!strcmp(parser->token,"nil")) // gestion du nil - { - bc->addchar(OPnil); - return createnodetype(TYPENAME_UNDEF); - } - else if (!strcmp(parser->token,"'")) // gestion des 'char - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : 'char expected (found EOF)\n"); - return MTLERR_SN; - } - bcint_byte_or_int(parser->token[0]&255); - if (k=parser->parsekeyword("'")) return k; - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if (islabel(parser->token)) // gestion des appels de fonctions ou références - return parseref(); - else if (isdecimal(parser->token)) // gestion des entiers - { - int i=mtl_atoi(parser->token); - bcint_byte_or_int(i); - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if ((parser->token[0]=='0')&&(parser->token[1]=='x') - &&(ishexadecimal(parser->token+2))) // gestion des entiers - { - int i=mtl_htoi(parser->token+2); - bcint_byte_or_int(i); - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if (parser->token[0]=='"') // gestion des chaines - { - if (k=parser->getstring(m,'"')) return k; - int val=INTTOVAL(nblabels(globals)); - if (k=addlabel(globals,"",val,STACKGET(m,0))) return k; // enregistrement d'une nouvelle globale - STACKDROP(m); - bc_byte_or_int(VALTOINT(val),OPgetglobalb,OPgetglobal); - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_S)); - } - else if (!strcmp(parser->token,"#")) // gestion des pointeurs sur fonction statique ou dynamique - return parsepntfun(); - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); - return MTLERR_SN; - } + int k; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"(")) // gestion des parenthèses + { + if (k=parseprogram()) + { + return k; + } + return parser->parsekeyword(")"); + } + else if (!strcmp(parser->token,"[")) // gestion des tuples + { + if (parser->next(0)) + { + if (islabel(parser->token)) + { + intptr_t *p; + if ((p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) + { + return parsefields(p); + } + } + parser->giveback(); + } + int nval=0; + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"]")) + { + bc_byte_or_int(nval,OPdeftabb,OPdeftab); + return createnodetuple(nval); + } + parser->giveback(); + if (k=parseexpression()) + { + return k; + } + nval++; + } + } + else if (!strcmp(parser->token,"{")) // gestion des tableaux + { + int nval=0; + if (k=createnodetype(TYPENAME_TAB)) + { + return k; + } + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + intptr_t* p=VALTOPNT(STACKPULL(m)); + + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"}")) + { + bc_byte_or_int(nval,OPdeftabb,OPdeftab); + return 0; + } + parser->giveback(); + if (k=parseexpression()) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,0)),p)) + { + return k; + } + STACKDROP(m); + nval++; + } + } + else if (!strcmp(parser->token,"if")) // gestion du if + { + return parseif(); + } + else if (!strcmp(parser->token,"let")) // gestion du let + { + return parselet(); + } + else if (!strcmp(parser->token,"set")) // gestion du set + { + return parseset(); + } + else if (!strcmp(parser->token,"while")) // gestion du while + { + return parsewhile(); + } + else if (!strcmp(parser->token,"for")) // gestion du for + { + return parsefor(); + } + else if (!strcmp(parser->token,"match")) // gestion du match + { + return parsematch(); + } + else if (!strcmp(parser->token,"call")) // gestion du while + { + return parsecall(); + } + else if (!strcmp(parser->token,"update")) // gestion du update + { + return parseupdate(); + } + else if (!strcmp(parser->token,"nil")) // gestion du nil + { + bc->addchar(OPnil); + return createnodetype(TYPENAME_UNDEF); + } + else if (!strcmp(parser->token,"'")) // gestion des 'char + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : 'char expected (found EOF)\n"); + return MTLERR_SN; + } + bcint_byte_or_int(parser->token[0]&255); + if (k=parser->parsekeyword("'")) + { + return k; + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if (islabel(parser->token)) // gestion des appels de fonctions ou références + { + return parseref(); + } + else if (isdecimal(parser->token)) // gestion des entiers + { + int i=mtl_atoi(parser->token); + bcint_byte_or_int(i); + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if ((parser->token[0]=='0')&&(parser->token[1]=='x') + &&(ishexadecimal(parser->token+2))) // gestion des entiers + { + int i=mtl_htoi(parser->token+2); + bcint_byte_or_int(i); + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if (parser->token[0]=='"') // gestion des chaines + { + if (k=parser->getstring(m,'"')) + { + return k; + } + int val=INTTOVAL(nblabels(globals)); + if (k=addlabel(globals,"",val,STACKGET(m,0))) + { + return k; // enregistrement d'une nouvelle globale + } + STACKDROP(m); + bc_byte_or_int(VALTOINT(val),OPgetglobalb,OPgetglobal); + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_S)); + } + else if (!strcmp(parser->token,"#")) // gestion des pointeurs sur fonction statique ou dynamique + { + return parsepntfun(); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); + return MTLERR_SN; + } } // parsing de la création d'une structure // p contient la référence du premier champ int Compiler::parsefields(intptr_t* p) { - int k; - intptr_t* type=VALTOPNT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_TYPE)); - int n=VALTOINT(TABGET(type,REF_VAL)); - bc_byte_or_int(n,OPmktabb,OPmktab); // création de la structure - if (k=copytype(VALTOPNT(TABGET(type,REF_TYPE)))) return k; // création du type - - int loop=1; - while(loop) - { - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; // création du type du champ - if (k=unif(VALTOPNT(STACKGET(m,1)), - VALTOPNT(TABGET(argsfromfun(VALTOPNT(STACKGET(m,0))),TYPEHEADER_LENGTH)) )) return k; - STACKSET(m,0,TABGET(VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH+1)); - - if (k=parser->parsekeyword(":")) return k; - if (k=parseexpression()) return k; - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROPN(m,2); - bc_byte_or_int( VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)), OPsetstructb,OPsetstruct); - - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"]")) - { - return 0; - } - loop=0; - if (islabel(parser->token)) - { - if ((p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) - loop=1; - } - } - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found '%s')\n",parser->token); - return MTLERR_SN; + int k; + intptr_t* type=VALTOPNT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_TYPE)); + int n=VALTOINT(TABGET(type,REF_VAL)); + bc_byte_or_int(n,OPmktabb,OPmktab); // création de la structure + if (k=copytype(VALTOPNT(TABGET(type,REF_TYPE)))) + { + return k; // création du type + } + + int loop=1; + while(loop) + { + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; // création du type du champ + } + if (k=unif(VALTOPNT(STACKGET(m,1)), + VALTOPNT(TABGET(argsfromfun(VALTOPNT(STACKGET(m,0))),TYPEHEADER_LENGTH)) )) + { + return k; + } + STACKSET(m,0,TABGET(VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH+1)); + + if (k=parser->parsekeyword(":")) + { + return k; + } + if (k=parseexpression()) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROPN(m,2); + bc_byte_or_int( VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)), OPsetstructb,OPsetstruct); + + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"]")) + { + return 0; + } + loop=0; + if (islabel(parser->token)) + { + if ((p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) + { + loop=1; + } + } + } + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found '%s')\n",parser->token); + return MTLERR_SN; } // parsing d'une référence quelconque (déjà présente dans parser->token) int Compiler::parseref() { - int k; - - int val; - intptr_t ref; - if (!searchlabel_byname(locals,parser->token,&val,&ref)) // recherche dans les variables locales - { - bc_byte_or_int(VALTOINT(val),OPgetlocalb,OPgetlocal); - if (k=STACKPUSH(m,ref)) return k; - return parsegetpoint(); - } - val=-1; - - intptr_t *p; - if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - { - ref=PNTTOVAL(p); - int code=VALTOINT(TABGET(p,REF_CODE)); - if (code==CODE_CONS0) - { - bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); - bc->addchar(OPdeftabb); - bc->addchar(1); - return copytype(VALTOPNT(TABGET(VALTOPNT(TABGET(p,REF_TYPE)),TYPEHEADER_LENGTH+1))); - } - else if (code==CODE_CONS) - { - bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); - if (k=parseexpression()) return k; - bc->addchar(OPdeftabb); - bc->addchar(2); - if (k=createnodetuple(1)) return k; - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - return unif_argfun(); - } - val=0; - } - - if (val!=-1) - { - intptr_t* p=VALTOPNT(ref); - int code=VALTOINT(TABGET(p,REF_CODE)); - - if (code>=0) // appel d'une fonction - { - int i; - for(i=0;iaddchar((char)VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FUN_BC))); - } - else - { // appel normal - bcint_byte_or_int(VALTOINT(TABGET(p,REF_PACKAGE))); - bc->addchar(OPexec); - } - if (k=createnodetuple(code)) return k; - if (p!=newref) - { - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - } - else if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) return k; - return unif_argfun(); - } - else if (code==CODE_VAR || code==CODE_CONST) // lecture d'une référence - { - bc_byte_or_int(VALTOINT(TABGET(p,REF_PACKAGE)),OPgetglobalb,OPgetglobal); - if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) return k; - return parsegetpoint(); - } - } - - PRINTF(m)(LOG_COMPILER,"Compiler : unknown label '%s'\n",parser->token); - return MTLERR_SN; + int k; + + int val; + intptr_t ref; + if (!searchlabel_byname(locals,parser->token,&val,&ref)) // recherche dans les variables locales + { + bc_byte_or_int(VALTOINT(val),OPgetlocalb,OPgetlocal); + if (k=STACKPUSH(m,ref)) + { + return k; + } + return parsegetpoint(); + } + val=-1; + + intptr_t *p; + if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + { + ref=PNTTOVAL(p); + int code=VALTOINT(TABGET(p,REF_CODE)); + if (code==CODE_CONS0) + { + bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); + bc->addchar(OPdeftabb); + bc->addchar(1); + return copytype(VALTOPNT(TABGET(VALTOPNT(TABGET(p,REF_TYPE)),TYPEHEADER_LENGTH+1))); + } + else if (code==CODE_CONS) + { + bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); + if (k=parseexpression()) + { + return k; + } + bc->addchar(OPdeftabb); + bc->addchar(2); + if (k=createnodetuple(1)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + return unif_argfun(); + } + val=0; + } + + if (val!=-1) + { + intptr_t* p=VALTOPNT(ref); + int code=VALTOINT(TABGET(p,REF_CODE)); + + if (code>=0) // appel d'une fonction + { + int i; + for(i=0; iaddchar((char)VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FUN_BC))); + } + else + { + // appel normal + bcint_byte_or_int(VALTOINT(TABGET(p,REF_PACKAGE))); + bc->addchar(OPexec); + } + if (k=createnodetuple(code)) + { + return k; + } + if (p!=newref) + { + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + } + else if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) + { + return k; + } + return unif_argfun(); + } + else if (code==CODE_VAR || code==CODE_CONST) // lecture d'une référence + { + bc_byte_or_int(VALTOINT(TABGET(p,REF_PACKAGE)),OPgetglobalb,OPgetglobal); + if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) + { + return k; + } + return parsegetpoint(); + } + } + + PRINTF(m)(LOG_COMPILER,"Compiler : unknown label '%s'\n",parser->token); + return MTLERR_SN; } // parsing (.Term/champ)* en lecture int Compiler::parsegetpoint() { - int k; - while(1) - { - if (!parser->next(0)) return 0; - if (strcmp(parser->token,".")) - { - parser->giveback(); - return 0; - } - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : expression or field name expected (found EOF)\n"); - return MTLERR_SN; - } - - intptr_t* p; - if ((islabel(parser->token)) - &&(p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) - { - bc_byte_or_int(VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)),OPfetchb,OPfetch); - if (k=createnodetuple(1)) return k; - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - if (k=unif_argfun()) return k; - } - else - { - parser->giveback(); - if (k=parseterm()) return k; - bc->addchar(OPfetch); - if (k=createnodetuple(2)) return k; - if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__tab_u0_I__u0)))) return k; - if (k=unif_argfun()) return k; - } - } + int k; + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,".")) + { + parser->giveback(); + return 0; + } + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : expression or field name expected (found EOF)\n"); + return MTLERR_SN; + } + + intptr_t* p; + if ((islabel(parser->token)) + &&(p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) + { + bc_byte_or_int(VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)),OPfetchb,OPfetch); + if (k=createnodetuple(1)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } + } + else + { + parser->giveback(); + if (k=parseterm()) + { + return k; + } + bc->addchar(OPfetch); + if (k=createnodetuple(2)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__tab_u0_I__u0)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } + } + } } // parsing du if ... then ... else (le 'if' a déjà été lu) int Compiler::parseif() { - int k; + int k; - if (k=parseexpression()) return k; // lire la condition + if (k=parseexpression()) + { + return k; // lire la condition + } - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - STACKDROP(m); - if (k=parser->parsekeyword("then")) return k; + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + STACKDROP(m); + if (k=parser->parsekeyword("then")) + { + return k; + } - bc->addchar(OPelse); - int bc_else=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut 'else' + bc->addchar(OPelse); + int bc_else=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut 'else' - if (k=parseexpression()) return k; // lire l'expression 'then' + if (k=parseexpression()) + { + return k; // lire l'expression 'then' + } - bc->addchar(OPgoto); - int bc_goto=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut du 'then' + bc->addchar(OPgoto); + int bc_goto=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut du 'then' - bc->setshort(bc_else,bc->getsize()); // on règle le saut du else + bc->setshort(bc_else,bc->getsize()); // on règle le saut du else - if ((parser->next(0))&&(!strcmp(parser->token,"else"))) - { - if (k=parseexpression()) return k; // lire l'expression 'else' - } - else // pas de else, on remplace par nil - { - parser->giveback(); - bc->addchar(OPnil); - if (k=createnodetype(TYPENAME_UNDEF)) return k; - } - bc->setshort(bc_goto,bc->getsize()); // on règle le saut du 'goto' + if ((parser->next(0))&&(!strcmp(parser->token,"else"))) + { + if (k=parseexpression()) + { + return k; // lire l'expression 'else' + } + } + else // pas de else, on remplace par nil + { + parser->giveback(); + bc->addchar(OPnil); + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + } + bc->setshort(bc_goto,bc->getsize()); // on règle le saut du 'goto' - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - return 0; + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + return 0; } // parsing du while ... do ... (le 'while' a déjà été lu) int Compiler::parsewhile() { - int k; + int k; - bc->addchar(OPnil); // on empile le premier résultat + bc->addchar(OPnil); // on empile le premier résultat - int bc_while=bc->getsize(); // on retient la position pour le saut 'while' - if (k=parseexpression()) return k; // lire la condition - - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - STACKDROP(m); - if (k=parser->parsekeyword("do")) return k; - bc->addchar(OPelse); - int bc_end=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut 'end' - bc->addchar(OPdrop); // on ignore le résultat précédent + int bc_while=bc->getsize(); // on retient la position pour le saut 'while' + if (k=parseexpression()) + { + return k; // lire la condition + } - if (k=parseexpression()) return k; // lire l'expression 'do' + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + STACKDROP(m); + if (k=parser->parsekeyword("do")) + { + return k; + } + bc->addchar(OPelse); + int bc_end=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut 'end' + bc->addchar(OPdrop); // on ignore le résultat précédent + + if (k=parseexpression()) + { + return k; // lire l'expression 'do' + } - bc->addchar(OPgoto); - bc->addshort(bc_while); // on retourne à la condition + bc->addchar(OPgoto); + bc->addshort(bc_while); // on retourne à la condition - bc->setshort(bc_end,bc->getsize()); // on règle le saut 'end' + bc->setshort(bc_end,bc->getsize()); // on règle le saut 'end' - return 0; + return 0; } // parsing du for ...=... ; cond ; nextvalue do ... (le 'for' a déjà été lu) int Compiler::parsefor() { - int k; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found EOF)\n"); - return MTLERR_SN; - } - if (!islabel(parser->token)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - if (k=STRPUSH(m,parser->token)) return k; - // [name_it] - if (k=parser->parsekeyword("=")) return k; - if (k=parseexpression()) return k; // lire la valeur d'initialisation - // [type_init name_it] - int i=nblabels(locals); - if (k=addlabel(locals,STRSTART(VALTOPNT(STACKGET(m,1))),INTTOVAL(i),STACKGET(m,0))) return k; - if (i+1>nblocals) nblocals=i+1; // nombre maximum de variables locales - - // [type_init name_it] - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); - - if (k=parser->parsekeyword(";")) return k; - - bc->addchar(OPnil); // on empile le premier résultat - - int bc_cond=bc->getsize(); // on retient la position pour le saut 'while' - if (k=parseexpression()) return k; // lire la condition - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - STACKDROP(m); - - bc->addchar(OPelse); - int bc_end=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut 'end' - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : 'do' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,";")) - { - bc->addchar(OPgoto); - int bc_expr=bc->getsize(); - bc->addshort(0); // on prépare le champ pour le saut 'expr' - - int bc_next=bc->getsize(); - if (k=parseexpression()) return k; // lire la valeur next - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); // mise à jour de l'itérateur - bc->addchar(OPgoto); - bc->addshort(bc_cond); - - if (k=parser->parsekeyword("do")) return k; - - bc->setshort(bc_expr,bc->getsize()); - bc->addchar(OPdrop); // on ignore le résultat précédent - if (k=parseexpression()) return k; // lire la valeur itérée - bc->addchar(OPgoto); - bc->addshort(bc_next); // on retourne à l'itérateur - } - else - { - parser->giveback(); - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) return k; - if (k=parser->parsekeyword("do")) return k; - bc->addchar(OPdrop); // on ignore le résultat précédent - if (k=parseexpression()) return k; // lire la valeur itérée - bc_byte_or_int(i,OPgetlocalb,OPgetlocal); // i+1 - bc->addchar(OPintb); - bc->addchar(1); - bc->addchar(OPadd); - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); // mise à jour de l'itérateur - bc->addchar(OPgoto); - bc->addshort(bc_cond); // on retourne à la condition - } - bc->setshort(bc_end,bc->getsize()); - - removenlabels(locals,1); - - STACKSET(m,2,STACKGET(m,0)); - STACKDROPN(m,2); - - return 0; + int k; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found EOF)\n"); + return MTLERR_SN; + } + if (!islabel(parser->token)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : label expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + if (k=STRPUSH(m,parser->token)) + { + return k; + } + // [name_it] + if (k=parser->parsekeyword("=")) + { + return k; + } + if (k=parseexpression()) + { + return k; // lire la valeur d'initialisation + } + // [type_init name_it] + int i=nblabels(locals); + if (k=addlabel(locals,STRSTART(VALTOPNT(STACKGET(m,1))),INTTOVAL(i),STACKGET(m,0))) + { + return k; + } + if (i+1>nblocals) + { + nblocals=i+1; // nombre maximum de variables locales + } + + // [type_init name_it] + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); + + if (k=parser->parsekeyword(";")) + { + return k; + } + + bc->addchar(OPnil); // on empile le premier résultat + + int bc_cond=bc->getsize(); // on retient la position pour le saut 'while' + if (k=parseexpression()) + { + return k; // lire la condition + } + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + STACKDROP(m); + + bc->addchar(OPelse); + int bc_end=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut 'end' + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : 'do' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,";")) + { + bc->addchar(OPgoto); + int bc_expr=bc->getsize(); + bc->addshort(0); // on prépare le champ pour le saut 'expr' + + int bc_next=bc->getsize(); + if (k=parseexpression()) + { + return k; // lire la valeur next + } + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); // mise à jour de l'itérateur + bc->addchar(OPgoto); + bc->addshort(bc_cond); + + if (k=parser->parsekeyword("do")) + { + return k; + } + + bc->setshort(bc_expr,bc->getsize()); + bc->addchar(OPdrop); // on ignore le résultat précédent + if (k=parseexpression()) + { + return k; // lire la valeur itérée + } + bc->addchar(OPgoto); + bc->addshort(bc_next); // on retourne à l'itérateur + } + else + { + parser->giveback(); + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I)))) + { + return k; + } + if (k=parser->parsekeyword("do")) + { + return k; + } + bc->addchar(OPdrop); // on ignore le résultat précédent + if (k=parseexpression()) + { + return k; // lire la valeur itérée + } + bc_byte_or_int(i,OPgetlocalb,OPgetlocal); // i+1 + bc->addchar(OPintb); + bc->addchar(1); + bc->addchar(OPadd); + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); // mise à jour de l'itérateur + bc->addchar(OPgoto); + bc->addshort(bc_cond); // on retourne à la condition + } + bc->setshort(bc_end,bc->getsize()); + + removenlabels(locals,1); + + STACKSET(m,2,STACKGET(m,0)); + STACKDROPN(m,2); + + return 0; } // parsing du match ... with ... (le 'match' a déjà été lu) int Compiler::parsematch() { - int k; + int k; - if (k=parseexpression()) return k; // lire l'objet - if (k=createnodetype(TYPENAME_UNDEF)) return k; // préparer le type du résultat - // [result src] + if (k=parseexpression()) + { + return k; // lire l'objet + } + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; // préparer le type du résultat + } + // [result src] - if (k=parser->parsekeyword("with")) return k; + if (k=parser->parsekeyword("with")) + { + return k; + } - intptr_t end; - if (k=parsematchcons(&end)) return k; - STACKSET(m,1,STACKGET(m,0)); - STACKDROP(m); - return 0; + intptr_t end; + if (k=parsematchcons(&end)) + { + return k; + } + STACKSET(m,1,STACKGET(m,0)); + STACKDROP(m); + return 0; } int Compiler::parsematchcons(intptr_t* end) { - int k; - if (k=parser->parsekeyword("(")) return k; - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found EOF)\n"); - return MTLERR_SN; - } - intptr_t* p=NULL; - if (!strcmp(parser->token,"_")) // cas par défaut - { - bc->addchar(OPdrop); - if (k=parser->parsekeyword("->")) return k; - if (k=parseprogram()) return k; // lire le résultat - // [type_result result src] - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - // [result src] - if (k=parser->parsekeyword(")")) return k; - *end=bc->getsize(); - return 0; - } - if ((islabel(parser->token)) // constructeur - &&(p=searchref(PNTTOVAL(newpackage),parser->token))) // recherche dans les autres globales - { - int code=VALTOINT(TABGET(p,REF_CODE)); - if ((code==CODE_CONS)||(code==CODE_CONS0)) - { - // [result src] - bc->addchar(OPfirst); - bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); - bc->addchar(OPeq); - bc->addchar(OPelse); - int bc_else=bc->getsize(); // on retient la position pour le saut 'else' - bc->addshort(0); - - int nloc=nblabels(locals); // on sauvegarde le nombre de locales - if (code==CODE_CONS) - { - bc->addchar(OPfetchb); - bc->addchar(1); - if (k=parselocals()) return k; - if (k=createnodetuple(1)) return k; - } - else - { - bc->addchar(OPdrop); - if (k=createnodetuple(0)) return k; - } - // [[locals] result src] - int newnloc=nblabels(locals); // on sauvegarde le nombre de locales - if (newnloc>nblocals) nblocals=newnloc; // nombre maximum de variables locales - int nloctodelete=newnloc-nloc; // combien de variables locales ont été crées ? - - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - if (k=unif_argfun()) return k; - // [type_src result src] - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,2)))) return k; - STACKDROP(m); - // [result src] - if (k=parser->parsekeyword("->")) return k; - if (k=parseprogram()) return k; // lire le résultat - // [type_result result src] - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - // [result src] - removenlabels(locals,nloctodelete); - if (k=parser->parsekeyword(")")) return k; - bc->addchar(OPgoto); - int bc_goto=bc->getsize(); // on retient la position pour le saut 'else' - bc->addshort(0); - bc->setshort(bc_else,bc->getsize()); - if ((parser->next(0))&&(!strcmp(parser->token,"|"))) - { - if (k=parsematchcons(end)) return k; - bc->setshort(bc_goto,*end); - return 0; - } - parser->giveback(); - bc->addchar(OPdrop); - bc->addchar(OPnil); - *end=bc->getsize(); - bc->setshort(bc_goto,*end); - return 0; - } - } - PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found '%s')\n",parser->token); - return MTLERR_SN; + int k; + if (k=parser->parsekeyword("(")) + { + return k; + } + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found EOF)\n"); + return MTLERR_SN; + } + intptr_t* p=NULL; + if (!strcmp(parser->token,"_")) // cas par défaut + { + bc->addchar(OPdrop); + if (k=parser->parsekeyword("->")) + { + return k; + } + if (k=parseprogram()) + { + return k; // lire le résultat + } + // [type_result result src] + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + // [result src] + if (k=parser->parsekeyword(")")) + { + return k; + } + *end=bc->getsize(); + return 0; + } + if ((islabel(parser->token)) // constructeur + &&(p=searchref(PNTTOVAL(newpackage),parser->token))) // recherche dans les autres globales + { + int code=VALTOINT(TABGET(p,REF_CODE)); + if ((code==CODE_CONS)||(code==CODE_CONS0)) + { + // [result src] + bc->addchar(OPfirst); + bcint_byte_or_int(VALTOINT(TABGET(p,REF_VAL))); + bc->addchar(OPeq); + bc->addchar(OPelse); + int bc_else=bc->getsize(); // on retient la position pour le saut 'else' + bc->addshort(0); + + int nloc=nblabels(locals); // on sauvegarde le nombre de locales + if (code==CODE_CONS) + { + bc->addchar(OPfetchb); + bc->addchar(1); + if (k=parselocals()) + { + return k; + } + if (k=createnodetuple(1)) + { + return k; + } + } + else + { + bc->addchar(OPdrop); + if (k=createnodetuple(0)) + { + return k; + } + } + // [[locals] result src] + int newnloc=nblabels(locals); // on sauvegarde le nombre de locales + if (newnloc>nblocals) + { + nblocals=newnloc; // nombre maximum de variables locales + } + int nloctodelete=newnloc-nloc; // combien de variables locales ont été crées ? + + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } + // [type_src result src] + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,2)))) + { + return k; + } + STACKDROP(m); + // [result src] + if (k=parser->parsekeyword("->")) + { + return k; + } + if (k=parseprogram()) + { + return k; // lire le résultat + } + // [type_result result src] + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + // [result src] + removenlabels(locals,nloctodelete); + if (k=parser->parsekeyword(")")) + { + return k; + } + bc->addchar(OPgoto); + int bc_goto=bc->getsize(); // on retient la position pour le saut 'else' + bc->addshort(0); + bc->setshort(bc_else,bc->getsize()); + if ((parser->next(0))&&(!strcmp(parser->token,"|"))) + { + if (k=parsematchcons(end)) + { + return k; + } + bc->setshort(bc_goto,*end); + return 0; + } + parser->giveback(); + bc->addchar(OPdrop); + bc->addchar(OPnil); + *end=bc->getsize(); + bc->setshort(bc_goto,*end); + return 0; + } + } + PRINTF(m)(LOG_COMPILER,"Compiler : constructor expected (found '%s')\n",parser->token); + return MTLERR_SN; } @@ -632,426 +892,609 @@ int Compiler::parsematchcons(intptr_t* end) intptr_t Compiler::parselet() { - intptr_t k; - //~ PRINTF(m)(LOG_COMPILER,"Compiler : let===================================\n"); - //~ asm volatile ("int3;"); - if (k=parseexpression()) return k; // lire la source - if (k=parser->parsekeyword("->")) return k; + intptr_t k; + //~ PRINTF(m)(LOG_COMPILER,"Compiler : let===================================\n"); + //~ asm volatile ("int3;"); + if (k=parseexpression()) + { + return k; // lire la source + } + if (k=parser->parsekeyword("->")) + { + return k; + } - int nloc=nblabels(locals); // on sauvegarde le nombre de locales + int nloc=nblabels(locals); // on sauvegarde le nombre de locales - if (k=parselocals()) return k; + if (k=parselocals()) + { + return k; + } - int newnloc=nblabels(locals); // on sauvegarde le nombre de locales - if (newnloc>nblocals) nblocals=newnloc; // nombre maximum de variables locales - int nloctodelete=newnloc-nloc; // combien de variables locales ont été crées ? + int newnloc=nblabels(locals); // on sauvegarde le nombre de locales + if (newnloc>nblocals) + { + nblocals=newnloc; // nombre maximum de variables locales + } + int nloctodelete=newnloc-nloc; // combien de variables locales ont été crées ? - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROPN(m,2); - if (k=parser->parsekeyword("in")) return k; + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROPN(m,2); + if (k=parser->parsekeyword("in")) + { + return k; + } - if (k=parseexpression()) return k; // lire l'expression du let - removenlabels(locals,nloctodelete); - return 0; + if (k=parseexpression()) + { + return k; // lire l'expression du let + } + removenlabels(locals,nloctodelete); + return 0; } // parsing de variables locales structurées int Compiler::parselocals() { - int k; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); - return MTLERR_SN; - } - else if (!strcmp(parser->token,"[")) - { - int n=0; - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"]")) - { - bc->addchar(OPdrop); - return createnodetuple(n); - } - else if (!strcmp(parser->token,"_")) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - n++; - } - else - { - parser->giveback(); - bc->addchar(OPdup); - bc_byte_or_int(n,OPfetchb,OPfetch); - if (k=parselocals()) return k; - n++; - } - } - } - else if (!strcmp(parser->token,"(")) - { - if (k=createnodetype(TYPENAME_LIST)) return k; - intptr_t* plist=VALTOPNT(STACKGET(m,0)); - if (k=createnodetype(TYPENAME_UNDEF)) return k; - intptr_t* pval=VALTOPNT(STACKGET(m,0)); - TABSET(m,plist,TYPEHEADER_LENGTH,STACKGET(m,0)); - - while(1) // la liste est dans la pile - { - if (!parser->next(0)) return parselocals(); - if (!strcmp(parser->token,"_")) - { - if ((parser->next(0))&&(!strcmp(parser->token,")"))) - { - bc->addchar(OPdrop); - STACKDROP(m); - return 0; - } - parser->giveback(); - } - else if (islabel(parser->token)) - { - int i=nblabels(locals); - if (k=createnodetype(TYPENAME_UNDEF)) return k; - if (k=addlabel(locals,parser->token,INTTOVAL(i),STACKGET(m,0))) return k; - if ((parser->next(0))&&(!strcmp(parser->token,")"))) - { - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); - if (k=unif(VALTOPNT(STACKGET(m,0)),plist)) return k; - STACKDROPN(m,2); - return 0; - } - parser->giveback(); - bc->addchar(OPfirst); - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); - if (k=unif(VALTOPNT(STACKGET(m,0)),pval)) return k; - STACKDROP(m); - } - else - { - parser->giveback(); - bc->addchar(OPfirst); - if (k=parselocals()) return k; - if (k=unif(VALTOPNT(STACKGET(m,0)),pval)) return k; - STACKDROP(m); - } - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '::' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"::")) - { - bc->addchar(OPfetchb); - bc->addchar(1); - } - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : '::' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - } - } - else if (!strcmp(parser->token,"_")) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - bc->addchar(OPdrop); - return 0; - } - else if (islabel(parser->token)) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - int i=nblabels(locals); - bc_byte_or_int(i,OPsetlocalb,OPsetlocal); - - if (k=addlabel(locals,parser->token,INTTOVAL(i),STACKGET(m,0))) return k; - return 0; - } - PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); - return MTLERR_SN; + int k; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); + return MTLERR_SN; + } + else if (!strcmp(parser->token,"[")) + { + int n=0; + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"]")) + { + bc->addchar(OPdrop); + return createnodetuple(n); + } + else if (!strcmp(parser->token,"_")) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + n++; + } + else + { + parser->giveback(); + bc->addchar(OPdup); + bc_byte_or_int(n,OPfetchb,OPfetch); + if (k=parselocals()) + { + return k; + } + n++; + } + } + } + else if (!strcmp(parser->token,"(")) + { + if (k=createnodetype(TYPENAME_LIST)) + { + return k; + } + intptr_t* plist=VALTOPNT(STACKGET(m,0)); + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + intptr_t* pval=VALTOPNT(STACKGET(m,0)); + TABSET(m,plist,TYPEHEADER_LENGTH,STACKGET(m,0)); + + while(1) // la liste est dans la pile + { + if (!parser->next(0)) + { + return parselocals(); + } + if (!strcmp(parser->token,"_")) + { + if ((parser->next(0))&&(!strcmp(parser->token,")"))) + { + bc->addchar(OPdrop); + STACKDROP(m); + return 0; + } + parser->giveback(); + } + else if (islabel(parser->token)) + { + int i=nblabels(locals); + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + if (k=addlabel(locals,parser->token,INTTOVAL(i),STACKGET(m,0))) + { + return k; + } + if ((parser->next(0))&&(!strcmp(parser->token,")"))) + { + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); + if (k=unif(VALTOPNT(STACKGET(m,0)),plist)) + { + return k; + } + STACKDROPN(m,2); + return 0; + } + parser->giveback(); + bc->addchar(OPfirst); + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); + if (k=unif(VALTOPNT(STACKGET(m,0)),pval)) + { + return k; + } + STACKDROP(m); + } + else + { + parser->giveback(); + bc->addchar(OPfirst); + if (k=parselocals()) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,0)),pval)) + { + return k; + } + STACKDROP(m); + } + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '::' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"::")) + { + bc->addchar(OPfetchb); + bc->addchar(1); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : '::' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + } + } + else if (!strcmp(parser->token,"_")) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + bc->addchar(OPdrop); + return 0; + } + else if (islabel(parser->token)) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + int i=nblabels(locals); + bc_byte_or_int(i,OPsetlocalb,OPsetlocal); + + if (k=addlabel(locals,parser->token,INTTOVAL(i),STACKGET(m,0))) + { + return k; + } + return 0; + } + PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); + return MTLERR_SN; } // parsing du update ... with ... (le 'update' a déjà été lu) int Compiler::parseupdate() { - int k; + int k; - if (k=parseexpression()) return k; // lire la source - if (k=parser->parsekeyword("with")) return k; - if (k=parser->parsekeyword("[")) return k; + if (k=parseexpression()) + { + return k; // lire la source + } + if (k=parser->parsekeyword("with")) + { + return k; + } + if (k=parser->parsekeyword("[")) + { + return k; + } - if (k=parseupdatevals()) return k; + if (k=parseupdatevals()) + { + return k; + } - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - return 0; + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + return 0; } // parsing des valeurs d'un update (le premier '[' a déjà été lu) int Compiler::parseupdatevals() { - int k; - - int n=0; - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"[")) - { - bc->addchar(OPdup); - bc_byte_or_int(n,OPfetchb,OPfetch); - parseupdatevals(); - bc->addchar(OPdrop); - n++; - } - else if (!strcmp(parser->token,"]")) - { - return createnodetuple(n); - } - else if (!strcmp(parser->token,"_")) - { - if (k=createnodetype(TYPENAME_UNDEF)) return k; - n++; - } - else - { - parser->giveback(); - if (k=parseexpression()) return k; - bc_byte_or_int(n,OPsetstructb,OPsetstruct); - n++; - } - } + int k; + + int n=0; + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"[")) + { + bc->addchar(OPdup); + bc_byte_or_int(n,OPfetchb,OPfetch); + parseupdatevals(); + bc->addchar(OPdrop); + n++; + } + else if (!strcmp(parser->token,"]")) + { + return createnodetuple(n); + } + else if (!strcmp(parser->token,"_")) + { + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + n++; + } + else + { + parser->giveback(); + if (k=parseexpression()) + { + return k; + } + bc_byte_or_int(n,OPsetstructb,OPsetstruct); + n++; + } + } } // parsing d'un set ... = ... (le 'set' a déjà été lu) int Compiler::parseset() { - int k; - int val; - intptr_t ref; - int opstore=-1; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found EOF)\n"); - return MTLERR_SN; - } - if (!islabel(parser->token)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - - if (!searchlabel_byname(locals,parser->token,&val,&ref)) // recherche dans les variables locales - { - if (k=STACKPUSH(m,ref)) return k; - if (k=parsesetpoint(1,VALTOINT(val),&opstore)) return k; - } - else - { - val=-1; - intptr_t *p; - if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - { - ref=PNTTOVAL(p); - val=0; - } - if (val!=-1) - { - intptr_t* p=VALTOPNT(ref); - int code=VALTOINT(TABGET(p,REF_CODE)); - - if (code==CODE_VAR) // variable - { - if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) return k; - if (k=parsesetpoint(0,VALTOINT(TABGET(p,REF_PACKAGE)),&opstore)) return k; - - // la variable a été settée au moins une fois maintenant - TABSET(m,p,REF_SET,INTTOVAL(2)); - } - else if (code == CODE_CONST) // constante - { - PRINTF(m)(LOG_COMPILER,"Compiler : '%s' is a const, it cannot be set\n",parser->token); - return MTLERR_SN; - } - } - } - if (opstore==-1) - { - PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - if (k=parser->parsekeyword("=")) return k; - if (k=parseexpression()) return k; // lire la source - bc->addchar(opstore); - - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) return k; - STACKDROP(m); - return 0; + int k; + int val; + intptr_t ref; + int opstore=-1; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found EOF)\n"); + return MTLERR_SN; + } + if (!islabel(parser->token)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + + if (!searchlabel_byname(locals,parser->token,&val,&ref)) // recherche dans les variables locales + { + if (k=STACKPUSH(m,ref)) + { + return k; + } + if (k=parsesetpoint(1,VALTOINT(val),&opstore)) + { + return k; + } + } + else + { + val=-1; + intptr_t *p; + if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + { + ref=PNTTOVAL(p); + val=0; + } + if (val!=-1) + { + intptr_t* p=VALTOPNT(ref); + int code=VALTOINT(TABGET(p,REF_CODE)); + + if (code==CODE_VAR) // variable + { + if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) + { + return k; + } + if (k=parsesetpoint(0,VALTOINT(TABGET(p,REF_PACKAGE)),&opstore)) + { + return k; + } + + // la variable a été settée au moins une fois maintenant + TABSET(m,p,REF_SET,INTTOVAL(2)); + } + else if (code == CODE_CONST) // constante + { + PRINTF(m)(LOG_COMPILER,"Compiler : '%s' is a const, it cannot be set\n",parser->token); + return MTLERR_SN; + } + } + } + if (opstore==-1) + { + PRINTF(m)(LOG_COMPILER,"Compiler : reference expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + if (k=parser->parsekeyword("=")) + { + return k; + } + if (k=parseexpression()) + { + return k; // lire la source + } + bc->addchar(opstore); + + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,1)))) + { + return k; + } + STACKDROP(m); + return 0; } // parsing (.Term/champ)* en écriture int Compiler::parsesetpoint(int local,int ind,int* opstore) { - int k; - - if (!parser->next(0)) return 0; - if (strcmp(parser->token,".")) - { - bcint_byte_or_int(ind); - if (local) *opstore=OPsetlocal2; - else *opstore=OPsetglobal; - parser->giveback(); - return 0; - } - if (local) bc_byte_or_int(ind,OPgetlocalb,OPgetlocal); - else bc_byte_or_int(ind,OPgetglobalb,OPgetglobal); - *opstore=OPstore; - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : expression or field name expected (found EOF)\n"); - return MTLERR_SN; - } - - ind=-1; - intptr_t* p; - if ((islabel(parser->token)) - &&(p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) - { - ind=VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)); - if (k=createnodetuple(1)) return k; - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - if (k=unif_argfun()) return k; - } - else - { - parser->giveback(); - - if (k=parseterm()) return k; - if (k=createnodetuple(2)) return k; - if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__tab_u0_I__u0)))) return k; - if (k=unif_argfun()) return k; - } - if (!parser->next(0)) return 0; - if (strcmp(parser->token,".")) - { - parser->giveback(); - if (ind>=0) - { - bcint_byte_or_int(ind); - } - return 0; - } - if (ind>=0) bc_byte_or_int(ind,OPfetchb,OPfetch); - else bc->addchar(OPfetch); - } + int k; + + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,".")) + { + bcint_byte_or_int(ind); + if (local) + { + *opstore=OPsetlocal2; + } + else + { + *opstore=OPsetglobal; + } + parser->giveback(); + return 0; + } + if (local) + { + bc_byte_or_int(ind,OPgetlocalb,OPgetlocal); + } + else + { + bc_byte_or_int(ind,OPgetglobalb,OPgetglobal); + } + *opstore=OPstore; + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : expression or field name expected (found EOF)\n"); + return MTLERR_SN; + } + + ind=-1; + intptr_t* p; + if ((islabel(parser->token)) + &&(p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + &&(VALTOINT(TABGET(p,REF_CODE))==CODE_FIELD)) + { + ind=VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FIELD_NUM)); + if (k=createnodetuple(1)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } + } + else + { + parser->giveback(); + + if (k=parseterm()) + { + return k; + } + if (k=createnodetuple(2)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__tab_u0_I__u0)))) + { + return k; + } + if (k=unif_argfun()) + { + return k; + } + } + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,".")) + { + parser->giveback(); + if (ind>=0) + { + bcint_byte_or_int(ind); + } + return 0; + } + if (ind>=0) + { + bc_byte_or_int(ind,OPfetchb,OPfetch); + } + else + { + bc->addchar(OPfetch); + } + } } // parsing d'un pointeur de fonction int Compiler::parsepntfun() { - int k; - - int val; - int ref; - - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : function name expected (found EOF)\n"); - return MTLERR_SN; - } - val=-1; - intptr_t *p; - if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales - { - ref=PNTTOVAL(p); - val=0; - } - if (val!=-1) - { - intptr_t* p=VALTOPNT(ref); - int code=VALTOINT(TABGET(p,REF_CODE)); - - if (code>=0) - { - int v=0; - if (TABGET(p,REF_PACKAGE)!=NIL) v=VALTOINT(TABGET(p,REF_PACKAGE)); - else - { - char *ppp=parser->token; - v=-VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FUN_BC)); - } - // pointeur d'une fonction virtuelle - bcint_byte_or_int(v); - if (p!=newref) - { - if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) return k; - } - else if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) return k; - return 0; - } - } - - PRINTF(m)(LOG_COMPILER,"Compiler : function name expected (found '%s')\n",parser->token); - return MTLERR_SN; + int k; + + int val; + int ref; + + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : function name expected (found EOF)\n"); + return MTLERR_SN; + } + val=-1; + intptr_t *p; + if (p=searchref(PNTTOVAL(newpackage),parser->token)) // recherche dans les autres globales + { + ref=PNTTOVAL(p); + val=0; + } + if (val!=-1) + { + intptr_t* p=VALTOPNT(ref); + int code=VALTOINT(TABGET(p,REF_CODE)); + + if (code>=0) + { + int v=0; + if (TABGET(p,REF_PACKAGE)!=NIL) + { + v=VALTOINT(TABGET(p,REF_PACKAGE)); + } + else + { + char *ppp=parser->token; + v=-VALTOINT(TABGET(VALTOPNT(TABGET(p,REF_VAL)),FUN_BC)); + } + // pointeur d'une fonction virtuelle + bcint_byte_or_int(v); + if (p!=newref) + { + if (k=copytype(VALTOPNT(TABGET(p,REF_TYPE)))) + { + return k; + } + } + else if (k=STACKPUSH(m,TABGET(p,REF_TYPE))) + { + return k; + } + return 0; + } + } + + PRINTF(m)(LOG_COMPILER,"Compiler : function name expected (found '%s')\n",parser->token); + return MTLERR_SN; } // parsing du call ... ... (le 'call' a déjà été lu) int Compiler::parsecall() { - int k; - - if (k=parseexpression()) return k; // lire la fonction - - if ((parser->next(0))&&(!strcmp(parser->token,"["))) - { - int nval=0; - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"]")) - { - bc_byte_or_int(nval,OPcallrb,OPcallr); - if (k=createnodetuple(nval)) return k; - if (k=createnodetuple(2)) return k; - if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__fun_u0_u1_u0__u1)))) return k; - return unif_argfun(); - } - parser->giveback(); - if (k=parseexpression()) return k; - nval++; - } - } - else - { - parser->giveback(); - if (k=parseexpression()) return k; // lire les arguments - - bc->addchar(OPcall); - if (k=createnodetuple(2)) return k; - if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__fun_u0_u1_u0__u1)))) return k; - return unif_argfun(); - } + int k; + + if (k=parseexpression()) + { + return k; // lire la fonction + } + + if ((parser->next(0))&&(!strcmp(parser->token,"["))) + { + int nval=0; + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"]")) + { + bc_byte_or_int(nval,OPcallrb,OPcallr); + if (k=createnodetuple(nval)) + { + return k; + } + if (k=createnodetuple(2)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__fun_u0_u1_u0__u1)))) + { + return k; + } + return unif_argfun(); + } + parser->giveback(); + if (k=parseexpression()) + { + return k; + } + nval++; + } + } + else + { + parser->giveback(); + if (k=parseexpression()) + { + return k; // lire les arguments + } + + bc->addchar(OPcall); + if (k=createnodetuple(2)) + { + return k; + } + if (k=copytype(VALTOPNT(TABGET(stdtypes,STDTYPE_fun__fun_u0_u1_u0__u1)))) + { + return k; + } + return unif_argfun(); + } } diff --git a/src/vcomp/compiler_type.cpp b/src/vcomp/compiler_type.cpp index d7885b7..ae70f42 100644 --- a/src/vcomp/compiler_type.cpp +++ b/src/vcomp/compiler_type.cpp @@ -21,311 +21,461 @@ // création d'un noeud de type basique (éventuellement paramétrique) intptr_t Compiler::createnodetypecore(const char* name) { - intptr_t k; + intptr_t k; - intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+2); - if (!p) return MTLERR_OM; - if (STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM; + intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+2); + if (!p) + { + return MTLERR_OM; + } + if (STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } - if (k=STRPUSH(m,name)) return k; + if (k=STRPUSH(m,name)) + { + return k; + } - TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_CORE)); - TABSET(m,p,TYPEHEADER_LENGTH+1,STACKPULL(m)); - return 0; + TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_CORE)); + TABSET(m,p,TYPEHEADER_LENGTH+1,STACKPULL(m)); + return 0; } // création d'un noeud de type basique (éventuellement paramétrique) intptr_t Compiler::createnodetypecore(int name) { - intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+2); - if (!p) return MTLERR_OM; - if (STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM; + intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+2); + if (!p) + { + return MTLERR_OM; + } + if (STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } - TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_CORE)); - TABSET(m,p,TYPEHEADER_LENGTH+1,name); - return 0; + TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_CORE)); + TABSET(m,p,TYPEHEADER_LENGTH+1,name); + return 0; } // création d'un noeud de type non basique intptr_t Compiler::createnodetype(int type) { - int size=0; - if ((type==TYPENAME_UNDEF)||(type==TYPENAME_WEAK)) size=0; - if ((type==TYPENAME_LIST)||(type==TYPENAME_TAB)||(type==TYPENAME_REC)) size=1; - if (type==TYPENAME_FUN) size=2; + int size=0; + if ((type==TYPENAME_UNDEF)||(type==TYPENAME_WEAK)) + { + size=0; + } + if ((type==TYPENAME_LIST)||(type==TYPENAME_TAB)||(type==TYPENAME_REC)) + { + size=1; + } + if (type==TYPENAME_FUN) + { + size=2; + } - intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); - if (!p) return MTLERR_OM; - if (STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM; + intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); + if (!p) + { + return MTLERR_OM; + } + if (STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } - TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(type)); - return 0; + TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(type)); + return 0; } // création d'un noeud de type tuple (n éléments empilés) // empile le résultat intptr_t Compiler::createnodetuple(int size) { - intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); - if (!p) return MTLERR_OM; - TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_TUPLE)); - int i; for(i=size-1;i>=0;i--) TABSET(m,p,TYPEHEADER_LENGTH+i,STACKPULL(m)); - if (STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM; - return 0; + intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); + if (!p) + { + return MTLERR_OM; + } + TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_TUPLE)); + int i; + for(i=size-1; i>=0; i--) + { + TABSET(m,p,TYPEHEADER_LENGTH+i,STACKPULL(m)); + } + if (STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } + return 0; } // création d'un noeud de type tuple dans la compilation de valeurs (n éléments empilés au rangs 0, 2, 4, 2n-2) // empile le résultat, sans dépiler les valeurs du tuple intptr_t Compiler::createnodetupleval(int size) { - intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); - if (!p) return MTLERR_OM; - TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_TUPLE)); - int i; for(i=size-1;i>=0;i--) TABSET(m,p,TYPEHEADER_LENGTH+i,STACKGET(m,2*(size-1-i))); - if (STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM; - return 0; + intptr_t* p=MALLOCCLEAR(m,TYPEHEADER_LENGTH+size); + if (!p) + { + return MTLERR_OM; + } + TABSET(m,p,TYPEHEADER_CODE,INTTOVAL(TYPENAME_TUPLE)); + int i; + for(i=size-1; i>=0; i--) + { + TABSET(m,p,TYPEHEADER_LENGTH+i,STACKGET(m,2*(size-1-i))); + } + if (STACKPUSH(m,PNTTOVAL(p))) + { + return MTLERR_OM; + } + return 0; } // trouve le type équivalent intptr_t* Compiler::actualtype(intptr_t* p) { - int vp=TABGET(p,TYPEHEADER_ACTUAL); - if (vp!=NIL) return actualtype(VALTOPNT(vp)); - return p; + int vp=TABGET(p,TYPEHEADER_ACTUAL); + if (vp!=NIL) + { + return actualtype(VALTOPNT(vp)); + } + return p; } // production d'un type int Compiler::parsegraph(Parser* p,int env,int mono,int rec,int labels,int newvars,intptr_t* rnode) { - int k,n; - - if (!p->next(0)) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : uncomplete type reaches EOF\n"); - return MTLERR_SN; - } - - int val; - if (!searchlabel_byname(labels,p->token,&val,NULL)) return STACKPUSH(m,val); - if ((p->token[0]=='w')&&(isdecimal(p->token+1))) - { - if (k=createnodetype(TYPENAME_WEAK)) return k; - return addlabel(labels,p->token,STACKGET(m,0),NIL); - } - if (!strcmp(p->token,"_")) - { - return createnodetype(TYPENAME_WEAK); - } - if ((p->token[0]=='u')&&(isdecimal(p->token+1))) - { - if (mono) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : polymorphism (%s) not accepted here\n",p->token); - return MTLERR_SN; - } - if (!newvars) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : cannot accept linked variable %s here\n",p->token); - return MTLERR_SN; - } - if (k=createnodetype(TYPENAME_UNDEF)) return k; - return addlabel(labels,p->token,STACKGET(m,0),NIL); - } - else if ((p->token[0]=='r')&&(isdecimal(p->token+1))) - { - int i=mtl_atoi(p->token+1); - if ((i<0)||(i>=rec)) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : recursivity out of range %d [0 %d[\n",i,rec); - return MTLERR_SN; - } - if (k=createnodetype(TYPENAME_REC)) return k; - TABSET(m,VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH,INTTOVAL(i)); - *rnode=1; - return 0; - } - else if (!strcmp(p->token,"tab")) - { - if (k=createnodetype(TYPENAME_TAB)) return k; - if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - STACKDROP(m); - return 0; - } - else if (!strcmp(p->token,"list")) - { - if (k=createnodetype(TYPENAME_LIST)) return k; - if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - STACKDROP(m); - return 0; - } - else if (!strcmp(p->token,"fun")) - { - if (k=createnodetype(TYPENAME_FUN)) return k; - int nblab; - if (newvars) - { - if (k=parsegraph(p,env,mono,rec+1,labels,1,rnode)) return k; - nblab=0; - } - else - { - nblab=nblabels(labels); - if (k=parsegraph(p,env,mono,rec+1,labels,1,rnode)) return k; - nblab=nblabels(labels)-nblab; - } - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - STACKDROP(m); - if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); - STACKDROP(m); - removenlabels(labels,nblab); - return 0; - } - else if (!strcmp(p->token,"[")) - { - n=0; - while(1) - { - if (!p->next(0)) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : uncomplete type reaches EOF\n"); - return MTLERR_SN; - } - if (!strcmp(p->token,"]")) - { - return createnodetuple(n); - } - else - { - p->giveback(); - if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) return k; - n++; - } - } - } - else if (islabel(p->token)) - { - intptr_t* t=searchtype(env,p->token); - if (!t) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : unknown type %s\n",p->token); - return MTLERR_SN; - } - intptr_t* q=VALTOPNT(TABGET(t,REF_TYPE)); - int vargs=TABGET(q,TYPEHEADER_LENGTH); - if (vargs==NIL) return STACKPUSH(m,TABGET(t,REF_TYPE)); - else - { - if (k=createnodetypecore(TABGET(q,TYPEHEADER_LENGTH+1))) return k; - int n=TABLEN(VALTOPNT(vargs)); - intptr_t* t0=MALLOCCLEAR(m,TABLEN(VALTOPNT(vargs))); - if (!t0) return MTLERR_OM; - if (k=STACKPUSH(m,PNTTOVAL(t0))) return k; - if (!p->next(0)) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : '(' expected (found EOF)\n"); - return MTLERR_SN; - } - if (strcmp(p->token,"(")) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : '(' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - int i;for(i=0;inext(0)) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : ')' expected (found EOF)\n"); - return MTLERR_SN; - } - if (strcmp(p->token,")")) - { - PRINTF(m)(LOG_RUNTIME,"Compiler : ')' expected (found '%s')\n",parser->token); - return MTLERR_SN; - } - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - STACKDROP(m); - } - } - else - { - PRINTF(m)(LOG_RUNTIME,"Compiler : unknown token %s\n",p->token); - return MTLERR_SN; - } - return 0; + int k,n; + + if (!p->next(0)) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : uncomplete type reaches EOF\n"); + return MTLERR_SN; + } + + int val; + if (!searchlabel_byname(labels,p->token,&val,NULL)) + { + return STACKPUSH(m,val); + } + if ((p->token[0]=='w')&&(isdecimal(p->token+1))) + { + if (k=createnodetype(TYPENAME_WEAK)) + { + return k; + } + return addlabel(labels,p->token,STACKGET(m,0),NIL); + } + if (!strcmp(p->token,"_")) + { + return createnodetype(TYPENAME_WEAK); + } + if ((p->token[0]=='u')&&(isdecimal(p->token+1))) + { + if (mono) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : polymorphism (%s) not accepted here\n",p->token); + return MTLERR_SN; + } + if (!newvars) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : cannot accept linked variable %s here\n",p->token); + return MTLERR_SN; + } + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + return addlabel(labels,p->token,STACKGET(m,0),NIL); + } + else if ((p->token[0]=='r')&&(isdecimal(p->token+1))) + { + int i=mtl_atoi(p->token+1); + if ((i<0)||(i>=rec)) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : recursivity out of range %d [0 %d[\n",i,rec); + return MTLERR_SN; + } + if (k=createnodetype(TYPENAME_REC)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,0)),TYPEHEADER_LENGTH,INTTOVAL(i)); + *rnode=1; + return 0; + } + else if (!strcmp(p->token,"tab")) + { + if (k=createnodetype(TYPENAME_TAB)) + { + return k; + } + if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + STACKDROP(m); + return 0; + } + else if (!strcmp(p->token,"list")) + { + if (k=createnodetype(TYPENAME_LIST)) + { + return k; + } + if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + STACKDROP(m); + return 0; + } + else if (!strcmp(p->token,"fun")) + { + if (k=createnodetype(TYPENAME_FUN)) + { + return k; + } + int nblab; + if (newvars) + { + if (k=parsegraph(p,env,mono,rec+1,labels,1,rnode)) + { + return k; + } + nblab=0; + } + else + { + nblab=nblabels(labels); + if (k=parsegraph(p,env,mono,rec+1,labels,1,rnode)) + { + return k; + } + nblab=nblabels(labels)-nblab; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + STACKDROP(m); + if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); + STACKDROP(m); + removenlabels(labels,nblab); + return 0; + } + else if (!strcmp(p->token,"[")) + { + n=0; + while(1) + { + if (!p->next(0)) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : uncomplete type reaches EOF\n"); + return MTLERR_SN; + } + if (!strcmp(p->token,"]")) + { + return createnodetuple(n); + } + else + { + p->giveback(); + if (k=parsegraph(p,env,mono,rec+1,labels,newvars,rnode)) + { + return k; + } + n++; + } + } + } + else if (islabel(p->token)) + { + intptr_t* t=searchtype(env,p->token); + if (!t) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : unknown type %s\n",p->token); + return MTLERR_SN; + } + intptr_t* q=VALTOPNT(TABGET(t,REF_TYPE)); + int vargs=TABGET(q,TYPEHEADER_LENGTH); + if (vargs==NIL) + { + return STACKPUSH(m,TABGET(t,REF_TYPE)); + } + else + { + if (k=createnodetypecore(TABGET(q,TYPEHEADER_LENGTH+1))) + { + return k; + } + int n=TABLEN(VALTOPNT(vargs)); + intptr_t* t0=MALLOCCLEAR(m,TABLEN(VALTOPNT(vargs))); + if (!t0) + { + return MTLERR_OM; + } + if (k=STACKPUSH(m,PNTTOVAL(t0))) + { + return k; + } + if (!p->next(0)) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : '(' expected (found EOF)\n"); + return MTLERR_SN; + } + if (strcmp(p->token,"(")) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : '(' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + int i; + for(i=0; inext(0)) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : ')' expected (found EOF)\n"); + return MTLERR_SN; + } + if (strcmp(p->token,")")) + { + PRINTF(m)(LOG_RUNTIME,"Compiler : ')' expected (found '%s')\n",parser->token); + return MTLERR_SN; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + STACKDROP(m); + } + } + else + { + PRINTF(m)(LOG_RUNTIME,"Compiler : unknown token %s\n",p->token); + return MTLERR_SN; + } + return 0; } // gestion des noeuds rec : on les lie avec le champ ACTUAL intptr_t Compiler::parse_rnode(intptr_t *p) { - intptr_t k,i; - - int c=VALTOINT(TABGET(p,TYPEHEADER_CODE)); - if ((c==TYPENAME_WEAK)||(c==TYPENAME_UNDEF)) return 0; // type faible ou non défini, pas de récursion - if ((c==TYPENAME_CORE)&&(TABGET(p,TYPEHEADER_LENGTH)==NIL)) return 0; // type de base, non paramétrique - - if (c==TYPENAME_REC) - { - TABSET(m,p,TYPEHEADER_ACTUAL,STACKGET(m,VALTOINT(TABGET(p,TYPEHEADER_LENGTH)))); - return 0; - } - if (k=STACKPUSH(m,PNTTOVAL(p))) return k; - - if (c==TYPENAME_CORE) - { - intptr_t* tup=VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)); - for(i=0;iterm,src); - int k=creategraph(p,env,mono); - delete p; - return k; + Parser* p=new Parser(m->term,src); + int k=creategraph(p,env,mono); + delete p; + return k; } @@ -345,529 +495,744 @@ intptr_t Compiler::creategraph(const char* src,int env,int mono) int Compiler::recechograph(Prodbuffer *output,intptr_t* p,int rec,int labels) { - int i,k; - p=actualtype(p); - if (k=STACKPUSH(m,PNTTOVAL(p))) return k; - - for(i=0;iprintf("r%d",i); - return 0; - } - int v=VALTOINT(TABGET(p,TYPEHEADER_CODE)); - if (v==TYPENAME_CORE) - { - output->printf("%s",STRSTART(VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)))); - int vargs=TABGET(p,TYPEHEADER_LENGTH); - if (vargs!=NIL) - { - output->printf("("); - intptr_t* tup=VALTOPNT(vargs); - for(i=0;iprintf(" "); - recechograph(output,VALTOPNT(TABGET(tup,i)),rec+1,labels); - } - output->printf(")"); - } - } - else if (v==TYPENAME_FUN) - { - output->printf("fun "); - recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); - output->printf(" "); - recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)),rec+1,labels); - } - else if (v==TYPENAME_LIST) - { - output->printf("list "); - recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); - } - else if (v==TYPENAME_TAB) - { - output->printf("tab "); - recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); - } - else if (v==TYPENAME_TUPLE) - { - output->printf("["); - for(i=TYPEHEADER_LENGTH;iTYPEHEADER_LENGTH) output->printf(" "); - recechograph(output,VALTOPNT(TABGET(p,i)),rec+1,labels); - } - output->printf("]"); - } - else if (v==TYPENAME_UNDEF) - { - char* name; - if (!searchlabel_byval(labels,PNTTOVAL(p),&name)) - output->printf("%s",name); - else - { - char buf[16]; - sprintf(buf,"u%d",nblabels(labels)); - addlabel(labels,buf,PNTTOVAL(p),NIL); - output->printf("%s",buf); - } - } - else if (v==TYPENAME_WEAK) - { - char* name; - if (!searchlabel_byval(labels,PNTTOVAL(p),&name)) - output->printf("%s",name); - else - { - char buf[16]; - sprintf(buf,"w%d",nblabels(labels)); - addlabel(labels,buf,PNTTOVAL(p),NIL); - output->printf("%s",buf); - } - } - STACKDROP(m); - return 0; + int i,k; + p=actualtype(p); + if (k=STACKPUSH(m,PNTTOVAL(p))) + { + return k; + } + + for(i=0; iprintf("r%d",i); + return 0; + } + int v=VALTOINT(TABGET(p,TYPEHEADER_CODE)); + if (v==TYPENAME_CORE) + { + output->printf("%s",STRSTART(VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)))); + int vargs=TABGET(p,TYPEHEADER_LENGTH); + if (vargs!=NIL) + { + output->printf("("); + intptr_t* tup=VALTOPNT(vargs); + for(i=0; iprintf(" "); + } + recechograph(output,VALTOPNT(TABGET(tup,i)),rec+1,labels); + } + output->printf(")"); + } + } + else if (v==TYPENAME_FUN) + { + output->printf("fun "); + recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); + output->printf(" "); + recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)),rec+1,labels); + } + else if (v==TYPENAME_LIST) + { + output->printf("list "); + recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); + } + else if (v==TYPENAME_TAB) + { + output->printf("tab "); + recechograph(output,VALTOPNT(TABGET(p,TYPEHEADER_LENGTH)),rec+1,labels); + } + else if (v==TYPENAME_TUPLE) + { + output->printf("["); + for(i=TYPEHEADER_LENGTH; iTYPEHEADER_LENGTH) + { + output->printf(" "); + } + recechograph(output,VALTOPNT(TABGET(p,i)),rec+1,labels); + } + output->printf("]"); + } + else if (v==TYPENAME_UNDEF) + { + char* name; + if (!searchlabel_byval(labels,PNTTOVAL(p),&name)) + { + output->printf("%s",name); + } + else + { + char buf[16]; + sprintf(buf,"u%d",nblabels(labels)); + addlabel(labels,buf,PNTTOVAL(p),NIL); + output->printf("%s",buf); + } + } + else if (v==TYPENAME_WEAK) + { + char* name; + if (!searchlabel_byval(labels,PNTTOVAL(p),&name)) + { + output->printf("%s",name); + } + else + { + char buf[16]; + sprintf(buf,"w%d",nblabels(labels)); + addlabel(labels,buf,PNTTOVAL(p),NIL); + output->printf("%s",buf); + } + } + STACKDROP(m); + return 0; } intptr_t Compiler::echograph(Prodbuffer *output,intptr_t* p) { - int k,labels; - if (k=STACKPUSH(m,NIL)) return k; - labels=STACKREF(m); - recechograph(output,p,0,labels); - STACKDROP(m); - return 0; + int k,labels; + if (k=STACKPUSH(m,NIL)) + { + return k; + } + labels=STACKREF(m); + recechograph(output,p,0,labels); + STACKDROP(m); + return 0; } // copie de graphe intptr_t Compiler::reccopytype(intptr_t *p) { - int k,i; - - p=actualtype(p); - int vq=TABGET(p,TYPEHEADER_COPY); - if (vq!=NIL) return STACKPUSH(m,vq); // élément déjà copié - int c=VALTOINT(TABGET(p,TYPEHEADER_CODE)); - if ((c==TYPENAME_CORE)&&(TABGET(p,TYPEHEADER_LENGTH)==NIL)) - return STACKPUSH(m,PNTTOVAL(p)); // type basique, ne pas copier - - if (c==TYPENAME_TUPLE) - { - for(i=TYPEHEADER_LENGTH;iprintf("Compiler :\n "); - echograph(output,x); - output->printf("\ndoes not match with\n "); - echograph(output,y); - output->printf("\n"); + output->printf("Compiler :\n "); + echograph(output,x); + output->printf("\ndoes not match with\n "); + echograph(output,y); + output->printf("\n"); - PRINTF(m)(LOG_COMPILER,"%s",output->getstart()); - delete output; + PRINTF(m)(LOG_COMPILER,"%s",output->getstart()); + delete output; - return l; + return l; } // [fun [arg]] intptr_t Compiler::unif_argfun() { - intptr_t k; - intptr_t* fun=VALTOPNT(STACKPULL(m)); - intptr_t* arg=VALTOPNT(STACKGET(m,0)); + intptr_t k; + intptr_t* fun=VALTOPNT(STACKPULL(m)); + intptr_t* arg=VALTOPNT(STACKGET(m,0)); - if (k=unif(VALTOPNT(TABGET(fun,TYPEHEADER_LENGTH)),arg)) return k; - STACKSET(m,0,TABGET(fun,TYPEHEADER_LENGTH+1)); - return 0; + if (k=unif(VALTOPNT(TABGET(fun,TYPEHEADER_LENGTH)),arg)) + { + return k; + } + STACKSET(m,0,TABGET(fun,TYPEHEADER_LENGTH+1)); + return 0; } intptr_t* Compiler::argsfromfun(intptr_t *f) { - return VALTOPNT(TABGET(f,TYPEHEADER_LENGTH)); + return VALTOPNT(TABGET(f,TYPEHEADER_LENGTH)); } void Compiler::echonode(int code,intptr_t* p) { - if (code==TYPENAME_CORE) PRINTF(m)(LOG_COMPILER,"%s",STRSTART(VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)))); - else if (code==TYPENAME_UNDEF) PRINTF(m)(LOG_COMPILER,"u*"); - else if (code==TYPENAME_WEAK) PRINTF(m)(LOG_COMPILER,"w*"); - else if (code==TYPENAME_TAB) PRINTF(m)(LOG_COMPILER,"tab"); - else if (code==TYPENAME_LIST) PRINTF(m)(LOG_COMPILER,"list"); - else if (code==TYPENAME_TUPLE) PRINTF(m)(LOG_COMPILER,"tuple"); - else if (code==TYPENAME_FUN) PRINTF(m)(LOG_COMPILER,"fun"); + if (code==TYPENAME_CORE) + { + PRINTF(m)(LOG_COMPILER,"%s",STRSTART(VALTOPNT(TABGET(p,TYPEHEADER_LENGTH+1)))); + } + else if (code==TYPENAME_UNDEF) + { + PRINTF(m)(LOG_COMPILER,"u*"); + } + else if (code==TYPENAME_WEAK) + { + PRINTF(m)(LOG_COMPILER,"w*"); + } + else if (code==TYPENAME_TAB) + { + PRINTF(m)(LOG_COMPILER,"tab"); + } + else if (code==TYPENAME_LIST) + { + PRINTF(m)(LOG_COMPILER,"list"); + } + else if (code==TYPENAME_TUPLE) + { + PRINTF(m)(LOG_COMPILER,"tuple"); + } + else if (code==TYPENAME_FUN) + { + PRINTF(m)(LOG_COMPILER,"fun"); + } } // unification d'un plus grand vers un plus petit // attention, OPsearch suppose que seul MTLERR_TYPE peut arriver intptr_t Compiler::recunifbigger(intptr_t* s,intptr_t* t) { - intptr_t* s0=s; // on retient le s initial - s=actualtype(s); - t=actualtype(t); - if (s==t) return 0; // ceci gère le cas des types basiques - - int ns=VALTOINT(TABGET(s,TYPEHEADER_CODE)); - int nt=VALTOINT(TABGET(t,TYPEHEADER_CODE)); - int ok=1; - if ((ns!=TYPENAME_UNDEF)&&(ns!=TYPENAME_WEAK) - &&(nt!=TYPENAME_UNDEF)&&(nt!=TYPENAME_WEAK) // si les deux noeuds bien définis ... - &&(ns!=nt) ) ok=0; - else if ((ns==nt)&&(ns==TYPENAME_CORE)&&(TABGET(s,TYPEHEADER_LENGTH+1)!=TABGET(t,TYPEHEADER_LENGTH+1))) ok=0; - if (!ok) - { - PRINTF(m)(LOG_COMPILER,"Compiler : "); - echonode(ns,s); - PRINTF(m)(LOG_COMPILER," cannot be unified with "); - echonode(nt,t); - PRINTF(m)(LOG_COMPILER,"\n"); - return MTLERR_TYPE; // ... il doivent être du même type - } - - int vt=TABGET(t,TYPEHEADER_ACTUAL); // on sauvegarde avant unification - int vs=TABGET(s,TYPEHEADER_ACTUAL); - - if ((nt==TYPENAME_UNDEF)&&(ns!=TYPENAME_UNDEF)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : "); - echonode(ns,s); - PRINTF(m)(LOG_COMPILER," is smaller than "); - echonode(nt,t); - PRINTF(m)(LOG_COMPILER,"\n"); - return MTLERR_TYPE; - } - if ((ns==TYPENAME_UNDEF)&&(s0!=s)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : u* already unified\n"); - return MTLERR_TYPE; - } - - if ((ns==TYPENAME_UNDEF) /* union des noeuds */ - || ((ns==TYPENAME_WEAK)&&(nt!=TYPENAME_UNDEF)) ) - { - TABSET(m,s,TYPEHEADER_ACTUAL,PNTTOVAL(t)); - if (ns==TYPENAME_WEAK) - { - recgoweak(t); - recresetcopy(t); - } - } - else - { - TABSET(m,t,TYPEHEADER_ACTUAL,PNTTOVAL(s)); - if (nt==TYPENAME_WEAK) - { - recgoweak(s); - recresetcopy(s); - } - } - - if (ns==nt) - { - int k; - if ((ns==TYPENAME_TAB)||(ns==TYPENAME_LIST)||(ns==TYPENAME_FUN)) - { - if (k=recunifbigger(VALTOPNT(TABGET(s,TYPEHEADER_LENGTH)),VALTOPNT(TABGET(t,TYPEHEADER_LENGTH)))) - return restoreactual(t,s,vt,vs,k); - if ((ns==TYPENAME_FUN) - &&(k=recunifbigger(VALTOPNT(TABGET(s,TYPEHEADER_LENGTH+1)),VALTOPNT(TABGET(t,TYPEHEADER_LENGTH+1)))) ) - return restoreactual(t,s,vt,vs,k); - } - else if (ns==TYPENAME_TUPLE) - { - int len=TABLEN(s); - if (len!=TABLEN(t)) return restoreactual(t,s,vt,vs,MTLERR_TYPE); - int i; for(i=TYPEHEADER_LENGTH;iprintf("Compiler :\n "); - echograph(output,x); - output->printf("\ndoes not match with or is smaller than\n "); - echograph(output,y); - output->printf("\n"); + output->printf("Compiler :\n "); + echograph(output,x); + output->printf("\ndoes not match with or is smaller than\n "); + echograph(output,y); + output->printf("\n"); - PRINTF(m)(LOG_COMPILER,"%s",output->getstart()); - delete output; + PRINTF(m)(LOG_COMPILER,"%s",output->getstart()); + delete output; - return l; + return l; } diff --git a/src/vcomp/compiler_var.cpp b/src/vcomp/compiler_var.cpp index 85326ba..dcf47ed 100644 --- a/src/vcomp/compiler_var.cpp +++ b/src/vcomp/compiler_var.cpp @@ -20,271 +20,483 @@ int Compiler::parseval() { - int k; + int k; - if (k=parseval3()) return k; + if (k=parseval3()) + { + return k; + } - if (!parser->next(0)) return 0; - if (strcmp(parser->token,"::")) + if (!parser->next(0)) + { + return 0; + } + if (strcmp(parser->token,"::")) + { + parser->giveback(); + return 0; + } + if (k=parseval()) { - parser->giveback(); - return 0; + return k; // récursion } - if (k=parseval()) return k; // récursion - if (k=createnodetype(TYPENAME_LIST)) return k; - if (k=createnodetype(TYPENAME_UNDEF)) return k; // noeud élement - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud élément au noeud list - if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,4)))) return k; // unification élement - if (k=unif(VALTOPNT(STACKGET(m,1)),VALTOPNT(STACKGET(m,2)))) return k; // unification list - STACKSET(m,4,STACKGET(m,1)); // remplacement du type + if (k=createnodetype(TYPENAME_LIST)) + { + return k; + } + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; // noeud élement + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud élément au noeud list + if (k=unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(STACKGET(m,4)))) + { + return k; // unification élement + } + if (k=unif(VALTOPNT(STACKGET(m,1)),VALTOPNT(STACKGET(m,2)))) + { + return k; // unification list + } + STACKSET(m,4,STACKGET(m,1)); // remplacement du type - intptr_t* p=MALLOCCLEAR(m,LIST_LENGTH); // création du tuple liste - if (!p) return MTLERR_OM; - TABSET(m,p,LIST_VAL,STACKGET(m,5)); - TABSET(m,p,LIST_NEXT,STACKGET(m,3)); - STACKSET(m,5,PNTTOVAL(p)); - STACKDROPN(m,4); - return 0; + intptr_t* p=MALLOCCLEAR(m,LIST_LENGTH); // création du tuple liste + if (!p) + { + return MTLERR_OM; + } + TABSET(m,p,LIST_VAL,STACKGET(m,5)); + TABSET(m,p,LIST_NEXT,STACKGET(m,3)); + STACKSET(m,5,PNTTOVAL(p)); + STACKDROPN(m,4); + return 0; } int Compiler::parseval3() { - int k,op,typ; + int k,op,typ; - if (k=parseval4()) return k; - while(1) + if (k=parseval4()) + { + return k; + } + while(1) { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"+")) { op=OPadd; typ=1;} - else if (!strcmp(parser->token,"-")) { op=OPsub; typ=1;} - else - { - parser->giveback(); - return 0; - } - if (k=parseval4()) return k; - if (op==OPadd) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))+VALTOINT(STACKGET(m,1)))); - else if (op==OPsub) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))-VALTOINT(STACKGET(m,1)))); - intptr_t* ptyp; - if (typ==1) ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; - if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) return k; - STACKDROPN(m,2); + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"+")) + { + op=OPadd; + typ=1; + } + else if (!strcmp(parser->token,"-")) + { + op=OPsub; + typ=1; + } + else + { + parser->giveback(); + return 0; + } + if (k=parseval4()) + { + return k; + } + if (op==OPadd) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))+VALTOINT(STACKGET(m,1)))); + } + else if (op==OPsub) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))-VALTOINT(STACKGET(m,1)))); + } + intptr_t* ptyp; + if (typ==1) + { + ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); + } + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) + { + return k; + } + STACKDROPN(m,2); } } int Compiler::parseval4() { - int k,op,typ; + int k,op,typ; - if (k=parseval5()) return k; - while(1) + if (k=parseval5()) { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"*")) { op=OPmul; typ=1;} - else if (!strcmp(parser->token,"/")) { op=OPdiv; typ=1;} - else if (!strcmp(parser->token,"%")) { op=OPmod; typ=1;} - else - { - parser->giveback(); - return 0; - } - if (k=parseval5()) return k; + return k; + } + while(1) + { + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"*")) + { + op=OPmul; + typ=1; + } + else if (!strcmp(parser->token,"/")) + { + op=OPdiv; + typ=1; + } + else if (!strcmp(parser->token,"%")) + { + op=OPmod; + typ=1; + } + else + { + parser->giveback(); + return 0; + } + if (k=parseval5()) + { + return k; + } - if (op==OPmul) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))*VALTOINT(STACKGET(m,1)))); - else if (op==OPdiv) - { - if (VALTOINT(STACKGET(m,1))==0) return MTLERR_DIV; - STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))/VALTOINT(STACKGET(m,1)))); - } - else if (op==OPmod) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))%VALTOINT(STACKGET(m,1)))); - intptr_t* ptyp; - if (typ==1) ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; - if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) return k; - STACKDROPN(m,2); + if (op==OPmul) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))*VALTOINT(STACKGET(m,1)))); + } + else if (op==OPdiv) + { + if (VALTOINT(STACKGET(m,1))==0) + { + return MTLERR_DIV; + } + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))/VALTOINT(STACKGET(m,1)))); + } + else if (op==OPmod) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))%VALTOINT(STACKGET(m,1)))); + } + intptr_t* ptyp; + if (typ==1) + { + ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); + } + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) + { + return k; + } + STACKDROPN(m,2); } } int Compiler::parseval5() { - int k,op; + int k,op; - if (k=parseval6()) return k; - while(1) + if (k=parseval6()) + { + return k; + } + while(1) { - if (!parser->next(0)) return 0; - if (!strcmp(parser->token,"&")) op=OPand; - else if (!strcmp(parser->token,"|")) op=OPor; - else if (!strcmp(parser->token,"^")) op=OPeor; - else if (!strcmp(parser->token,"<<")) op=OPshl; - else if (!strcmp(parser->token,">>")) op=OPshr; - else - { - parser->giveback(); - return 0; - } - if (k=parseval6()) return k; + if (!parser->next(0)) + { + return 0; + } + if (!strcmp(parser->token,"&")) + { + op=OPand; + } + else if (!strcmp(parser->token,"|")) + { + op=OPor; + } + else if (!strcmp(parser->token,"^")) + { + op=OPeor; + } + else if (!strcmp(parser->token,"<<")) + { + op=OPshl; + } + else if (!strcmp(parser->token,">>")) + { + op=OPshr; + } + else + { + parser->giveback(); + return 0; + } + if (k=parseval6()) + { + return k; + } - if (op==OPand) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))&VALTOINT(STACKGET(m,1)))); - else if (op==OPor) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))|VALTOINT(STACKGET(m,1)))); - else if (op==OPeor) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))^VALTOINT(STACKGET(m,1)))); - else if (op==OPshl) STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))<>VALTOINT(STACKGET(m,1)))); + if (op==OPand) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))&VALTOINT(STACKGET(m,1)))); + } + else if (op==OPor) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))|VALTOINT(STACKGET(m,1)))); + } + else if (op==OPeor) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))^VALTOINT(STACKGET(m,1)))); + } + else if (op==OPshl) + { + STACKSET(m,3,INTTOVAL(VALTOINT(STACKGET(m,3))<>VALTOINT(STACKGET(m,1)))); + } - intptr_t* ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); - if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) return k; - if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) return k; - STACKDROPN(m,2); + intptr_t* ptyp=VALTOPNT(TABGET(stdtypes,STDTYPE_I)); + if (k=unif(VALTOPNT(STACKGET(m,0)),ptyp)) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,2)),ptyp)) + { + return k; + } + STACKDROPN(m,2); } } int Compiler::parseval6() { - int k; + int k; - if (!parser->next(0)) return parseval7(); + if (!parser->next(0)) + { + return parseval7(); + } - if (!strcmp(parser->token,"-")) - { - if (!parser->next(0)) return parseval6(); - if (isdecimal(parser->token)) // gestion des entiers - { - int i=-mtl_atoi(parser->token); - if (k=STACKPUSH(m,INTTOVAL(i))) return k; - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - parser->giveback(); - if (k=parseval6()) return k; - STACKSET(m,1,INTTOVAL(-VALTOINT(STACKGET(m,0)))); - return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); - } - else if (!strcmp(parser->token,"~")) - { - if (k=parseval6()) return k; - STACKSET(m,1,INTTOVAL(~VALTOINT(STACKGET(m,0)))); - return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); - } - parser->giveback(); - return parseval7(); + if (!strcmp(parser->token,"-")) + { + if (!parser->next(0)) + { + return parseval6(); + } + if (isdecimal(parser->token)) // gestion des entiers + { + int i=-mtl_atoi(parser->token); + if (k=STACKPUSH(m,INTTOVAL(i))) + { + return k; + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + parser->giveback(); + if (k=parseval6()) + { + return k; + } + STACKSET(m,1,INTTOVAL(-VALTOINT(STACKGET(m,0)))); + return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); + } + else if (!strcmp(parser->token,"~")) + { + if (k=parseval6()) + { + return k; + } + STACKSET(m,1,INTTOVAL(~VALTOINT(STACKGET(m,0)))); + return unif(VALTOPNT(STACKGET(m,0)),VALTOPNT(TABGET(stdtypes,STDTYPE_I))); + } + parser->giveback(); + return parseval7(); } int Compiler::parseval7() { - int k; + int k; - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"(")) // gestion des parenthèses - { - if (k=parseval()) return k; - return parser->parsekeyword(")"); - } - else if (!strcmp(parser->token,"[")) // gestion des tuples - { - int nval=0; - if (k=STACKPUSH(m,NIL)) return k; // valeur finale - if (k=STACKPUSH(m,NIL)) return k; // type final - int sref=STACKREF(m); - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"]")) - { - intptr_t* p=MALLOCCLEAR(m,nval); // création du tuple liste - if (!p) return MTLERR_OM; - int i; - for(i=0;igiveback(); - if (k=parseval()) return k; - nval++; - } - } - else if (!strcmp(parser->token,"{")) // gestion des tableaux - { - int nval=0; - if (k=STACKPUSH(m,NIL)) return k; // valeur finale - if (k=createnodetype(TYPENAME_TAB)) return k; // type final - if (k=createnodetype(TYPENAME_UNDEF)) return k; - TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); - intptr_t* p=VALTOPNT(STACKPULL(m)); + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : term expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"(")) // gestion des parenthèses + { + if (k=parseval()) + { + return k; + } + return parser->parsekeyword(")"); + } + else if (!strcmp(parser->token,"[")) // gestion des tuples + { + int nval=0; + if (k=STACKPUSH(m,NIL)) + { + return k; // valeur finale + } + if (k=STACKPUSH(m,NIL)) + { + return k; // type final + } + int sref=STACKREF(m); + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : ']' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"]")) + { + intptr_t* p=MALLOCCLEAR(m,nval); // création du tuple liste + if (!p) + { + return MTLERR_OM; + } + int i; + for(i=0; igiveback(); + if (k=parseval()) + { + return k; + } + nval++; + } + } + else if (!strcmp(parser->token,"{")) // gestion des tableaux + { + int nval=0; + if (k=STACKPUSH(m,NIL)) + { + return k; // valeur finale + } + if (k=createnodetype(TYPENAME_TAB)) + { + return k; // type final + } + if (k=createnodetype(TYPENAME_UNDEF)) + { + return k; + } + TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); + intptr_t* p=VALTOPNT(STACKPULL(m)); - while(1) - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); - return MTLERR_SN; - } - if (!strcmp(parser->token,"}")) - { + while(1) + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : '}' expected (found EOF)\n"); + return MTLERR_SN; + } + if (!strcmp(parser->token,"}")) + { - if (k=DEFTAB(m,nval)) return k; + if (k=DEFTAB(m,nval)) + { + return k; + } - STACKSET(m,2,STACKGET(m,0)); // report de la valeur du tableau - STACKDROP(m); - return 0; - } - parser->giveback(); - if (k=parseval()) return k; - if (k=unif(VALTOPNT(STACKGET(m,0)),p)) return k; - STACKDROP(m); - nval++; - } - } - else if (!strcmp(parser->token,"nil")) // gestion du nil - { - if (k=STACKPUSH(m,NIL)) return k; // valeur - return createnodetype(TYPENAME_WEAK); - } - else if (!strcmp(parser->token,"'")) // gestion des 'char - { - if (!parser->next(0)) - { - PRINTF(m)(LOG_COMPILER,"Compiler : 'char expected (found EOF)\n"); - return MTLERR_SN; - } - if (k=STACKPUSH(m,INTTOVAL(parser->token[0]&255))) return k; // valeur - if (k=parser->parsekeyword("'")) return k; - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if (isdecimal(parser->token)) // gestion des entiers - { - int i=mtl_atoi(parser->token); - if (k=STACKPUSH(m,INTTOVAL(i))) return k; // valeur - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if ((parser->token[0]=='0')&&(parser->token[1]=='x') - &&(ishexadecimal(parser->token+2))) // gestion des entiers - { - int i=mtl_htoi(parser->token+2); - if (k=STACKPUSH(m,INTTOVAL(i))) return k; // valeur - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); - } - else if (parser->token[0]=='"') // gestion des chaines - { - if (k=parser->getstring(m,'"')) return k; - return STACKPUSH(m,TABGET(stdtypes,STDTYPE_S)); - } - else - { - PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); - return MTLERR_SN; - } + STACKSET(m,2,STACKGET(m,0)); // report de la valeur du tableau + STACKDROP(m); + return 0; + } + parser->giveback(); + if (k=parseval()) + { + return k; + } + if (k=unif(VALTOPNT(STACKGET(m,0)),p)) + { + return k; + } + STACKDROP(m); + nval++; + } + } + else if (!strcmp(parser->token,"nil")) // gestion du nil + { + if (k=STACKPUSH(m,NIL)) + { + return k; // valeur + } + return createnodetype(TYPENAME_WEAK); + } + else if (!strcmp(parser->token,"'")) // gestion des 'char + { + if (!parser->next(0)) + { + PRINTF(m)(LOG_COMPILER,"Compiler : 'char expected (found EOF)\n"); + return MTLERR_SN; + } + if (k=STACKPUSH(m,INTTOVAL(parser->token[0]&255))) + { + return k; // valeur + } + if (k=parser->parsekeyword("'")) + { + return k; + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if (isdecimal(parser->token)) // gestion des entiers + { + int i=mtl_atoi(parser->token); + if (k=STACKPUSH(m,INTTOVAL(i))) + { + return k; // valeur + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if ((parser->token[0]=='0')&&(parser->token[1]=='x') + &&(ishexadecimal(parser->token+2))) // gestion des entiers + { + int i=mtl_htoi(parser->token+2); + if (k=STACKPUSH(m,INTTOVAL(i))) + { + return k; // valeur + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_I)); + } + else if (parser->token[0]=='"') // gestion des chaines + { + if (k=parser->getstring(m,'"')) + { + return k; + } + return STACKPUSH(m,TABGET(stdtypes,STDTYPE_S)); + } + else + { + PRINTF(m)(LOG_COMPILER,"Compiler : unexpected term '%s'\n",parser->token); + return MTLERR_SN; + } } diff --git a/src/vcomp/file.cpp b/src/vcomp/file.cpp index 0da2e85..98ae5ae 100644 --- a/src/vcomp/file.cpp +++ b/src/vcomp/file.cpp @@ -20,60 +20,81 @@ File::File(FileSystem *fs) { - filesystem=fs; - file=NULL; - sizeinternalbuffer=64; - internalbuffer=new char[sizeinternalbuffer]; + filesystem=fs; + file=NULL; + sizeinternalbuffer=64; + internalbuffer=new char[sizeinternalbuffer]; } File::~File() { - if (file) fclose(file); - delete internalbuffer; + if (file) + { + fclose(file); + } + delete internalbuffer; } int File::close() { - if (file) fclose(file); - file=NULL; - return 0; + if (file) + { + fclose(file); + } + file=NULL; + return 0; } int File::openread(const char* name) { - file=fopen(name,"rb"); - if (file) return 0; - return -1; + file=fopen(name,"rb"); + if (file) + { + return 0; + } + return -1; } char* File::getcontent(int *size) { - if (file==NULL) return NULL; + if (file==NULL) + { + return NULL; + } - fseek(file,0,SEEK_END); - *size=ftell(file); - fseek(file,0,SEEK_SET); + fseek(file,0,SEEK_END); + *size=ftell(file); + fseek(file,0,SEEK_SET); - char* buffer; - buffer=new char[(*size)+1]; - fread((void*)buffer,1,*size,file); - buffer[*size]=0; - close(); - return buffer; + char* buffer; + buffer=new char[(*size)+1]; + fread((void*)buffer,1,*size,file); + buffer[*size]=0; + close(); + return buffer; } int File::openwrite(const char* name) { - file=fopen(name,"wb"); - if (file) return 0; - return -1; + file=fopen(name,"wb"); + if (file) + { + return 0; + } + return -1; } int File::write(const char *outbuf,int size) { - if (file==NULL) return 0; + if (file==NULL) + { + return 0; + } - int k=fwrite((void*)outbuf,1,size,file); - if (k<0) return -1; - fflush(file); - return 0; + int k=fwrite((void*)outbuf,1,size,file); + if (k<0) + { + return -1; + } + fflush(file); + return 0; } diff --git a/src/vcomp/interpreter.cpp b/src/vcomp/interpreter.cpp index 8a5c930..7759658 100644 --- a/src/vcomp/interpreter.cpp +++ b/src/vcomp/interpreter.cpp @@ -22,62 +22,69 @@ void displaybc(Memory* m,char* p) { - PRINTF(m)(LOG_DEVCORE,"dump %x\n",p); - const char* spaces=" "; - int ind=0; - while(1) - { - int i=*p; - if ((i<0)||(i>=MaxOpcode)) PRINTF(m)(LOG_DEVCORE,"%4d ??\n",ind); - else if ((i==OPint)) - { - p++; - int v=(p[0]&255)+((p[1]&255)<<8)+((p[2]&255)<<16)+((p[3]&255)<<24); - PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); - ind+=4; - p+=3; - } - else if ((i==OPgoto)||(i==OPelse)) - { - p++; - int v=(p[0]&255)+((p[1]&255)<<8); - PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); - ind+=2; - p+=1; - } else if ((i==OPgetlocalb)||(i==OPgetglobalb)||(i==OPfetchb)||(i==OPdeftabb)||(i==OPsetlocalb)|| - (i==OPmktabb)||(i==OPsetstructb)||(i==OPcallrb)||(i==OPintb)) - { - p++; - int v=p[0]&255; - PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); - ind++; - } - else if (i==OPret) - { - PRINTF(m)(LOG_DEVCORE,"%4d %s%s\n",ind,strbytecod[i],spaces+strlen(strbytecod[i])); - return; - } - else PRINTF(m)(LOG_DEVCORE,"%4d %s%s\n",ind,strbytecod[i],spaces+strlen(strbytecod[i])); - ind++; - p++; - } + PRINTF(m)(LOG_DEVCORE,"dump %x\n",p); + const char* spaces=" "; + int ind=0; + while(1) + { + int i=*p; + if ((i<0)||(i>=MaxOpcode)) + { + PRINTF(m)(LOG_DEVCORE,"%4d ??\n",ind); + } + else if ((i==OPint)) + { + p++; + int v=(p[0]&255)+((p[1]&255)<<8)+((p[2]&255)<<16)+((p[3]&255)<<24); + PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); + ind+=4; + p+=3; + } + else if ((i==OPgoto)||(i==OPelse)) + { + p++; + int v=(p[0]&255)+((p[1]&255)<<8); + PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); + ind+=2; + p+=1; + } + else if ((i==OPgetlocalb)||(i==OPgetglobalb)||(i==OPfetchb)||(i==OPdeftabb)||(i==OPsetlocalb)|| + (i==OPmktabb)||(i==OPsetstructb)||(i==OPcallrb)||(i==OPintb)) + { + p++; + int v=p[0]&255; + PRINTF(m)(LOG_DEVCORE,"%4d %s%s %d\n",ind,strbytecod[i],spaces+strlen(strbytecod[i]),v); + ind++; + } + else if (i==OPret) + { + PRINTF(m)(LOG_DEVCORE,"%4d %s%s\n",ind,strbytecod[i],spaces+strlen(strbytecod[i])); + return; + } + else + { + PRINTF(m)(LOG_DEVCORE,"%4d %s%s\n",ind,strbytecod[i],spaces+strlen(strbytecod[i])); + } + ind++; + p++; + } } Interpreter::Interpreter(Memory* memory) { - m=memory; - output=new Prodbuffer(); + m=memory; + output=new Prodbuffer(); }; Interpreter::~Interpreter() { - delete output; + delete output; }; int Interpreter::start() { - return 0; + return 0; } void Interpreter::stop() diff --git a/src/vcomp/memory.cpp b/src/vcomp/memory.cpp index b689fd9..b569936 100644 --- a/src/vcomp/memory.cpp +++ b/src/vcomp/memory.cpp @@ -22,48 +22,55 @@ // Gestion de la pile Stack::Stack() { - base=NULL; + base=NULL; } Stack::~Stack() { - if (base) delete base; + if (base) + { + delete base; + } } void Stack::dump(FILE *f) { - const char* buf="stck"; - fwrite((void*)buf,1,4,f); - fwrite((void*)&size,1,4,f); - fwrite((void*)&pp,1,4,f); - fwrite((void*)&base,1,4,f); - fwrite((void*)base,1,4*size,f); + const char* buf="stck"; + fwrite((void*)buf,1,4,f); + fwrite((void*)&size,1,4,f); + fwrite((void*)&pp,1,4,f); + fwrite((void*)&base,1,4,f); + fwrite((void*)base,1,4*size,f); } void Stack::initialize(int s) { - size=s; - base=new intptr_t[size]; - pp=base; + size=s; + base=new intptr_t[size]; + pp=base; } int Stack::bigger(Memory* m) { - int currentpp=pp-base; - - size*=2; - - intptr_t* newbase=new intptr_t[size]; - if (!newbase) - { - PRINTF(m)(LOG_RUNTIME,"Stack : out of Stack Memory\n"); - AbortMetal(m,1); - return MTLERR_OM; // impossible d'augmenter la taille de la pile - } - int i; for(i=0;i<=currentpp;i++) newbase[i]=base[i]; - delete base; - base=newbase; - pp=&base[currentpp]; - return 0; + int currentpp=pp-base; + + size*=2; + + intptr_t* newbase=new intptr_t[size]; + if (!newbase) + { + PRINTF(m)(LOG_RUNTIME,"Stack : out of Stack Memory\n"); + AbortMetal(m,1); + return MTLERR_OM; // impossible d'augmenter la taille de la pile + } + int i; + for(i=0; i<=currentpp; i++) + { + newbase[i]=base[i]; + } + delete base; + base=newbase; + pp=&base[currentpp]; + return 0; } @@ -72,73 +79,82 @@ int Stack::bigger(Memory* m) // ajout d'une racine intptr_t Memory::addroot(intptr_t *p) { - intptr_t k; - - if (k=push(PNTTOVAL(p))) return k; - intptr_t* r=malloc(LIST_LENGTH,TYPE_TAB); - TABSET(this,r,LIST_VAL,PNTTOVAL(p)); - TABSET(this,r,LIST_NEXT,root); - root=PNTTOVAL(r); - STACKDROP(this); - return 0; + intptr_t k; + + if (k=push(PNTTOVAL(p))) + { + return k; + } + intptr_t* r=malloc(LIST_LENGTH,TYPE_TAB); + TABSET(this,r,LIST_VAL,PNTTOVAL(p)); + TABSET(this,r,LIST_NEXT,root); + root=PNTTOVAL(r); + STACKDROP(this); + return 0; } // suppression d'une racine void Memory::removeroot(intptr_t *p) { - intptr_t* last=NULL; // pointeur vers le précédent maillon de la liste - intptr_t vq=root; - while(vq!=NIL) - { - intptr_t* q=VALTOPNT(vq); - if (TABGET(q,LIST_VAL)==PNTTOVAL(p)) - { - if (last) TABSET(this,last,LIST_NEXT,TABGET(q,LIST_NEXT)); - else root=TABGET(q,LIST_NEXT); - return; - } - last=q; - vq=TABGET(q,LIST_NEXT); - } + intptr_t* last=NULL; // pointeur vers le précédent maillon de la liste + intptr_t vq=root; + while(vq!=NIL) + { + intptr_t* q=VALTOPNT(vq); + if (TABGET(q,LIST_VAL)==PNTTOVAL(p)) + { + if (last) + { + TABSET(this,last,LIST_NEXT,TABGET(q,LIST_NEXT)); + } + else + { + root=TABGET(q,LIST_NEXT); + } + return; + } + last=q; + vq=TABGET(q,LIST_NEXT); + } } Memory::Memory(int size,Terminal *t,FileSystem *fs) { - term=t; - filesystem=fs; - size0=size; // on retient la taille initiale, elle sert d'ordre de grandeur - gcincperiod=GCincPeriod; - abort=0; - - util=new Util(this); - winutil=NULL; + term=t; + filesystem=fs; + size0=size; // on retient la taille initiale, elle sert d'ordre de grandeur + gcincperiod=GCincPeriod; + abort=0; + + util=new Util(this); + winutil=NULL; // listing(); } Memory::~Memory() { // if (winutil) delete winutil; - delete util; + delete util; } void Memory::stop() { - abort=1; - util->stop(); - root=NIL; - stack.pp=0; + abort=1; + util->stop(); + root=NIL; + stack.pp=0; } int Memory::start() { - root=NIL; + root=NIL; - stack.initialize(STACK_FIRST_SIZE); + stack.initialize(STACK_FIRST_SIZE); - PRINTF(this)(LOG_RUNTIME,"Metal Virtual Machine\n"); - PRINTF(this)(LOG_RUNTIME,"V0.2 - Sylvain Huet - 2005\n"); - PRINTF(this)(LOG_RUNTIME,"--------------------------\n"); - return util->start(); + PRINTF(this)(LOG_RUNTIME,"Metal Virtual Machine\n"); + PRINTF(this)(LOG_RUNTIME,"V0.2 - Sylvain Huet - 2005\n"); + PRINTF(this)(LOG_RUNTIME,"--------------------------\n"); + return util->start(); } void Memory::dump() @@ -148,95 +164,150 @@ void Memory::dump() intptr_t* Memory::malloc(int size,int type) { - intptr_t *p=NULL; + intptr_t *p=NULL; - int blocsize=size+HEADER_LENGTH; - p=new intptr_t[blocsize]; - if (!p) return p; + int blocsize=size+HEADER_LENGTH; + p=new intptr_t[blocsize]; + if (!p) + { + return p; + } - HEADERSETSIZETYPE(p,blocsize,type); + HEADERSETSIZETYPE(p,blocsize,type); - return p; + return p; } intptr_t* Memory::mallocClear(int size) { - intptr_t* p=malloc(size,TYPE_TAB); - if (!p) return p; - int i; - for(i=0;i>2); - - intptr_t* p=malloc(l,TYPE_BINARY); - if (!p) return p; - STRSETLEN(p,size); - STRSTART(p)[size]=0; - return p; + // calcul de la taille d'un bloc pouvant contenir une certain nombre de caractères + // il faut 1 mot pour la taille et un octet nul final + int l=2+(size>>2); + + intptr_t* p=malloc(l,TYPE_BINARY); + if (!p) + { + return p; + } + STRSETLEN(p,size); + STRSTART(p)[size]=0; + return p; } intptr_t* Memory::storebinary(const char *src,int size) { - // calcul de la taille d'un bloc pouvant contenir une certain nombre de caractères - // il faut 1 mot pour la taille et un octet nul final - int l=2+(size>>2); - - intptr_t* p=malloc(l,TYPE_BINARY); - if (!p) return p; - STRSETLEN(p,size); - memcpy(STRSTART(p),src,size); - STRSTART(p)[size]=0; - return p; + // calcul de la taille d'un bloc pouvant contenir une certain nombre de caractères + // il faut 1 mot pour la taille et un octet nul final + int l=2+(size>>2); + + intptr_t* p=malloc(l,TYPE_BINARY); + if (!p) + { + return p; + } + STRSETLEN(p,size); + memcpy(STRSTART(p),src,size); + STRSTART(p)[size]=0; + return p; } intptr_t* Memory::storestring(const char *src) { - return storebinary(src,strlen(src)); + return storebinary(src,strlen(src)); } int Memory::deftab(int size) { - intptr_t* p=malloc(size,TYPE_TAB); - if (!p) return MTLERR_OM; - int i; for(i=size-1;i>=0;i--) TABSET(this,p,i,STACKPULL(this)); - return push(PNTTOVAL(p)); + intptr_t* p=malloc(size,TYPE_TAB); + if (!p) + { + return MTLERR_OM; + } + int i; + for(i=size-1; i>=0; i--) + { + TABSET(this,p,i,STACKPULL(this)); + } + return push(PNTTOVAL(p)); } const char* Memory::errorname(int err) { - if (err==MTLERR_OM) return "Out of memory"; - else if (err==MTLERR_OP) return "Unknown Operand"; - else if (err==MTLERR_DIV) return "Division by zero"; - else if (err==MTLERR_RET) return "Bad implementation on 'return'"; - else if (err==MTLERR_NOFUN) return "No function defined"; - else if (err==MTLERR_SN) return "Syntax error"; - else if (err==MTLERR_TYPE) return "Typechecking error"; - else if (err==MTLERR_ABORT) return "Application aborted"; - else return "Unknown error"; + if (err==MTLERR_OM) + { + return "Out of memory"; + } + else if (err==MTLERR_OP) + { + return "Unknown Operand"; + } + else if (err==MTLERR_DIV) + { + return "Division by zero"; + } + else if (err==MTLERR_RET) + { + return "Bad implementation on 'return'"; + } + else if (err==MTLERR_NOFUN) + { + return "No function defined"; + } + else if (err==MTLERR_SN) + { + return "Syntax error"; + } + else if (err==MTLERR_TYPE) + { + return "Typechecking error"; + } + else if (err==MTLERR_ABORT) + { + return "Application aborted"; + } + else + { + return "Unknown error"; + } } diff --git a/src/vcomp/parser.cpp b/src/vcomp/parser.cpp index 2a17f50..a4b49ec 100644 --- a/src/vcomp/parser.cpp +++ b/src/vcomp/parser.cpp @@ -22,408 +22,563 @@ // test de lettre int isletter(int c) { - if ((c>='A')&&(c<='Z')) return 1; - if ((c>='a')&&(c<='z')) return 1; - if (c=='_') return 1; - return 0; + if ((c>='A')&&(c<='Z')) + { + return 1; + } + if ((c>='a')&&(c<='z')) + { + return 1; + } + if (c=='_') + { + return 1; + } + return 0; } // test de chiffre decimal int isnum(int c) { - if ((c>='0')&&(c<='9')) return 1; - return 0; + if ((c>='0')&&(c<='9')) + { + return 1; + } + return 0; } // test de lettre int ishex(int c) { - if ((c>='0')&&(c<='9')) return 1; - if ((c>='A')&&(c<='F')) return 1; - if ((c>='a')&&(c<='f')) return 1; - return 0; + if ((c>='0')&&(c<='9')) + { + return 1; + } + if ((c>='A')&&(c<='F')) + { + return 1; + } + if ((c>='a')&&(c<='f')) + { + return 1; + } + return 0; } // test de lettre ou de chiffre int isletnum(int c) { - if ((c>='A')&&(c<='Z')) return 1; - if ((c>='a')&&(c<='z')) return 1; - if ((c>='0')&&(c<='9')) return 1; - if (c=='_') return 1; - return 0; + if ((c>='A')&&(c<='Z')) + { + return 1; + } + if ((c>='a')&&(c<='z')) + { + return 1; + } + if ((c>='0')&&(c<='9')) + { + return 1; + } + if (c=='_') + { + return 1; + } + return 0; } // test si un mot est un label (une lettre puis des lettres ou des chiffres) int islabel(char* src) { - if (!isletter(*src++)) return 0; - while(*src) - { - if (!isletnum(*src++)) return 0; - } - return 1; + if (!isletter(*src++)) + { + return 0; + } + while(*src) + { + if (!isletnum(*src++)) + { + return 0; + } + } + return 1; } // test si un mot est un nombre décimal (que des chiffres) int isdecimal(char* src) { - while(*src) - { - if (!isnum(*src++)) return 0; - } - return 1; + while(*src) + { + if (!isnum(*src++)) + { + return 0; + } + } + return 1; } // test si un mot est un nombre hexadécimal (que des chiffres) int ishexadecimal(char* src) { - while(*src) - { - if (!ishex(*src++)) return 0; - } - return 1; + while(*src) + { + if (!ishex(*src++)) + { + return 0; + } + } + return 1; } // test si un mot est un nombre flottant (que des chiffres avec un point) int isfloat(char* src) { - int debut=1; - int point=0; - while(*src) - { - if ((*src=='.')&&(!debut)&&(!point)) point=1; - else if (!isnum(*src)) return 0; - src++; - debut=0; - } - return point; + int debut=1; + int point=0; + while(*src) + { + if ((*src=='.')&&(!debut)&&(!point)) + { + point=1; + } + else if (!isnum(*src)) + { + return 0; + } + src++; + debut=0; + } + return point; } // retourne le code ascii correspondant à un chiffre hexadécimal int ctoh(int c) { - c&=15; - if (c<10) return '0'+c; - return 'a'+c-10; + c&=15; + if (c<10) + { + return '0'+c; + } + return 'a'+c-10; } // retourne le chiffre correspondant à un code ascii hexadécimal int htoc(int c) { - if ((c>='0')&&(c<='9')) return c-'0'; - else if ((c>='A')&&(c<='F')) return c-'A'+10; - else if ((c>='a')&&(c<='f')) return c-'a'+10; - return 0; + if ((c>='0')&&(c<='9')) + { + return c-'0'; + } + else if ((c>='A')&&(c<='F')) + { + return c-'A'+10; + } + else if ((c>='a')&&(c<='f')) + { + return c-'a'+10; + } + return 0; } // lecture d'une chaîne décimale (s'arrête au premier caractère incorrect) int mtl_atoi(char* src) { - int x,c,s; - x=s=0; - if ((*src)=='-') { s=1; src++; } - while(c=*src++) - { - if ((c>='0')&&(c<='9')) x=(x*10)+c-'0'; - else return (s?(-x):x); - } - return (s?(-x):x); + int x,c,s; + x=s=0; + if ((*src)=='-') + { + s=1; + src++; + } + while(c=*src++) + { + if ((c>='0')&&(c<='9')) + { + x=(x*10)+c-'0'; + } + else + { + return (s?(-x):x); + } + } + return (s?(-x):x); } // lecture d'une chaîne hexadécimale (s'arrête au premier caractère incorrect) int mtl_htoi(char* src) { - int x,c; - x=0; - while(c=*src++) - { - if ((c>='0')&&(c<='9')) x=(x<<4)+c-'0'; - else if ((c>='A')&&(c<='F')) x=(x<<4)+c-'A'+10; - else if ((c>='a')&&(c<='f')) x=(x<<4)+c-'a'+10; - else return x; - } - return x; + int x,c; + x=0; + while(c=*src++) + { + if ((c>='0')&&(c<='9')) + { + x=(x<<4)+c-'0'; + } + else if ((c>='A')&&(c<='F')) + { + x=(x<<4)+c-'A'+10; + } + else if ((c>='a')&&(c<='f')) + { + x=(x<<4)+c-'a'+10; + } + else + { + return x; + } + } + return x; } // lecture d'une chaîne flottante (s'arrête au premier caractère incorrect) mtl_float mtl_atof(char* src) { - int c; - mtl_float f=0; - mtl_float div=1; - int point=0; - while(c=*src++) - { - if ((c=='.')&&(!point)) point=1; - else if ((c>='0')&&(c<='9')) - { - f=(f*10)+c-'0'; - if (point) div*=10; - } - else return f/div; - } - return f/div; + int c; + mtl_float f=0; + mtl_float div=1; + int point=0; + while(c=*src++) + { + if ((c=='.')&&(!point)) + { + point=1; + } + else if ((c>='0')&&(c<='9')) + { + f=(f*10)+c-'0'; + if (point) + { + div*=10; + } + } + else + { + return f/div; + } + } + return f/div; } Parser::Parser(Terminal* t,FileSystem* fs,char *filename) { - term=t; + term=t; - srcname=new char[strlen(filename)+1]; - strcpy(srcname,filename); + srcname=new char[strlen(filename)+1]; + strcpy(srcname,filename); - File* file=new File(fs); - int size; + File* file=new File(fs); + int size; - src=NULL; - if (!file->openread(filename)) src=file->getcontent(&size); - delete file; - if (!src) - { - t->printf(LOG_WARNING,"Parser : cannot open file %s\n",filename); - src=new char[1]; - src[0]=0; - } - index=0; + src=NULL; + if (!file->openread(filename)) + { + src=file->getcontent(&size); + } + delete file; + if (!src) + { + t->printf(LOG_WARNING,"Parser : cannot open file %s\n",filename); + src=new char[1]; + src[0]=0; + } + index=0; - indexsavedchar=-1; + indexsavedchar=-1; - again=0; + again=0; - lign=offligne=0; - lign0=offligne0=offchar0=0; + lign=offligne=0; + lign0=offligne0=offchar0=0; - token=NULL; + token=NULL; } Parser::Parser(Terminal* t,const char *buf) { - srcname=NULL; - term=t; - src=new char[strlen(buf)+1]; - strcpy(src,buf); - index=0; + srcname=NULL; + term=t; + src=new char[strlen(buf)+1]; + strcpy(src,buf); + index=0; - indexsavedchar=-1; + indexsavedchar=-1; - again=0; + again=0; - lign=offligne=0; - lign0=offligne0=offchar0=0; + lign=offligne=0; + lign0=offligne0=offchar0=0; - token=NULL; + token=NULL; } Parser::~Parser() { - delete src; - if (srcname) delete srcname; + delete src; + if (srcname) + { + delete srcname; + } } void Parser::giveback() { - again=1; + again=1; } char* Parser::next(int filter) { - if (again) - { - again=0; - return token; - } - - int i; - while(1) - { - int k; - do - { - k=gettoken(filter); - } while (k==-2); - if (k) - { - token=NULL; - return NULL; - } - //~ term->printf(0,"token=%s\n",token); - //~ asm volatile ("int3;"); - if (strcmp(token,"/*")) return token; // ok - i=1; // gestion des commentaires - while(i) + if (again) + { + again=0; + return token; + } + + int i; + while(1) + { + int k; + do + { + k=gettoken(filter); + } + while (k==-2); + if (k) + { + token=NULL; + return NULL; + } + //~ term->printf(0,"token=%s\n",token); + //~ asm volatile ("int3;"); + if (strcmp(token,"/*")) { - if (gettoken(filter)==-1) - { - term->printf(LOG_COMPILER,"unbalanced comment reaches EOF\n"); - token=NULL; - return NULL; - } - if (!strcmp(token,"/*")) i++; - if (!strcmp(token,"*/")) i--; + return token; // ok + } + i=1; // gestion des commentaires + while(i) + { + if (gettoken(filter)==-1) + { + term->printf(LOG_COMPILER,"unbalanced comment reaches EOF\n"); + token=NULL; + return NULL; + } + if (!strcmp(token,"/*")) + { + i++; + } + if (!strcmp(token,"*/")) + { + i--; + } } } } void Parser::savechar(int i) { - indexsavedchar=i; - savedchar=src[i]; - src[i]=0; + indexsavedchar=i; + savedchar=src[i]; + src[i]=0; } // demande de relire le dernier caractère void Parser::againchar() { - index--; - if (src[index]==10) lign--; + index--; + if (src[index]==10) + { + lign--; + } } // lit le prochain caractère int Parser::nextchar() { - int c; - - c=src[index]; - if (c) - { - index++; - if (c==10) - { - offligne=index; - lign++; - } - } - return c; + int c; + + c=src[index]; + if (c) + { + index++; + if (c==10) + { + offligne=index; + lign++; + } + } + return c; } void Parser::restorechar() { - // on restaure le caractère escamoté à la fin du token précédent - if (indexsavedchar!=-1) - { - src[indexsavedchar]=savedchar; - indexsavedchar=-1; - } + // on restaure le caractère escamoté à la fin du token précédent + if (indexsavedchar!=-1) + { + src[indexsavedchar]=savedchar; + indexsavedchar=-1; + } } int Parser::gettoken(int filter) { - int c,d,f; - - restorechar(); - - do - { // recherche le début du token - // on sauvegarde la position dans le source - lign0=lign; - offligne0=offligne; - offchar0=index; - c=nextchar(); - if (!c) return -1; // fin du fichier, pas de nouveau token - } while(c<=32); - - token=&src[offchar0]; - - f=0; - if (c=='\"') // token chaîne de caractères - while(1) - { - c=nextchar(); - if (!c) - { - term->printf(LOG_COMPILER,"uncomplete string reaches EOF\n"); - return -1; - } - if ((c=='\"')&&(f==0)) - { - savechar(index); - return 0; - } - if (c=='\\') f=1-f; - else f=0; - } - if (isletnum(c)) // token nombre ou label - { - int onlynum=1; - while(1) - { - if (!isnum(c)) onlynum=0; - c=nextchar(); - if (!c) return 0; - if ((c=='.')&&(onlynum)) // nombre flottant - { - while(1) - { - c=nextchar(); - if (!c) return 0; - if (!isnum(c)) - { - againchar(); - savechar(index); - return 0; - } - } - } - if (!isletnum(c)) - { - againchar(); - savechar(index); - return 0; - } - } - } - d=nextchar(); - if (!d) return 0; // fin du fichier sur un caractère spécial - - if ( ((c=='&')&&(d=='&')) - ||((c=='|')&&(d=='|')) - ||((c==':')&&(d==':')) - ||((c=='^')&&(d=='^')) - ||((c==';')&&(d==';')) - ||((c=='-')&&(d=='>')) - ||((c=='<')&&((d=='<')||(d=='/'))) - ||((c=='>')&&(d=='>')) - ||((c=='=')&&(d=='=')) - ||((c=='/')&&((d=='*')||(d=='>'))) - ||((c=='*')&&(d=='/')) - ) - { - // caractère double reconnu - } - else if ((c=='/')&&(d=='/')) - { - do // cas du commentaire // - { - c=nextchar(); - if (c==10) return -2; - } while (c); - return -1; // fin de fichier, pas de nouveau token - } - else if (((c=='!')||(c=='>')||(c=='<'))&&(d=='=')) - { -/* d=nextchar(); - if (!d) return 0; // fin du fichier sur un caractère spécial composé - if (d!='.') againchar(); -*/ } - else - againchar(); - savechar(index); - return 0; + int c,d,f; + + restorechar(); + + do + { + // recherche le début du token + // on sauvegarde la position dans le source + lign0=lign; + offligne0=offligne; + offchar0=index; + c=nextchar(); + if (!c) + { + return -1; // fin du fichier, pas de nouveau token + } + } + while(c<=32); + + token=&src[offchar0]; + + f=0; + if (c=='\"') // token chaîne de caractères + while(1) + { + c=nextchar(); + if (!c) + { + term->printf(LOG_COMPILER,"uncomplete string reaches EOF\n"); + return -1; + } + if ((c=='\"')&&(f==0)) + { + savechar(index); + return 0; + } + if (c=='\\') + { + f=1-f; + } + else + { + f=0; + } + } + if (isletnum(c)) // token nombre ou label + { + int onlynum=1; + while(1) + { + if (!isnum(c)) + { + onlynum=0; + } + c=nextchar(); + if (!c) + { + return 0; + } + if ((c=='.')&&(onlynum)) // nombre flottant + { + while(1) + { + c=nextchar(); + if (!c) + { + return 0; + } + if (!isnum(c)) + { + againchar(); + savechar(index); + return 0; + } + } + } + if (!isletnum(c)) + { + againchar(); + savechar(index); + return 0; + } + } + } + d=nextchar(); + if (!d) + { + return 0; // fin du fichier sur un caractère spécial + } + + if ( ((c=='&')&&(d=='&')) + ||((c=='|')&&(d=='|')) + ||((c==':')&&(d==':')) + ||((c=='^')&&(d=='^')) + ||((c==';')&&(d==';')) + ||((c=='-')&&(d=='>')) + ||((c=='<')&&((d=='<')||(d=='/'))) + ||((c=='>')&&(d=='>')) + ||((c=='=')&&(d=='=')) + ||((c=='/')&&((d=='*')||(d=='>'))) + ||((c=='*')&&(d=='/')) + ) + { + // caractère double reconnu + } + else if ((c=='/')&&(d=='/')) + { + do // cas du commentaire // + { + c=nextchar(); + if (c==10) + { + return -2; + } + } + while (c); + return -1; // fin de fichier, pas de nouveau token + } + else if (((c=='!')||(c=='>')||(c=='<'))&&(d=='=')) + { + /* d=nextchar(); + if (!d) return 0; // fin du fichier sur un caractère spécial composé + if (d!='.') againchar(); + */ + } + else + { + againchar(); + } + savechar(index); + return 0; } void Parser::echoposition() { - restorechar(); - if (srcname) term->printf(LOG_COMPILER,">line %d in '%s' :\n>",lign0+1,srcname); - else term->printf(LOG_COMPILER,">line %d :\n>",lign0+1); - char* p=&src[offligne0]; - int i=0; - while((p[i])&&(p[i]!='\n')) i++; - term->echo(LOG_COMPILER,p,i); - term->printf(LOG_COMPILER,"\n>"); - for(i=0;iprintf(LOG_COMPILER, "%c", (p[i]!='\t') ? ' ' : '\t'); } - term->printf(LOG_COMPILER,"^\n"); + restorechar(); + if (srcname) + { + term->printf(LOG_COMPILER,">line %d in '%s' :\n>",lign0+1,srcname); + } + else + { + term->printf(LOG_COMPILER,">line %d :\n>",lign0+1); + } + char* p=&src[offligne0]; + int i=0; + while((p[i])&&(p[i]!='\n')) + { + i++; + } + term->echo(LOG_COMPILER,p,i); + term->printf(LOG_COMPILER,"\n>"); + for(i=0; iprintf(LOG_COMPILER, "%c", (p[i]!='\t') ? ' ' : '\t'); + } + term->printf(LOG_COMPILER,"^\n"); } diff --git a/src/vcomp/parser_xml.cpp b/src/vcomp/parser_xml.cpp index ade3142..01ba987 100644 --- a/src/vcomp/parser_xml.cpp +++ b/src/vcomp/parser_xml.cpp @@ -23,103 +23,115 @@ // lit un keyword imposé int Parser::parsekeyword(const char* keyword) { - if (!next(0)) - { - term->printf(LOG_COMPILER,"Parser : '%s' expected (found EOF)\n"); - return MTLERR_SN; - } - if (strcmp(token,keyword)) - { - term->printf(LOG_COMPILER,"Parser : '%s' expected (found '%s')\n",keyword,token); - return MTLERR_SN; - } - return 0; + if (!next(0)) + { + term->printf(LOG_COMPILER,"Parser : '%s' expected (found EOF)\n"); + return MTLERR_SN; + } + if (strcmp(token,keyword)) + { + term->printf(LOG_COMPILER,"Parser : '%s' expected (found '%s')\n",keyword,token); + return MTLERR_SN; + } + return 0; } // lit un keyword imposé int Parser::parsekeyword(const char* keyword,int filter) { - if (!next(filter)) - { - term->printf(LOG_COMPILER,"Parser : '%s' expected (found EOF)\n"); - return MTLERR_SN; - } - if (strcmp(token,keyword)) - { - term->printf(LOG_COMPILER,"Parser : '%s' expected (found '%s')\n",keyword,token); - return MTLERR_SN; - } - return 0; + if (!next(filter)) + { + term->printf(LOG_COMPILER,"Parser : '%s' expected (found EOF)\n"); + return MTLERR_SN; + } + if (strcmp(token,keyword)) + { + term->printf(LOG_COMPILER,"Parser : '%s' expected (found '%s')\n",keyword,token); + return MTLERR_SN; + } + return 0; } // parsing d'une chaîne de caractères int Parser::getstring(Memory* m,char separator) { - int c,n,i; + int c,n,i; - char* name=token; - name++; + char* name=token; + name++; - Prodbuffer* output=m->util->interpreter->output; - output->reinit(); + Prodbuffer* output=m->util->interpreter->output; + output->reinit(); - n=0; - while(1) + n=0; + while(1) { - c=*(name++); - if (c=='\\') + c=*(name++); + if (c=='\\') { - c=(*(name++))&255; - if (c<32) - { - while(((*name)&255)<32) name++; - } - else - { - if (c=='n') c=10; - else if (c=='z') c=0; - else if (c=='$') - { - i=0; - c=*name; - if (ishex(c)) - { - name++; - i=htoc(c); - c=*name; - if (ishex(c)) - { - name++; - i=(i<<4)+htoc(c); - } - } - c=i; - } - else if ((c>='0')&&(c<='9')) - { - i=c-'0'; - c=*name; - if ((c>='0')&&(c<='9')) - { - name++; - i=(i*10)+c-'0'; - c=*name; - if ((c>='0')&&(c<='9')) - { - name++; - i=(i*10)+c-'0'; - } - } - c=i; - } - output->addchar(c); + c=(*(name++))&255; + if (c<32) + { + while(((*name)&255)<32) + { + name++; + } + } + else + { + if (c=='n') + { + c=10; + } + else if (c=='z') + { + c=0; + } + else if (c=='$') + { + i=0; + c=*name; + if (ishex(c)) + { + name++; + i=htoc(c); + c=*name; + if (ishex(c)) + { + name++; + i=(i<<4)+htoc(c); + } + } + c=i; + } + else if ((c>='0')&&(c<='9')) + { + i=c-'0'; + c=*name; + if ((c>='0')&&(c<='9')) + { + name++; + i=(i*10)+c-'0'; + c=*name; + if ((c>='0')&&(c<='9')) + { + name++; + i=(i*10)+c-'0'; + } + } + c=i; + } + output->addchar(c); } } - else if (c==separator) + else if (c==separator) + { + return STRPUSHBINARY(m,output->getstart(),output->getsize()); + } + else { - return STRPUSHBINARY(m,output->getstart(),output->getsize()); + output->addchar(c); } - else output->addchar(c); } } diff --git a/src/vcomp/prodbuffer.cpp b/src/vcomp/prodbuffer.cpp index 7a8673f..44961b0 100644 --- a/src/vcomp/prodbuffer.cpp +++ b/src/vcomp/prodbuffer.cpp @@ -16,110 +16,131 @@ Prodbuffer::Prodbuffer() { - size=PRODBUFFER_FIRSTSIZE; - buffer=new char[size]; - index=0; + size=PRODBUFFER_FIRSTSIZE; + buffer=new char[size]; + index=0; }; Prodbuffer::~Prodbuffer() { - delete buffer; + delete buffer; }; // initialiation du buffer (à appeler au début de toute nouvelle production) void Prodbuffer::reinit() { - index=0; + index=0; } void Prodbuffer::bigger() { - char* newbuf=new char[size*2]; - memcpy(newbuf,buffer,size); - delete buffer; - size*=2; - buffer=newbuf; + char* newbuf=new char[size*2]; + memcpy(newbuf,buffer,size); + delete buffer; + size*=2; + buffer=newbuf; } // ajout d'un caractère en fin de buffer void Prodbuffer::addchar(char c) { - if (index>=size) bigger(); - buffer[index++]=c; + if (index>=size) + { + bigger(); + } + buffer[index++]=c; } // printf à la suite du buffer void Prodbuffer::printf(const char *format, ...) { - va_list arglist; - int sizeout; - - va_start(arglist,format); - while(((sizeout=vsnprintf(buffer+index,size-index-1,format,arglist))<0)||(sizeout>=size-index-1)) bigger(); - va_end(arglist); - index+=sizeout; + va_list arglist; + int sizeout; + + va_start(arglist,format); + while(((sizeout=vsnprintf(buffer+index,size-index-1,format,arglist))<0)||(sizeout>=size-index-1)) + { + bigger(); + } + va_end(arglist); + index+=sizeout; } // ajout d'un entier en fin de buffer void Prodbuffer::addint(int i) { - addchar(i); i>>=8; - addchar(i); i>>=8; - addchar(i); i>>=8; - addchar(i); + addchar(i); + i>>=8; + addchar(i); + i>>=8; + addchar(i); + i>>=8; + addchar(i); } // ajout d'un short en fin de buffer void Prodbuffer::addshort(int i) { - addchar(i); i>>=8; - addchar(i); + addchar(i); + i>>=8; + addchar(i); } // ajout d'une chaîne void Prodbuffer::addstr(char *src,int len) { // addint(len); - int i; for(i=0;i>=8; - setchar(ind+1,i); i>>=8; - setchar(ind+2,i); i>>=8; - setchar(ind+3,i); + setchar(ind,i); + i>>=8; + setchar(ind+1,i); + i>>=8; + setchar(ind+2,i); + i>>=8; + setchar(ind+3,i); } // modification d'un short dans le buffer void Prodbuffer::setshort(int ind,int i) { - setchar(ind,i); i>>=8; - setchar(ind+1,i); + setchar(ind,i); + i>>=8; + setchar(ind+1,i); } // diminue la taille actuelle void Prodbuffer::cut(int len) { - if ((len>=0)&&(len=0)&&(len 115 -/*16*/"strright","strcrypt8","loadf","savef" -}; + /* 1*/ {NULL,NULL,NULL,NULL,NULL,"hd","tl","Secholn","Secho","Iecholn", //10 + /* 2*/"Iecho","time_ms","tabnew","abs","min","max","rand","srand", //10+8 + /* 3*/"time","strnew","strset","strcpy","vstrcmp","strfind","strfindrev","strlen", //10+8+8 + /* 4*/"strget","strsub","strcat","tablen","strcatlist","led","motorset","motorget", //10+8+8+8 + /* 5*/"fixarg1","fixarg2","fixarg3","fixarg4","fixarg5","fixarg6","fixarg7","fixarg8", //10+8+8+8+8 + /* 6*/"button2","button3","playStart","playFeed","playStop","recStart","recStop","recVol", //10+8+8+8+8+8 + /* 7*/"load","gc","save","bytecode","loopcb", //10+8+8+8+8+8+5 + /* 8*/"udpStart","udpCb","udpStop","udpSend","tcpOpen","tcpClose","tcpSend","tcpCb","tcpListen","tcpEnable", //10+8+8+8+8+8+5+10 + /* 9*/"envget","envset","sndVol","rfidGet","playTime","sndRefresh","sndWrite","sndRead","sndFeed","sndAmpli", //10+8+8+8+8+8+5+10+10 + /*10*/"netCb","netSend","netState","netMac","netChk","netSetmode","netScan","netAuth","netPmk","netRssi", //10+8+8+8+8+8+5+10+10+10 + /*11*/"netSeqAdd","strgetword","strputword","atoi","htoi","itoa","ctoa","itoh", //10+8+8+8+8+8+5+10+10+10+8 + /*12*/"ctoh","itobin2","listswitch","listswitchstr","corePP","corePush","corePull","coreBit0", //10+8+8+8+8+8+5+10+10+10+8+8 + /*13*/"reboot","flashFirmware","strcmp","adp2wav","wav2adp","alaw2wav","wav2alaw", //10+8+8+8+8+8+5+10+10+10+8+8+7 + /*14*/"crypt","uncrypt","rfidGetList","rfidRead","rfidWrite", //10+8+8+8+8+8+5+10+10+10+8+8+7+5 + /*15*/"i2cRead", "i2cWrite", //10+8+8+8+8+8+5+10+10+10+8+8+7+5+2 => 115 + /*16*/"strright","strcrypt8","loadf","savef" + }; // Opcode int coreval[]= -/* 1*/{0,0,0,0,0,OPhd,OPtl,OPSecholn,OPSecho,OPIecholn, -/* 2*/OPIecho,OPtime_ms,OPtabnew,OPabs,OPmin,OPmax,OPrand,OPsrand, -/* 3*/OPtime,OPstrnew,OPstrset,OPstrcpy,OPvstrcmp,OPstrfind,OPstrfindrev,OPstrlen, -/* 4*/OPstrget,OPstrsub,OPstrcat,OPtablen,OPstrcatlist,OPled,OPmotorset,OPmotorget, -/* 5*/OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg, -/* 6*/OPbutton2,OPbutton3,OPplayStart,OPplayFeed,OPplayStop,OPrecStart,OPrecStop,OPrecVol, -/* 7*/OPload,OPgc,OPsave,OPbytecode,OPloopcb, -/* 8*/OPudpStart,OPudpCb,OPudpStop,OPudpSend,OPtcpOpen,OPtcpClose,OPtcpSend,OPtcpCb,OPtcpListen,OPtcpEnable, -/* 9*/OPenvget,OPenvset,OPsndVol,OPrfidGet,OPplayTime,OPsndRefresh,OPsndWrite,OPsndRead,OPsndFeed,OPsndAmpli, -/*10*/OPnetCb,OPnetSend,OPnetState,OPnetMac,OPnetChk,OPnetSetmode,OPnetScan,OPnetAuth,OPnetPmk,OPnetRssi, -/*11*/OPnetSeqAdd,OPstrgetword,OPstrputword,OPatoi,OPhtoi,OPitoa,OPctoa,OPitoh, -/*12*/OPctoh,OPitobin2,OPlistswitch,OPlistswitchstr,OPcorePP,OPcorePush,OPcorePull,OPcoreBit0, -/*13*/OPreboot,OPflashFirmware,OPstrcmp,OPadp2wav,OPwav2adp,OPalaw2wav,OPwav2alaw, -/*14*/OPcrypt,OPuncrypt,OPrfidGetList,OPrfidRead,OPrfidWrite, -/*15*/OPi2cRead, OPi2cWrite, -/*16*/OPstrright,OPstrright,OPstrright,OPstrright, -}; + /* 1*/ {0,0,0,0,0,OPhd,OPtl,OPSecholn,OPSecho,OPIecholn, + /* 2*/OPIecho,OPtime_ms,OPtabnew,OPabs,OPmin,OPmax,OPrand,OPsrand, + /* 3*/OPtime,OPstrnew,OPstrset,OPstrcpy,OPvstrcmp,OPstrfind,OPstrfindrev,OPstrlen, + /* 4*/OPstrget,OPstrsub,OPstrcat,OPtablen,OPstrcatlist,OPled,OPmotorset,OPmotorget, + /* 5*/OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg,OPfixarg, + /* 6*/OPbutton2,OPbutton3,OPplayStart,OPplayFeed,OPplayStop,OPrecStart,OPrecStop,OPrecVol, + /* 7*/OPload,OPgc,OPsave,OPbytecode,OPloopcb, + /* 8*/OPudpStart,OPudpCb,OPudpStop,OPudpSend,OPtcpOpen,OPtcpClose,OPtcpSend,OPtcpCb,OPtcpListen,OPtcpEnable, + /* 9*/OPenvget,OPenvset,OPsndVol,OPrfidGet,OPplayTime,OPsndRefresh,OPsndWrite,OPsndRead,OPsndFeed,OPsndAmpli, + /*10*/OPnetCb,OPnetSend,OPnetState,OPnetMac,OPnetChk,OPnetSetmode,OPnetScan,OPnetAuth,OPnetPmk,OPnetRssi, + /*11*/OPnetSeqAdd,OPstrgetword,OPstrputword,OPatoi,OPhtoi,OPitoa,OPctoa,OPitoh, + /*12*/OPctoh,OPitobin2,OPlistswitch,OPlistswitchstr,OPcorePP,OPcorePush,OPcorePull,OPcoreBit0, + /*13*/OPreboot,OPflashFirmware,OPstrcmp,OPadp2wav,OPwav2adp,OPalaw2wav,OPwav2alaw, + /*14*/OPcrypt,OPuncrypt,OPrfidGetList,OPrfidRead,OPrfidWrite, + /*15*/OPi2cRead, OPi2cWrite, + /*16*/OPstrright,OPstrright,OPstrright,OPstrright, + }; // N args int corecode[]= -/* 1*/{CODE_TYPE,CODE_TYPE,CODE_TYPE,CODE_TYPE,CODE_TYPE,1,1,1,1,1, -/* 2*/1,0,2,1,2,2,0,1, -/* 3*/0,1,3,5,5,5,5,1, -/* 4*/2,3,2,1,1,2,2,1, -/* 5*/2,2,2,2,2,2,2,2, -/* 6*/0,0,2,3,0,3,0,2, -/* 7*/5,0,5,1,1, -/* 8*/1,1,1,6,2,1,4,1,1,2, -/* 9*/0,1,1,0,0,0,2,1,3,1, -/*10*/1,6,0,0,4,3,1,4,2,0, -/*11*/2,2,3,1,1,1,1,1, -/*12*/1,1,2,2,0,1,1,2, -/*13*/2,3,2,5,5,6,6, -/*14*/5,5,0,2,3, -/*15*/2,3, -/*16*/2,3,1,2 -}; + /* 1*/ {CODE_TYPE,CODE_TYPE,CODE_TYPE,CODE_TYPE,CODE_TYPE,1,1,1,1,1, + /* 2*/1,0,2,1,2,2,0,1, + /* 3*/0,1,3,5,5,5,5,1, + /* 4*/2,3,2,1,1,2,2,1, + /* 5*/2,2,2,2,2,2,2,2, + /* 6*/0,0,2,3,0,3,0,2, + /* 7*/5,0,5,1,1, + /* 8*/1,1,1,6,2,1,4,1,1,2, + /* 9*/0,1,1,0,0,0,2,1,3,1, + /*10*/1,6,0,0,4,3,1,4,2,0, + /*11*/2,2,3,1,1,1,1,1, + /*12*/1,1,2,2,0,1,1,2, + /*13*/2,3,2,5,5,6,6, + /*14*/5,5,0,2,3, + /*15*/2,3, + /*16*/2,3,1,2 + }; // Proto const char* coretype[]= -/* 1*/{"I","S","F","Env","Xml","fun[list u0]u0","fun[list u0]list u0","fun[S]S","fun[S]S","fun[u0]u0", -/* 2*/"fun[u0]u0","fun[]I","fun[u0 I]tab u0","fun[I]I","fun[I I]I","fun[I I]I","fun[]I","fun[I]I", -/* 3*/"fun[]I","fun[I]S","fun[S I I]S","fun[S I S I I]S","fun[S I S I I]I","fun[S I S I I]I","fun[S I S I I]I","fun[S]I", -/* 4*/"fun[S I]I","fun[S I I]S","fun[S S]S","fun[tab u0]I","fun[list S]S","fun[I I]I","fun[I I]I","fun[I]I", -/* 5*/"fun[fun[u0]u100 u0]fun[]u100", - "fun[fun[u1 u0]u100 u0]fun[u1]u100", - "fun[fun[u1 u2 u0]u100 u0]fun[u1 u2]u100", - "fun[fun[u1 u2 u3 u0]u100 u0]fun[u1 u2 u3]u100", - "fun[fun[u1 u2 u3 u4 u0]u100 u0]fun[u1 u2 u3 u4]u100", - "fun[fun[u1 u2 u3 u4 u5 u0]u100 u0]fun[u1 u2 u3 u4 u5]u100", - "fun[fun[u1 u2 u3 u4 u5 u6 u0]u100 u0]fun[u1 u2 u3 u4 u5 u6]u100", - "fun[fun[u1 u2 u3 u4 u5 u6 u7 u0]u100 u0]fun[u1 u2 u3 u4 u5 u6 u7]u100", -/* 6*/"fun[]I","fun[]I","fun[I fun[I]I]I","fun[S I I]I","fun[]I","fun[I I fun[S]I]I","fun[]I","fun[S I]I", -/* 7*/"fun[S I S I I]I","fun[]I","fun[S I S I I]I","fun[S]S","fun[fun[]I]fun[]I", -/* 8*/"fun[I]I","fun[fun[I S S]I]fun[I S S]I","fun[I]I","fun[I S I S I I]I","fun[S I]I","fun[I]I","fun[I S I I]I","fun[fun[I I S]I]fun[I I S]I","fun[I]I","fun[I I]I", -/* 9*/"fun[]S","fun[S]S","fun[I]I","fun[]S","fun[]I","fun[]I","fun[I I]I","fun[I]I","fun[S I I]I","fun[I]I", -/*10*/"fun[fun[S S]I]fun[S S]I","fun[S I I S I I]I","fun[]I","fun[]S","fun[S I I I]I","fun[I S I]I","fun[S]list[S S S I I I I]","fun[[S S S I I I I] I I S][S S S I I I I]","fun[S S]S","fun[]I", -/*11*/"fun[S I]S","fun[S I]I","fun[S I I]S","fun[S]I","fun[S]I","fun[I]S","fun[I]S","fun[I]S", -/*12*/"fun[I]S","fun[I]S","fun[list[u0 u1] u0]u1","fun[list[S u1] S]u1","fun[]I","fun[u0]u0","fun[I]I","fun[u0 I]u0", -/*13*/"fun[I I]I","fun[S I I]I","fun[S S]I","fun[S I S I I]S","fun[S I S I I]S","fun[S I S I I I]S","fun[S I S I I I]S", -/*14*/"fun[S I I I I]I","fun[S I I I I]I","fun[]list S","fun[S I]S","fun[S I S]I", -/*15*/"fun[I I]S", "fun[I S I]I", -/*16*/"fun[S I]S","fun[S I I][S I]","fun[S]S","fun[S S]I" -}; + /* 1*/ {"I","S","F","Env","Xml","fun[list u0]u0","fun[list u0]list u0","fun[S]S","fun[S]S","fun[u0]u0", + /* 2*/"fun[u0]u0","fun[]I","fun[u0 I]tab u0","fun[I]I","fun[I I]I","fun[I I]I","fun[]I","fun[I]I", + /* 3*/"fun[]I","fun[I]S","fun[S I I]S","fun[S I S I I]S","fun[S I S I I]I","fun[S I S I I]I","fun[S I S I I]I","fun[S]I", + /* 4*/"fun[S I]I","fun[S I I]S","fun[S S]S","fun[tab u0]I","fun[list S]S","fun[I I]I","fun[I I]I","fun[I]I", + /* 5*/"fun[fun[u0]u100 u0]fun[]u100", + "fun[fun[u1 u0]u100 u0]fun[u1]u100", + "fun[fun[u1 u2 u0]u100 u0]fun[u1 u2]u100", + "fun[fun[u1 u2 u3 u0]u100 u0]fun[u1 u2 u3]u100", + "fun[fun[u1 u2 u3 u4 u0]u100 u0]fun[u1 u2 u3 u4]u100", + "fun[fun[u1 u2 u3 u4 u5 u0]u100 u0]fun[u1 u2 u3 u4 u5]u100", + "fun[fun[u1 u2 u3 u4 u5 u6 u0]u100 u0]fun[u1 u2 u3 u4 u5 u6]u100", + "fun[fun[u1 u2 u3 u4 u5 u6 u7 u0]u100 u0]fun[u1 u2 u3 u4 u5 u6 u7]u100", + /* 6*/"fun[]I","fun[]I","fun[I fun[I]I]I","fun[S I I]I","fun[]I","fun[I I fun[S]I]I","fun[]I","fun[S I]I", + /* 7*/"fun[S I S I I]I","fun[]I","fun[S I S I I]I","fun[S]S","fun[fun[]I]fun[]I", + /* 8*/"fun[I]I","fun[fun[I S S]I]fun[I S S]I","fun[I]I","fun[I S I S I I]I","fun[S I]I","fun[I]I","fun[I S I I]I","fun[fun[I I S]I]fun[I I S]I","fun[I]I","fun[I I]I", + /* 9*/"fun[]S","fun[S]S","fun[I]I","fun[]S","fun[]I","fun[]I","fun[I I]I","fun[I]I","fun[S I I]I","fun[I]I", + /*10*/"fun[fun[S S]I]fun[S S]I","fun[S I I S I I]I","fun[]I","fun[]S","fun[S I I I]I","fun[I S I]I","fun[S]list[S S S I I I I]","fun[[S S S I I I I] I I S][S S S I I I I]","fun[S S]S","fun[]I", + /*11*/"fun[S I]S","fun[S I]I","fun[S I I]S","fun[S]I","fun[S]I","fun[I]S","fun[I]S","fun[I]S", + /*12*/"fun[I]S","fun[I]S","fun[list[u0 u1] u0]u1","fun[list[S u1] S]u1","fun[]I","fun[u0]u0","fun[I]I","fun[u0 I]u0", + /*13*/"fun[I I]I","fun[S I I]I","fun[S S]I","fun[S I S I I]S","fun[S I S I I]S","fun[S I S I I I]S","fun[S I S I I I]S", + /*14*/"fun[S I I I I]I","fun[S I I I I]I","fun[]list S","fun[S I]S","fun[S I S]I", + /*15*/"fun[I I]S", "fun[I S I]I", + /*16*/"fun[S I]S","fun[S I I][S I]","fun[S]S","fun[S S]I" + }; intptr_t Compiler::addstdlibcore() { - intptr_t k; - if (k=addnative(NBcore,corename,coreval,corecode,coretype,m)) return k; -/* FILE* f=fopen("bc.txt","w"); - for(k=0;ksizeinternalbuffer) - { - delete internalbuffer; - sizeinternalbuffer=size+1; - internalbuffer=new char[sizeinternalbuffer]; - } - memcpy(internalbuffer,s,size); - internalbuffer[size]=0; - echo(msk,internalbuffer); + if (size+1>sizeinternalbuffer) + { + delete internalbuffer; + sizeinternalbuffer=size+1; + internalbuffer=new char[sizeinternalbuffer]; + } + memcpy(internalbuffer,s,size); + internalbuffer[size]=0; + echo(msk,internalbuffer); } void Terminal::printf(int msk,const char *format, ...) { - va_list arglist; - int sizeout; - va_start(arglist,format); - while(((sizeout=vsnprintf(internalbuffer,sizeinternalbuffer-1,format,arglist))<0)||(sizeout>=sizeinternalbuffer-1)) - { - delete internalbuffer; - sizeinternalbuffer*=2; - internalbuffer=new char[sizeinternalbuffer]; - } - va_end(arglist); - echo(msk,internalbuffer); + va_list arglist; + int sizeout; + va_start(arglist,format); + while(((sizeout=vsnprintf(internalbuffer,sizeinternalbuffer-1,format,arglist))<0)||(sizeout>=sizeinternalbuffer-1)) + { + delete internalbuffer; + sizeinternalbuffer*=2; + internalbuffer=new char[sizeinternalbuffer]; + } + va_end(arglist); + echo(msk,internalbuffer); } diff --git a/src/vcomp/util.cpp b/src/vcomp/util.cpp index 8cd15bb..67f5076 100644 --- a/src/vcomp/util.cpp +++ b/src/vcomp/util.cpp @@ -22,28 +22,34 @@ Util::Util(Memory* mem) { - m=mem; - compiler=new Compiler(m); - interpreter=new Interpreter(m); + m=mem; + compiler=new Compiler(m); + interpreter=new Interpreter(m); } Util::~Util() { - delete interpreter; - delete compiler; + delete interpreter; + delete compiler; } void Util::stop() { - interpreter->stop(); - compiler->stop(); + interpreter->stop(); + compiler->stop(); } int Util::start() { - int k; - if (k=compiler->start()) return k; - if (k=interpreter->start()) return k; - return 0; + int k; + if (k=compiler->start()) + { + return k; + } + if (k=interpreter->start()) + { + return k; + } + return 0; } diff --git a/src/vm/vaudio.c b/src/vm/vaudio.c index 23f7919..18634cb 100644 --- a/src/vm/vaudio.c +++ b/src/vm/vaudio.c @@ -28,127 +28,163 @@ int play_r; void audioInit() { - play_w=play_r=0; + play_w=play_r=0; } void audioPlayStart(int freq,int bps,int stereo,int trytofeed) { - play_w=play_r=0; + play_w=play_r=0; #ifdef VSIMU - PlayStart(freq,stereo,44100,bps,3); + PlayStart(freq,stereo,44100,bps,3); #endif #ifdef VREAL - // set_vlsi_volume(get_adc_value()/2); //volume on 8bits, 0x00 => maximum - play_start(trytofeed); + // set_vlsi_volume(get_adc_value()/2); //volume on 8bits, 0x00 => maximum + play_start(trytofeed); #endif } int audioPlayFeed(char *src,int len) { - //#ifdef VSIMU - int i_end; - if (!src) - { + //#ifdef VSIMU + int i_end; + if (!src) + { #ifdef VSIMU - PlayEof(); + PlayEof(); #endif #ifdef VREAL play_eof(); #endif - return 0; - } - i_end=play_r-1; - if (play_w>=play_r) i_end=AUDIO_FIFOPLAY+play_r-1; - if (play_w+len>=i_end) len=i_end-play_w; - else i_end=play_w+len; - - if (i_end<=play_w) return 0; - if (i_end maximum - //Play file - play_audio((UBYTE*)src,len); - #endif - */ + return 0; + } + i_end=play_r-1; + if (play_w>=play_r) + { + i_end=AUDIO_FIFOPLAY+play_r-1; + } + if (play_w+len>=i_end) + { + len=i_end-play_w; + } + else + { + i_end=play_w+len; + } + + if (i_end<=play_w) + { + return 0; + } + if (i_end maximum + //Play file + play_audio((UBYTE*)src,len); + #endif + */ } void audioPlayStop() { #ifdef VSIMU - PlayStop(); + PlayStop(); #endif #ifdef VREAL - play_stop(); + play_stop(); #endif } int audioPlayTryFeed(int ask) { - int lastw; - int play_w0=play_w; - int dispo=play_w-play_r; - if (dispo<0) dispo+=AUDIO_FIFOPLAY; - if (dispo>=ask) return 0; - do - { - lastw=play_w; - dispo=play_w-play_r; - if (dispo<0) dispo+=AUDIO_FIFOPLAY; - - VPUSH(INTTOVAL(AUDIO_FIFOPLAY-dispo)); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBPLAY)); - if (VSTACKGET(0)==NIL) - { - VPULL(); - VPULL(); - return 0; - } - interpGo(); - VPULL(); - } - while(play_w!=lastw); - if (play_w!=play_w0) return 1; - return 0; + int lastw; + int play_w0=play_w; + int dispo=play_w-play_r; + if (dispo<0) + { + dispo+=AUDIO_FIFOPLAY; + } + if (dispo>=ask) + { + return 0; + } + do + { + lastw=play_w; + dispo=play_w-play_r; + if (dispo<0) + { + dispo+=AUDIO_FIFOPLAY; + } + + VPUSH(INTTOVAL(AUDIO_FIFOPLAY-dispo)); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBPLAY)); + if (VSTACKGET(0)==NIL) + { + VPULL(); + VPULL(); + return 0; + } + interpGo(); + VPULL(); + } + while(play_w!=lastw); + if (play_w!=play_w0) + { + return 1; + } + return 0; } int audioPlayFetchByte() { - int res; - if (play_w==play_r) return -1; - res=audioFifoPlay[play_r++]&255; - if (play_r>=AUDIO_FIFOPLAY) play_r-=AUDIO_FIFOPLAY; - return res; + int res; + if (play_w==play_r) + { + return -1; + } + res=audioFifoPlay[play_r++]&255; + if (play_r>=AUDIO_FIFOPLAY) + { + play_r-=AUDIO_FIFOPLAY; + } + return res; } int audioPlayFetch(char* dst,int ask) { - int n=0; - int c; - audioPlayTryFeed(ask); - while(n88) - stepIndex = 88; - StepIndex = stepIndex; - - diff = step>>3; - if(adpcm&4) - diff += step; - if(adpcm&2) - diff += step>>1; - if(adpcm&1) - diff += step>>2; - - predicedValue = PredictedValue; - if(adpcm&8) - predicedValue -= diff; - else - predicedValue += diff; - if(predicedValue<-0x8000) - predicedValue = -0x8000; - else if(predicedValue>0x7fff) - predicedValue = 0x7fff; - PredictedValue = predicedValue; - // if (predicedValue>vmax) vmax=predicedValue; - // if (predicedValue>16; - return predicedValue; + int diff; + int predicedValue; + int stepIndex = StepIndex; + int step = IMA_ADPCMStepTable[stepIndex]; + + stepIndex += IMA_ADPCMIndexTable[adpcm&7]; + if(stepIndex<0) + { + stepIndex = 0; + } + else if(stepIndex>88) + { + stepIndex = 88; + } + StepIndex = stepIndex; + + diff = step>>3; + if(adpcm&4) + { + diff += step; + } + if(adpcm&2) + { + diff += step>>1; + } + if(adpcm&1) + { + diff += step>>2; + } + + predicedValue = PredictedValue; + if(adpcm&8) + { + predicedValue -= diff; + } + else + { + predicedValue += diff; + } + if(predicedValue<-0x8000) + { + predicedValue = -0x8000; + } + else if(predicedValue>0x7fff) + { + predicedValue = 0x7fff; + } + PredictedValue = predicedValue; + // if (predicedValue>vmax) vmax=predicedValue; + // if (predicedValue>16; + return predicedValue; } int myvolume() { - // if (vmax>-vmin) return vmax; - // return -vmin; - return vol>>8; + // if (vmax>-vmin) return vmax; + // return -vmin; + return vol>>8; } int audioRecVol(unsigned char* src,int len,int start) { - int s0,st,i; + int s0,st,i; - if (start+256>len) return 0; - src+=start; - s0=(src[0]&255)+((src[1]&255)<<8); src+=2; - st=(src[0]&255)+((src[1]&255)<<8); src+=2; - mydecodeinit(s0,st); - for(i=0;i<252;i++) - { - int c=(*(src++))&255; - mydecode(c&15); - mydecode((c>>4)&15); - } - return myvolume(); + if (start+256>len) + { + return 0; + } + src+=start; + s0=(src[0]&255)+((src[1]&255)<<8); + src+=2; + st=(src[0]&255)+((src[1]&255)<<8); + src+=2; + mydecodeinit(s0,st); + for(i=0; i<252; i++) + { + int c=(*(src++))&255; + mydecode(c&15); + mydecode((c>>4)&15); + } + return myvolume(); } void adpcmdecode(unsigned char* src,char *dstc) { - short* dst=(short*)dstc; - int s0,st,i; - s0=(src[0]&255)+((src[1]&255)<<8); src+=2; - st=(src[0]&255)+((src[1]&255)<<8); src+=2; - mydecodeinit(s0,st); - *(dst++)=s0; - for(i=0;i<252;i++) - { - int c=(*(src++))&255; - *(dst++)=mydecode(c&15); - *(dst++)=mydecode((c>>4)&15); - } + short* dst=(short*)dstc; + int s0,st,i; + s0=(src[0]&255)+((src[1]&255)<<8); + src+=2; + st=(src[0]&255)+((src[1]&255)<<8); + src+=2; + mydecodeinit(s0,st); + *(dst++)=s0; + for(i=0; i<252; i++) + { + int c=(*(src++))&255; + *(dst++)=mydecode(c&15); + *(dst++)=mydecode((c>>4)&15); + } } int myencode(int16_t pcm16) { - int step,diff; - int predicedValue = PredictedValue; - int stepIndex = StepIndex; - - int delta = pcm16-predicedValue; - unsigned int value; - if(delta>=0) - value = 0; - else - { - value = 8; - delta = -delta; - } - - step = IMA_ADPCMStepTable[stepIndex]; - diff = step>>3; - if(delta>step) - { - value |= 4; - delta -= step; - diff += step; - } - step >>= 1; - if(delta>step) - { - value |= 2; - delta -= step; - diff += step; - } - step >>= 1; - if(delta>step) - { - value |= 1; - diff += step; - } - - if(value&8) - predicedValue -= diff; - else - predicedValue += diff; - if(predicedValue<-0x8000) - predicedValue = -0x8000; - else if(predicedValue>0x7fff) - predicedValue = 0x7fff; - PredictedValue = predicedValue; - - stepIndex += IMA_ADPCMIndexTable[value&7]; - if(stepIndex<0) - stepIndex = 0; - else if(stepIndex>88) - stepIndex = 88; - StepIndex = stepIndex; - - return value&15; + int step,diff; + int predicedValue = PredictedValue; + int stepIndex = StepIndex; + + int delta = pcm16-predicedValue; + unsigned int value; + if(delta>=0) + { + value = 0; + } + else + { + value = 8; + delta = -delta; + } + + step = IMA_ADPCMStepTable[stepIndex]; + diff = step>>3; + if(delta>step) + { + value |= 4; + delta -= step; + diff += step; + } + step >>= 1; + if(delta>step) + { + value |= 2; + delta -= step; + diff += step; + } + step >>= 1; + if(delta>step) + { + value |= 1; + diff += step; + } + + if(value&8) + { + predicedValue -= diff; + } + else + { + predicedValue += diff; + } + if(predicedValue<-0x8000) + { + predicedValue = -0x8000; + } + else if(predicedValue>0x7fff) + { + predicedValue = 0x7fff; + } + PredictedValue = predicedValue; + + stepIndex += IMA_ADPCMIndexTable[value&7]; + if(stepIndex<0) + { + stepIndex = 0; + } + else if(stepIndex>88) + { + stepIndex = 88; + } + StepIndex = stepIndex; + + return value&15; } void adpcmencode(short* src,char *dst) { - int delta,stepIndex,i; - short sample1,sample2; - short* p; - - sample1=src[0]; - sample2=src[1]; - - PredictedValue = sample1; - delta = sample2-sample1; - if(delta<0) - delta = - delta; - if(delta>32767) - delta = 32767; - stepIndex = 0; - while(IMA_ADPCMStepTable[stepIndex]<(unsigned)delta) - stepIndex++; - StepIndex = stepIndex; - - p=(short*)dst; - p[0]=sample1; - p[1]=StepIndex; - - dst+=4; - for(i=1;i<505;i+=2) - { - *(dst++)=myencode(src[i])+(myencode(src[i+1])<<4); - } -} - -const short alaw[256]={ --5504,-5248,-6016,-5760,-4480,-4224,-4992,-4736,-7552,-7296,-8064,-7808,-6528,-6272,-7040,-6784,-2752,-2624,-3008,-2880,-2240,-2112,-2496,-2368,-3776,-3648,-4032,-3904,-3264,-3136,-3520,-3392, --22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,-11008,-10496,-12032,-11520,-8960,-8448,-9984,-9472,-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568, --344,-328,-376,-360,-280,-264,-312,-296,-472,-456,-504,-488,-408,-392,-440,-424,-88,-72,-120,-104,-24,-8,-56,-40,-216,-200,-248,-232,-152,-136,-184,-168, --1376,-1312,-1504,-1440,-1120,-1056,-1248,-1184,-1888,-1824,-2016,-1952,-1632,-1568,-1760,-1696,-688,-656,-752,-720,-560,-528,-624,-592,-944,-912,-1008,-976,-816,-784,-880,-848, -5504,5248,6016,5760,4480,4224,4992,4736,7552,7296,8064,7808,6528,6272,7040,6784,2752,2624,3008,2880,2240,2112,2496,2368,3776,3648,4032,3904,3264,3136,3520,3392, -22016,20992,24064,23040,17920,16896,19968,18944,30208,29184,32256,31232,26112,25088,28160,27136,11008,10496,12032,11520,8960,8448,9984,9472,15104,14592,16128,15616,13056,12544,14080,13568, -344,328,376,360,280,264,312,296,472,456,504,488,408,392,440,424,88,72,120,104,24,8,56,40,216,200,248,232,152,136,184,168, -1376,1312,1504,1440,1120,1056,1248,1184,1888,1824,2016,1952,1632,1568,1760,1696,688,656,752,720,560,528,624,592,944,912,1008,976,816,784,880,848}; -const short mulaw[256]={ --32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,-11900,-11388,-10876,-10364,-9852,-9340,-8828,-8316, --7932,-7676,-7420,-7164,-6908,-6652,-6396,-6140,-5884,-5628,-5372,-5116,-4860,-4604,-4348,-4092,-3900,-3772,-3644,-3516,-3388,-3260,-3132,-3004,-2876,-2748,-2620,-2492,-2364,-2236,-2108,-1980, --1884,-1820,-1756,-1692,-1628,-1564,-1500,-1436,-1372,-1308,-1244,-1180,-1116,-1052,-988,-924,-876,-844,-812,-780,-748,-716,-684,-652,-620,-588,-556,-524,-492,-460,-428,-396, --372,-356,-340,-324,-308,-292,-276,-260,-244,-228,-212,-196,-180,-164,-148,-132,-120,-112,-104,-96,-88,-80,-72,-64,-56,-48,-40,-32,-24,-16,-8,0, -32124,31100,30076,29052,28028,27004,25980,24956,23932,22908,21884,20860,19836,18812,17788,16764,15996,15484,14972,14460,13948,13436,12924,12412,11900,11388,10876,10364,9852,9340,8828,8316, -7932,7676,7420,7164,6908,6652,6396,6140,5884,5628,5372,5116,4860,4604,4348,4092,3900,3772,3644,3516,3388,3260,3132,3004,2876,2748,2620,2492,2364,2236,2108,1980, -1884,1820,1756,1692,1628,1564,1500,1436,1372,1308,1244,1180,1116,1052,988,924,876,844,812,780,748,716,684,652,620,588,556,524,492,460,428,396, -372,356,340,324,308,292,276,260,244,228,212,196,180,164,148,132,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0}; -const char exposant[256]={ -0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, -6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}; + int delta,stepIndex,i; + short sample1,sample2; + short* p; + + sample1=src[0]; + sample2=src[1]; + + PredictedValue = sample1; + delta = sample2-sample1; + if(delta<0) + { + delta = - delta; + } + if(delta>32767) + { + delta = 32767; + } + stepIndex = 0; + while(IMA_ADPCMStepTable[stepIndex]<(unsigned)delta) + { + stepIndex++; + } + StepIndex = stepIndex; + + p=(short*)dst; + p[0]=sample1; + p[1]=StepIndex; + + dst+=4; + for(i=1; i<505; i+=2) + { + *(dst++)=myencode(src[i])+(myencode(src[i+1])<<4); + } +} + +const short alaw[256]= +{ + -5504,-5248,-6016,-5760,-4480,-4224,-4992,-4736,-7552,-7296,-8064,-7808,-6528,-6272,-7040,-6784,-2752,-2624,-3008,-2880,-2240,-2112,-2496,-2368,-3776,-3648,-4032,-3904,-3264,-3136,-3520,-3392, + -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,-11008,-10496,-12032,-11520,-8960,-8448,-9984,-9472,-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568, + -344,-328,-376,-360,-280,-264,-312,-296,-472,-456,-504,-488,-408,-392,-440,-424,-88,-72,-120,-104,-24,-8,-56,-40,-216,-200,-248,-232,-152,-136,-184,-168, + -1376,-1312,-1504,-1440,-1120,-1056,-1248,-1184,-1888,-1824,-2016,-1952,-1632,-1568,-1760,-1696,-688,-656,-752,-720,-560,-528,-624,-592,-944,-912,-1008,-976,-816,-784,-880,-848, + 5504,5248,6016,5760,4480,4224,4992,4736,7552,7296,8064,7808,6528,6272,7040,6784,2752,2624,3008,2880,2240,2112,2496,2368,3776,3648,4032,3904,3264,3136,3520,3392, + 22016,20992,24064,23040,17920,16896,19968,18944,30208,29184,32256,31232,26112,25088,28160,27136,11008,10496,12032,11520,8960,8448,9984,9472,15104,14592,16128,15616,13056,12544,14080,13568, + 344,328,376,360,280,264,312,296,472,456,504,488,408,392,440,424,88,72,120,104,24,8,56,40,216,200,248,232,152,136,184,168, + 1376,1312,1504,1440,1120,1056,1248,1184,1888,1824,2016,1952,1632,1568,1760,1696,688,656,752,720,560,528,624,592,944,912,1008,976,816,784,880,848 +}; +const short mulaw[256]= +{ + -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,-11900,-11388,-10876,-10364,-9852,-9340,-8828,-8316, + -7932,-7676,-7420,-7164,-6908,-6652,-6396,-6140,-5884,-5628,-5372,-5116,-4860,-4604,-4348,-4092,-3900,-3772,-3644,-3516,-3388,-3260,-3132,-3004,-2876,-2748,-2620,-2492,-2364,-2236,-2108,-1980, + -1884,-1820,-1756,-1692,-1628,-1564,-1500,-1436,-1372,-1308,-1244,-1180,-1116,-1052,-988,-924,-876,-844,-812,-780,-748,-716,-684,-652,-620,-588,-556,-524,-492,-460,-428,-396, + -372,-356,-340,-324,-308,-292,-276,-260,-244,-228,-212,-196,-180,-164,-148,-132,-120,-112,-104,-96,-88,-80,-72,-64,-56,-48,-40,-32,-24,-16,-8,0, + 32124,31100,30076,29052,28028,27004,25980,24956,23932,22908,21884,20860,19836,18812,17788,16764,15996,15484,14972,14460,13948,13436,12924,12412,11900,11388,10876,10364,9852,9340,8828,8316, + 7932,7676,7420,7164,6908,6652,6396,6140,5884,5628,5372,5116,4860,4604,4348,4092,3900,3772,3644,3516,3388,3260,3132,3004,2876,2748,2620,2492,2364,2236,2108,1980, + 1884,1820,1756,1692,1628,1564,1500,1436,1372,1308,1244,1180,1116,1052,988,924,876,844,812,780,748,716,684,652,620,588,556,524,492,460,428,396, + 372,356,340,324,308,292,276,260,244,228,212,196,180,164,148,132,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0 +}; +const char exposant[256]= +{ + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 +}; char alawencode(int pcm) { - int exponent,mantissa,sign; - char alaw; + int exponent,mantissa,sign; + char alaw; sign = (pcm & 0x8000) >> 8; - if (sign != 0) pcm = -pcm; + if (sign != 0) + { + pcm = -pcm; + } //The magnitude must fit in 15 bits to avoid overflow - if (pcm > 32635) pcm = 32635; + if (pcm > 32635) + { + pcm = 32635; + } - exponent=exposant[pcm>>8]; + exponent=exposant[pcm>>8]; // exponent = 7; // for (expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1) { } @@ -478,17 +577,23 @@ char alawencode(int pcm) char mulawencode(int pcm) //16-bit { - int exponent,mantissa,sign; - char mulaw; + int exponent,mantissa,sign; + char mulaw; //Get the sign bit. Shift it for later //use without further modification sign = (pcm & 0x8000) >> 8; //If the number is negative, make it //positive (now it's a magnitude) - if (sign != 0) pcm = -pcm; + if (sign != 0) + { + pcm = -pcm; + } //The magnitude must be less than 32635 to avoid overflow - if (pcm > 32635) pcm = 32635; + if (pcm > 32635) + { + pcm = 32635; + } //Add 132 to guarantee a 1 in //the eight bits after the sign bit pcm += 0x84; @@ -503,7 +608,7 @@ char mulawencode(int pcm) //16-bit * (i.e. if first 1 at position 7 -> exponent = 2) */ // exponent = 7; //Move to the right and decrement exponent until we hit the 1 - exponent=exposant[pcm>>8]; + exponent=exposant[pcm>>8]; // for (expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { } /* The last part - the "mantissa" @@ -528,150 +633,198 @@ char mulawencode(int pcm) //16-bit short alawdecode(unsigned char v) { - return alaw[v]; + return alaw[v]; } short mulawdecode(unsigned char v) { - return mulaw[v]; + return mulaw[v]; } void AudioAdp2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len) { - if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return; - if (len&255) return; // que des blocs de 256 - if ((len>lsrc-isrc)||(((len>>8)*505*2)>ldst-idst)) return; + if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) + { + return; + } + if (len&255) + { + return; // que des blocs de 256 + } + if ((len>lsrc-isrc)||(((len>>8)*505*2)>ldst-idst)) + { + return; + } // printf("a2w%d\n",len); - src+=isrc; - dst+=idst; - while(len>0) - { - adpcmdecode((unsigned char*)src,dst); - src+=256; - dst+=505*2; - len-=256; - } + src+=isrc; + dst+=idst; + while(len>0) + { + adpcmdecode((unsigned char*)src,dst); + src+=256; + dst+=505*2; + len-=256; + } } void AudioWav2adp(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len) { - int n=0; - int l=len; + int n=0; + int l=len; - if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return; - while(l>0) - { - l-=505*2; - n++; - } - if (l) return;// que des blocs de 505 échantillons 16 bits + if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) + { + return; + } + while(l>0) + { + l-=505*2; + n++; + } + if (l) + { + return; // que des blocs de 505 échantillons 16 bits + } - if ((len>lsrc-isrc)||((n<<8)>ldst-idst)) return; + if ((len>lsrc-isrc)||((n<<8)>ldst-idst)) + { + return; + } // printf("w2a%d\n",len); - src+=isrc; - dst+=idst; - while(len>0) - { - adpcmencode((short*)src,dst); - src+=505*2; - dst+=256; - len-=505*2; - } + src+=isrc; + dst+=idst; + while(len>0) + { + adpcmencode((short*)src,dst); + src+=505*2; + dst+=256; + len-=505*2; + } } void AudioWav2alaw(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu) { - short* p; + short* p; - if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return; - if (len&1) return; + if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) + { + return; + } + if (len&1) + { + return; + } - if ((len>lsrc-isrc)||((len>>1)>ldst-idst)) return; + if ((len>lsrc-isrc)||((len>>1)>ldst-idst)) + { + return; + } // printf("w2l%d\n",len); - src+=isrc; - dst+=idst; - p=(short*)src; - len>>=1; - if (mu) - { - while(len--) *(dst++)=mulawencode(*(p++)); - } - else - { - while(len--) *(dst++)=alawencode(*(p++)); - } + src+=isrc; + dst+=idst; + p=(short*)src; + len>>=1; + if (mu) + { + while(len--) + { + *(dst++)=mulawencode(*(p++)); + } + } + else + { + while(len--) + { + *(dst++)=alawencode(*(p++)); + } + } } void AudioAlaw2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu) { - short* p; - unsigned char* q; + short* p; + unsigned char* q; - if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return; + if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) + { + return; + } - if ((len>lsrc-isrc)||(len+len>ldst-idst)) return; + if ((len>lsrc-isrc)||(len+len>ldst-idst)) + { + return; + } // printf("l2w%d\n",len); - src+=isrc; - dst+=idst; - p=(short*)dst; - q=(unsigned char*)src; - if (mu) - { - while(len--) *(p++)=mulaw[*(q++)]; - } - else - { - while(len--) *(p++)=alaw[*(q++)]; - } + src+=isrc; + dst+=idst; + p=(short*)dst; + q=(unsigned char*)src; + if (mu) + { + while(len--) + { + *(p++)=mulaw[*(q++)]; + } + } + else + { + while(len--) + { + *(p++)=alaw[*(q++)]; + } + } } void audioWrite(int reg,int val) { #ifdef VSIMU - printf("xxxx audioWrite %d %d\n",reg,val); + printf("xxxx audioWrite %d %d\n",reg,val); #endif #ifdef VREAL - vlsi_write_sci(reg,val); + vlsi_write_sci(reg,val); #endif } int audioRead(int reg) { #ifdef VSIMU - printf("xxxx audioRead %d\n",reg); - return 0; + printf("xxxx audioRead %d\n",reg); + return 0; #endif #ifdef VREAL - if (reg==-1) return vlsi_fifo_ready(); - return vlsi_read_sci(reg); + if (reg==-1) + { + return vlsi_fifo_ready(); + } + return vlsi_read_sci(reg); #endif } int audioFeed(char *src,int len) { #ifdef VSIMU - printf("xxxx audioFeed %d\n",len); - return 0; + printf("xxxx audioFeed %d\n",len); + return 0; #endif #ifdef VREAL - return vlsi_feed_sdi((uchar*)src,len); + return vlsi_feed_sdi((uchar*)src,len); #endif } void audioRefresh() { #ifdef VSIMU - printf("xxxx audioRefresh\n"); + printf("xxxx audioRefresh\n"); #endif #ifdef VREAL - play_check(0); - rec_check(); + play_check(0); + rec_check(); #endif } void audioAmpli(int on) { #ifdef VSIMU - printf("xxxx audioAmpli %d\n",on); + printf("xxxx audioAmpli %d\n",on); #endif #ifdef VREAL - vlsi_ampli(on); + vlsi_ampli(on); #endif } diff --git a/src/vm/vinterp.c b/src/vm/vinterp.c index e50063f..318a012 100644 --- a/src/vm/vinterp.c +++ b/src/vm/vinterp.c @@ -63,18 +63,21 @@ void displaybc(int off,int base) // test si dernier appel ? int TFCtest(int p,int pbase) { - int i; - while(bytecode[p]!=OPret) - { - if (bytecode[p]==OPgoto) - { - p++; - i=(bytecode[p]&255)+((bytecode[p+1]&255)<<8); - p=pbase+i; - } - else return 0; - } - return 1; + int i; + while(bytecode[p]!=OPret) + { + if (bytecode[p]==OPgoto) + { + p++; + i=(bytecode[p]&255)+((bytecode[p+1]&255)<<8); + p=pbase+i; + } + else + { + return 0; + } + } + return 1; } @@ -87,1297 +90,1691 @@ int tron=0; // on distingue le cas selon le fait que la valeur empilée est un pointeur ou un entier void interpGo() { - int pc=-1; - int pcbase=-1; - int callstack=1; - int callstackres=1; - int op=OPexec; - int cont; - if (tron) - { - consolestr("go:"); - consoleint(VSTACKGET(0));consolestr("."); - consoleint(vmem_stack);consolestr("/"); - } + int pc=-1; + int pcbase=-1; + int callstack=1; + int callstackres=1; + int op=OPexec; + int cont; + if (tron) + { + consolestr("go:"); + consoleint(VSTACKGET(0)); + consolestr("."); + consoleint(vmem_stack); + consolestr("/"); + } - if (vmem_broken) return; - while(1) - { - do - { - cont=0; - currentop=op; - if (tron) - { + if (vmem_broken) + { + return; + } + while(1) + { + do + { + cont=0; + currentop=op; + if (tron) + { // displaybc(pc-1,pcbase); -/* - consoleint(pc);consolestr(":"); - consoleint(op); - if (op) {consolestr(":");consoleint(255&bytecode[pc]);} - consolestr("\n"); -*/ - // if (bytecode[0x2450]==0) tron=0; - // dump(&bytecode[0x2440],32); - } - CLR_WDT; - switch(op) - { - case OPexec: - { - int p,npc,narg,nloc,i,fun; - p=VSTACKGET(0); - if (ISVALPNT(p)) - { - p=VALTOPNT(p); - fun=VALTOINT(VFETCH(p,0)); - VSTACKSET(0,VFETCH(p,1)); - while(VSTACKGET(0)!=NIL) - { - VPUSH(VFETCH(VALTOPNT(VSTACKGET(0)),1)); - VSTACKSET(1,VFETCH(VALTOPNT(VSTACKGET(1)),0)); - } - // VPULL(); - } - else fun=VALTOINT(p); - VPULL(); - if (fun<0) - { - op=-fun; - cont=1; - break; - } - npc=loaderFunstart(fun); - narg=bytecode[npc]; - nloc=loaderGetShort(bytecode+npc+1); - npc+=3; + /* + consoleint(pc);consolestr(":"); + consoleint(op); + if (op) {consolestr(":");consoleint(255&bytecode[pc]);} + consolestr("\n"); + */ + // if (bytecode[0x2450]==0) tron=0; + // dump(&bytecode[0x2440],32); + } + CLR_WDT; + switch(op) + { + case OPexec: + { + int p,npc,narg,nloc,i,fun; + p=VSTACKGET(0); + if (ISVALPNT(p)) + { + p=VALTOPNT(p); + fun=VALTOINT(VFETCH(p,0)); + VSTACKSET(0,VFETCH(p,1)); + while(VSTACKGET(0)!=NIL) + { + VPUSH(VFETCH(VALTOPNT(VSTACKGET(0)),1)); + VSTACKSET(1,VFETCH(VALTOPNT(VSTACKGET(1)),0)); + } + // VPULL(); + } + else + { + fun=VALTOINT(p); + } + VPULL(); + if (fun<0) + { + op=-fun; + cont=1; + break; + } + npc=loaderFunstart(fun); + narg=bytecode[npc]; + nloc=loaderGetShort(bytecode+npc+1); + npc+=3; - if (callstack<=0) - { - if (TFCtest(pc,pcbase)) - { - int ncall; - pc=VALTOINT(VCALLSTACKGET(callstackres,-3)); - pcbase=VALTOINT(VCALLSTACKGET(callstackres,-2)); - ncall=VALTOINT(VCALLSTACKGET(callstackres,-1)); - callstackres=VALTOINT(VCALLSTACKGET(callstackres,0)); - for(i=0;i>VALTOINT(VSTACKGET(0)))); - VDROPN(1); - break; - case OPneg: - VSTACKSET(0,INTTOVAL(-VALTOINT(VSTACKGET(0)))); - break; - case OPnot: - VSTACKSET(0,INTTOVAL(~VALTOINT(VSTACKGET(0)))); - break; - case OPnon: - VSTACKSET(0,INTTOVAL(VALTOINT(VSTACKGET(0))?0:1)); - break; - case OPeq: - VSTACKSET(1,INTTOVAL((VSTACKGET(1)==VSTACKGET(0))?1:0)); - VDROPN(1); - break; - case OPne: - VSTACKSET(1,INTTOVAL((VSTACKGET(1)!=VSTACKGET(0))?1:0)); - VDROPN(1); - break; - case OPlt: - VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))VALTOINT(VSTACKGET(0)))?1:0)); - VDROPN(1); - break; - case OPle: - VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))<=VALTOINT(VSTACKGET(0)))?1:0)); - VDROPN(1); - break; - case OPge: - VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))>=VALTOINT(VSTACKGET(0)))?1:0)); - VDROPN(1); - break; - case OPgoto: - pc=pcbase+loaderGetShort(&bytecode[pc]); - break; - case OPelse: - if (!VPULL()) pc=pcbase+loaderGetShort(&bytecode[pc]); - else pc+=2; - break; - case OPmktabb: - VPUSH(PNTTOVAL(VMALLOCCLEAR(255&bytecode[pc++]))); - break; - case OPmktab: - VPUSH(PNTTOVAL(VMALLOCCLEAR(VALTOINT(VPULL())))); - break; - case OPdeftabb: - VMKTAB(255&bytecode[pc++]); - break; - case OPdeftab: - VMKTAB(VALTOINT(VPULL())); - break; - case OPfetchb: - { - int i=255&bytecode[pc++]; - int p=VSTACKGET(0); - if (p!=NIL) VSTACKSET(0,VFETCH(VALTOPNT(p),i)); - } - break; - case OPfetch: - { - int i=VPULL(); - int p=VSTACKGET(0); - if ((p==NIL)||(i==NIL)) VSTACKSET(0,NIL); - else - { - i=VALTOINT(i); - p=VALTOPNT(p); - if ((i<0)||(i>=VSIZE(p))) VSTACKSET(0,NIL); - else VSTACKSET(0,VFETCH(p,i)); - } - } - break; - case OPgetglobalb: - VPUSH(VCALLSTACKGET(global_start,255&bytecode[pc++])); - break; - case OPgetglobal: - VSTACKSET(0,VCALLSTACKGET(global_start,VALTOINT(VSTACKGET(0)))); - break; - case OPSecho: - logSecho(VSTACKGET(0),0); - break; - case OPIecho: - logIecho(VSTACKGET(0),0); - break; - case OPsetlocalb: - VCALLSTACKSET(callstack,255&bytecode[pc++],VPULL()); - break; - case OPsetlocal: - { - int v=VALTOINT(VPULL()); - VCALLSTACKSET(callstack,v,VPULL()); - } - break; - case OPsetglobal: - VCALLSTACKSET(global_start,VALTOINT(VSTACKGET(1)),VSTACKGET(0)); - VSTACKSET(1,VSTACKGET(0)); - VPULL(); - break; - case OPsetstructb: - { - int i=255&bytecode[pc++]; - int v=VPULL(); - int p=VSTACKGET(0); - if ((p!=NIL)&&(i!=NIL)) - { - p=VALTOPNT(p); - if ((i>=0)||(i=0)||(i=0)||(i0;i--) VSTACKSET(i,VSTACKGET(i-1)); - VSTACKSET(0,valn); - op=OPexec; - cont=1; - } - } - break; - case OPcallr: - { - int n=VALTOINT(VPULL()); - int valn=VSTACKGET(n); - if (valn==NIL) VDROPN(n); - else - { - int i; - for(i=n;i>0;i--) VSTACKSET(i,VSTACKGET(i-1)); - VSTACKSET(0,valn); - op=OPexec; - cont=1; - } - } - break; - case OPfirst: - { - int p=VSTACKGET(0); - if (p!=NIL) VPUSH(VFETCH(VALTOPNT(p),0)); - else VPUSH(NIL); - } - break; - case OPtime_ms: - VPUSH(INTTOVAL(sysTimems())); - break; - case OPtabnew: - { - int v=VPULL(); - if (v==NIL) VSTACKSET(0,NIL); - else - { - int p,i,v0; - v=VALTOINT(v); - p=VMALLOC(v); - v0=VSTACKGET(0); - for(i=0;iw?w:v)); - } - break; - case OPrand: - VPUSH(INTTOVAL(sysRand())); - break; - case OPsrand: - sysSrand(VALTOINT(VSTACKGET(0))); - break; - case OPtime: - VPUSH(INTTOVAL(sysTime())); - break; - case OPstrnew: - { - int n=VALTOINT(VSTACKGET(0)); - if (n<0) VSTACKSET(0,NIL); - else VSTACKSET(0,PNTTOVAL(VMALLOCBIN(n))); - } - break; - case OPstrset: - { - int v=VALTOINT(VPULL()); - int n=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(n>=0)&&(n=VSIZEBIN(p))) VSTACKSET(0,NIL); - else VSTACKSET(0,INTTOVAL(((VSTARTBIN(p))[n]&255))); - } - break; - case OPstrright: - { - int n=VALTOINT(VPULL()); // 2nd arg, nb of chars to keep - int p=VALTOPNT(VSTACKGET(0)); // 1st arg, pointer to the source string in stack - if(p != NIL) - { - consolestr("Nb:"); - consolehx(n); - consolestr("Str:"); - consolehx(p); - vmemDump(); - } else - VSTACKSET(0,NIL); + for(i=0; i>VALTOINT(VSTACKGET(0)))); + VDROPN(1); + break; + case OPneg: + VSTACKSET(0,INTTOVAL(-VALTOINT(VSTACKGET(0)))); + break; + case OPnot: + VSTACKSET(0,INTTOVAL(~VALTOINT(VSTACKGET(0)))); + break; + case OPnon: + VSTACKSET(0,INTTOVAL(VALTOINT(VSTACKGET(0))?0:1)); + break; + case OPeq: + VSTACKSET(1,INTTOVAL((VSTACKGET(1)==VSTACKGET(0))?1:0)); + VDROPN(1); + break; + case OPne: + VSTACKSET(1,INTTOVAL((VSTACKGET(1)!=VSTACKGET(0))?1:0)); + VDROPN(1); + break; + case OPlt: + VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))VALTOINT(VSTACKGET(0)))?1:0)); + VDROPN(1); + break; + case OPle: + VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))<=VALTOINT(VSTACKGET(0)))?1:0)); + VDROPN(1); + break; + case OPge: + VSTACKSET(1,INTTOVAL((VALTOINT(VSTACKGET(1))>=VALTOINT(VSTACKGET(0)))?1:0)); + VDROPN(1); + break; + case OPgoto: + pc=pcbase+loaderGetShort(&bytecode[pc]); + break; + case OPelse: + if (!VPULL()) + { + pc=pcbase+loaderGetShort(&bytecode[pc]); + } + else + { + pc+=2; + } + break; + case OPmktabb: + VPUSH(PNTTOVAL(VMALLOCCLEAR(255&bytecode[pc++]))); + break; + case OPmktab: + VPUSH(PNTTOVAL(VMALLOCCLEAR(VALTOINT(VPULL())))); + break; + case OPdeftabb: + VMKTAB(255&bytecode[pc++]); + break; + case OPdeftab: + VMKTAB(VALTOINT(VPULL())); + break; + case OPfetchb: + { + int i=255&bytecode[pc++]; + int p=VSTACKGET(0); + if (p!=NIL) + { + VSTACKSET(0,VFETCH(VALTOPNT(p),i)); + } + } + break; + case OPfetch: + { + int i=VPULL(); + int p=VSTACKGET(0); + if ((p==NIL)||(i==NIL)) + { + VSTACKSET(0,NIL); + } + else + { + i=VALTOINT(i); + p=VALTOPNT(p); + if ((i<0)||(i>=VSIZE(p))) + { + VSTACKSET(0,NIL); + } + else + { + VSTACKSET(0,VFETCH(p,i)); + } + } + } + break; + case OPgetglobalb: + VPUSH(VCALLSTACKGET(global_start,255&bytecode[pc++])); + break; + case OPgetglobal: + VSTACKSET(0,VCALLSTACKGET(global_start,VALTOINT(VSTACKGET(0)))); + break; + case OPSecho: + logSecho(VSTACKGET(0),0); + break; + case OPIecho: + logIecho(VSTACKGET(0),0); + break; + case OPsetlocalb: + VCALLSTACKSET(callstack,255&bytecode[pc++],VPULL()); + break; + case OPsetlocal: + { + int v=VALTOINT(VPULL()); + VCALLSTACKSET(callstack,v,VPULL()); + } + break; + case OPsetglobal: + VCALLSTACKSET(global_start,VALTOINT(VSTACKGET(1)),VSTACKGET(0)); + VSTACKSET(1,VSTACKGET(0)); + VPULL(); + break; + case OPsetstructb: + { + int i=255&bytecode[pc++]; + int v=VPULL(); + int p=VSTACKGET(0); + if ((p!=NIL)&&(i!=NIL)) + { + p=VALTOPNT(p); + if ((i>=0)||(i=0)||(i=0)||(i0; i--) + { + VSTACKSET(i,VSTACKGET(i-1)); + } + VSTACKSET(0,valn); + op=OPexec; + cont=1; + } + } + break; + case OPcallr: + { + int n=VALTOINT(VPULL()); + int valn=VSTACKGET(n); + if (valn==NIL) + { + VDROPN(n); + } + else + { + int i; + for(i=n; i>0; i--) + { + VSTACKSET(i,VSTACKGET(i-1)); + } + VSTACKSET(0,valn); + op=OPexec; + cont=1; + } + } + break; + case OPfirst: + { + int p=VSTACKGET(0); + if (p!=NIL) + { + VPUSH(VFETCH(VALTOPNT(p),0)); + } + else + { + VPUSH(NIL); + } + } + break; + case OPtime_ms: + VPUSH(INTTOVAL(sysTimems())); + break; + case OPtabnew: + { + int v=VPULL(); + if (v==NIL) + { + VSTACKSET(0,NIL); + } + else + { + int p,i,v0; + v=VALTOINT(v); + p=VMALLOC(v); + v0=VSTACKGET(0); + for(i=0; iw?w:v)); + } + break; + case OPrand: + VPUSH(INTTOVAL(sysRand())); + break; + case OPsrand: + sysSrand(VALTOINT(VSTACKGET(0))); + break; + case OPtime: + VPUSH(INTTOVAL(sysTime())); + break; + case OPstrnew: + { + int n=VALTOINT(VSTACKGET(0)); + if (n<0) + { + VSTACKSET(0,NIL); + } + else + { + VSTACKSET(0,PNTTOVAL(VMALLOCBIN(n))); + } + } + break; + case OPstrset: + { + int v=VALTOINT(VPULL()); + int n=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(n>=0)&&(n=VSIZEBIN(p))) + { + VSTACKSET(0,NIL); + } + else + { + VSTACKSET(0,INTTOVAL(((VSTARTBIN(p))[n]&255))); + } + } + break; + case OPstrright: + { + int n=VALTOINT(VPULL()); // 2nd arg, nb of chars to keep + int p=VALTOPNT(VSTACKGET(0)); // 1st arg, pointer to the source string in stack + if(p != NIL) + { + consolestr("Nb:"); + consolehx(n); + consolestr("Str:"); + consolehx(p); + vmemDump(); + } + else + { + VSTACKSET(0,NIL); + } - } - break; - case OPstrsub: - // vmemDump(); - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VSTACKGET(0)); - if (q!=NIL) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - if (start+len>VSIZEBIN(q)) len=VSIZEBIN(q)-start; - if ((start>=0)&&(len>=0)) - { - int p=VMALLOCBIN(len); - mystrcpy(VSTARTBIN(p),VSTARTBIN(VALTOPNT(VSTACKGET(0)))+start,len); - VSTACKSET(0,PNTTOVAL(p)); - } - else VSTACKSET(0,NIL); - } - else VSTACKSET(0,NIL); - } - // vmemDump(); - break; - case OPstrcat: - { - int r; - int len=0; - int q=VALTOPNT(VSTACKGET(0)); - int p=VALTOPNT(VSTACKGET(1)); - if (p!=NIL) len+=VSIZEBIN(p); - if (q!=NIL) len+=VSIZEBIN(q); - r=VMALLOCBIN(len); - len=0; - q=VALTOPNT(VPULL()); - p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) - { - len=VSIZEBIN(p); - mystrcpy(VSTARTBIN(r),VSTARTBIN(p),len); - } - if (q!=NIL) mystrcpy(VSTARTBIN(r)+len,VSTARTBIN(q),VSIZEBIN(q)); - VSTACKSET(0,PNTTOVAL(r)); - } - break; - case OPtablen: - { - int p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) VSTACKSET(0,INTTOVAL(VSIZE(p))); - } - break; - case OPstrcatlist: - { - int r,q; - int len=0; - int p=VALTOPNT(VSTACKGET(0)); - while(p!=NIL) - { - q=VALTOPNT(VFETCH(p,0)); - if (q!=NIL) len+=VSIZEBIN(q); - p=VALTOPNT(VFETCH(p,1)); - } - r=VMALLOCBIN(len); - if (r<0) return; - len=0; - p=VALTOPNT(VSTACKGET(0)); - while(p!=NIL) - { - q=VALTOPNT(VFETCH(p,0)); - if (q!=NIL) - { - mystrcpy(VSTARTBIN(r)+len,VSTARTBIN(q),VSIZEBIN(q)); - len+=VSIZEBIN(q); - } - p=VALTOPNT(VFETCH(p,1)); - } - VSTACKSET(0,PNTTOVAL(r)); - } - break; - case OPled: - sysLed(VALTOINT(VSTACKGET(1)),VALTOINT(VSTACKGET(0))); - VPULL(); - break; - case OPmotorset: - sysMotorset(VALTOINT(VSTACKGET(1)),VALTOINT(VSTACKGET(0))); - VPULL(); - break; - case OPmotorget: - VSTACKSET(0,INTTOVAL(sysMotorget(VALTOINT(VSTACKGET(0))))); - break; - case OPbutton2: - VPUSH(INTTOVAL(sysButton2())); - break; - case OPbutton3: - VPUSH(INTTOVAL(sysButton3())); - break; - case OPplayStart: - { - int k; - VCALLSTACKSET(sys_start,SYS_CBPLAY,VPULL()); - k=VALTOINT(VPULL()); - VPUSH(INTTOVAL(0)); - audioPlayStart(44100,16,2,k); - } - break; - case OPplayFeed: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VSTACKGET(0)); - if (q!=NIL) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - if (start+len>VSIZEBIN(q)) len=VSIZEBIN(q)-start; - if ((start>=0)&&(len>=0)) - { - len=audioPlayFeed(VSTARTBIN(q)+start,len); - VSTACKSET(0,INTTOVAL(len)); - } - else VSTACKSET(0,NIL); - } - else - { - audioPlayFeed(NULL,0); - VSTACKSET(0,NIL); - } - } - break; - case OPplayStop: - audioPlayStop(); - VPUSH(INTTOVAL(0)); - break; - case OPrecStart: - { - int freq,gain; - VCALLSTACKSET(sys_start,SYS_CBREC,VPULL()); - gain=VALTOINT(VPULL()); - freq=VALTOINT(VPULL()); - VPUSH(INTTOVAL(0)); - audioRecStart(freq,gain); - } - break; - case OPrecStop: - audioRecStop(); - VPUSH(INTTOVAL(0)); - break; - case OPrecVol: - { - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VSTACKGET(0)); - if (q!=NIL) - { - VSTACKSET(0,INTTOVAL(audioRecVol((unsigned char*)VSTARTBIN(q),VSIZEBIN(q),start))); - } - } - break; - /* - case OPloadf: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - len=sysLoad(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); - VSTACKSET(0,INTTOVAL(len)); - } - else VSTACKSET(0,NIL); - } - break; - */ - case OPload: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - len=sysLoad(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); - VSTACKSET(0,INTTOVAL(len)); - } - else VSTACKSET(0,NIL); - } - break; - case OPgc: - vmemGC(); - VPUSH(INTTOVAL((vmem_heapindex-vmem_stack)*100/VMEM_LENGTH)); - break; - case OPsave: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - len=sysSave(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); - VSTACKSET(0,INTTOVAL(len)); - } - else VSTACKSET(0,NIL); - } - break; - case OPbytecode: - consolestr("#################### OPbytecode"ENDLINE); - if (VSTACKGET(0)!=NIL) - { - char* q; - int env=-1; - int p=VALTOPNT(VCALLSTACKGET(sys_start,SYS_ENV)); - audioPlayStop(); - audioRecStop(); - if (p!=NIL) - { - env=VSIZEBIN(p); - memcpy(audioFifoPlay,VSTARTBIN(p),env); - } + } + break; + case OPstrsub: + // vmemDump(); + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VSTACKGET(0)); + if (q!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + if (start+len>VSIZEBIN(q)) + { + len=VSIZEBIN(q)-start; + } + if ((start>=0)&&(len>=0)) + { + int p=VMALLOCBIN(len); + mystrcpy(VSTARTBIN(p),VSTARTBIN(VALTOPNT(VSTACKGET(0)))+start,len); + VSTACKSET(0,PNTTOVAL(p)); + } + else + { + VSTACKSET(0,NIL); + } + } + else + { + VSTACKSET(0,NIL); + } + } + // vmemDump(); + break; + case OPstrcat: + { + int r; + int len=0; + int q=VALTOPNT(VSTACKGET(0)); + int p=VALTOPNT(VSTACKGET(1)); + if (p!=NIL) + { + len+=VSIZEBIN(p); + } + if (q!=NIL) + { + len+=VSIZEBIN(q); + } + r=VMALLOCBIN(len); + len=0; + q=VALTOPNT(VPULL()); + p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + len=VSIZEBIN(p); + mystrcpy(VSTARTBIN(r),VSTARTBIN(p),len); + } + if (q!=NIL) + { + mystrcpy(VSTARTBIN(r)+len,VSTARTBIN(q),VSIZEBIN(q)); + } + VSTACKSET(0,PNTTOVAL(r)); + } + break; + case OPtablen: + { + int p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + VSTACKSET(0,INTTOVAL(VSIZE(p))); + } + } + break; + case OPstrcatlist: + { + int r,q; + int len=0; + int p=VALTOPNT(VSTACKGET(0)); + while(p!=NIL) + { + q=VALTOPNT(VFETCH(p,0)); + if (q!=NIL) + { + len+=VSIZEBIN(q); + } + p=VALTOPNT(VFETCH(p,1)); + } + r=VMALLOCBIN(len); + if (r<0) + { + return; + } + len=0; + p=VALTOPNT(VSTACKGET(0)); + while(p!=NIL) + { + q=VALTOPNT(VFETCH(p,0)); + if (q!=NIL) + { + mystrcpy(VSTARTBIN(r)+len,VSTARTBIN(q),VSIZEBIN(q)); + len+=VSIZEBIN(q); + } + p=VALTOPNT(VFETCH(p,1)); + } + VSTACKSET(0,PNTTOVAL(r)); + } + break; + case OPled: + sysLed(VALTOINT(VSTACKGET(1)),VALTOINT(VSTACKGET(0))); + VPULL(); + break; + case OPmotorset: + sysMotorset(VALTOINT(VSTACKGET(1)),VALTOINT(VSTACKGET(0))); + VPULL(); + break; + case OPmotorget: + VSTACKSET(0,INTTOVAL(sysMotorget(VALTOINT(VSTACKGET(0))))); + break; + case OPbutton2: + VPUSH(INTTOVAL(sysButton2())); + break; + case OPbutton3: + VPUSH(INTTOVAL(sysButton3())); + break; + case OPplayStart: + { + int k; + VCALLSTACKSET(sys_start,SYS_CBPLAY,VPULL()); + k=VALTOINT(VPULL()); + VPUSH(INTTOVAL(0)); + audioPlayStart(44100,16,2,k); + } + break; + case OPplayFeed: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VSTACKGET(0)); + if (q!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + if (start+len>VSIZEBIN(q)) + { + len=VSIZEBIN(q)-start; + } + if ((start>=0)&&(len>=0)) + { + len=audioPlayFeed(VSTARTBIN(q)+start,len); + VSTACKSET(0,INTTOVAL(len)); + } + else + { + VSTACKSET(0,NIL); + } + } + else + { + audioPlayFeed(NULL,0); + VSTACKSET(0,NIL); + } + } + break; + case OPplayStop: + audioPlayStop(); + VPUSH(INTTOVAL(0)); + break; + case OPrecStart: + { + int freq,gain; + VCALLSTACKSET(sys_start,SYS_CBREC,VPULL()); + gain=VALTOINT(VPULL()); + freq=VALTOINT(VPULL()); + VPUSH(INTTOVAL(0)); + audioRecStart(freq,gain); + } + break; + case OPrecStop: + audioRecStop(); + VPUSH(INTTOVAL(0)); + break; + case OPrecVol: + { + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VSTACKGET(0)); + if (q!=NIL) + { + VSTACKSET(0,INTTOVAL(audioRecVol((unsigned char*)VSTARTBIN(q),VSIZEBIN(q),start))); + } + } + break; + /* + case OPloadf: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); + len=sysLoad(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); + VSTACKSET(0,INTTOVAL(len)); + } + else VSTACKSET(0,NIL); + } + break; + */ + case OPload: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + len=sysLoad(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); + VSTACKSET(0,INTTOVAL(len)); + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPgc: + vmemGC(); + VPUSH(INTTOVAL((vmem_heapindex-vmem_stack)*100/VMEM_LENGTH)); + break; + case OPsave: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + len=sysSave(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,len); + VSTACKSET(0,INTTOVAL(len)); + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPbytecode: + consolestr("#################### OPbytecode"ENDLINE); + if (VSTACKGET(0)!=NIL) + { + char* q; + int env=-1; + int p=VALTOPNT(VCALLSTACKGET(sys_start,SYS_ENV)); + audioPlayStop(); + audioRecStop(); + if (p!=NIL) + { + env=VSIZEBIN(p); + memcpy(audioFifoPlay,VSTARTBIN(p),env); + } - q=VSTARTBIN(VALTOPNT(VSTACKGET(0))); - // dump(q+0x2df0,128); + q=VSTARTBIN(VALTOPNT(VSTACKGET(0))); + // dump(q+0x2df0,128); - loaderInit(q); - // dump(&bytecode[0x2440],32); + loaderInit(q); + // dump(&bytecode[0x2440],32); - if (env>=0) - { - // dump(audioFifoPlay,env); - VCALLSTACKSET(sys_start,SYS_ENV, - PNTTOVAL(VMALLOCSTR(audioFifoPlay,env))); - } - VPUSH(INTTOVAL(0)); - // tron=1; - interpGo(); - // VPULL(); - consolestr("#################### OPbytecode done"ENDLINE); - // tron=0; + if (env>=0) + { + // dump(audioFifoPlay,env); + VCALLSTACKSET(sys_start,SYS_ENV, + PNTTOVAL(VMALLOCSTR(audioFifoPlay,env))); + } + VPUSH(INTTOVAL(0)); + // tron=1; + interpGo(); + // VPULL(); + consolestr("#################### OPbytecode done"ENDLINE); + // tron=0; // tron=1; - return; - } - break; - case OPloopcb: - VCALLSTACKSET(sys_start,SYS_CBLOOP,VSTACKGET(0)); - break; + return; + } + break; + case OPloopcb: + VCALLSTACKSET(sys_start,SYS_CBLOOP,VSTACKGET(0)); + break; #ifdef VSIMU - case OPudpStart: - VSTACKSET(0,INTTOVAL(udpcreate(VALTOINT(VSTACKGET(0))))); - break; - case OPudpCb: - VCALLSTACKSET(sys_start,SYS_CBUDP,VSTACKGET(0)); - break; - case OPudpStop: - VSTACKSET(0,INTTOVAL(udpclose(VALTOINT(VSTACKGET(0))))); - break; - case OPudpSend: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int dstport=VALTOINT(VPULL()); - int p=VALTOPNT(VPULL()); - int localport=VALTOINT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - if (start+len>VSIZEBIN(q)) len=VSIZEBIN(q)-start; - if ((start>=0)&&(len>=0)) - { - int k=udpsend(localport,VSTARTBIN(p),dstport,VSTARTBIN(q)+start,len); - VSTACKSET(0,INTTOVAL(k)); - } - else VSTACKSET(0,NIL); - } - else VSTACKSET(0,NIL); + case OPudpStart: + VSTACKSET(0,INTTOVAL(udpcreate(VALTOINT(VSTACKGET(0))))); + break; + case OPudpCb: + VCALLSTACKSET(sys_start,SYS_CBUDP,VSTACKGET(0)); + break; + case OPudpStop: + VSTACKSET(0,INTTOVAL(udpclose(VALTOINT(VSTACKGET(0))))); + break; + case OPudpSend: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int dstport=VALTOINT(VPULL()); + int p=VALTOPNT(VPULL()); + int localport=VALTOINT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + if (start+len>VSIZEBIN(q)) + { + len=VSIZEBIN(q)-start; + } + if ((start>=0)&&(len>=0)) + { + int k=udpsend(localport,VSTARTBIN(p),dstport,VSTARTBIN(q)+start,len); + VSTACKSET(0,INTTOVAL(k)); + } + else + { + VSTACKSET(0,NIL); + } + } + else + { + VSTACKSET(0,NIL); + } - } - break; - case OPtcpOpen: - { - int port=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) VSTACKSET(0,INTTOVAL(tcpopen(VSTARTBIN(p),port))); - } - break; - case OPtcpClose: - VSTACKSET(0,INTTOVAL(tcpclose(VALTOINT(VSTACKGET(0))))); - break; - case OPtcpSend: - { - int len=VPULL(); - int start=VPULL(); - int q=VALTOPNT(VPULL()); - int tcp=VALTOINT(VSTACKGET(0)); - if (start==NIL) start=0; - else start=VALTOINT(start); - if (q!=NIL) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - if (start+len>VSIZEBIN(q)) len=VSIZEBIN(q)-start; - if ((start>=0)&&(len>=0)) - { - int k; - k=tcpsend(tcp,VSTARTBIN(q)+start,len); - if (k>=0) start+=k; - VSTACKSET(0,INTTOVAL(start)); - } - else VSTACKSET(0,NIL); - } - else VSTACKSET(0,NIL); - } - break; - case OPtcpCb: - VCALLSTACKSET(sys_start,SYS_CBTCP,VSTACKGET(0)); - break; - case OPtcpListen: - { - int port=VALTOINT(VSTACKGET(0)); - VSTACKSET(0,INTTOVAL(tcpservercreate(port))); - } - break; - case OPtcpEnable: - { - int enable=VALTOINT(VPULL()); - tcpenable(VALTOINT(VSTACKGET(0)),enable); - } - break; + } + break; + case OPtcpOpen: + { + int port=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + VSTACKSET(0,INTTOVAL(tcpopen(VSTARTBIN(p),port))); + } + } + break; + case OPtcpClose: + VSTACKSET(0,INTTOVAL(tcpclose(VALTOINT(VSTACKGET(0))))); + break; + case OPtcpSend: + { + int len=VPULL(); + int start=VPULL(); + int q=VALTOPNT(VPULL()); + int tcp=VALTOINT(VSTACKGET(0)); + if (start==NIL) + { + start=0; + } + else + { + start=VALTOINT(start); + } + if (q!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + if (start+len>VSIZEBIN(q)) + { + len=VSIZEBIN(q)-start; + } + if ((start>=0)&&(len>=0)) + { + int k; + k=tcpsend(tcp,VSTARTBIN(q)+start,len); + if (k>=0) + { + start+=k; + } + VSTACKSET(0,INTTOVAL(start)); + } + else + { + VSTACKSET(0,NIL); + } + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPtcpCb: + VCALLSTACKSET(sys_start,SYS_CBTCP,VSTACKGET(0)); + break; + case OPtcpListen: + { + int port=VALTOINT(VSTACKGET(0)); + VSTACKSET(0,INTTOVAL(tcpservercreate(port))); + } + break; + case OPtcpEnable: + { + int enable=VALTOINT(VPULL()); + tcpenable(VALTOINT(VSTACKGET(0)),enable); + } + break; #endif - case OPSecholn: - logSecho(VSTACKGET(0),1); - // if (tron)dump(&bytecode[0x2440],32); - break; - case OPIecholn: - logIecho(VSTACKGET(0),1); - break; - case OPenvget: - VPUSH(VCALLSTACKGET(sys_start,SYS_ENV)); - break; - case OPenvset: - VCALLSTACKSET(sys_start,SYS_ENV,VSTACKGET(0)); - break; - case OPsndVol: - audioVol(VALTOINT(VSTACKGET(0))); - break; - case OPrfidGet: - { - char *p; - p=sysRfidget(); - if (!p) VPUSH(NIL); - else VPUSH(PNTTOVAL(VMALLOCSTR(p,8))); - } - break; - case OPplayTime: - VPUSH(INTTOVAL(audioPlayTime())); - break; - case OPnetCb: - VCALLSTACKSET(sys_start,SYS_CBTCP,VSTACKGET(0)); - break; - case OPnetSend: - { - int speed=VALTOINT(VPULL()); - int indmac=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int len=VPULL(); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - VSTACKSET(0,INTTOVAL(netSend(VSTARTBIN(p),index,len,VSIZEBIN(p),VSTARTBIN(q),indmac,VSIZEBIN(q),speed))); - } - else VSTACKSET(0,NIL); - } - break; - case OPnetState: - VPUSH(INTTOVAL(netState())); - break; - case OPnetMac: - VPUSH(PNTTOVAL(VMALLOCSTR(netMac(),6))); - break; - case OPnetChk: - { - int val=VALTOINT(VPULL()); - int len=VPULL(); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - VSTACKSET(0,INTTOVAL(netChk(VSTARTBIN(p),index,len,VSIZEBIN(p),val))); - } - else VSTACKSET(0,INTTOVAL(val)); - } - break; - case OPnetSetmode: - { - int chn=VALTOINT(VPULL()); - int p=VALTOPNT(VPULL()); - int md=VALTOINT(VSTACKGET(0)); - char* ssid=(p==NIL)?NULL:VSTARTBIN(p); - netSetmode(md,ssid,chn); - } - break; - case OPnetScan: - { - int p=VALTOPNT(VPULL()); - char* ssid=(p==NIL)?NULL:VSTARTBIN(p); - netScan(ssid); - } - break; - case OPnetAuth: - { - int key=VALTOPNT(VPULL()); - int encrypt=VALTOINT(VPULL()); - int authmode=VALTOINT(VPULL()); - int scan=VALTOPNT(VSTACKGET(0)); - if (scan!=NIL) - { - int ssid=VALTOPNT(VFETCH(scan,0)); - int mac=VALTOPNT(VFETCH(scan,1)); - int bssid=VALTOPNT(VFETCH(scan,2)); - int chn=VALTOINT(VFETCH(scan,4)); - int rate=VALTOINT(VFETCH(scan,5)); - if ((mac!=NIL)&&(bssid!=NIL)) - { - netAuth((ssid==NIL)?NULL:VSTARTBIN(ssid),VSTARTBIN(mac), - VSTARTBIN(bssid),chn,rate,authmode, - encrypt,(key==NIL)?NULL:VSTARTBIN(key)); - } - } - } - break; - case OPnetSeqAdd: - { - int inc=VALTOINT(VPULL()); - int seq=VALTOPNT(VPULL()); - if ((seq==NIL)||(VSIZEBIN(seq)<4)) VPUSH(NIL); - else netSeqAdd((unsigned char*)VSTARTBIN(seq),inc); - } - break; - case OPstrgetword: - { - int ind=VALTOINT(VPULL()); - int src=VALTOPNT(VSTACKGET(0)); - if (src!=NIL) VSTACKSET(0,INTTOVAL(sysStrgetword((unsigned char*)VSTARTBIN(src),VSIZEBIN(src),ind))); - } - break; - case OPstrputword: - { - int val=VALTOINT(VPULL()); - int ind=VALTOINT(VPULL()); - int src=VALTOPNT(VSTACKGET(0)); - if (src!=NIL) sysStrputword((unsigned char*)VSTARTBIN(src),VSIZEBIN(src),ind,val); - } - break; - case OPatoi: - { - int src=VALTOPNT(VSTACKGET(0)); - if (src!=NIL) VSTACKSET(0,INTTOVAL(sysAtoi(VSTARTBIN(src)))); - } - break; - case OPhtoi: - { - int src=VALTOPNT(VSTACKGET(0)); - if (src!=NIL) - { - VSTACKSET(0,INTTOVAL(sysHtoi(VSTARTBIN(src)))); - } - } - break; - case OPitoa: - sysItoa(VALTOINT(VPULL())); - break; - case OPctoa: - { - int val=VALTOINT(VPULL()); - sysCtoa(val); - } - break; - case OPitoh: - sysItoh(VALTOINT(VPULL())); - break; - case OPctoh: - sysCtoh(VALTOINT(VPULL())); - break; - case OPitobin2: - sysItobin2(VALTOINT(VPULL())); - break; - case OPlistswitch: - { - int key=VPULL(); - int p=VALTOPNT(VSTACKGET(0)); - VSTACKSET(0,sysListswitch(p,key)); - } - break; - case OPlistswitchstr: - { - int key=VPULL(); - int p=VALTOPNT(VSTACKGET(0)); - if (key==NIL) VSTACKSET(0,sysListswitch(p,key)); - else VSTACKSET(0,sysListswitchstr(p,VSTARTBIN(VALTOPNT(key)))); - } - break; - case OPsndRefresh: - audioRefresh(); - VPUSH(NIL); - break; - case OPsndWrite: - { - int val=VALTOINT(VPULL()); - audioWrite(VALTOINT(VSTACKGET(0)),val); - } - break; - case OPsndRead: - VSTACKSET(0,INTTOVAL(audioRead(VALTOINT(VSTACKGET(0))))); - break; - case OPsndFeed: - { - int len=VPULL(); - int start=VPULL(); - int q=VALTOPNT(VSTACKGET(0)); - if (start==NIL) start=0; - else start=VALTOINT(start); - if (q!=NIL) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - if (start+len>VSIZEBIN(q)) len=VSIZEBIN(q)-start; - if ((start>=0)&&(len>=0)) - { - int k=audioFeed(VSTARTBIN(q)+start,len); - VSTACKSET(0,INTTOVAL(k)); - } - else VSTACKSET(0,NIL); - } - else VSTACKSET(0,NIL); - } - break; - case OPsndAmpli: - audioAmpli(VALTOINT(VSTACKGET(0))); - break; - case OPcorePP: - VPUSH(INTTOVAL(vmem_stack)); - break; - case OPcorePush: - VPUSH(VSTACKGET(0)); - break; - case OPcorePull: - VSTACKSET(1,VSTACKGET(0)); - VPULL(); - break; - case OPcoreBit0: - { - int v=VALTOINT(VPULL()); - VSTACKSET(0,(VSTACKGET(0)&0xfffffffe)|(v&1)); - } - break; - case OPreboot: - { - int w=VALTOINT(VPULL()); - int v=VALTOINT(VSTACKGET(0)); - consolestr("reboot "); - consolehx(v); - consolestr("."); - consolehx(w); - consolestr("\n"); - if ((v==0x0407FE58)&&(w==0x13fb6754)) - { - consolestr("************REBOOT NOW"); - sysReboot(); - } - } - break; - case OPstrcmp: - { - int q=VALTOPNT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - int pl=VSIZEBIN(p); - int ql=VSIZEBIN(q); + case OPSecholn: + logSecho(VSTACKGET(0),1); + // if (tron)dump(&bytecode[0x2440],32); + break; + case OPIecholn: + logIecho(VSTACKGET(0),1); + break; + case OPenvget: + VPUSH(VCALLSTACKGET(sys_start,SYS_ENV)); + break; + case OPenvset: + VCALLSTACKSET(sys_start,SYS_ENV,VSTACKGET(0)); + break; + case OPsndVol: + audioVol(VALTOINT(VSTACKGET(0))); + break; + case OPrfidGet: + { + char *p; + p=sysRfidget(); + if (!p) + { + VPUSH(NIL); + } + else + { + VPUSH(PNTTOVAL(VMALLOCSTR(p,8))); + } + } + break; + case OPplayTime: + VPUSH(INTTOVAL(audioPlayTime())); + break; + case OPnetCb: + VCALLSTACKSET(sys_start,SYS_CBTCP,VSTACKGET(0)); + break; + case OPnetSend: + { + int speed=VALTOINT(VPULL()); + int indmac=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int len=VPULL(); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + VSTACKSET(0,INTTOVAL(netSend(VSTARTBIN(p),index,len,VSIZEBIN(p),VSTARTBIN(q),indmac,VSIZEBIN(q),speed))); + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPnetState: + VPUSH(INTTOVAL(netState())); + break; + case OPnetMac: + VPUSH(PNTTOVAL(VMALLOCSTR(netMac(),6))); + break; + case OPnetChk: + { + int val=VALTOINT(VPULL()); + int len=VPULL(); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + VSTACKSET(0,INTTOVAL(netChk(VSTARTBIN(p),index,len,VSIZEBIN(p),val))); + } + else + { + VSTACKSET(0,INTTOVAL(val)); + } + } + break; + case OPnetSetmode: + { + int chn=VALTOINT(VPULL()); + int p=VALTOPNT(VPULL()); + int md=VALTOINT(VSTACKGET(0)); + char* ssid=(p==NIL)?NULL:VSTARTBIN(p); + netSetmode(md,ssid,chn); + } + break; + case OPnetScan: + { + int p=VALTOPNT(VPULL()); + char* ssid=(p==NIL)?NULL:VSTARTBIN(p); + netScan(ssid); + } + break; + case OPnetAuth: + { + int key=VALTOPNT(VPULL()); + int encrypt=VALTOINT(VPULL()); + int authmode=VALTOINT(VPULL()); + int scan=VALTOPNT(VSTACKGET(0)); + if (scan!=NIL) + { + int ssid=VALTOPNT(VFETCH(scan,0)); + int mac=VALTOPNT(VFETCH(scan,1)); + int bssid=VALTOPNT(VFETCH(scan,2)); + int chn=VALTOINT(VFETCH(scan,4)); + int rate=VALTOINT(VFETCH(scan,5)); + if ((mac!=NIL)&&(bssid!=NIL)) + { + netAuth((ssid==NIL)?NULL:VSTARTBIN(ssid),VSTARTBIN(mac), + VSTARTBIN(bssid),chn,rate,authmode, + encrypt,(key==NIL)?NULL:VSTARTBIN(key)); + } + } + } + break; + case OPnetSeqAdd: + { + int inc=VALTOINT(VPULL()); + int seq=VALTOPNT(VPULL()); + if ((seq==NIL)||(VSIZEBIN(seq)<4)) + { + VPUSH(NIL); + } + else + { + netSeqAdd((unsigned char*)VSTARTBIN(seq),inc); + } + } + break; + case OPstrgetword: + { + int ind=VALTOINT(VPULL()); + int src=VALTOPNT(VSTACKGET(0)); + if (src!=NIL) + { + VSTACKSET(0,INTTOVAL(sysStrgetword((unsigned char*)VSTARTBIN(src),VSIZEBIN(src),ind))); + } + } + break; + case OPstrputword: + { + int val=VALTOINT(VPULL()); + int ind=VALTOINT(VPULL()); + int src=VALTOPNT(VSTACKGET(0)); + if (src!=NIL) + { + sysStrputword((unsigned char*)VSTARTBIN(src),VSIZEBIN(src),ind,val); + } + } + break; + case OPatoi: + { + int src=VALTOPNT(VSTACKGET(0)); + if (src!=NIL) + { + VSTACKSET(0,INTTOVAL(sysAtoi(VSTARTBIN(src)))); + } + } + break; + case OPhtoi: + { + int src=VALTOPNT(VSTACKGET(0)); + if (src!=NIL) + { + VSTACKSET(0,INTTOVAL(sysHtoi(VSTARTBIN(src)))); + } + } + break; + case OPitoa: + sysItoa(VALTOINT(VPULL())); + break; + case OPctoa: + { + int val=VALTOINT(VPULL()); + sysCtoa(val); + } + break; + case OPitoh: + sysItoh(VALTOINT(VPULL())); + break; + case OPctoh: + sysCtoh(VALTOINT(VPULL())); + break; + case OPitobin2: + sysItobin2(VALTOINT(VPULL())); + break; + case OPlistswitch: + { + int key=VPULL(); + int p=VALTOPNT(VSTACKGET(0)); + VSTACKSET(0,sysListswitch(p,key)); + } + break; + case OPlistswitchstr: + { + int key=VPULL(); + int p=VALTOPNT(VSTACKGET(0)); + if (key==NIL) + { + VSTACKSET(0,sysListswitch(p,key)); + } + else + { + VSTACKSET(0,sysListswitchstr(p,VSTARTBIN(VALTOPNT(key)))); + } + } + break; + case OPsndRefresh: + audioRefresh(); + VPUSH(NIL); + break; + case OPsndWrite: + { + int val=VALTOINT(VPULL()); + audioWrite(VALTOINT(VSTACKGET(0)),val); + } + break; + case OPsndRead: + VSTACKSET(0,INTTOVAL(audioRead(VALTOINT(VSTACKGET(0))))); + break; + case OPsndFeed: + { + int len=VPULL(); + int start=VPULL(); + int q=VALTOPNT(VSTACKGET(0)); + if (start==NIL) + { + start=0; + } + else + { + start=VALTOINT(start); + } + if (q!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + if (start+len>VSIZEBIN(q)) + { + len=VSIZEBIN(q)-start; + } + if ((start>=0)&&(len>=0)) + { + int k=audioFeed(VSTARTBIN(q)+start,len); + VSTACKSET(0,INTTOVAL(k)); + } + else + { + VSTACKSET(0,NIL); + } + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPsndAmpli: + audioAmpli(VALTOINT(VSTACKGET(0))); + break; + case OPcorePP: + VPUSH(INTTOVAL(vmem_stack)); + break; + case OPcorePush: + VPUSH(VSTACKGET(0)); + break; + case OPcorePull: + VSTACKSET(1,VSTACKGET(0)); + VPULL(); + break; + case OPcoreBit0: + { + int v=VALTOINT(VPULL()); + VSTACKSET(0,(VSTACKGET(0)&0xfffffffe)|(v&1)); + } + break; + case OPreboot: + { + int w=VALTOINT(VPULL()); + int v=VALTOINT(VSTACKGET(0)); + consolestr("reboot "); + consolehx(v); + consolestr("."); + consolehx(w); + consolestr("\n"); + if ((v==0x0407FE58)&&(w==0x13fb6754)) + { + consolestr("************REBOOT NOW"); + sysReboot(); + } + } + break; + case OPstrcmp: + { + int q=VALTOPNT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + int pl=VSIZEBIN(p); + int ql=VSIZEBIN(q); - VSTACKSET(0,INTTOVAL(memcmp(VSTARTBIN(p),VSTARTBIN(q),(pl>ql)?pl:ql))); - } - else VSTACKSET(0,INTTOVAL((p!=NIL)?1:(q!=NIL)?-1:0)); - } - break; - case OPadp2wav: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - AudioAdp2wav(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len); - } - } - break; - case OPwav2adp: - { - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - AudioWav2adp(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len); - } - } - break; - case OPalaw2wav: - { - int mu=VALTOINT(VPULL()); - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - AudioAlaw2wav(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len,mu); - } - } - break; - case OPwav2alaw: - { - int mu=VALTOINT(VPULL()); - int len=VPULL(); - int start=VALTOINT(VPULL()); - int q=VALTOPNT(VPULL()); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if ((p!=NIL)&&(q!=NIL)) - { - if (len==NIL) len=VSIZEBIN(q)-start; else len=VALTOINT(len); - AudioWav2alaw(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len,mu); - } - } - break; - case OPnetPmk: - { - int key=VALTOPNT(VPULL()); - int ssid=VALTOPNT(VPULL()); - if ((key==NIL)||(ssid==NIL)) VPUSH(NIL); - else - { - char pmk[32]; - netPmk(VSTARTBIN(ssid),VSTARTBIN(key),pmk); - VPUSH(PNTTOVAL(VMALLOCSTR(pmk,32))); - } - } - break; - case OPflashFirmware: - { - int w=VALTOINT(VPULL()); - int v=VALTOINT(VPULL()); - int firmware=VALTOPNT(VSTACKGET(0)); - consolestr("flash firmware "); - consolehx(v); - consolestr("."); - consolehx(w); - consolestr("\n"); - if ((firmware!=NIL)&&(v==0x13fb6754)&&(w==0x0407FE58)) - { - consolestr("************FLASH AND REBOOT NOW"); - sysFlash(VSTARTBIN(firmware),VSIZEBIN(firmware)); - } - } - break; - case OPcrypt: - { - int alpha=VALTOINT(VPULL()); - int key=VALTOINT(VPULL()); - int len=VPULL(); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - VSTACKSET(0,INTTOVAL(sysCrypt(VSTARTBIN(p),index,len,VSIZEBIN(p),key,alpha))); - } - else VSTACKSET(0,NIL); - } - break; - case OPuncrypt: - { - int alpha=VALTOINT(VPULL()); - int key=VALTOINT(VPULL()); - int len=VPULL(); - int index=VALTOINT(VPULL()); - int p=VALTOPNT(VSTACKGET(0)); - if (p!=NIL) - { - if (len==NIL) len=VSIZEBIN(p)-index; else len=VALTOINT(len); - VSTACKSET(0,INTTOVAL(sysUncrypt(VSTARTBIN(p),index,len,VSIZEBIN(p),key,alpha))); - } - else VSTACKSET(0,NIL); - } - break; - case OPnetRssi: - VPUSH(INTTOVAL(netRssi())); - break; - default: - consolestr("unknown opcode ");consoleint(op); - consolestr(" at ");consoleint(pc-1);consolestr("\n"); - // dump(bytecode,256); - return; - } - /* if (bytecode[8]==0x48) - { - consolestr("bytecode erase ");consoleint(op); - consolestr(" at ");consoleint(pc-1);consolestr("\n"); - dump(bytecode,256); - } - */ }while(cont); - currentop=-1; - if (callstack>0)return; - if(pc>=0) - { - // vmemDumpStack(); - // displaybc(pc); + VSTACKSET(0,INTTOVAL(memcmp(VSTARTBIN(p),VSTARTBIN(q),(pl>ql)?pl:ql))); + } + else + { + VSTACKSET(0,INTTOVAL((p!=NIL)?1:(q!=NIL)?-1:0)); + } + } + break; + case OPadp2wav: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + AudioAdp2wav(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len); + } + } + break; + case OPwav2adp: + { + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + AudioWav2adp(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len); + } + } + break; + case OPalaw2wav: + { + int mu=VALTOINT(VPULL()); + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + AudioAlaw2wav(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len,mu); + } + } + break; + case OPwav2alaw: + { + int mu=VALTOINT(VPULL()); + int len=VPULL(); + int start=VALTOINT(VPULL()); + int q=VALTOPNT(VPULL()); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if ((p!=NIL)&&(q!=NIL)) + { + if (len==NIL) + { + len=VSIZEBIN(q)-start; + } + else + { + len=VALTOINT(len); + } + AudioWav2alaw(VSTARTBIN(p),index,VSIZEBIN(p),VSTARTBIN(q),start,VSIZEBIN(q),len,mu); + } + } + break; + case OPnetPmk: + { + int key=VALTOPNT(VPULL()); + int ssid=VALTOPNT(VPULL()); + if ((key==NIL)||(ssid==NIL)) + { + VPUSH(NIL); + } + else + { + char pmk[32]; + netPmk(VSTARTBIN(ssid),VSTARTBIN(key),pmk); + VPUSH(PNTTOVAL(VMALLOCSTR(pmk,32))); + } + } + break; + case OPflashFirmware: + { + int w=VALTOINT(VPULL()); + int v=VALTOINT(VPULL()); + int firmware=VALTOPNT(VSTACKGET(0)); + consolestr("flash firmware "); + consolehx(v); + consolestr("."); + consolehx(w); + consolestr("\n"); + if ((firmware!=NIL)&&(v==0x13fb6754)&&(w==0x0407FE58)) + { + consolestr("************FLASH AND REBOOT NOW"); + sysFlash(VSTARTBIN(firmware),VSIZEBIN(firmware)); + } + } + break; + case OPcrypt: + { + int alpha=VALTOINT(VPULL()); + int key=VALTOINT(VPULL()); + int len=VPULL(); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + VSTACKSET(0,INTTOVAL(sysCrypt(VSTARTBIN(p),index,len,VSIZEBIN(p),key,alpha))); + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPuncrypt: + { + int alpha=VALTOINT(VPULL()); + int key=VALTOINT(VPULL()); + int len=VPULL(); + int index=VALTOINT(VPULL()); + int p=VALTOPNT(VSTACKGET(0)); + if (p!=NIL) + { + if (len==NIL) + { + len=VSIZEBIN(p)-index; + } + else + { + len=VALTOINT(len); + } + VSTACKSET(0,INTTOVAL(sysUncrypt(VSTARTBIN(p),index,len,VSIZEBIN(p),key,alpha))); + } + else + { + VSTACKSET(0,NIL); + } + } + break; + case OPnetRssi: + VPUSH(INTTOVAL(netRssi())); + break; + default: + consolestr("unknown opcode "); + consoleint(op); + consolestr(" at "); + consoleint(pc-1); + consolestr("\n"); + // dump(bytecode,256); + return; + } + /* if (bytecode[8]==0x48) + { + consolestr("bytecode erase ");consoleint(op); + consolestr(" at ");consoleint(pc-1);consolestr("\n"); + dump(bytecode,256); + } + */ + } + while(cont); + currentop=-1; + if (callstack>0) + { + return; + } + if(pc>=0) + { + // vmemDumpStack(); + // displaybc(pc); - // getchar(); - } - op=255&bytecode[pc++]; - } + // getchar(); + } + op=255&bytecode[pc++]; + } } diff --git a/src/vm/vloader.c b/src/vm/vloader.c index 5852f1f..185c793 100644 --- a/src/vm/vloader.c +++ b/src/vm/vloader.c @@ -12,21 +12,21 @@ int global_start; int loaderGetByte(char *src) { - int i; - i=(src[0]&255); - return i; + int i; + i=(src[0]&255); + return i; } int loaderGetShort(char *src) { - int i; - i=((src[1]&255)<<8)+(src[0]&255); - return i; + int i; + i=((src[1]&255)<<8)+(src[0]&255); + return i; } int loaderGetInt(char *src) { - int i; - i=((src[3]&255)<<24)+((src[2]&255)<<16)+((src[1]&255)<<8)+(src[0]&255); - return i; + int i; + i=((src[3]&255)<<24)+((src[2]&255)<<16)+((src[1]&255)<<8)+(src[0]&255); + return i; } /** @@ -34,86 +34,98 @@ int loaderGetInt(char *src) */ char *loaderInitRec(char *src) { - int l,i; - - l=loaderGetInt(src); - src+=4; - if (l==-1) - { - // printf("nil\n",l>>1); - VPUSH(NIL); - } - else if (l&1) - { - l>>=1; - if (l&1) - { - l>>=1; - // printf("tuple %d\n",l); - for(i=0;i>=1; - // printf("string taille %d\n",l); - VPUSH(PNTTOVAL(VMALLOCSTR(src,l))); - src+=l; - } - } - else - { - // printf("int %d\n",l>>1); - VPUSH(l); - } - return src; + int l,i; + + l=loaderGetInt(src); + src+=4; + if (l==-1) + { + // printf("nil\n",l>>1); + VPUSH(NIL); + } + else if (l&1) + { + l>>=1; + if (l&1) + { + l>>=1; + // printf("tuple %d\n",l); + for(i=0; i>=1; + // printf("string taille %d\n",l); + VPUSH(PNTTOVAL(VMALLOCSTR(src,l))); + src+=l; + } + } + else + { + // printf("int %d\n",l>>1); + VPUSH(l); + } + return src; } int loaderSizeBC(char *src) { - int n,b; - n=loaderGetInt(src); - src+=n; - b=loaderGetInt(src); - src+=4+b; - n+=4+b; - n+=2+(loaderGetShort(src)<<2); - return n; + int n,b; + n=loaderGetInt(src); + src+=n; + b=loaderGetInt(src); + src+=4+b; + n+=4+b; + n+=2+(loaderGetShort(src)<<2); + return n; } int loaderInit(char *src) { - int n,nw,i; - char* src0; - char* dst; - - n=loaderSizeBC(src); - dst=(char*)vmem_heap; - memcpy(dst,src,n); - nw=(n+3)>>2; - vmemInit(nw); - - src=(char*)vmem_heap; - - sys_start=vmem_stack-1; - for(i=0;i>2); - - return 0; + int n,nw,i; + char* src0; + char* dst; + + n=loaderSizeBC(src); + dst=(char*)vmem_heap; + memcpy(dst,src,n); + nw=(n+3)>>2; + vmemInit(nw); + + src=(char*)vmem_heap; + + sys_start=vmem_stack-1; + for(i=0; i>2); + + return 0; } @@ -122,6 +134,6 @@ int loaderInit(char *src) */ int loaderFunstart(int funnumber) { - return loaderGetInt(bc_tabfun+(funnumber<<2)); + return loaderGetInt(bc_tabfun+(funnumber<<2)); } diff --git a/src/vm/vlog.c b/src/vm/vlog.c index bb032a3..a0cc0f5 100644 --- a/src/vm/vlog.c +++ b/src/vm/vlog.c @@ -45,30 +45,55 @@ void logSecho(int p,int nl) { - if (p==NIL) consolestr("NIL"); - else consolebin((unsigned char*)VSTARTBIN(VALTOPNT(p)),VSIZEBIN(VALTOPNT(p))); - if (nl) consolestr(ENDLINE); + if (p==NIL) + { + consolestr("NIL"); + } + else + { + consolebin((unsigned char*)VSTARTBIN(VALTOPNT(p)),VSIZEBIN(VALTOPNT(p))); + } + if (nl) + { + consolestr(ENDLINE); + } } void logIecho(int i,int nl) { - if (i==NIL) consolestr("NIL"); - else consoleint(VALTOINT(i)); - if (nl) consolestr(ENDLINE); + if (i==NIL) + { + consolestr("NIL"); + } + else + { + consoleint(VALTOINT(i)); + } + if (nl) + { + consolestr(ENDLINE); + } } extern int currentop; void logGC() { - consolestr("#GC : sp=");consoleint(-vmem_stack); - consolestr(" hp=");consoleint(vmem_heapindex); - consolestr(" used=");consoleint((vmem_heapindex-vmem_stack)*100/VMEM_LENGTH); - consolestr("%"ENDLINE); - consolestr(" b:");consolehx(*((int*)vmem_heap)); - consolestr(" bc:");consolehx(*((int*)bytecode)); - consolestr(" st:");consolehx(vmem_start); - consolestr(" op:");consolehx(currentop); - consolestr(ENDLINE); + consolestr("#GC : sp="); + consoleint(-vmem_stack); + consolestr(" hp="); + consoleint(vmem_heapindex); + consolestr(" used="); + consoleint((vmem_heapindex-vmem_stack)*100/VMEM_LENGTH); + consolestr("%"ENDLINE); + consolestr(" b:"); + consolehx(*((int*)vmem_heap)); + consolestr(" bc:"); + consolehx(*((int*)bytecode)); + consolestr(" st:"); + consolehx(vmem_start); + consolestr(" op:"); + consolehx(currentop); + consolestr(ENDLINE); } @@ -78,29 +103,56 @@ void logGC() int sysLoad(char *dst,int i,int ldst,char *filename,int j,int len) { #ifdef VSIMU - FILE *f; - if ((j<0)||(i<0)||(len<=0)) return 0; - if (i+len>ldst) len=ldst-i; - if (len<=0) return 0; - f=fopen(filename,"rb"); - if (!f) return 0; - fseek(f,j,SEEK_SET); - len=fread(dst,1,len,f); - fclose(f); - return len; + FILE *f; + if ((j<0)||(i<0)||(len<=0)) + { + return 0; + } + if (i+len>ldst) + { + len=ldst-i; + } + if (len<=0) + { + return 0; + } + f=fopen(filename,"rb"); + if (!f) + { + return 0; + } + fseek(f,j,SEEK_SET); + len=fread(dst,1,len,f); + fclose(f); + return len; #endif #ifdef VREAL -/* set_vlsi_volume(0); //volume on 8bits, 0x00 => maximum - encode_adpcm((UBYTE*)dst+i,(ldst-i-256)>>8, j); + /* set_vlsi_volume(0); //volume on 8bits, 0x00 => maximum + encode_adpcm((UBYTE*)dst+i,(ldst-i-256)>>8, j); + return 0; + */ + if ((j<0)||(i<0)||(len<=0)) + { return 0; -*/ - if ((j<0)||(i<0)||(len<=0)) return 0; - if (i+len>ldst) len=ldst-i; - if (len<=0) return 0; - if (j+len>4096) len=4096-j; - if (len<=0) return 0; - read_uc_flash(j,(unsigned char*)dst,len); - return len; + } + if (i+len>ldst) + { + len=ldst-i; + } + if (len<=0) + { + return 0; + } + if (j+len>4096) + { + len=4096-j; + } + if (len<=0) + { + return 0; + } + read_uc_flash(j,(unsigned char*)dst,len); + return len; #endif } @@ -110,28 +162,58 @@ uchar buffer_temp[4096]; int sysSave(char *dst,int i,int ldst,char *filename,int j,int len) { #ifdef VSIMU - FILE *f; - if ((j<0)||(i<0)||(len<=0)) return 0; - if (i+len>ldst) len=ldst-i; - if (len<=0) return 0; - f=fopen(filename,"rb+"); - if (!f) f=fopen(filename,"wb+"); - if (!f) return 0; - fseek(f,j,SEEK_SET); - len=fwrite(dst,1,len,f); - fclose(f); - return len; + FILE *f; + if ((j<0)||(i<0)||(len<=0)) + { + return 0; + } + if (i+len>ldst) + { + len=ldst-i; + } + if (len<=0) + { + return 0; + } + f=fopen(filename,"rb+"); + if (!f) + { + f=fopen(filename,"wb+"); + } + if (!f) + { + return 0; + } + fseek(f,j,SEEK_SET); + len=fwrite(dst,1,len,f); + fclose(f); + return len; #endif #ifdef VREAL - if ((j<0)||(i<0)||(len<=0)) return 0; - if (i+len>ldst) len=ldst-i; - if (len<=0) return 0; - if (j+len>4096) len=4096-j; - if (len<=0) return 0; - __disable_interrupt(); - write_uc_flash(j,(unsigned char*)dst,len,buffer_temp); - __enable_interrupt(); - return len; + if ((j<0)||(i<0)||(len<=0)) + { + return 0; + } + if (i+len>ldst) + { + len=ldst-i; + } + if (len<=0) + { + return 0; + } + if (j+len>4096) + { + len=4096-j; + } + if (len<=0) + { + return 0; + } + __disable_interrupt(); + write_uc_flash(j,(unsigned char*)dst,len,buffer_temp); + __enable_interrupt(); + return len; #endif } @@ -141,44 +223,50 @@ static struct timeval firstTime; int sysTimems() { #ifdef VSIMU - int res; - struct timeval tv; - if (firstTimeSet) { - gettimeofday(&tv, NULL); - res = (tv.tv_sec - firstTime.tv_sec) * 1000; - res += (tv.tv_usec - firstTime.tv_usec) / 1000; - } else { - gettimeofday(&firstTime, NULL); - firstTimeSet = 1; - res = 0; - } - return res; + int res; + struct timeval tv; + if (firstTimeSet) + { + gettimeofday(&tv, NULL); + res = (tv.tv_sec - firstTime.tv_sec) * 1000; + res += (tv.tv_usec - firstTime.tv_usec) / 1000; + } + else + { + gettimeofday(&firstTime, NULL); + firstTimeSet = 1; + res = 0; + } + return res; #endif // ! VSIMU #ifdef VREAL - return counter_timer; + return counter_timer; #endif } int sysTime() { #ifdef VSIMU - int res; - static int firstTimeSet = 0; - static struct timeval firstTime; - struct timeval tv; - if (firstTimeSet) { - gettimeofday(&tv, NULL); - res = (tv.tv_sec - firstTime.tv_sec); - res += (tv.tv_usec >= firstTime.tv_usec) ? 0 : -1; - } else { - gettimeofday(&firstTime, NULL); - firstTimeSet = 1; - res = 0; - } - return res; + int res; + static int firstTimeSet = 0; + static struct timeval firstTime; + struct timeval tv; + if (firstTimeSet) + { + gettimeofday(&tv, NULL); + res = (tv.tv_sec - firstTime.tv_sec); + res += (tv.tv_usec >= firstTime.tv_usec) ? 0 : -1; + } + else + { + gettimeofday(&firstTime, NULL); + firstTimeSet = 1; + res = 0; + } + return res; #endif #ifdef VREAL - return counter_timer_s; + return counter_timer_s; #endif } @@ -188,242 +276,335 @@ int rndval; // retourne une valeur aléatoire entre 0 et 65535 int sysRand() { - rndval=rndval*0x1234567+11; - return (rndval>>8)&0xffff; + rndval=rndval*0x1234567+11; + return (rndval>>8)&0xffff; } void sysSrand(int seed) { - rndval=seed; + rndval=seed; } void sysCpy(char *dst,int i,int ldst,char *src,int j,int lsrc,int len) { - if ((i<0)||(j<0)||(len<=0)) return; - if (i+len>ldst) len=ldst-i; - if (len<=0) return; - if (j+len>lsrc) len=lsrc-j; - if (len<=0) return; - dst+=i; - src+=j; - while((len--)>0) *(dst++)=*(src++); + if ((i<0)||(j<0)||(len<=0)) + { + return; + } + if (i+len>ldst) + { + len=ldst-i; + } + if (len<=0) + { + return; + } + if (j+len>lsrc) + { + len=lsrc-j; + } + if (len<=0) + { + return; + } + dst+=i; + src+=j; + while((len--)>0) + { + *(dst++)=*(src++); + } } int sysCmp(char *dst,int i,int ldst,char *src,int j,int lsrc,int len) { - if ((i<0)||(j<0)||(len<=0)) return 0; - if ((i+len>ldst)&&(j+len>lsrc)) + if ((i<0)||(j<0)||(len<=0)) + { + return 0; + } + if ((i+len>ldst)&&(j+len>lsrc)) + { + if (ldst-i>lsrc-j) + { + len=ldst-i; + } + else { - if (ldst-i>lsrc-j) len=ldst-i; - else len=lsrc-j; + len=lsrc-j; } - dst+=i; - src+=j; - while((len--)>0) if (((unsigned char)*dst)>((unsigned char)*src)) return 1; - else if (((unsigned char)*(dst++))<((unsigned char)*(src++))) return -1; - return 0; + } + dst+=i; + src+=j; + while((len--)>0) if (((unsigned char)*dst)>((unsigned char)*src)) + { + return 1; + } + else if (((unsigned char)*(dst++))<((unsigned char)*(src++))) + { + return -1; + } + return 0; } int mystrcmp(char *dst,char *src,int len) { - while((len--)>0) if ((*(dst++))!=(*(src++))) return 1; - return 0; + while((len--)>0) if ((*(dst++))!=(*(src++))) + { + return 1; + } + return 0; } void mystrcpy(char *dst,char *src,int len) { - while((len--)>0) *(dst++)=*(src++); + while((len--)>0) + { + *(dst++)=*(src++); + } } int sysFind(char *dst,int i,int ldst,char *src,int j,int lsrc,int len) { - if ((j<0)||(j+len>lsrc)) return NIL; - src+=j; - if (i<0) i=0; - while(i+len<=ldst) - { - if (!mystrcmp(dst+i,src,len)) return INTTOVAL(i); - i++; - } - return NIL; + if ((j<0)||(j+len>lsrc)) + { + return NIL; + } + src+=j; + if (i<0) + { + i=0; + } + while(i+len<=ldst) + { + if (!mystrcmp(dst+i,src,len)) + { + return INTTOVAL(i); + } + i++; + } + return NIL; } int sysFindrev(char *dst,int i,int ldst,char *src,int j,int lsrc,int len) { - if ((j<0)||(j+len>lsrc)) return NIL; - src+=j; - if(i+len>ldst) i=ldst-len; - while(i>=0) - { - if (!mystrcmp(dst+i,src,len)) return INTTOVAL(i); - i--; - } - return NIL; + if ((j<0)||(j+len>lsrc)) + { + return NIL; + } + src+=j; + if(i+len>ldst) + { + i=ldst-len; + } + while(i>=0) + { + if (!mystrcmp(dst+i,src,len)) + { + return INTTOVAL(i); + } + i--; + } + return NIL; } int sysStrgetword(unsigned char *src,int len,int ind) { - int n; - if ((ind<0)||(ind+2>len)) return -1; - n=(src[ind]<<8)+src[ind+1]; - return n; + int n; + if ((ind<0)||(ind+2>len)) + { + return -1; + } + n=(src[ind]<<8)+src[ind+1]; + return n; } void sysStrputword(unsigned char *src,int len,int ind,int val) { - if ((ind<0)||(ind+2>len)) return; - src[ind+1]=val; val>>=8; - src[ind]=val; + if ((ind<0)||(ind+2>len)) + { + return; + } + src[ind+1]=val; + val>>=8; + src[ind]=val; } // lecture d'une chaîne décimale (s'arrête au premier caractère incorrect) int sysAtoi(char* src) { - int x,c,s; - x=s=0; - if ((*src)=='-') { s=1; src++; } - while(c=*src++) - { - if ((c>='0')&&(c<='9')) x=(x*10)+c-'0'; - else return (s?(-x):x); - } - return (s?(-x):x); + int x,c,s; + x=s=0; + if ((*src)=='-') + { + s=1; + src++; + } + while(c=*src++) + { + if ((c>='0')&&(c<='9')) + { + x=(x*10)+c-'0'; + } + else + { + return (s?(-x):x); + } + } + return (s?(-x):x); } // lecture d'une chaîne hexadécimale (s'arrête au premier caractère incorrect) int sysHtoi(char* src) { - int x,c; - x=0; - while(c=*src++) - { - if ((c>='0')&&(c<='9')) x=(x<<4)+c-'0'; - else if ((c>='A')&&(c<='F')) x=(x<<4)+c-'A'+10; - else if ((c>='a')&&(c<='f')) x=(x<<4)+c-'a'+10; - else return x; - } - return x; + int x,c; + x=0; + while(c=*src++) + { + if ((c>='0')&&(c<='9')) + { + x=(x<<4)+c-'0'; + } + else if ((c>='A')&&(c<='F')) + { + x=(x<<4)+c-'A'+10; + } + else if ((c>='a')&&(c<='f')) + { + x=(x<<4)+c-'a'+10; + } + else + { + return x; + } + } + return x; } void sysCtoa(int c) { - unsigned char res[1]; - res[0]=c; - VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,1))); + unsigned char res[1]; + res[0]=c; + VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,1))); } -const int itoarsc[10]={ - 1000000000,100000000,10000000, - 1000000 ,100000 ,10000, - 1000 ,100 ,10, - 1 +const int itoarsc[10]= +{ + 1000000000,100000000,10000000, + 1000000,100000,10000, + 1000,100,10, + 1 };; void sysItoa(int v) { - char res[16]; - int ires=0; - if (v==0) - { - res[ires++]='0'; - } - else - { - int start=1; - int imul=0; - if (v<0) + char res[16]; + int ires=0; + if (v==0) { - v=-v; - res[ires++]='-'; + res[ires++]='0'; } - while(imul<10) + else { - int k=0; - while(v>=itoarsc[imul]) - { - k++; - v-=itoarsc[imul]; - } - if ((k)||(!start)) - { - start=0; - res[ires++]='0'+k; - } - imul++; + int start=1; + int imul=0; + if (v<0) + { + v=-v; + res[ires++]='-'; + } + while(imul<10) + { + int k=0; + while(v>=itoarsc[imul]) + { + k++; + v-=itoarsc[imul]; + } + if ((k)||(!start)) + { + start=0; + res[ires++]='0'+k; + } + imul++; + } } - } - VPUSH(PNTTOVAL(VMALLOCSTR(res,ires))); + VPUSH(PNTTOVAL(VMALLOCSTR(res,ires))); } void sysItoh(int v) { - char res[16]; - int ires=0; - if (v==0) - { - res[ires++]='0'; - } - else - { - int start=1; - int imul=28; - while(imul>=0) - { - int c=(v>>imul)&15; - if ((c)||(!start)) - { - start=0; - res[ires++]=(c<10)?'0'+c:'a'+c-10; - } - imul-=4; - } - } - VPUSH(PNTTOVAL(VMALLOCSTR(res,ires))); + char res[16]; + int ires=0; + if (v==0) + { + res[ires++]='0'; + } + else + { + int start=1; + int imul=28; + while(imul>=0) + { + int c=(v>>imul)&15; + if ((c)||(!start)) + { + start=0; + res[ires++]=(c<10)?'0'+c:'a'+c-10; + } + imul-=4; + } + } + VPUSH(PNTTOVAL(VMALLOCSTR(res,ires))); } void sysCtoh(int c) { - unsigned char res[2]; - int v=(c>>4)&15; - res[0]=(v<10)?'0'+v:'a'+v-10; - v=c&15; - res[1]=(v<10)?'0'+v:'a'+v-10; - VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2))); + unsigned char res[2]; + int v=(c>>4)&15; + res[0]=(v<10)?'0'+v:'a'+v-10; + v=c&15; + res[1]=(v<10)?'0'+v:'a'+v-10; + VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2))); } void sysItobin2(int c) { - unsigned char res[2]; - res[1]=c; - c>>=8; - res[0]=c; - VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2))); + unsigned char res[2]; + res[1]=c; + c>>=8; + res[0]=c; + VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,2))); } int sysListswitch(int p,int key) { - while(p!=NIL) - { - int q=VALTOPNT(VFETCH(p,0)); - if ((q!=NIL)&&(VFETCH(q,0)==key)) return VFETCH(q,1); - p=VALTOPNT(VFETCH(p,1)); - } - return NIL; + while(p!=NIL) + { + int q=VALTOPNT(VFETCH(p,0)); + if ((q!=NIL)&&(VFETCH(q,0)==key)) + { + return VFETCH(q,1); + } + p=VALTOPNT(VFETCH(p,1)); + } + return NIL; } int sysListswitchstr(int p,char* key) { - while(p!=NIL) - { - int q=VALTOPNT(VFETCH(p,0)); - if (q!=NIL) + while(p!=NIL) { - int r=VALTOPNT(VFETCH(q,0)); - if ((r!=NIL)&&(!strcmp(VSTARTBIN(r),key))) return VFETCH(q,1); + int q=VALTOPNT(VFETCH(p,0)); + if (q!=NIL) + { + int r=VALTOPNT(VFETCH(q,0)); + if ((r!=NIL)&&(!strcmp(VSTARTBIN(r),key))) + { + return VFETCH(q,1); + } + } + p=VALTOPNT(VFETCH(p,1)); } - p=VALTOPNT(VFETCH(p,1)); - } - return NIL; + return NIL; } void simuSetLed(int i,int val); @@ -436,27 +617,33 @@ char* get_rfid(); void sysLed(int led,int col) { #ifdef VSIMU - simuSetLed(led,col); + simuSetLed(led,col); #endif #ifdef VREAL - set_led((UWORD)led,(UWORD)col); + set_led((UWORD)led,(UWORD)col); #endif } void sysMotorset(int motor,int sens) { #ifdef VSIMU - set_motor_dir(motor,sens); + set_motor_dir(motor,sens); #endif #ifdef VREAL // char buffer[256]; - motor=1+(motor&1); + motor=1+(motor&1); // sprintf(buffer,"setmotor %d sens %d\r\n",motor,sens); // consolestr(buffer); - if (sens==0) stop_motor(motor); - else run_motor(motor,255,(sens>0)?REVERSE:FORWARD/*:REVERSE*/); + if (sens==0) + { + stop_motor(motor); + } + else + { + run_motor(motor,255,(sens>0)?REVERSE:FORWARD/*:REVERSE*/); + } #endif } @@ -466,56 +653,56 @@ int kvmotor[3]; int sysMotorget(int motor) { #ifdef VSIMU - return get_motor_val(motor); + return get_motor_val(motor); #endif #ifdef VREAL // char buffer[256]; - int k,kx; - motor=1+(motor&1); - kx=(int)get_motor_position(motor); -/* k=(int)get_motor_position(motor); - if (kmotor[motor]!=k) - { - kmotor[motor]=k; - kvmotor[motor]++; - } - kx=kvmotor[motor]; -*/ + int k,kx; + motor=1+(motor&1); + kx=(int)get_motor_position(motor); + /* k=(int)get_motor_position(motor); + if (kmotor[motor]!=k) + { + kmotor[motor]=k; + kvmotor[motor]++; + } + kx=kvmotor[motor]; + */ // sprintf(buffer,"getmotor %d pos %x / %x\r\n",motor,k,kx); // if(motor==2) // consolestr(buffer); - return kx; + return kx; #endif } int sysButton2() { #ifdef VSIMU - return getButton(); + return getButton(); #endif #ifdef VREAL - return push_button_value(); + return push_button_value(); #endif } int sysButton3() { #ifdef VSIMU - return get_button3(); + return get_button3(); #endif #ifdef VREAL - return 255-get_adc_value(); + return 255-get_adc_value(); #endif } char* sysRfidget() { #ifdef VSIMU - return get_rfid(); + return get_rfid(); #endif #ifdef VREAL - return get_rfid_first_device(); + return get_rfid_first_device(); #endif } @@ -540,50 +727,56 @@ void sysFlash(char* firmware,int len) exit(0); #endif #ifdef VREAL - __disable_interrupt(); - flash_uc((unsigned char*)firmware,len,buffer_temp); + __disable_interrupt(); + flash_uc((unsigned char*)firmware,len,buffer_temp); #endif } const uchar inv8[128]= { -1,171,205,183,57,163,197,239,241,27,61,167,41,19,53,223,225,139,173,151,25,131,165,207,209,251,29,135,9,243,21,191,193,107,141,119,249,99,133,175,177,219,253,103,233,211,245,159,161,75,109,87,217,67,101,143,145,187,221,71,201,179,213,127,129,43,77,55,185,35,69,111,113,155,189,39,169,147,181,95,97,11,45,23,153,3,37,79,81,123,157,7,137,115,149,63,65,235,13,247,121,227,5,47,49,91,125,231,105,83,117,31,33,203,237,215,89,195,229,15,17,59,93,199,73,51,85,255 + 1,171,205,183,57,163,197,239,241,27,61,167,41,19,53,223,225,139,173,151,25,131,165,207,209,251,29,135,9,243,21,191,193,107,141,119,249,99,133,175,177,219,253,103,233,211,245,159,161,75,109,87,217,67,101,143,145,187,221,71,201,179,213,127,129,43,77,55,185,35,69,111,113,155,189,39,169,147,181,95,97,11,45,23,153,3,37,79,81,123,157,7,137,115,149,63,65,235,13,247,121,227,5,47,49,91,125,231,105,83,117,31,33,203,237,215,89,195,229,15,17,59,93,199,73,51,85,255 }; int decode8(uchar* src,int len,uchar key,uchar alpha) { - while(len--) - { - uchar v=((*src)-alpha)*key; - *(src++)=v; - key=v+v+1; - } - return key; + while(len--) + { + uchar v=((*src)-alpha)*key; + *(src++)=v; + key=v+v+1; + } + return key; } int encode8(uchar* src,int len,uchar key,uchar alpha) { - while(len--) - { - uchar v=*src; - *(src++)=alpha+(v*inv8[key>>1]); - key=v+v+1; - } - return key; + while(len--) + { + uchar v=*src; + *(src++)=alpha+(v*inv8[key>>1]); + key=v+v+1; + } + return key; } int sysCrypt(char* src,int indexsrc,int len,int lensrc,unsigned int key,int alpha) { - if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0)) return -1; - return encode8(src+indexsrc,len,key,alpha); + if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0)) + { + return -1; + } + return encode8(src+indexsrc,len,key,alpha); } int sysUncrypt(char* src,int indexsrc,int len,int lensrc,unsigned int key,int alpha) { - if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0)) return -1; - return decode8(src+indexsrc,len,key,alpha); + if ((indexsrc<0)||(indexsrc+len>lensrc)||(len<=0)) + { + return -1; + } + return decode8(src+indexsrc,len,key,alpha); } diff --git a/src/vm/vmem.c b/src/vm/vmem.c index c8c8721..5d394d2 100644 --- a/src/vm/vmem.c +++ b/src/vm/vmem.c @@ -26,399 +26,436 @@ int vmem_broken; // initialisation de la mémoire void vmemInit(int start) { - vmem_top=&vmem_heap[VMEM_LENGTH]; - vmem_stack=0; - vmem_start=vmem_heapindex=start; - vmem_broken=0; + vmem_top=&vmem_heap[VMEM_LENGTH]; + vmem_stack=0; + vmem_start=vmem_heapindex=start; + vmem_broken=0; } void vmemSetstart(int start) { - int size; + int size; - if (start>=vmem_start-HEADER_LENGTH) return; + if (start>=vmem_start-HEADER_LENGTH) + { + return; + } - size=((vmem_start-HEADER_LENGTH-start)<<2)-1; + size=((vmem_start-HEADER_LENGTH-start)<<2)-1; - vmem_heap[start]=(size<<8)+(TYPE_BINARY); - vmem_heap[start+HEADER_GC]=0; - vmem_heap[start+HEADER_LIST]=0; + vmem_heap[start]=(size<<8)+(TYPE_BINARY); + vmem_heap[start+HEADER_GC]=0; + vmem_heap[start+HEADER_LIST]=0; - vmem_start=start; + vmem_start=start; } void vmemGCfirst() { - int i,k,j,n; - int first; - - first=-1; - for(i=vmem_stack;i<0;i++) - { - k=vmem_top[i]; - if ((ISVALPNT(k))&&(k!=NIL)) - { - k=VALTOPNT(k); + int i,k,j,n; + int first; + + first=-1; + for(i=vmem_stack; i<0; i++) + { + k=vmem_top[i]; + if ((ISVALPNT(k))&&(k!=NIL)) + { + k=VALTOPNT(k); #ifdef VMEM_DBG - if ((k<0)||(k>=VMEM_LENGTH)) consolestr("1.k out of space\n"); + if ((k<0)||(k>=VMEM_LENGTH)) + { + consolestr("1.k out of space\n"); + } #endif - if (!HEADER_USED(k)) - { - HEADER_MARK(k); - vmem_heap[k+HEADER_LIST]=first; - first=k; - } - } - } - while(first!=-1) - { - k=first; + if (!HEADER_USED(k)) + { + HEADER_MARK(k); + vmem_heap[k+HEADER_LIST]=first; + first=k; + } + } + } + while(first!=-1) + { + k=first; #ifdef VMEM_DBG - if ((k<0)||(k>=VMEM_LENGTH)) consolestr("1.first out of space\n"); + if ((k<0)||(k>=VMEM_LENGTH)) + { + consolestr("1.first out of space\n"); + } #endif - first=vmem_heap[k+HEADER_LIST]; - if (HEADER_TYPE(k)) // bloc table - { - n=VSIZE(k); - j=k+HEADER_LENGTH; - for(i=0;i=VMEM_LENGTH)) consolestr("1.k2 out of space\n"); + if ((k<0)||(k>=VMEM_LENGTH)) + { + consolestr("1.k2 out of space\n"); + } #endif - if (!HEADER_USED(k)) - { - HEADER_MARK(k); - vmem_heap[k+HEADER_LIST]=first; - first=k; - } - } - } - } - } + if (!HEADER_USED(k)) + { + HEADER_MARK(k); + vmem_heap[k+HEADER_LIST]=first; + first=k; + } + } + } + } + } } #ifdef VMEM_DBG void dumpheap() { - int pos,realsize; + int pos,realsize; #ifdef VSIMU - FILE *f; - - f=fopen("mem.bin","wb"); - fwrite(&vmem_start,1,4,f); - fwrite(&vmem_heapindex,1,4,f); - fwrite(&vmem_stack,1,4,f); - fwrite(vmem_heap,VMEM_LENGTH,1,f); - fclose(f); + FILE *f; + + f=fopen("mem.bin","wb"); + fwrite(&vmem_start,1,4,f); + fwrite(&vmem_heapindex,1,4,f); + fwrite(&vmem_stack,1,4,f); + fwrite(vmem_heap,VMEM_LENGTH,1,f); + fclose(f); #endif - pos=vmem_start; - - while(pos < vmem_heapindex) - { - realsize=VSIZE(pos)+HEADER_LENGTH; - consolehx(pos); consolestr(":pos ");consolehx(realsize); - consolestr(":realsize\n"); - if ((realsize<0)||(realsize>=VMEM_LENGTH)) - { - consolestr("2.realsize out of range\n"); - dump((char*)&vmem_heap[pos-32],128); - return; - } - dump((char*)&vmem_heap[pos],32); - pos+=realsize; - } + pos=vmem_start; + + while(pos < vmem_heapindex) + { + realsize=VSIZE(pos)+HEADER_LENGTH; + consolehx(pos); + consolestr(":pos "); + consolehx(realsize); + consolestr(":realsize\n"); + if ((realsize<0)||(realsize>=VMEM_LENGTH)) + { + consolestr("2.realsize out of range\n"); + dump((char*)&vmem_heap[pos-32],128); + return; + } + dump((char*)&vmem_heap[pos],32); + pos+=realsize; + } } #endif void vmemGCsecond() { - int pos,newpos,realsize; - - pos=newpos=vmem_start; - - while(pos < vmem_heapindex) - { - realsize=VSIZE(pos)+HEADER_LENGTH; + int pos,newpos,realsize; + + pos=newpos=vmem_start; + + while(pos < vmem_heapindex) + { + realsize=VSIZE(pos)+HEADER_LENGTH; #ifdef VMEM_DBG - if ((realsize<0)||(realsize>=VMEM_LENGTH)) - { - dumpheap(); + if ((realsize<0)||(realsize>=VMEM_LENGTH)) + { + dumpheap(); #ifdef VSIMU - printf("CORE DUMP\n"); - getchar(); - exit(0); + printf("CORE DUMP\n"); + getchar(); + exit(0); #endif - } + } #endif - if (HEADER_USED(pos)) - { - vmem_heap[pos+HEADER_GC]=newpos<<1; - newpos+=realsize; - } - pos+=realsize; - } + if (HEADER_USED(pos)) + { + vmem_heap[pos+HEADER_GC]=newpos<<1; + newpos+=realsize; + } + pos+=realsize; + } } void vmemGCthird() { - int i,k,j,n; - int first; - - first=-1; - for(i=vmem_stack;i<0;i++) - { - k=vmem_top[i]; - if ((ISVALPNT(k))&&(k!=NIL)) - { - k=VALTOPNT(k); - vmem_top[i]=vmem_heap[k+HEADER_GC]|1; // attention, hack - if (!HEADER_USED(k)) - { - HEADER_MARK(k); - vmem_heap[k+HEADER_LIST]=first; - first=k; - } - } - } - while(first!=-1) - { - k=first; - first=vmem_heap[k+HEADER_LIST]; - if (HEADER_TYPE(k)) // bloc table - { - n=VSIZE(k); - j=k+HEADER_LENGTH; - for(i=0;i>2); - - if (VMEM_LENGTH+vmem_stack-vmem_heapindex-wsize>2); + + if (VMEM_LENGTH+vmem_stack-vmem_heapindex-wsize=0) - { - int i; - for(i=0;i=0) + { + int i; + for(i=0; i0) q[--n]=VPULL(); - VPUSH(PNTTOVAL(p)); + int *q; + int p=vmemAllocTab(n,0); + q=VSTART(p); + while(n>0) + { + q[--n]=VPULL(); + } + VPUSH(PNTTOVAL(p)); } void vmemDumpHeap() { #ifdef VSIMU - int i,pos,realsize,n; + int i,pos,realsize,n; #endif - consolestr("HEAP\n----\n"); + consolestr("HEAP\n----\n"); #ifdef VSIMU - pos=vmem_start; - n=0; - while(pos < vmem_heapindex) - { - realsize=VSIZE(pos)+HEADER_LENGTH; - printf("%06X : %s %d\n",pos,HEADER_TYPE(pos)?"Tab":"Bin",VSIZE(pos)); - for(i=0;i=vmem_stack;i--) - { - k=vmem_top[i]; - printf("%d : %6x -> %6x (%d)\n",i,k,k>>1,k>>1); - } + for(i=-1; i>=vmem_stack; i--) + { + k=vmem_top[i]; + printf("%d : %6x -> %6x (%d)\n",i,k,k>>1,k>>1); + } #endif } void vmemDump() { - vmemDumpHeap(); - vmemDumpStack(); + vmemDumpHeap(); + vmemDumpStack(); } void vmemDumpShort() { - logGC(); + logGC(); } diff --git a/src/vm/vnet.c b/src/vm/vnet.c index 77944e8..debc172 100644 --- a/src/vm/vnet.c +++ b/src/vm/vnet.c @@ -20,38 +20,60 @@ int netstatesimu=1; int netState() { #ifdef VSIMU - return netstatesimu; + return netstatesimu; #endif #ifdef VREAL - return rt2501_state(); + return rt2501_state(); #endif } int netSend(char* src,int indexsrc,int lentosend,int lensrc,char* macdst,int inddst,int lendst,int speed) { #ifdef VSIMU - my_printf(LOG_SIMUNET, "xxxx netSend\n"); - return -1; + my_printf(LOG_SIMUNET, "xxxx netSend\n"); + return -1; #endif #ifdef VREAL - if (indexsrc<0) return -1; - if (indexsrc+lentosend>lensrc) lentosend=lensrc-indexsrc; - if (lentosend<=0) return -1; - if (inddst<0) return -1; - if (inddst+6>lendst) return -1; - return rt2501_send((const unsigned char *)(src+indexsrc),lentosend,(const unsigned char *)(macdst+inddst),speed,1); + if (indexsrc<0) + { + return -1; + } + if (indexsrc+lentosend>lensrc) + { + lentosend=lensrc-indexsrc; + } + if (lentosend<=0) + { + return -1; + } + if (inddst<0) + { + return -1; + } + if (inddst+6>lendst) + { + return -1; + } + return rt2501_send((const unsigned char *)(src+indexsrc),lentosend,(const unsigned char *)(macdst+inddst),speed,1); #endif } int netCb(char* src,int lensrc,char* macsrc) { - VPUSH(PNTTOVAL(VMALLOCSTR(src,lensrc))); - VPUSH(PNTTOVAL(VMALLOCSTR(macsrc,6))); - VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); - if (VSTACKGET(0)!=NIL) interpGo(); - else { VPULL();VPULL();} - VPULL(); - return 0; + VPUSH(PNTTOVAL(VMALLOCSTR(src,lensrc))); + VPUSH(PNTTOVAL(VMALLOCSTR(macsrc,6))); + VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP)); + if (VSTACKGET(0)!=NIL) + { + interpGo(); + } + else + { + VPULL(); + VPULL(); + } + VPULL(); + return 0; } extern unsigned char rt2501_mac[6]; @@ -59,11 +81,11 @@ extern unsigned char rt2501_mac[6]; #ifdef VSIMU int htoi2(char* src) { - char buf[3]; - buf[0]=src[0]; - buf[1]=src[1]; - buf[2]=0; - return sysHtoi(buf); + char buf[3]; + buf[0]=src[0]; + buf[1]=src[1]; + buf[2]=0; + return sysHtoi(buf); } #endif @@ -72,50 +94,65 @@ char mac[6]; char* netMac() { #ifdef VSIMU - int i; - char *p=PropGet("MAC"); - for(i=0;i<6;i++) mac[i]=htoi2(&p[i*2]); - return mac; + int i; + char *p=PropGet("MAC"); + for(i=0; i<6; i++) + { + mac[i]=htoi2(&p[i*2]); + } + return mac; // return "abcdef"; #endif #ifdef VREAL - return (char*) rt2501_mac; + return (char*) rt2501_mac; #endif } int netChk(char* src,int indexsrc,int lentosend,int lensrc,unsigned int val) { - unsigned short* p; - - if (indexsrc<0) return val; - if (indexsrc+lentosend>lensrc) lentosend=lensrc-indexsrc; - if (lentosend<=0) return val; - - src+=indexsrc; - p=(unsigned short*)src; - - val=((val<<8)&0xff00)+((val>>8)&0xff); - while(lentosend>1) - { - val+=*(p++); - lentosend-=2; - } - if (lentosend) val+=*(unsigned char*)p; - - val=(val>>16)+(val&0xffff); - val=(val>>16)+(val&0xffff); - val=((val<<8)&0xff00)+((val>>8)&0xff); - return val; + unsigned short* p; + + if (indexsrc<0) + { + return val; + } + if (indexsrc+lentosend>lensrc) + { + lentosend=lensrc-indexsrc; + } + if (lentosend<=0) + { + return val; + } + + src+=indexsrc; + p=(unsigned short*)src; + + val=((val<<8)&0xff00)+((val>>8)&0xff); + while(lentosend>1) + { + val+=*(p++); + lentosend-=2; + } + if (lentosend) + { + val+=*(unsigned char*)p; + } + + val=(val>>16)+(val&0xffff); + val=(val>>16)+(val&0xffff); + val=((val<<8)&0xff00)+((val>>8)&0xff); + return val; } void netSetmode(int mode,char* ssid,int chn) { #ifdef VSIMU - my_printf(LOG_SIMUNET, "xxxx netSetmode %d %s %d\n",mode,ssid,chn); - netstatesimu=(mode)?5:4; + my_printf(LOG_SIMUNET, "xxxx netSetmode %d %s %d\n",mode,ssid,chn); + netstatesimu=(mode)?5:4; #endif #ifdef VREAL - rt2501_setmode(mode,ssid,chn); + rt2501_setmode(mode,ssid,chn); #endif } @@ -124,109 +161,115 @@ int nscan; #ifdef VREAL void netScan_(struct rt2501_scan_result *scan_result, void *userparam) { - char buf[256]; - sprintf(buf,">>> %s %d %d %d %d\n",scan_result->ssid,scan_result->rssi,scan_result->channel,scan_result->rateset,scan_result->encryption); - consolestr((UBYTE*)buf); - VPUSH(PNTTOVAL(VMALLOCSTR(scan_result->ssid,strlen(scan_result->ssid)))); - VPUSH(PNTTOVAL(VMALLOCSTR((char*)scan_result->mac,6))); - VPUSH(PNTTOVAL(VMALLOCSTR((char*)scan_result->bssid,6))); - VPUSH(INTTOVAL(scan_result->rssi)); - VPUSH(INTTOVAL(scan_result->channel)); - VPUSH(INTTOVAL(scan_result->rateset)); - VPUSH(INTTOVAL(scan_result->encryption)); - VMKTAB(7); - nscan++; + char buf[256]; + sprintf(buf,">>> %s %d %d %d %d\n",scan_result->ssid,scan_result->rssi,scan_result->channel,scan_result->rateset,scan_result->encryption); + consolestr((UBYTE*)buf); + VPUSH(PNTTOVAL(VMALLOCSTR(scan_result->ssid,strlen(scan_result->ssid)))); + VPUSH(PNTTOVAL(VMALLOCSTR((char*)scan_result->mac,6))); + VPUSH(PNTTOVAL(VMALLOCSTR((char*)scan_result->bssid,6))); + VPUSH(INTTOVAL(scan_result->rssi)); + VPUSH(INTTOVAL(scan_result->channel)); + VPUSH(INTTOVAL(scan_result->rateset)); + VPUSH(INTTOVAL(scan_result->encryption)); + VMKTAB(7); + nscan++; } #endif void netScan(char* ssid) { - nscan=0; + nscan=0; #ifdef VSIMU - VPUSH(PNTTOVAL(VMALLOCSTR("foo",3))); - VPUSH(PNTTOVAL(VMALLOCSTR("xyzxyz",6))); - VPUSH(PNTTOVAL(VMALLOCSTR("765432",6))); - VPUSH(INTTOVAL(-20)); - VPUSH(INTTOVAL(11)); - VPUSH(INTTOVAL(1)); - VPUSH(INTTOVAL(0)); - VMKTAB(7); - nscan++; - VPUSH(PNTTOVAL(VMALLOCSTR("foo",3))); - VPUSH(PNTTOVAL(VMALLOCSTR("xyzxyz",6))); - VPUSH(PNTTOVAL(VMALLOCSTR("765432",6))); - VPUSH(INTTOVAL(-10)); - VPUSH(INTTOVAL(11)); - VPUSH(INTTOVAL(1)); - VPUSH(INTTOVAL(0)); - VMKTAB(7); - nscan++; - VPUSH(PNTTOVAL(VMALLOCSTR("zzzz",3))); - VPUSH(PNTTOVAL(VMALLOCSTR("xyzxyz",6))); - VPUSH(PNTTOVAL(VMALLOCSTR("765432",6))); - VPUSH(INTTOVAL(-10)); - VPUSH(INTTOVAL(11)); - VPUSH(INTTOVAL(1)); - VPUSH(INTTOVAL(0)); - VMKTAB(7); - nscan++; - VPUSH(PNTTOVAL(VMALLOCSTR("",3))); - VPUSH(PNTTOVAL(VMALLOCSTR("xyzxyz",6))); - VPUSH(PNTTOVAL(VMALLOCSTR("765432",6))); - VPUSH(INTTOVAL(-10)); - VPUSH(INTTOVAL(11)); - VPUSH(INTTOVAL(1)); - VPUSH(INTTOVAL(0)); - VMKTAB(7); - nscan++; - VPUSH(PNTTOVAL(VMALLOCSTR("a\"a>=8; - res[2]=val; val>>=8; - res[1]=val; val>>=8; - res[0]=val; - VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,4))); + unsigned char res[4]; + unsigned int val; + val=(seq[0]<<24)+(seq[1]<<16)+(seq[2]<<8)+seq[3]; + val+=n; + res[3]=val; + val>>=8; + res[2]=val; + val>>=8; + res[1]=val; + val>>=8; + res[0]=val; + VPUSH(PNTTOVAL(VMALLOCSTR((char*)res,4))); } void mypassword_to_pmk(const char *password, char *ssid, int ssidlength, unsigned char *pmk); @@ -234,22 +277,22 @@ void mypassword_to_pmk(const char *password, char *ssid, int ssidlength, unsigne void netPmk(char* ssid,char* key,char* buf) { #ifdef VSIMU - my_printf(LOG_SIMUNET, "xxxx netPmk %s %s\n",ssid,key); - strcpy(buf,"01234567012345670123456701234567"); + my_printf(LOG_SIMUNET, "xxxx netPmk %s %s\n",ssid,key); + strcpy(buf,"01234567012345670123456701234567"); #endif #ifdef VREAL // strcpy(buf,"01234567012345670123456701234567"); - mypassword_to_pmk(key,ssid,strlen(ssid), (unsigned char*)buf); + mypassword_to_pmk(key,ssid,strlen(ssid), (unsigned char*)buf); #endif } int netRssi() { #ifdef VSIMU - return -20; + return -20; #endif #ifdef VREAL - return rt2501_rssi_average(); + return rt2501_rssi_average(); #endif }