xb2c.c 29.6 KB
Newer Older
kollo's avatar
kollo committed
1
/* xb2c.C   The X11-basic to C translator   (c) Markus Hoffmann 2010-2017
kollo's avatar
kollo committed
2 3 4 5 6 7 8 9 10 11 12
*/

/* This file is part of X11BASIC, the basic interpreter for Unix/X
 * ======================================================================
 * X11BASIC is free software and comes with NO WARRANTY - read the file
 * COPYING for details
 */


#include <stdio.h>
#include <stdlib.h>
13
#include <stdint.h>
kollo's avatar
kollo committed
14
#include <errno.h>
kollo's avatar
kollo committed
15
#include <ctype.h>
kollo's avatar
kollo committed
16
#include <string.h>
kollo's avatar
kollo committed
17
#if defined WINDOWS || defined ANDROID
kollo's avatar
kollo committed
18 19 20 21 22 23
#define EX_CANTCREAT	73	/* can't create (user) output file */
#define EX_NOINPUT	66	/* cannot open input */
#define EX_OK 0
#else
#include <sysexits.h>
#endif
kollo's avatar
kollo committed
24

kollo's avatar
kollo committed
25
#include "defs.h"
kollo's avatar
kollo committed
26
#include "x11basic.h"
kollo's avatar
kollo committed
27
#include "xbasic.h"
kollo's avatar
kollo committed
28
#include "bytecode.h"
kollo's avatar
kollo committed
29
#include "variablen.h"
kollo's avatar
kollo committed
30
#include "file.h"
kollo's avatar
kollo committed
31
#include "virtual-machine.h"
kollo's avatar
kollo committed
32 33 34
//#include "xb2c.h"


kollo's avatar
kollo committed
35 36
char ifilename[128]="b.b";       /* Standard inputfile  */
char ofilename[128]="11.c";       /* Standard outputfile     */
kollo's avatar
kollo committed
37
int loadfile=FALSE;
kollo's avatar
kollo committed
38 39 40
#ifdef ATARI
int verbose=1;
#else
kollo's avatar
kollo committed
41
int verbose=0;
kollo's avatar
kollo committed
42
#endif
kollo's avatar
kollo committed
43
int nomain=0;
kollo's avatar
kollo committed
44

kollo's avatar
kollo committed
45
BYTECODE_HEADER *bytecode;
kollo's avatar
kollo committed
46

kollo's avatar
kollo committed
47 48 49
BYTECODE_SYMBOL *symtab;
char *strings;
unsigned char *datasec;
kollo's avatar
kollo committed
50 51
char *rodata;

kollo's avatar
kollo committed
52 53 54 55 56

FILE *optr;

/* X11-Basic needs these declar<ations:  */
int prglen=0;
57
const char version[]="1.25";        /* Programmversion*/
kollo's avatar
kollo committed
58
const char vdate[]="2017-01-01";
kollo's avatar
kollo committed
59
char *programbuffer=NULL;
kollo's avatar
kollo committed
60
char **program=NULL;
kollo's avatar
kollo committed
61
int programbufferlen=0;
kollo's avatar
kollo committed
62 63 64

extern void memdump(unsigned char *,int);

kollo's avatar
kollo committed
65 66 67 68 69 70 71 72 73 74
#ifdef DUMMY_LIST
/*Kurzform der Error-Routine, tritt hier nur bei IO-Errors auf.*/
void xberror(char errnr, const char *bem) {
  printf("ERROR: #%d %s\n",errnr,bem);
  perror(strerror(errno));
}
#else 
void xberror(char errnr, const char *bem);
#endif

kollo's avatar
kollo committed
75 76 77
static void intro(){
  printf("********************************************************\n"
         "*     X11-Basic bytecode to C translator               *\n"
kollo's avatar
kollo committed
78
         "*                  by Markus Hoffmann 1997-2017 (c)    *\n"
kollo's avatar
kollo committed
79 80
         "* V.%s/%04x    date: %30s  *\n"
         "********************************************************\n",version,BC_VERSION,vdate);
kollo's avatar
kollo committed
81
}
kollo's avatar
kollo committed
82
static void usage(){
kollo's avatar
kollo committed
83
  printf("\n Usage:\n ------ \n"
kollo's avatar
kollo committed
84 85 86 87 88 89
         " %s [-o <outputfile> -h] [<filename>] --- translate program [%s]\n\n"
         "-o <outputfile>\t--- put result in file [%s]\n"
         "-h --help\t--- Usage\n"
         "-l       \t--- suppress the main function\n"
         "-v\t\t--- be more verbose\n"
	 ,"xb2c",ifilename,ofilename);
kollo's avatar
kollo committed
90 91
}

kollo's avatar
kollo committed
92
static void kommandozeile(int anzahl, char *argumente[]) {
kollo's avatar
kollo committed
93 94 95 96 97 98
  int count;

  /* Kommandozeile bearbeiten   */
  for(count=1;count<anzahl;count++) {
    if (strcmp(argumente[count],"-o")==FALSE) {
      strcpy(ofilename,argumente[++count]);
kollo's avatar
kollo committed
99
    } else if (strcmp(argumente[count],"-h")==FALSE || strcmp(argumente[count],"--help")==FALSE) {
kollo's avatar
kollo committed
100
      intro();usage();
kollo's avatar
kollo committed
101 102 103 104
    } else if (strcmp(argumente[count],"-v")==FALSE) verbose++;
    else if(strcmp(argumente[count],"-q")==FALSE)   verbose--;
    else if(strcmp(argumente[count],"-l")==FALSE)   nomain=1;
    else {
kollo's avatar
kollo committed
105 106 107 108 109
      if(!loadfile) {
        loadfile=TRUE;
        strcpy(ifilename,argumente[count]);
      }
    }
kollo's avatar
kollo committed
110
  }
kollo's avatar
kollo committed
111 112
}

kollo's avatar
kollo committed
113
static int havesymbol(int adr,int typ) {
kollo's avatar
kollo committed
114 115 116 117 118 119
  int i,c=bytecode->symbolseglen/sizeof(BYTECODE_SYMBOL);
  for(i=0;i<c;i++) {
    if(symtab[i].adr==adr && symtab[i].typ==typ) return(i);
  }
  return(-1);
}
120
static int frishmemcpy(unsigned char *d,unsigned char *s,unsigned int n) {
kollo's avatar
kollo committed
121 122 123 124 125 126
  int i,j=0;
  for(i=0;i<n;i++) {
    if(s[i]=='\"' || s[i]=='\\') {
      d[j++]='\\';
      d[j++]=s[i];  
    } else if(!isprint(s[i])) {
127 128 129
      sprintf((char *)&d[j],"\\x%02x\" \"",s[i]);
     /* printf("sonderzeichen %d %02x <%s>\n",s[i],s[i],(char *)&d[j]); */
      j+=7;
kollo's avatar
kollo committed
130 131 132 133
    } else d[j++]=s[i];
  }
  return(j);  
}
kollo's avatar
kollo committed
134
static void data_section() {
kollo's avatar
kollo committed
135 136
  int i=0,c;
  int count=0;
kollo's avatar
kollo committed
137 138 139 140
  if(verbose) {
    printf("Make data section ...");
    fflush(stdout);
  }
kollo's avatar
kollo committed
141
  fprintf(optr,"/* Data section compiled by xb2c. */\n\n");
kollo's avatar
kollo committed
142 143
  if(bytecode->sdataseglen) {
    fprintf(optr,"static const char datasec[%d]=\n",(int)bytecode->sdataseglen);
kollo's avatar
kollo committed
144
    fprintf(optr,"\"");
kollo's avatar
kollo committed
145
    while(i<bytecode->sdataseglen) {
kollo's avatar
kollo committed
146 147 148 149 150 151 152 153 154 155 156
      if(havesymbol(i,STT_DATAPTR)>=0) {
        fprintf(optr,"\"\n    \"");
	count=0;
      }
      if(datasec[i]=='\"') {
        fprintf(optr,"\\\"");
	count+=2;
       
      } else if(isprint(datasec[i])) {
        fprintf(optr,"%c",datasec[i]);
	count++;
kollo's avatar
kollo committed
157
      } else {
kollo's avatar
kollo committed
158 159
        fprintf(optr,"\\x%02x",datasec[i]);
        count+=4;
kollo's avatar
kollo committed
160
      }
kollo's avatar
kollo committed
161 162 163
      if(count>64) {
        fprintf(optr,"\"\n    \"");
	count=0;
kollo's avatar
kollo committed
164
      }
kollo's avatar
kollo committed
165
      i++;
kollo's avatar
kollo committed
166
    }
kollo's avatar
kollo committed
167 168 169 170 171 172 173
    fprintf(optr,"\";\n\n");
    c=bytecode->symbolseglen/sizeof(BYTECODE_SYMBOL);
    for(i=0;i<c;i++) {
      if(symtab[i].typ==STT_DATAPTR) {
        if(symtab[i].name)
           fprintf(optr,"#define DTA_%s 0x%x\n",&strings[symtab[i].name],(unsigned int)symtab[i].adr);
        else  fprintf(optr,"#define DTA_%x 0x%x\n",(unsigned int)symtab[i].adr,(unsigned int)symtab[i].adr);
kollo's avatar
kollo committed
174
      }
kollo's avatar
kollo committed
175 176 177
    }

  }
kollo's avatar
kollo committed
178
  if(verbose) printf(" (done.)\n");
kollo's avatar
kollo committed
179 180
}

kollo's avatar
kollo committed
181
static void translate() {
182
  unsigned char *buf;
kollo's avatar
kollo committed
183
  
kollo's avatar
kollo committed
184
  signed char c;
kollo's avatar
kollo committed
185
  int i,n,b,redo;
kollo's avatar
kollo committed
186
  short ss,ss2;
kollo's avatar
kollo committed
187 188 189
  double d;
  char *bcpc=(char *)bytecode+sizeof(BYTECODE_HEADER);
  unsigned char cmd;
kollo's avatar
kollo committed
190 191
  int pcount=0;

kollo's avatar
kollo committed
192 193
  int a=bytecode->symbolseglen/sizeof(BYTECODE_SYMBOL);
  int vidx[a];
kollo's avatar
kollo committed
194 195 196 197
  if(verbose) {
    printf("Translate ... ");
    fflush(stdout);
  }
kollo's avatar
kollo committed
198
  
kollo's avatar
kollo committed
199
  if(a>0) { 
kollo's avatar
kollo committed
200
    if(verbose) {printf("protos ... ");fflush(stdout);}
kollo's avatar
kollo committed
201
    fprintf(optr,"\n/* Function prototypes */\n");
kollo's avatar
kollo committed
202 203 204
    for(i=0;i<a;i++) {
      if(symtab[i].typ==STT_FUNC) {
        if(symtab[i].name)
kollo's avatar
kollo committed
205 206
           fprintf(optr,"void proc_%s(); \t/* $%x */\n",&strings[symtab[i].name],(unsigned int)symtab[i].adr);
        else  fprintf(optr,"void FUNC_%x(); \t/* 0x%x */\n",(unsigned int)symtab[i].adr,(unsigned int)symtab[i].adr);
kollo's avatar
kollo committed
207 208
      }
    }
kollo's avatar
kollo committed
209
  }
kollo's avatar
kollo committed
210 211 212
  
  /*Jetzt Variablen:*/
  if(a>0) { 
kollo's avatar
kollo committed
213
    if(verbose) {printf("variables ... ");fflush(stdout);}
kollo's avatar
kollo committed
214 215 216 217 218 219 220 221 222 223 224 225
    int count=0;
    fprintf(optr,"\n/* Variables */\n");
    for(i=0;i<a;i++) {
      if(symtab[i].typ==STT_OBJECT) {
        int typ,subtyp;
	typ=symtab[i].subtyp;
	if(typ&ARRAYTYP) {
  	  subtyp=typ&(~ARRAYTYP);
	  typ=ARRAYTYP;
        } else subtyp=0;
	if(typ==INTTYP)         fprintf(optr,"int    VARi_");
	else if(typ==FLOATTYP)  fprintf(optr,"double VARf_");
kollo's avatar
kollo committed
226 227
	else if(typ==ARBINTTYP) fprintf(optr,"ARBINT VARai_");
	else if(typ==COMPLEXTYP)fprintf(optr,"COMPLEX VARc_");
kollo's avatar
kollo committed
228 229 230 231
	else if(typ==STRINGTYP) fprintf(optr,"STRING VARs_");
	else if(typ==ARRAYTYP)  fprintf(optr,"ARRAY  VARa_");
        vidx[count]=i;
        if(symtab[i].name)
kollo's avatar
kollo committed
232 233
              fprintf(optr,"%s; \t/* typ=0x%x, subtyp=0x%x; vnr=%d sym=%d $%04x: */\n",&strings[symtab[i].name],typ,subtyp,count,i,(unsigned int)symtab[i].adr);
        else  fprintf(optr,"_%d; \t/* typ=0x%x, subtyp=0x%x;        sym=%d $%04x */\n",count,typ,subtyp,vidx[count],(unsigned int)symtab[i].adr);
kollo's avatar
kollo committed
234 235 236 237 238
        count++;
      }
      fflush(optr);
    }
  }
kollo's avatar
kollo committed
239
  if(verbose) {printf("init ... ");fflush(stdout);}
kollo's avatar
kollo committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  
  if(nomain) {
    char nn[256];
    int i=0;
    int j=0;
    while(ifilename[i]) {
      nn[j++]=ifilename[i];
      if(ifilename[i]=='/' || ifilename[i]=='\\') j=0;
      if(ifilename[i]=='.') {j--;break;}
      i++;
    }
    nn[j]=0;
    
    fprintf(optr,"\nvoid lib_%s_init() {\n",nn);
  } else {
    fprintf(optr,"\nmain(int anzahl, char *argumente[]) {\n"
kollo's avatar
kollo committed
256 257 258
               "  MAIN_INIT;\n"
               "  databufferlen=%d;\n",(int)bytecode->dataseglen);
    if(bytecode->dataseglen) fprintf(optr,"  databuffer=datasec;\n");
kollo's avatar
kollo committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  }
  
  if(a>0) { 
    int count=0;
    for(i=0;i<a;i++) {
      if(symtab[i].typ==STT_OBJECT) {
        int typ,subtyp;
	char vnam[256];
	typ=symtab[i].subtyp;
	if(typ&ARRAYTYP) {
  	  subtyp=typ&(~ARRAYTYP);
	  typ=ARRAYTYP;
        } else subtyp=0;
        vidx[count]=i;
        
	if(typ==INTTYP)         sprintf(vnam,"VARi_");
	else if(typ==FLOATTYP)  sprintf(vnam,"VARf_");
kollo's avatar
kollo committed
276 277
	else if(typ==COMPLEXTYP)sprintf(vnam,"VARc_");
	else if(typ==ARBINTTYP) sprintf(vnam,"VARai_");
kollo's avatar
kollo committed
278 279 280
	else if(typ==STRINGTYP) sprintf(vnam,"VARs_");
	else if(typ==ARRAYTYP)  sprintf(vnam,"VARa_");
	
kollo's avatar
kollo committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294
#if 0	
/*Irgendwas ist hier noch faul....*/
          if(symtab[i].name)
                fprintf(optr,"  add_variable(\"%s\",0x%x,0,V_STATIC,(char *)&%s%s); \t/*%d $%02x: */\n",&strings[symtab[i].name],typ,vnam,&strings[symtab[i].name],count,(unsigned int)symtab[i].adr);
          else  fprintf(optr,"  add_variable(\"VAR_%d\",0x%x,0,V_STATIC,(char *)&%s_%d); \t/*%d $%04x */\n",count,typ,vnam,count,vidx[count],(unsigned int)symtab[i].adr);
 if(typ==STRINGTYP) {
          if(symtab[i].name) fprintf(optr,"  %s%s=create_string(NULL);\n",vnam,&strings[symtab[i].name]);
	  else               fprintf(optr,"  %s_%d=create_string(NULL);\n",vnam,count);
 } else if(typ==ARRAYTYP) {
          if(symtab[i].name) fprintf(optr,"  %s%s=create_array(0x%x,0,NULL);\n",vnam,&strings[symtab[i].name],subtyp);
	  else               fprintf(optr,"  %s_%d=create_array(0x%x,0,NULL);\n",vnam,count,subtyp);
 
 }
 #else
kollo's avatar
kollo committed
295
 	if(typ==INTTYP || typ==FLOATTYP|| typ==COMPLEXTYP) {
kollo's avatar
kollo committed
296
          if(symtab[i].name)
kollo's avatar
kollo committed
297 298
                fprintf(optr,"  add_variable(\"%s\",0x%x,0,V_STATIC,(char *)&%s%s); \t/*%d $%02x: */\n",&strings[symtab[i].name],typ,vnam,&strings[symtab[i].name],count,(unsigned int)symtab[i].adr);
          else  fprintf(optr,"  add_variable(\"VAR_%d\",0x%x,0,V_STATIC,(char *)&%s_%d); \t/*%d $%04x */\n",count,typ,vnam,count,vidx[count],(unsigned int)symtab[i].adr);
kollo's avatar
kollo committed
299 300
	} else {
          if(symtab[i].name)
kollo's avatar
kollo committed
301 302 303 304
                fprintf(optr,"  add_variable(\"%s\",0x%x,0x%x,V_DYNAMIC,NULL); \t/*%d $%02x: */\n",&strings[symtab[i].name],typ,subtyp,count,(unsigned int)symtab[i].adr);
          else  fprintf(optr,"  add_variable(\"VAR_%d\",0x%x,0x%x,V_DYNAMIC,NULL); \t/*%d $%04x */\n",count,typ,subtyp,vidx[count],(unsigned int)symtab[i].adr);
	} 
 #endif
kollo's avatar
kollo committed
305 306 307 308 309
        count++;
      }
      fflush(optr);
    }
  }
kollo's avatar
kollo committed
310
  if(verbose) {printf("TEXT ... ");fflush(stdout);}
kollo's avatar
kollo committed
311 312
  i=0;
  while((cmd=bcpc[i]) && i<bytecode->textseglen) {
kollo's avatar
kollo committed
313 314 315
    fflush(optr);
    
    redo=0;
316
    fprintf(optr,"/* %02x / %02x */",i,i+(unsigned int)sizeof(BYTECODE_HEADER));
kollo's avatar
kollo committed
317 318 319 320 321
    if((b=havesymbol(i,STT_LABEL))>=0) 
      fprintf(optr,"%s:  ",&strings[symtab[b].name]);
    else if((b=havesymbol(i,0))>=0) {
      if(symtab[b].name)
        fprintf(optr,"%s:  ",&strings[symtab[b].name]);
kollo's avatar
kollo committed
322
      else fprintf(optr,"LBL_%x:  ",(unsigned int)symtab[b].adr);
kollo's avatar
kollo committed
323
    } else fprintf(optr,"    ");
kollo's avatar
kollo committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
       
    
    if((b=havesymbol(i,STT_FUNC))>=0) {
      redo=1;
      if(pcount==0)
        fprintf(optr,";\n}\nvoid proc_%s() {\n",&strings[symtab[b].name]);
      else
        fprintf(optr,"exit(-1);\n}\nvoid proc_%s() {\n",&strings[symtab[b].name]);
      pcount++;
    } 
    if(redo && (b=havesymbol(i,STT_LABEL))>=0) {
      fprintf(optr,"/* %02x */",i);
      fprintf(optr,"%s:  ",&strings[symtab[b].name]);
    } else if(redo && (b=havesymbol(i,0))>=0) {
      fprintf(optr,"/* %02x */",i);
      if(symtab[b].name)
        fprintf(optr,"%s:  ",&strings[symtab[b].name]);
      else fprintf(optr,"LBL_%x:  ",(unsigned int)symtab[b].adr);
    } else if(redo) fprintf(optr,"    ");
kollo's avatar
kollo committed
343 344 345 346 347 348
    i++;
     switch(cmd) {
    case BC_NOOP:
      fprintf(optr,"NOOP;\n");
      break;
    case BC_JSR:
kollo's avatar
kollo committed
349
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
350 351
      a-=sizeof(BYTECODE_HEADER);
      if((b=havesymbol(a,STT_FUNC))>=0) {
kollo's avatar
kollo committed
352
        fprintf(optr,"proc_%s();",&strings[symtab[b].name]);
kollo's avatar
kollo committed
353 354 355 356
        fprintf(optr,"\t/* JSR(0x%x);*/\n",a);
      } else fprintf(optr,"JSR(0x%x);\n",a);
      break;
    case BC_JMP:
kollo's avatar
kollo committed
357
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
358 359 360 361 362 363 364 365
      a-=sizeof(BYTECODE_HEADER);
      if((b=havesymbol(a,STT_LABEL))>=0) 
        fprintf(optr,"goto %s;\t/* JMP(0x%x); */\n",&strings[symtab[b].name],a);
      else if((b=havesymbol(a,0))>=0) 
        fprintf(optr,"goto LBL_%x;\t/* JMP(0x%x); */\n",a,a);
      else fprintf(optr,"JMP(0x%x);\n",a);
      break;
    case BC_JEQ:
kollo's avatar
kollo committed
366
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
367 368
      a-=sizeof(BYTECODE_HEADER);
      if((b=havesymbol(a,STT_LABEL))>=0) 
kollo's avatar
kollo committed
369
        fprintf(optr,"JUMPIFZERO %s\t/* JEQ(0x%x); */\n",&strings[symtab[b].name],a);
kollo's avatar
kollo committed
370
      else if((b=havesymbol(a,0))>=0) 
kollo's avatar
kollo committed
371
        fprintf(optr,"JUMPIFZERO LBL_%x;\t/* JEQ(0x%x); */\n",a,a);
kollo's avatar
kollo committed
372 373 374
      else fprintf(optr,"JEQ(0x%x);\n",a);
      break;
    case BC_BRA:
kollo's avatar
kollo committed
375
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
376
     if((b=havesymbol(i+ss,STT_LABEL))>=0) 
kollo's avatar
kollo committed
377 378 379 380 381 382
        fprintf(optr,"goto %s;\t/*BRA(%d); $%x */\n",&strings[symtab[b].name],ss,i+ss);
      else if((b=havesymbol(i+ss,0))>=0) 
        fprintf(optr,"goto LBL_%x;\t/* BRA(%d); */\n",i+ss,ss);
      else fprintf(optr,"BRA(%d);\t/* $%x */\n",ss,i+ss); 
      break;
    case BC_BEQ:
kollo's avatar
kollo committed
383
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
384 385
      if((b=havesymbol(i+ss,STT_LABEL))>=0) 
        fprintf(optr,"JUMPIFZERO %s;\t/*BEQ(%d); $%x */\n",&strings[symtab[b].name],ss,i+ss);
kollo's avatar
kollo committed
386
      else if((b=havesymbol(i+ss,0))>=0) 
kollo's avatar
kollo committed
387
        fprintf(optr,"JUMPIFZERO LBL_%x;\t/* BEQ(%d); */\n",i+ss,ss);
kollo's avatar
kollo committed
388 389 390
      else fprintf(optr,"BEQ(%d);\t/* $%x */\n",ss,i+ss); 
      break;
    case BC_BSR:
kollo's avatar
kollo committed
391
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
392
      if((b=havesymbol(i+ss,STT_FUNC))>=0) {
kollo's avatar
kollo committed
393
        fprintf(optr,"proc_%s();",&strings[symtab[b].name]);
kollo's avatar
kollo committed
394 395 396
        fprintf(optr,"\t/* BSR(%d); $%x */\n",ss,i+ss);
      } else fprintf(optr,"BSR(%d);\n",ss);
      break;
kollo's avatar
kollo committed
397 398 399 400 401 402
    case BC_BLKEND:
      fprintf(optr,"restore_locals(sp--);\n");
      break;
    case BC_BLKSTART:
      fprintf(optr,"sp++;\n");
      break;
kollo's avatar
kollo committed
403 404 405 406 407
    case BC_RTS:
      fprintf(optr,"return;\n");
      break;
    case BC_BRAs:
      c=bcpc[i++];
kollo's avatar
kollo committed
408
      if((b=havesymbol(i+c,STT_LABEL))>=0) 
kollo's avatar
kollo committed
409 410 411 412 413 414 415
        fprintf(optr,"goto %s;\t/*BRA_s(%d); $%x */\n",&strings[symtab[b].name],c,i+c);
      else if((b=havesymbol(i+c,0))>=0) 
        fprintf(optr,"goto LBL_%x;\t/* BRA_s(%d); */\n",i+c,c);
      else fprintf(optr,"BRA_s(%d);\t/* $%x */\n",c,i+c); 
      break;
    case BC_BEQs:
      c=bcpc[i++];
kollo's avatar
kollo committed
416 417
      if((b=havesymbol(i+c,STT_LABEL))>=0) 
        fprintf(optr,"JUMPIFZERO %s;\t/*BEQ_s(%d); $%x */\n",&strings[symtab[b].name],c,i+c);
kollo's avatar
kollo committed
418
      else if((b=havesymbol(i+c,0))>=0) 
kollo's avatar
kollo committed
419
        fprintf(optr,"JUMPIFZERO LBL_%x;\t/* BEQ_s(%d); */\n",i+c,c);
kollo's avatar
kollo committed
420 421
      else fprintf(optr,"BEQ_s(%d);\t/* $%x */\n",c,i+c); 
      break;
kollo's avatar
kollo committed
422
    case BC_LOADi:
kollo's avatar
kollo committed
423
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
424 425 426
      fprintf(optr,"LOADi(0x%x);\n",a);
      break;
    case BC_LOADf:
kollo's avatar
kollo committed
427
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
428 429 430
      fprintf(optr,"LOADf(0x%x);\n",a);
      break;
    case BC_LOADs:
kollo's avatar
kollo committed
431
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
432 433 434
      fprintf(optr,"LOADs(0x%x);\n",a);
      break;
    case BC_LOADa:
kollo's avatar
kollo committed
435
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
436 437 438
      fprintf(optr,"LOADa(0x%x);\n",a);
      break;
    case BC_SAVEi:
kollo's avatar
kollo committed
439
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
440 441 442
      fprintf(optr,"SAVEi(0x%x);\n",a);
      break;
    case BC_SAVEf:
kollo's avatar
kollo committed
443
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
444 445 446
      fprintf(optr,"SAVEf(0x%x);\n",a);
      break;
    case BC_SAVEs:
kollo's avatar
kollo committed
447
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
448 449 450
      fprintf(optr,"SAVEs(0x%x);\n",a);
      break;
    case BC_SAVEa:
kollo's avatar
kollo committed
451
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
452 453
      fprintf(optr,"SAVEa(0x%x);\n",a);
      break;
kollo's avatar
kollo committed
454
    case BC_PUSHF:
kollo's avatar
kollo committed
455
      CP8(&d,&bcpc[i],i);
kollo's avatar
kollo committed
456
      fprintf(optr,"PUSHF(%.13g);\n",d);
kollo's avatar
kollo committed
457
      break;
kollo's avatar
kollo committed
458 459 460 461 462 463
    case BC_PUSHC:
      CP8(&d,&bcpc[i],i);
      fprintf(optr,"PUSHC(%.13g,",d);
      CP8(&d,&bcpc[i],i);
      fprintf(optr,"%.13g);\n",d);
      break;
kollo's avatar
kollo committed
464
    case BC_PUSHI:
kollo's avatar
kollo committed
465
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
466 467 468
      fprintf(optr,"PUSHI(%d);\n",a);
      break;
    case BC_PUSHW:
kollo's avatar
kollo committed
469
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
470 471 472 473 474
      fprintf(optr,"PUSHW(%d);\n",ss);
      break;
    case BC_PUSHB:
      fprintf(optr,"PUSHB(%d);\n",bcpc[i++]);
      break;
kollo's avatar
kollo committed
475 476 477 478 479
    case BC_PUSHLEER: fprintf(optr,"PUSHLEER;\n"); break;
    case BC_PUSH0:    fprintf(optr,"PUSH0;\n");    break;
    case BC_PUSH1:    fprintf(optr,"PUSH1;\n");    break;
    case BC_PUSH2:    fprintf(optr,"PUSH2;\n");    break;
    case BC_PUSHM1:   fprintf(optr,"PUSHM1;\n");   break;
kollo's avatar
kollo committed
480 481 482
    case BC_PUSHFUNC:
      a=bcpc[i++]&0xff;
      n=bcpc[i++]&0xff;
kollo's avatar
kollo committed
483 484 485 486 487 488 489 490 491 492 493 494
      if(a==find_func("ABS") || a==find_func("ACOS") || a==find_func("ACOSH") || a==find_func("ASIN") || a==find_func("ASINH") ||
         a==find_func("ATAN") || a==find_func("ATN") || a==find_func("ATANH")) 
        fprintf(optr,"PFUNC_%s; /* %s() */\n",pfuncs[a].name,pfuncs[a].name);
      else if(a==find_func("CBRT") || a==find_func("CEIL") || a==find_func("COS") || a==find_func("COSH") || a==find_func("EXP") ||
         a==find_func("EXPM1") || a==find_func("FLOOR") || a==find_func("HYPOT")) 
        fprintf(optr,"PFUNC_%s; /* %s() */\n",pfuncs[a].name,pfuncs[a].name);
       else if(a==find_func("LN") || a==find_func("LOG") || a==find_func("LOG10") || a==find_func("LOG1P") || a==find_func("LOGB") ||
         a==find_func("RAND") || a==find_func("SIN") || a==find_func("SINH")) 
        fprintf(optr,"PFUNC_%s; /* %s() */\n",pfuncs[a].name,pfuncs[a].name);
       else if(a==find_func("SQR") || a==find_func("SQRT") || a==find_func("TAN") || a==find_func("TANH")) 
        fprintf(optr,"PFUNC_%s; /* %s() */\n",pfuncs[a].name,pfuncs[a].name);
      else fprintf(optr,"PUSHFUNC(%d,%d); /* %s */\n",a,n,pfuncs[a].name);
kollo's avatar
kollo committed
495 496 497 498 499 500
      break;
    case BC_PUSHSFUNC:
      a=bcpc[i++]&0xff;
      n=bcpc[i++]&0xff;
      fprintf(optr,"PUSHSFUNC(%d,%d); /* %s */\n",a,n,psfuncs[a].name);
      break;
501 502 503 504 505
    case BC_PUSHAFUNC:
      a=bcpc[i++]&0xff;
      n=bcpc[i++]&0xff;
      fprintf(optr,"PUSHAFUNC(%d,%d); /* %s */\n",a,n,pafuncs[a].name);
      break;
kollo's avatar
kollo committed
506 507 508
    case BC_PUSHCOMM:
      a=bcpc[i++]&0xff;
      n=bcpc[i++]&0xff;
kollo's avatar
kollo committed
509 510 511 512 513
      if(a==find_comm("VSYNC") || a==find_comm("SHOWPAGE") || a==find_comm("BEEP") || a==find_comm("BELL") || a==find_comm("END") || 
                  a==find_comm("INC") || a==find_comm("DEC") ) {
         fprintf(optr,"COMM_%s; /* %d %d %s */\n",comms[a].name,a,n,comms[a].name);
      }
      else
kollo's avatar
kollo committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
      fprintf(optr,"PUSHCOMM(%d,%d); /* %s */\n",a,n,comms[a].name);
      break;
    case BC_PUSHSYS:
      a=bcpc[i++];
      fprintf(optr,"PUSHSYS(%d); /* %s */\n",a,sysvars[a].name);
      break;
    case BC_PUSHSSYS:
      a=bcpc[i++];
      fprintf(optr,"PUSHSSYS(%d); /* %s */\n",a,syssvars[a].name);
      break;
    case BC_PUSHASYS:
      a=bcpc[i++];
      fprintf(optr,"PUSHASYS(%d); /* %s */\n",a,"??");
      break;
    case BC_PUSHX:
kollo's avatar
kollo committed
529
      n=bcpc[i++];
kollo's avatar
kollo committed
530
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
531
      buf=malloc(n*8+8);
532
      b=frishmemcpy(buf,(unsigned char *)(rodata+a),n);
kollo's avatar
kollo committed
533
      buf[b]=0;
kollo's avatar
kollo committed
534
      fprintf(optr,"PUSHX(\"%s\"); /*len=%d*/\n",buf,n);
kollo's avatar
kollo committed
535 536
      free(buf);
      break;
kollo's avatar
kollo committed
537 538 539 540
    case BC_PUSHK:
      n=bcpc[i++];
      fprintf(optr,"PUSHK(%d);     /* %s */ \n",n,keywords[n]);
      break;
kollo's avatar
kollo committed
541 542
    case BC_PUSHS:
      { int len;
kollo's avatar
kollo committed
543 544
      CP4(&len,&bcpc[i],i);
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
545
      buf=malloc(8*len+8);
546
      b=frishmemcpy(buf,(unsigned char *)(rodata+a),len);
kollo's avatar
kollo committed
547 548 549 550 551
      buf[b]=0;
      fprintf(optr,"PUSHS(\"%s\"); /*len=%d*/\n",buf,len);
      free(buf);
      }
      break;
kollo's avatar
kollo committed
552 553 554 555
    case BC_PUSHAI:
      { int len;
      CP4(&len,&bcpc[i],i);
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
556
      buf=malloc(8*len+8);
557
      b=frishmemcpy(buf,(unsigned char *)(rodata+a),len);
kollo's avatar
kollo committed
558 559 560
      buf[b]=0;
      ARBINT a;
      mpz_init(a);
561
      mpz_set_str(a,(char *)buf,32);
kollo's avatar
kollo committed
562 563 564 565 566 567 568
      char *buf2=mpz_get_str(NULL,10,a);
      mpz_clear(a);
      fprintf(optr,"PUSHAI(\"%s\"); /*len=%d  %s*/\n",buf,len,buf2);
      free(buf);
      free(buf2);
      }
      break;
kollo's avatar
kollo committed
569 570
    case BC_PUSHA:
      { int len;
kollo's avatar
kollo committed
571 572
      CP4(&len,&bcpc[i],i);
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
573
      buf=malloc(8*len+8);
574
      b=frishmemcpy(buf,(unsigned char *)(rodata+a),len);
kollo's avatar
kollo committed
575 576 577 578 579
      buf[b]=0;
      fprintf(optr,"PUSHA(\"%s\",%d); /*len=%d*/\n",buf,len,len);
      free(buf);
      }
      break;
kollo's avatar
kollo committed
580 581 582 583 584
    case BC_COMMENT:
      a=bcpc[i++];
      fprintf(optr,"/* %s (%d)*/\n",&bcpc[i],a);
      i+=a;
      break;
kollo's avatar
kollo committed
585 586 587
    case BC_ADD:  fprintf(optr,"ADD;\n");  break;
    case BC_ADDi: fprintf(optr,"ADDi;\n"); break;
    case BC_ADDf: fprintf(optr,"ADDf;\n"); break;
kollo's avatar
kollo committed
588
    case BC_ADDc: fprintf(optr,"ADDc;\n"); break;
kollo's avatar
kollo committed
589 590
    case BC_ADDs: fprintf(optr,"ADDs;\n"); break;
    case BC_AND:  fprintf(optr,"AND;\n");  break;
kollo's avatar
kollo committed
591
    case BC_ANDi: fprintf(optr,"ANDi;\n");  break;
kollo's avatar
kollo committed
592
    case BC_OR:   fprintf(optr,"OR;\n");   break;
kollo's avatar
kollo committed
593
    case BC_ORi:  fprintf(optr,"ORi;\n");   break;
kollo's avatar
kollo committed
594
    case BC_XOR:  fprintf(optr,"XOR;\n");  break;
kollo's avatar
kollo committed
595
    case BC_XORi: fprintf(optr,"XORi;\n");  break;
kollo's avatar
kollo committed
596 597 598
    case BC_SUB:  fprintf(optr,"SUB;\n");  break;
    case BC_SUBi: fprintf(optr,"SUBi;\n"); break;
    case BC_SUBf: fprintf(optr,"SUBf;\n"); break;
kollo's avatar
kollo committed
599
    case BC_SUBc: fprintf(optr,"SUBc;\n"); break;
kollo's avatar
kollo committed
600 601 602
    case BC_MUL:  fprintf(optr,"MUL;\n");  break;
    case BC_MULi: fprintf(optr,"MULi;\n"); break;
    case BC_MULf: fprintf(optr,"MULf;\n"); break;
kollo's avatar
kollo committed
603
    case BC_MULc: fprintf(optr,"MULc;\n"); break;
kollo's avatar
kollo committed
604
    case BC_DIV:  fprintf(optr,"DIV;\n");  break;
kollo's avatar
kollo committed
605 606
    case BC_DIVf:  fprintf(optr,"DIVf;\n");  break;
    case BC_DIVc:  fprintf(optr,"DIVc;\n");  break;
kollo's avatar
kollo committed
607 608 609 610 611 612 613 614 615 616
    case BC_POW:  fprintf(optr,"POW;\n");  break;
    case BC_EQUAL:fprintf(optr,"EQUAL;\n");break;
    case BC_GREATER:fprintf(optr,"GREATER;\n");break;
    case BC_LESS: fprintf(optr,"LESS;\n"); break;
    case BC_DUP:  fprintf(optr,"DUP;\n");  break;
    case BC_EXCH: fprintf(optr,"EXCH;\n"); break;
    case BC_CLEAR:fprintf(optr,"CLEAR;\n");break;
    case BC_COUNT:fprintf(optr,"COUNT;\n");break;
    case BC_NEG:  fprintf(optr,"NEG;\n");  break;
    case BC_NOT:  fprintf(optr,"NOT;\n");  break;
kollo's avatar
kollo committed
617
    case BC_NOTi:  fprintf(optr,"NOTi;\n");  break;
kollo's avatar
kollo committed
618
    case BC_X2I:  fprintf(optr,"X2I;\n");  break;
kollo's avatar
kollo committed
619
    case BC_X2AI: fprintf(optr,"X2AI;\n");  break;
kollo's avatar
kollo committed
620
    case BC_X2F:  fprintf(optr,"X2F;\n");  break;
kollo's avatar
kollo committed
621 622 623 624
    case BC_I2F:  fprintf(optr,"I2F;\n");  break;
    case BC_I2FILE:fprintf(optr,"I2FILE;\n");  break;
    case BC_F2C:  fprintf(optr,"F2C;\n");  break;
    case BC_X2C:  fprintf(optr,"X2C;\n");  break;
kollo's avatar
kollo committed
625 626 627 628
    case BC_MOD:  fprintf(optr,"MOD;\n");  break;
    case BC_POP:  fprintf(optr,"POP;\n");  break;
    case BC_EVAL: fprintf(optr,"EVAL;\n"); break;
    case BC_LOCAL:
kollo's avatar
kollo committed
629
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
630 631 632 633
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"LOCAL(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"LOCAL(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
kollo's avatar
kollo committed
634
      break;
kollo's avatar
kollo committed
635
    case BC_ZUWEISi:
kollo's avatar
kollo committed
636
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
637 638 639 640 641 642
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"ZUWEIS(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"ZUWEISi(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
      break;
    case BC_ZUWEISf:
kollo's avatar
kollo committed
643
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
644 645 646 647 648
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"ZUWEIS(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"ZUWEISf(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
      break;
kollo's avatar
kollo committed
649 650 651 652 653 654 655
    case BC_ZUWEISc:
      CP2(&ss,&bcpc[i],i);
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"ZUWEIS(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"ZUWEISc(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
      break;
kollo's avatar
kollo committed
656
    case BC_ZUWEIS:
kollo's avatar
kollo committed
657
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
658 659 660 661
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"ZUWEIS(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"ZUWEIS(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
kollo's avatar
kollo committed
662 663
      break;
    case BC_ZUWEISINDEX:
kollo's avatar
kollo committed
664 665
      CP2(&ss,&bcpc[i],i);
      CP2(&ss2,&bcpc[i],i);
kollo's avatar
kollo committed
666
      fprintf(optr,"ZUWEISINDEX(%d,%d); /* %s */\n",ss,ss2,&strings[symtab[vidx[ss]].name]);
kollo's avatar
kollo committed
667 668
      break;
    case BC_PUSHV:
kollo's avatar
kollo committed
669
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
670 671 672 673 674 675
      if(ss==-1) {
        printf("Error, Variable gibts nicht.\n");
	fprintf(optr,"PUSHV(%d); /* ERROR */\n",ss);
      } else fprintf(optr,"PUSHV(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
      break;
    case BC_PUSHVV:
kollo's avatar
kollo committed
676
      CP2(&ss,&bcpc[i],i);
kollo's avatar
kollo committed
677 678 679
      fprintf(optr,"PUSHVV(%d); /* %s */\n",ss,&strings[symtab[vidx[ss]].name]);
      break;
    case BC_PUSHVVI:
kollo's avatar
kollo committed
680 681
      CP2(&ss,&bcpc[i],i);
      CP2(&ss2,&bcpc[i],i);
kollo's avatar
kollo committed
682
      fprintf(optr,"PUSHVVI(%d,%d); /* %s */\n",ss,ss2,&strings[symtab[vidx[ss]].name]);
kollo's avatar
kollo committed
683 684
      break;
    case BC_PUSHARRAYELEM:      
kollo's avatar
kollo committed
685 686
      CP2(&ss,&bcpc[i],i);
      CP2(&ss2,&bcpc[i],i);
kollo's avatar
kollo committed
687
      fprintf(optr,"PUSHARRAYELEM(%d,%d); /* %s */\n",ss,ss2,&strings[symtab[vidx[ss]].name]);
kollo's avatar
kollo committed
688 689
      break;
    case BC_RESTORE:
kollo's avatar
kollo committed
690
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
691 692 693 694
      fprintf(optr,"RESTORE(0x%x);",a);
      if((b=havesymbol(a,STT_DATAPTR))>=0) fprintf(optr,"\t/* %s */\n",&strings[symtab[b].name]);
      else fprintf(optr,"\n");
      break;
kollo's avatar
kollo committed
695
    case BC_PUSHLABEL:
kollo's avatar
kollo committed
696
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
697 698 699 700 701 702 703 704
      a-=sizeof(BYTECODE_HEADER);
      if((b=havesymbol(a,STT_LABEL))>=0) 
        fprintf(optr,"PUSHLABEL(&&%s);\t/* LABEL(0x%x); */\n",&strings[symtab[b].name],a);
      else if((b=havesymbol(a,0))>=0) 
        fprintf(optr,"PUSHLABEL(&&LBL_%x);\t/* LABEL(0x%x); */\n",a,a);
      else fprintf(optr,"PUSHLABEL(0x%x);\n",a);
      break;
    case BC_PUSHPROC: 
kollo's avatar
kollo committed
705
      CP4(&a,&bcpc[i],i);
kollo's avatar
kollo committed
706 707 708 709 710 711 712
      a-=sizeof(BYTECODE_HEADER);
      if((b=havesymbol(a,STT_FUNC))>=0) 
        fprintf(optr,"PUSHPROC(proc_%s);\t/* PROC(0x%x); */\n",&strings[symtab[b].name],a);
      else if((b=havesymbol(a,0))>=0) 
        fprintf(optr,"pushproc LBL_%x;\t/* PROC(0x%x); */\n",a,a);
      else fprintf(optr,"PUSHPROC(0x%x);\n",a);
      break;
kollo's avatar
kollo committed
713
    default:
kollo's avatar
kollo committed
714
      printf("xb2c: BC_ILLEGAL instruction %2x at %d\n",(int)cmd,i);
kollo's avatar
kollo committed
715
      memdump((unsigned char *)&(bcpc[i]),16);
kollo's avatar
kollo committed
716
    }
kollo's avatar
kollo committed
717 718
  }
  fprintf(optr,"}\n");
kollo's avatar
kollo committed
719
  if(verbose)  printf(" (done.)\n");
kollo's avatar
kollo committed
720 721
}

kollo's avatar
kollo committed
722
static int loadbcprg(char *filename) {  
kollo's avatar
kollo committed
723 724 725
  int len,i,c;
  char *p;
  FILE *dptr;
kollo's avatar
kollo committed
726
  dptr=fopen(filename,"rb"); len=lof(dptr); fclose(dptr);
kollo's avatar
kollo committed
727 728 729 730
  p=malloc(len);
  bload(filename,p,len);
  if(p[0]==BC_BRAs && p[1]==sizeof(BYTECODE_HEADER)-2) {
    bytecode=(BYTECODE_HEADER *)p;
731
    fprintf(optr,"/* X11-Basic-Compiler Version 1.25\n"
kollo's avatar
kollo committed
732
                 "   (c) Markus Hoffmann 2002-2017\n"
kollo's avatar
kollo committed
733 734 735
                 "\n"
                 "\nBytecode: %s (%d Bytes)\n\n",filename,len);
		 
kollo's avatar
kollo committed
736 737 738
#ifdef ATARI
WSWAP((char *)&bytecode->version);
#endif
kollo's avatar
kollo committed
739 740
    if(verbose) printf("Bytecode header found (V.%x)\n",bytecode->version);
    if(bytecode->version!=BC_VERSION) {
kollo's avatar
kollo committed
741 742 743
      printf("xb2c: ERROR: Cannot translate this bytecode.\nThis bytecode was compiled for a different version of "
      "X11-Basic.\nPlease consider to recompile it.\n");
      return(-1);
kollo's avatar
kollo committed
744
    }
kollo's avatar
kollo committed
745 746 747 748 749 750 751 752 753 754 755
#ifdef ATARI
  LWSWAP((short *)&bytecode->textseglen);
  LWSWAP((short *)&bytecode->rodataseglen);
  LWSWAP((short *)&bytecode->sdataseglen);
  LWSWAP((short *)&bytecode->dataseglen);
  LWSWAP((short *)&bytecode->bssseglen);
  LWSWAP((short *)&bytecode->symbolseglen);
  LWSWAP((short *)&bytecode->stringseglen);
  LWSWAP((short *)&bytecode->relseglen);
  WSWAP((char *)&bytecode->flags);
#endif
kollo's avatar
kollo committed
756
        /* Sicherstellen, dass der Speicherbereich auch gross genug ist fuer bss segment*/
kollo's avatar
kollo committed
757 758
    if(bytecode->bssseglen>bytecode->relseglen+bytecode->stringseglen+bytecode->symbolseglen) {
      len+=bytecode->bssseglen-bytecode->stringseglen-bytecode->symbolseglen;
kollo's avatar
kollo committed
759
      if(verbose>1) printf("xb2c INFO: need realloc\n");
kollo's avatar
kollo committed
760 761 762 763
      p=realloc(p,len);
      bytecode=(BYTECODE_HEADER *)p;
    }
    
764 765
    fprintf(optr,"txt:    $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER),(int)bytecode->textseglen);
    fprintf(optr,"rodata: $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
766 767
                                      (unsigned int)bytecode->textseglen,(int)bytecode->rodataseglen);
    rodata=p+sizeof(BYTECODE_HEADER)+(unsigned int)bytecode->textseglen;
768
    fprintf(optr,"sdata:  $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
769 770
                                      (unsigned int)bytecode->textseglen+
				      (unsigned int)bytecode->rodataseglen,(int)bytecode->sdataseglen);
771
    fprintf(optr,"data:   $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
772 773 774 775 776
                                    (unsigned int)bytecode->textseglen+
				    (unsigned int)bytecode->rodataseglen+
				    (unsigned int)bytecode->sdataseglen,(int)bytecode->dataseglen);
    datasec=(unsigned char *)&p[sizeof(BYTECODE_HEADER)+(unsigned int)bytecode->textseglen+
    bytecode->rodataseglen];
777
    fprintf(optr,"bss:    $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
778 779 780 781
                                    (unsigned int)bytecode->textseglen+
				    (unsigned int)bytecode->rodataseglen+
				    (unsigned int)bytecode->sdataseglen+
				    (unsigned int)bytecode->dataseglen,(int)bytecode->bssseglen);
782
    fprintf(optr,"str:    $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
783 784 785 786
                                   (unsigned int)bytecode->textseglen+
				   (unsigned int)bytecode->rodataseglen+
				   (unsigned int) bytecode->sdataseglen+
				   (unsigned int)bytecode->dataseglen,(int)bytecode->stringseglen);
787
    fprintf(optr,"sym:    $%08x %d\n",(unsigned int)sizeof(BYTECODE_HEADER)+
kollo's avatar
kollo committed
788 789 790 791 792 793 794
                                   (unsigned int)bytecode->textseglen+
				    (unsigned int)bytecode->rodataseglen+
				    (unsigned int)bytecode->sdataseglen+
				   (unsigned int)bytecode->dataseglen+
				   (unsigned int)bytecode->stringseglen,(int)bytecode->symbolseglen);
    strings=p+sizeof(BYTECODE_HEADER)+bytecode->textseglen+bytecode->rodataseglen+bytecode->sdataseglen+
    bytecode->dataseglen;
kollo's avatar
kollo committed
795
    fprintf(optr,"Strings: %s\n\n",strings);
kollo's avatar
kollo committed
796 797
    symtab=(BYTECODE_SYMBOL *)(p+sizeof(BYTECODE_HEADER)+bytecode->textseglen+bytecode->rodataseglen+bytecode->sdataseglen+
                                 bytecode->dataseglen+bytecode->stringseglen);
kollo's avatar
kollo committed
798
    c=bytecode->symbolseglen/sizeof(BYTECODE_SYMBOL);
kollo's avatar
kollo committed
799

kollo's avatar
kollo committed
800
    fprintf(optr,"%d symbols:\n",c);
kollo's avatar
kollo committed
801
    for(i=0;i<c;i++) {
kollo's avatar
kollo committed
802 803 804 805 806
	#ifdef ATARI
	  LWSWAP((short *)&symtab[i].name);
	  LWSWAP((short *)&symtab[i].adr);
	#endif
      if(verbose>1) fprintf(optr,"%4d : $%08x %s\n",i,(unsigned int)symtab[i].adr,&strings[symtab[i].name]);
kollo's avatar
kollo committed
807
    }
kollo's avatar
kollo committed
808 809
    fprintf(optr,"\t\t\t*/\n"
                 "#include <x11basic/xb2csol.h>\n\n");
kollo's avatar
kollo committed
810
    fflush(optr);
kollo's avatar
kollo committed
811 812
    return(0);
  } else {
kollo's avatar
kollo committed
813
    printf("xb2c: ERROR, file format not recognized.\n");
kollo's avatar
kollo committed
814 815 816
    return(-1);
  }
}
kollo's avatar
kollo committed
817 818


kollo's avatar
kollo committed
819
int main(int anzahl, char *argumente[]) {
kollo's avatar
kollo committed
820 821
  /* Initialize data segment buffer */
  if(anzahl<2) {    /* Kommandomodus */
kollo's avatar
kollo committed
822
    intro();usage();
kollo's avatar
kollo committed
823 824 825 826
  } else {
    kommandozeile(anzahl, argumente);    /* Kommandozeile bearbeiten */
    if(loadfile) {
      if(exist(ifilename)) {
kollo's avatar
kollo committed
827
          /* file oeffnen */
kollo's avatar
kollo committed
828 829 830 831
	#ifdef ATARI
	intro();
	printf("--> %s\n",ofilename);
	#endif
kollo's avatar
kollo committed
832 833 834
	optr=fopen(ofilename,"w");
        if(loadbcprg(ifilename)==0) {
          data_section();
kollo's avatar
kollo committed
835
	  translate();
kollo's avatar
kollo committed
836 837
	}
	fclose(optr);
kollo's avatar
kollo committed
838 839 840
	#ifdef ATARI
	printf("done.\n");
	#endif
kollo's avatar
kollo committed
841 842
      } else {
        printf("ERROR: %s not found !\n",ifilename);
kollo's avatar
kollo committed
843 844 845
	#ifdef ATARI
	sleep(2);
	#endif
kollo's avatar
kollo committed
846 847 848
        exit(EX_NOINPUT);
      }
    } else exit(EX_NOINPUT);
kollo's avatar
kollo committed
849
  }
kollo's avatar
kollo committed
850 851 852
  #ifdef ATARI
  sleep(2);
  #endif
kollo's avatar
kollo committed
853
  return(EX_OK);
kollo's avatar
kollo committed
854
}