File: imain.r

package info (click to toggle)
icon 9.4.3-7
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 16,312 kB
  • sloc: ansic: 52,463; makefile: 1,482; yacc: 926; lisp: 873; asm: 364; sh: 335; perl: 64; sed: 5
file content (384 lines) | stat: -rw-r--r-- 9,733 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#if !COMPILER
/*
 * File: imain.r
 * Interpreter main program, argument handling, and such.
 * Contents: main, iconx, ixopts, resolve
 */

#include "../h/version.h"
#include "../h/header.h"
#include "../h/opdefs.h"

static int iconx(int argc, char *argv[]);
static void ixopts(int argc, char *argv[], int *ip);

/*
 * Initial interpreter entry point (for all remaining platforms).
 */
int main(int argc, char *argv[]) {
   return iconx(argc, argv);
}

/*
 * Initial icode sequence. This is used to invoke the main procedure
 *  with one argument.  If main returns, the Op_Quit is executed.
 */
int iconx(int argc, char *argv[]) {
   int i, slen;
   static word istart[4];
   static int mterm = Op_Quit;

   #ifdef MultiThread
      /*
       * Look for MultiThread programming environment in which to execute
       * this program, specified by MTENV environment variable.
       */
      {
      char *p;
      char **new_argv;
      int i, j = 1, k = 1;
      if ((p = getenv("MTENV")) != NULL) {
         for(i=0;p[i];i++)
   	 if (p[i] == ' ')
   	    j++;
         new_argv = malloc((argc + j) * sizeof(char *));
         new_argv[0] = argv[0];
         for (i=0; p[i]; ) {
   	 new_argv[k++] = p+i;
   	 while (p[i] && (p[i] != ' '))
   	    i++;
   	 if (p[i] == ' ')
   	    p[i++] = '\0';
   	 }
         for(i=1;i<argc;i++)
   	 new_argv[k++] = argv[i];
         argc += j;
         argv = new_argv;
         }
      }
   #endif				/* MultiThread */

   ipc.opnd = NULL;

   #ifdef LoadFunc
      /*
       *  Append to FPATH the bin directory from which iconx was executed.
       */
      {
         char *p, *q, buf[1000];
         p = getenv("FPATH");
         q = relfile(argv[0], "/..");
         sprintf(buf, "FPATH=%s %s", (p ? p : "."), (q ? q : "."));
         putenv(buf);
         }
   #endif				/* LoadFunc */

   /*
    * Setup Icon interface.  It's done this way to avoid duplication
    *  of code, since the same thing has to be done if calling Icon
    *  is enabled.
    */

   ixopts(argc, argv, &i);

   if (i < 0) {
      argc++;
      argv--;
      i++;
      }

   while (i--) {			/* skip option arguments */
      argc--;
      argv++;
      }

   if (argc <= 1)
      error(NULL, "no icode file specified");

   /*
    * Call icon_init with the name of the icode file to execute.	[[I?]]
    */
   icon_init(argv[1], &argc, argv);

   /*
    *  Point sp at word after b_coexpr block for &main, point ipc at initial
    *	icode segment, and clear the gfp.
    */

   stackend = stack + mstksize/WordSize;
   sp = stack + Wsizeof(struct b_coexpr);

   ipc.opnd = istart;
   *ipc.op++ = Op_Noop;  /* aligns Invoke's operand */	/*	[[I?]] */
   *ipc.op++ = Op_Invoke;				/*	[[I?]] */
   *ipc.opnd++ = 1;
   *ipc.op = Op_Quit;
   ipc.opnd = istart;

   gfp = 0;

   /*
    * Set up expression frame marker to contain execution of the
    *  main procedure.  If failure occurs in this context, control
    *  is transferred to mterm, the address of an Op_Quit.
    */
   efp = (struct ef_marker *)(sp);
   efp->ef_failure.op = &mterm;
   efp->ef_gfp = 0;
   efp->ef_efp = 0;
   efp->ef_ilevel = 1;
   sp += Wsizeof(*efp) - 1;

   pfp = 0;
   ilevel = 0;

   /*
    * We have already loaded the
    * icode and initialized things, so it's time to just push main(),
    * build an Icon list for the rest of the arguments, and called
    * interp on a "invoke 1" bytecode.
    */

   /*
    * The first global variable holds the value of "main".  If it
    *  is not of type procedure, this is noted as run-time error 117.
    *  Otherwise, this value is pushed on the stack.
    */
   if (globals[0].dword != D_Proc)
      fatalerr(117, NULL);
   PushDesc(globals[0]);
   PushNull;
   glbl_argp = (dptr)(sp - 1);

   /*
    * If main() has a parameter, it is to be invoked with one argument, a list
    *  of the command line arguments.  The command line arguments are pushed
    *  on the stack as a series of descriptors and Ollist is called to create
    *  the list.  The null descriptor first pushed serves as Arg0 for
    *  Ollist and receives the result of the computation.
    */
   if (((struct b_proc *)BlkLoc(globals[0]))->nparam > 0) {
      for (i = 2; i < argc; i++) {
         char *tmp;
         slen = strlen(argv[i]);
         PushVal(slen);
         Protect(tmp=alcstr(argv[i],(word)slen), fatalerr(0,NULL));
         PushAVal(tmp);
         }

      Ollist(argc - 2, glbl_argp);
      }

   sp = (word *)glbl_argp + 1;
   glbl_argp = 0;
   ixinited = 1;		/* post fact that iconx is initialized */

   /*
    * Start things rolling by calling interp.  This call to interp
    *  returns only if an Op_Quit is executed.	If this happens,
    *  c_exit() is called to wrap things up.
    */
   interp(0,(dptr)NULL);
   c_exit(EXIT_SUCCESS);
   return 0;
}

/*
 * ixopts - handle interpreter command line options.
 */
void ixopts(argc,argv,ip)
int argc;
char **argv;
int *ip;
   {

   #ifdef TallyOpt
      extern int tallyopt;
   #endif				/* TallyOpt */

   *ip = 0;			/* number of arguments processed */

   #if MSWIN
      /*
       * if we didn't start with iconx.exe, backup one
       * so that our icode filename is argv[1].
       */
      {
      char tmp[256], *t2, *basename, *ext;
      int len = 0;
      strcpy(tmp, argv[0]);
      for (t2 = tmp; *t2; t2++) {
         switch (*t2) {
	    case ':':
	    case '/':
	    case '\\':
	       basename = t2 + 1;
	       ext = NULL;
	       break;
	    case '.':
	       ext = t2;
	       break;
	    default:
         *t2 = tolower(*t2);
	       break;
	    }
         }
      /* If present, cut the ".exe" extension. */
      if (ext != NULL && !strcmp(ext, ".exe"))
         *ext = 0;

      /*
       * if argv[0] is not a reference to our interpreter, take it as the
       * name of the icode file, and back up for it.
       */
      if (strcmp(basename, "iconx")) {
         argv--;
         argc++;
         (*ip)--;
         }
      }
   #endif				/* MSWIN */

   /*
    * Handle command line options.
    */
   while ( argv[1] != 0 && *argv[1] == '-' ) {

      switch ( *(argv[1]+1) ) {

         #ifdef TallyOpt
         /*
          * Set tallying flag if -T option given
          */
         case 'T':
            tallyopt = 1;
            break;
         #endif				/* TallyOpt */

         /*
          * Announce version on stderr if -V is given.
          */
         case 'V':
            fprintf(stderr, "%s  (%s)\n", Version, Config);
	    if (!argv[2])
	       exit(0);
            break;
   
         }

      argc--;
      (*ip)++;
      argv++;
      }
   }

/*
 * resolve - perform various fix-ups on the data read from the icode
 *  file.
 */
#ifdef MultiThread
   void resolve(pstate)
   struct progstate *pstate;
#else					/* MultiThread */
   void resolve()
#endif					/* MultiThread */

   {
   register word i, j;
   register struct b_proc *pp;
   register dptr dp;
   extern int Omkrec();
   #ifdef MultiThread
      register struct progstate *savedstate;
   #endif				/* MultiThread */

   #ifdef MultiThread
      savedstate = curpstate;
      if (pstate) curpstate = pstate;
   #endif				/* MultiThread */

   /*
    * Relocate the names of the global variables.
    */
   for (dp = gnames; dp < egnames; dp++)
      StrLoc(*dp) = strcons + (uword)StrLoc(*dp);

   /*
    * Scan the global variable array for procedures and fill in appropriate
    *  addresses.
    */
   for (j = 0; j < n_globals; j++) {

      if (globals[j].dword != D_Proc)
         continue;

      /*
       * The second word of the descriptor for procedure variables tells
       *  where the procedure is.  Negative values are used for built-in
       *  procedures and positive values are used for Icon procedures.
       */
      i = IntVal(globals[j]);

      if (i < 0) {
         /*
          * globals[j] points to a built-in function; call (bi_)strprc
	  *  to look it up by name in the interpreter's table of built-in
	  *  functions.
          */
	 if((BlkLoc(globals[j])= (union block *)bi_strprc(gnames+j,0)) == NULL)
            globals[j] = nulldesc;		/* undefined, set to &null */
         }
      else {

         /*
          * globals[j] points to an Icon procedure or a record; i is an offset
          *  to location of the procedure block in the code section.  Point
          *  pp at the block and replace BlkLoc(globals[j]).
          */
         pp = (struct b_proc *)(code + i);
         BlkLoc(globals[j]) = (union block *)pp;

         /*
          * Relocate the address of the name of the procedure.
          */
         StrLoc(pp->pname) = strcons + (uword)StrLoc(pp->pname);

         if (pp->ndynam == -2) {
            /*
             * This procedure is a record constructor.	Make its entry point
             *	be the entry point of Omkrec().
             */
            pp->entryp.ccode = Omkrec;

	    /*
	     * Initialize field names
	     */
            for (i = 0; i < pp->nfields; i++)
               StrLoc(pp->lnames[i]) = strcons + (uword)StrLoc(pp->lnames[i]);

	    }
         else {
            /*
             * This is an Icon procedure.  Relocate the entry point and
             *	the names of the parameters, locals, and static variables.
             */
            pp->entryp.icode = code + pp->entryp.ioff;
            for (i = 0; i < abs((int)pp->nparam)+pp->ndynam+pp->nstatic; i++)
               StrLoc(pp->lnames[i]) = strcons + (uword)StrLoc(pp->lnames[i]);
            }
         }
      }

   /*
    * Relocate the names of the fields.
    */

   for (dp = fnames; dp < efnames; dp++)
      StrLoc(*dp) = strcons + (uword)StrLoc(*dp);

   #ifdef MultiThread
      curpstate = savedstate;
   #endif				/* MultiThread */
   }

#endif					/* !COMPILER */