Initial revision
authorJosh Yelon <jyelon@uiuc.edu>
Tue, 9 Apr 1996 22:54:39 +0000 (22:54 +0000)
committerJosh Yelon <jyelon@uiuc.edu>
Tue, 9 Apr 1996 22:54:39 +0000 (22:54 +0000)
src/xlat-i/xi-generate.C [new file with mode: 0644]
src/xlat-i/xi-main.C [new file with mode: 0644]
src/xlat-i/xi-symbol.C [new file with mode: 0644]
src/xlat-i/xi-symbol.h [new file with mode: 0644]

diff --git a/src/xlat-i/xi-generate.C b/src/xlat-i/xi-generate.C
new file mode 100644 (file)
index 0000000..96d6353
--- /dev/null
@@ -0,0 +1,349 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <iostream.h>
+#include <fstream.h>
+#include "xi-symbol.h"
+#include "xi-parse.h"
+
+extern Module *thismodule ;
+
+int moduleHasMain = 0;
+
+void GenerateStructsFns(ofstream& top, ofstream& bot) ;
+void GenerateRegisterCalls(ofstream& top, ofstream& bot) ;
+
+
+void Generate(char *interfacefile)
+{
+        char modulename[1024], topname[1024], botname[1024] ;
+        strcpy(modulename, interfacefile) ;
+       modulename[strlen(interfacefile)-3] = '\0' ; // assume ModuleName.ci
+       strcpy(topname,modulename) ;
+       strcat(topname,".top.h") ;
+       strcpy(botname,modulename) ;
+       strcat(botname,".bot.h") ;
+
+       ofstream top(topname), bot(botname) ;
+
+       GenerateStructsFns(top, bot) ;
+       GenerateRegisterCalls(top, bot) ;
+}
+
+
+
+void GenerateStructsFns(ofstream& top, ofstream& bot)
+{
+       char str[2048] ;
+       Chare *c; 
+       Entry *e;
+       /* Output all chare and EP id variables. Note : if this chare is not
+          defined in this module, put "extern" and dont initialize.  */
+        
+       top << "typedef struct { void *obj, *m; CHARE_BLOCK *chareblock; } Element;" << endl ;
+       top << "CpvExtern(CHARE_BLOCK *,currentChareBlock);" << endl ;
+
+       for (c=thismodule->chares; c!=NULL; c=c->next ) {
+
+               sprintf(str,"int _CK_chare_%s = 0 ;",c->name) ;
+               top << str << endl ;
+
+               for (e=c->entries; e!=NULL; e=e->next ) {
+                       sprintf(str,"int _CK_ep_%s_%s = 0 ;",c->name,e->name) ;
+                       top << str << endl ;
+               }
+       }
+
+
+
+       /* Output EP stub functions. Note : we assume main::main always
+          has argc-argv. */
+       for ( c=thismodule->chares; c!=NULL; c=c->next ) {
+               for (e=c->entries; e!=NULL; e=e->next ) {
+                       // This is the main::main EP
+                       if ( strcmp(c->name,"main")==0 && 
+                                               strcmp(e->name,"main")==0 ) {
+                               bot << "void _CK_call_main_main(void *m, void *obj, int argc, char *argv[])" << endl ;
+                               bot << "{" << endl ;
+                               bot << "\tnew (obj) main(argc,argv) ;" << endl;
+                               bot << "}" << endl ;
+                               moduleHasMain = 1;
+                               // ERROR if isThreaded() or isReturnMsg()
+                               continue ;
+                       }
+
+                       // Is this a threaded EP
+                       if (e->isThreaded()){
+                               sprintf(str,"void _CK_call_threaded_%s_%s(void *velt)",c->name,e->name) ;
+                               bot << str << endl ;
+                               bot << "{" << endl ;
+                               bot << "\tElement *elt = (Element *) velt;" << endl;
+                               bot << "\tvoid *obj = elt->obj;" << endl;
+                               bot << "\tvoid *m = elt->m;" << endl;
+                               bot << "\tCpvAccess(currentChareBlock) = elt->chareblock;" << endl;
+
+                               /* This is the constructor EP */
+                               if ( strcmp(c->name, e->name) == 0 ) {
+
+                                       sprintf(str,"\tnew (obj) %s((%s *)m) ;",
+                                                       c->name, e->msgtype->name) ;
+                                       bot << str << endl ;
+
+                                       // ERROR if isReturnMsg()
+
+                               } else if (e->isReturnMsg()){ // Returns a message
+                                       bot << "ENVELOPE *env = ENVELOPE_UPTR(m);" << endl;
+                                       bot << "\tint i = GetEnv_ref(env);" << endl;
+                                       bot << "\tint j = GetEnv_pe(env);" << endl;
+                                       sprintf(str, "\t%s *m2 = ((%s *)obj)->%s((%s *)m);", e->returnMsg->name, c->name, e->name, e->msgtype->name);
+                                       bot << str << endl ;
+                                       bot << "\tSetRefNumber(m2, i);" << endl;
+                                       bot << "\tCSendToFuture(m2, j);" << endl;
+                               } else { // Regular EP
+                                       sprintf(str,"\t((%s *)obj)->%s((%s *)m) ;",
+                                               c->name,e->name,e->msgtype->name) ;
+                                       bot << str << endl ;
+                               }
+                               bot << "\tCmiFree(elt);" << endl;
+                               bot << "}" << endl ;
+
+                       }else{
+                               sprintf(str,"void _CK_call_%s_%s(void *m, void *obj)",c->name,e->name) ;
+                               bot << str << endl ;
+                               bot << "{" << endl ;
+
+                               /* This is the constructor EP */
+                               if ( strcmp(c->name, e->name) == 0 ) {
+
+                                       sprintf(str,"\tnew (obj) %s((%s *)m) ;",
+                                                       c->name, e->msgtype->name) ;
+                                       bot << str << endl ;
+
+                                       // ERROR if isReturnMsg()
+
+                               } else if (e->isReturnMsg()){ // Returns a message
+                                       bot << "ENVELOPE *env = ENVELOPE_UPTR(m);" << endl;
+                                       bot << "\tint i = GetEnv_ref(env);" << endl;
+                                       bot << "\tint j = GetEnv_pe(env);" << endl;
+                                       sprintf(str, "\t%s *m2 = ((%s *)obj)->%s((%s *)m);", e->returnMsg->name, c->name, e->name, e->msgtype->name);
+                                       bot << str << endl ;
+//                                     bot << "ENVELOPE *env2 = ENVELOPE_UPTR(m2);" << endl;
+                                       bot << "\tSetRefNumber(m2, i);" << endl;
+//                                     bot << "\tCMIsendmsg(env2, m2);" << endl;
+//                                     bot << "\tCkCheck_and_Send(j, env2);" << endl;
+                                       bot << "\tCSendToFuture( m2, j);" << endl;
+
+                               } else { // Regular EP
+                                       sprintf(str,"\t((%s *)obj)->%s((%s *)m) ;",
+                                               c->name,e->name,e->msgtype->name) ;
+                                       bot << str << endl ;
+                               }
+
+                               bot << "}" << endl ;
+                       }
+
+                       if (e->isThreaded()){
+                               sprintf(str,"void _CK_call_%s_%s(void *m, void *obj)",c->name,e->name) ;
+                               bot << str << endl ;
+                               bot << "{" << endl ;
+                               bot << "\tCthThread t;" << endl;
+                               bot << "\tElement *element = (Element *) CmiAlloc(sizeof(Element));" << endl;
+                               bot << "\telement->m = m;\n\telement->obj = obj;" << endl;
+                               bot << "\telement->chareblock = CpvAccess(currentChareBlock) ;" << endl ;
+
+                               sprintf(str,"\tt = CthCreate(_CK_call_threaded_%s_%s, (void *) element,0);",c->name,e->name) ;
+                               bot << str << endl;
+                               bot << "\tCthSetStrategyDefault(t);" << endl;
+                               bot << "\tCthAwaken(t);" << endl;
+
+                               bot << "}" << endl ;
+                       }
+
+               } // endfor e =
+       } // endfor c =
+
+
+       /* Output ids for readonly messages */
+        for ( ReadOnly *r=thismodule->readonlys; r!=NULL; r=r->next ) 
+               if ( r->ismsg )
+                       top << "int _CK_index_" << r->name << ";" << endl ;
+
+
+       /* Output ids for message types */
+        for ( Message *m=thismodule->messages; m!=NULL; m=m->next ) 
+               top << "int _CK_msg_" << m->name << "=0;" << endl ;
+
+
+
+       /* for packable MsgTypes output the pack - unpack stub functions */
+        for ( m=thismodule->messages; m!=NULL; m=m->next ) {
+               if ( !m->packable )
+                       continue ;
+
+               sprintf(str,"static void _CK_pack_%s(void *in, void **out, int *length)\n{\n",m->name) ; 
+               bot << str ;
+               sprintf(str,"\t(*out) = ((%s *)in)->pack(length) ;\n}\n",m->name) ;
+               bot << str ;
+
+
+               sprintf(str,"static void _CK_unpack_%s(void *in, void **out)\n{\n",m->name) ; 
+               bot << str ;
+               sprintf(str,"\t%s * m = (%s *)GenericCkAlloc(_CK_msg_%s,sizeof(%s),0) ;\n", m->name, m->name, m->name, m->name) ;
+               bot << str ;
+               bot << "\t(*out) = (void *) m ;" << endl ;
+               bot << "\tm->unpack(in) ;\n}" << endl ;
+       }
+
+
+
+       /* Output _CK_mod_CopyFromBuffer, _CK_mod_CopyToBuffer for readonlys */
+       bot << "extern \"C\" void _CK_13CopyFromBuffer(void *,int) ;" << endl ;
+       bot << "void _CK_" << thismodule->name << "_CopyFromBuffer(void **_CK_ReadMsgTable)\n{" << endl ;
+
+       for ( r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( r->ismsg )
+                       continue ;
+               sprintf(str,"\t_CK_13CopyFromBuffer(&%s,sizeof(%s)) ;\n",r->name,r->name) ;
+               bot << str ;
+       }
+       for ( r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( !r->ismsg )
+                       continue ;
+               sprintf(str,"    {   void **temp = (void **)(&%s);\n",r->name) ;
+               bot << str ;
+               sprintf(str,"        *temp = _CK_ReadMsgTable[_CK_index_%s];  }\n",r->name) ;
+               bot << str ;
+       }
+       bot << "}" << endl ;
+
+
+       bot << "extern \"C\" void _CK_13CopyToBuffer(void *,int) ;" << endl ;
+       bot << "extern \"C\" void ReadMsgInit(void *,int) ;" << endl ;
+       bot << "void _CK_" << thismodule->name << "_CopyToBuffer()" << endl ;
+       bot << "{" << endl ;
+       for ( r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( r->ismsg )
+                       continue ;
+               sprintf(str,"\t_CK_13CopyToBuffer(&%s,sizeof(%s)) ;\n",r->name,r->name) ;
+               bot << str ;
+       }
+       for ( r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( !r->ismsg )
+                       continue ;
+               sprintf(str,"\tReadMsgInit(%s,_CK_index_%s) ;\n",r->name,r->name) ;
+               bot << str ;
+       }
+       bot << "}\n\n\n" ;
+}
+
+
+
+
+
+
+void GenerateRegisterCalls(ofstream& top, ofstream& bot)
+{
+       char str[2048] ;
+
+/* generate the beginning of this Module's Init function */
+       sprintf(str,"extern \"C\" void _CK_%s_init() ;\n",thismodule->name) ;
+       bot << str ;
+       sprintf(str,"void _CK_%s_init()\n{\n",thismodule->name) ;
+       bot << str ;
+
+/* first register all messages */
+        for ( Message *m=thismodule->messages; m!=NULL; m=m->next ) {
+               sprintf(str,"_CK_msg_%s = registerMsg(\"%s\", (FUNCTION_PTR)&GenericCkAlloc, ", m->name, m->name) ;
+               bot << str ;
+
+               if ( !m->packable ) 
+                       sprintf(str,"0, 0, ") ; 
+               else 
+                       sprintf(str,"(FUNCTION_PTR)&_CK_pack_%s, (FUNCTION_PTR)&_CK_unpack_%s, ", m->name, m->name) ;
+               bot << str ;
+
+               sprintf(str,"sizeof(%s)) ;\n\n",m->name) ;
+               bot << str ;
+       }
+       sprintf(str,"\n\n") ;
+
+
+/* now register all chares and BOCs and their EPs */
+       for ( Chare *chare=thismodule->chares; chare!=NULL; chare=chare->next )
+       {
+               sprintf(str,"_CK_chare_%s = registerChare(\"%s\", sizeof(%s), 0) ;\n\n",chare->name,chare->name,chare->name) ;
+               bot << str ;
+
+                for  ( Entry *ep=chare->entries; ep!=NULL; ep=ep->next ) {
+
+                       if ( chare->chareboc == CHARE ) 
+                               sprintf(str,"_CK_ep_%s_%s = registerEp(\"%s\", (FUNCTION_PTR)&_CK_call_%s_%s, 1,", chare->name, ep->name, ep->name, chare->name,ep->name) ;
+                       else
+                               sprintf(str,"_CK_ep_%s_%s = registerBocEp(\"%s\", (FUNCTION_PTR)&_CK_call_%s_%s, 1,",chare->name, ep->name, ep->name, chare->name,ep->name) ;
+
+                       bot << str ;
+
+                       if ( strcmp(chare->name,"main")==0 && 
+                                               strcmp(ep->name,"main")==0 ) 
+                               sprintf(str,"0, _CK_chare_%s) ;\n\n",chare->name) ;
+                       else
+                               sprintf(str,"_CK_msg_%s, _CK_chare_%s) ;\n\n",ep->msgtype->name, chare->name) ;
+                       bot << str ;
+
+//                     if (ep->isThreaded()){
+//                             sprintf(str, "setThreadedEp(_CK_ep_%s_%s);\n\n", chare->name, ep->name) ;
+//                             bot << str ;
+//}
+
+               } // end for ep =
+       } // end for chare =
+       bot << "\n\n" ;
+
+
+if (moduleHasMain)
+{
+/* register the main chare */
+       sprintf(str,"registerMainChare(_CK_chare_main, _CK_ep_main_main, 1) ;\n\n\n") ;
+       bot << str ;
+}
+
+
+/* register distributed-table-variables */
+        for ( Table *t=thismodule->tables; t!=NULL; t=t->next ) {
+                sprintf(str,"%s.SetId(registerTable(\"%s\", 0, 0)) ;\n",t->name,t->name) ;
+               bot << str ;
+        }
+       bot << "\n\n" ;
+
+
+/* now register readonlies and readonli messages */
+        sprintf(str,"int readonlysize=0 ;\n") ;
+       bot << str ;
+       for ( ReadOnly *r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( r->ismsg )
+                       continue ;
+                sprintf(str,"readonlysize += sizeof(%s) ;\n",r->name) ;
+               bot << str ;
+       }
+
+        sprintf(str,"registerReadOnly(readonlysize, (FUNCTION_PTR)&_CK_%s_CopyFromBuffer, (FUNCTION_PTR)&_CK_%s_CopyToBuffer) ;\n",thismodule->name,thismodule->name) ;
+       bot << str ;
+
+
+       /* this is only for giving a unique index to all all readonly msgs */
+       for ( r=thismodule->readonlys; r!=NULL; r=r->next ) {
+               if ( !r->ismsg )
+                       continue ;
+                sprintf(str,"_CK_index_%s = registerReadOnlyMsg() ;\n",r->name) ;
+               bot << str ;
+       }
+
+
+/* This is the closing brace of the Module-init function */
+        bot << "\n}\n" ;
+}
+
+
+
diff --git a/src/xlat-i/xi-main.C b/src/xlat-i/xi-main.C
new file mode 100644 (file)
index 0000000..b853996
--- /dev/null
@@ -0,0 +1,19 @@
+
+#include "xi-symbol.h"
+
+extern void Generate(char *interfacefile) ;
+
+main(int argc, char *argv[])
+{
+       if ( argc != 2 ) {
+               cout << "Usage : " << argv[0] << " module.ci" << endl ;
+               exit(1) ;
+       }
+
+       Module *m = Parse(argv[1]) ;
+
+       Generate(argv[1]) ;
+
+       return 0 ;
+}
+
diff --git a/src/xlat-i/xi-symbol.C b/src/xlat-i/xi-symbol.C
new file mode 100644 (file)
index 0000000..701eca8
--- /dev/null
@@ -0,0 +1,129 @@
+//  DEFINITIONS OF FUNCTIONS IN symbol.h ---------------------------------------
+
+#include "xi-symbol.h"
+
+extern FILE *yyin;
+extern void yyrestart ( FILE *input_file );
+extern int yyparse (void);
+
+// Global Variable - used in parse.y
+Module *thismodule;    // current module
+
+//------------------------------------------------------------------------------
+
+Table::Table(char *n)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+       next = NULL ;
+}
+
+ReadOnly::ReadOnly(char *n, char *t, int i)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+       type = new char [strlen(t)+1] ;
+       strcpy(type,t) ;
+       ismsg = i ;
+       next = NULL ;
+}
+
+Message::Message(char *n, int p)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+       packable = p ;
+       next = NULL ;
+}
+
+Entry::Entry(char *n, char *m, int t, char *r)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+
+       msgtype = thismodule->FindMsg(m) ;
+       next = NULL ;
+
+       isthreaded = t;
+       returnMsg = thismodule->FindMsg(r) ;
+}
+
+
+Chare::Chare(char *n, int cb)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+       entries = NULL ;
+       chareboc = cb ;
+       next = NULL ;
+}
+void Chare::AddEntry(char *e, char *m, int t, char *r)
+{
+       Entry *newe = new Entry(e, m, t, r) ;
+       newe->next = entries ;
+       entries = newe ;
+}
+
+Module::Module(char *n)
+{
+       name = new char [strlen(n)+1] ;
+       strcpy(name,n) ;
+
+       chares = NULL ;
+       messages = NULL ;
+       readonlys = NULL ;
+       tables = NULL ;
+}
+void Module::AddChare(Chare *c)
+{
+       c->next = chares ;
+       chares = c ;
+}
+void Module::AddMessage(Message *m)
+{
+       m->next = messages ;
+       messages = m ;
+}
+void Module::AddReadOnly(ReadOnly *r)
+{
+       r->next = readonlys ;
+       readonlys = r ;
+}
+void Module::AddTable(Table *t)
+{
+       t->next = tables ;
+       tables = t ;
+}
+Message *Module::FindMsg(char *msg)
+{
+       if (msg != NULL)
+       for ( Message *m=messages; m!=NULL; m=m->next ) 
+               if ( strcmp(m->name,msg) == 0 )
+                       return m ; 
+       return NULL ;
+}
+
+
+Module *Parse(char *interfacefile)
+{
+       char *modulename = new char[strlen(interfacefile)+1] ;
+       strcpy(modulename, interfacefile) ;
+       modulename[strlen(interfacefile)-3] = '\0' ;
+
+       thismodule = new Module(modulename) ;
+       delete modulename;
+
+       FILE * fp = fopen (interfacefile, "r") ;
+       if (fp) {
+               yyin = fp ;
+               yyparse() ;
+               fclose(fp) ;
+       } else {
+               cout << "ERROR : could not open " << interfacefile << endl ;
+               delete thismodule;
+               thismodule = NULL;
+       }
+
+       return thismodule;
+}
+
diff --git a/src/xlat-i/xi-symbol.h b/src/xlat-i/xi-symbol.h
new file mode 100644 (file)
index 0000000..42edf43
--- /dev/null
@@ -0,0 +1,88 @@
+
+#ifndef SYMB_H
+#define SYMB_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <iostream.h>
+#include <fstream.h>
+
+/* A file contains one module.
+   A module contains a list of chares (normal or BOC), messages, 
+   readonly variables and tables.
+   A chare has a list of entry methods which have an associated msg type.
+   A readonly variable has a type and a name.
+   A table has only a name.
+*/
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+
+class Table { 
+public: char *name;
+        Table *next;
+
+       Table(char *n);
+} ;
+
+class ReadOnly { 
+public: char *name;
+        char *type;
+        int ismsg;      // 1 is this is a readonly-msg, 0 if a readonly-var
+        ReadOnly *next;
+
+       ReadOnly(char *n, char *t, int i);
+} ;
+class Message {
+public: char *name;
+        int packable;   // 1 is this msg type has pack/unpack functions
+        Message *next;
+
+       Message(char *n, int p);
+} ;
+
+class Entry {
+public: char *name;
+       int isthreaded;
+       Message *returnMsg;
+        Message *msgtype;
+        Entry *next;
+
+       Entry(char *n, char *m, int t = FALSE, char *r = NULL) ;
+       int isThreaded() { return isthreaded ; }
+       int isReturnMsg() { return returnMsg != NULL; }
+} ;
+
+class Chare {
+public: char *name;
+        Entry *entries;
+       int chareboc ;
+        Chare *next;
+
+       Chare(char *n, int cb) ;
+       void AddEntry(char *e, char *m, int t = FALSE, char *r = NULL) ;
+} ;
+
+class Module {
+       friend Module *Parse(char *interfacefile);
+
+public: char *name;
+        Chare *chares;
+        Message *messages;
+        ReadOnly *readonlys;
+        Table *tables;
+       Module *next;
+
+       Module(char *n) ;
+       void AddChare(Chare *c) ;
+       void AddMessage(Message *m) ;
+       void AddReadOnly(ReadOnly *r) ;
+       void AddTable(Table *t) ;
+       Message *FindMsg(char *msg) ;
+} ;
+
+#endif
+