home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / gofer / Sources / c / machdep < prev    next >
Encoding:
Text File  |  1993-03-04  |  15.4 KB  |  598 lines

  1. /* --------------------------------------------------------------------------
  2.  * machdep.c:   Copyright (c) Mark P Jones 1991-1993.   All rights reserved.
  3.  *              See goferite.h for details and conditions of use etc...
  4.  *              Gofer version 2.28 January 1993
  5.  *
  6.  * Machine dependent code
  7.  * RISCOS specific code provided by Bryan Scatergood, JBS
  8.  * ------------------------------------------------------------------------*/
  9.  
  10. #if UNIX
  11. #include <signal.h>
  12. #include <sys/ioctl.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #endif
  17.  
  18. #if (TURBOC | BCC)
  19. #include <dos.h>
  20. #include <conio.h>
  21. #include <io.h>
  22. #include <stdlib.h>
  23. #include <mem.h>
  24. #include <sys\stat.h>
  25. #include <time.h>
  26. extern unsigned _stklen = 8000;        /* Allocate an 8k stack segment       */
  27. #endif
  28.  
  29. #if DJGPP
  30. #include <dos.h>
  31. #include <stdlib.h>
  32. #include <std.h>
  33. #include <signal.h>
  34. #include <fcntl.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #endif
  38.  
  39. #if RISCOS
  40. #include <assert.h>
  41. #include <signal.h>
  42. #include "swis.h"
  43. #include "os.h"
  44. #endif
  45.  
  46. /* --------------------------------------------------------------------------
  47.  * Machine dependent code is used in each of:
  48.  *    - The gofer interpreter        MACHDEP_GOFER
  49.  *    - The gofer compiler        MACHDEP_GOFC
  50.  *    - The compiler runtime system    MACHDEP_RUNTIME
  51.  * In many cases, the the same code is used in each part.  The following
  52.  * sections of code are enclosed in suitable #if ... #endif directives to
  53.  * indicate which sections require particular parts of the code.  Each of
  54.  * the three systems above defines one of the three symbols on the right
  55.  * above as 1 and then #includes this file.  The following directives make
  56.  * sure that the other macros are set to the correct defaults.
  57.  * ------------------------------------------------------------------------*/
  58.  
  59. #ifndef MACHDEP_GOFER
  60. #define MACHDEP_GOFER   0
  61. #endif
  62. #ifndef MACHDEP_GOFC
  63. #define MACHDEP_GOFC    0
  64. #endif
  65. #ifndef MACHDEP_RUNTIME
  66. #define MACHDEP_RUNTIME 0
  67. #endif
  68.  
  69. /* --------------------------------------------------------------------------
  70.  * Find information about a file:
  71.  * ------------------------------------------------------------------------*/
  72.  
  73. #if (MACHDEP_GOFER | MACHDEP_GOFC)
  74. #if RISCOS
  75. typedef struct { unsigned hi, lo; } Time;
  76. #define timeChanged(now,thn)    (now.hi!=thn.hi || now.lo!=thn.lo)
  77. #define timeSet(var,tm)        var.hi = tm.hi; var.lo = tm.lo
  78. #else
  79. typedef time_t Time;
  80. #define timeChanged(now,thn)    (now!=thn)
  81. #define timeSet(var,tm)        var = tm
  82. #endif
  83.  
  84. static Void local getFileInfo    Args((String, Time *, Long *));
  85.  
  86. static Void local getFileInfo(s,tm,sz)    /* find time stamp and size of file*/
  87. String s;
  88. Time   *tm;
  89. Long   *sz; {
  90. #if RISCOS                /* get file info for RISCOS -- JBS */
  91.     os_regset r;            /* RISCOS PRM p.850 and p.837       */
  92.     r.r[0] = 17;            /* Read catalogue, no path       */
  93.     r.r[1] = (int)s;
  94.     os_swi(OS_File, &r);
  95.     if(r.r[0] == 1 && (r.r[2] & 0xFFF00000) == 0xFFF00000) {
  96.     tm->hi = r.r[2] & 0xFF;        /* Load address (high byte)       */
  97.     tm->lo = r.r[3];        /* Execution address (low 4 bytes) */
  98.     }
  99.     else                /* Not found, or not time-stamped  */
  100.     tm->hi = tm->lo = 0;
  101.     *sz = (Long)(r.r[0] == 1 ? r.r[4] : 0);
  102. #else                    /* normally just use stat()       */
  103.     static struct stat scbuf;
  104.     stat(s,&scbuf);
  105.     *tm = scbuf.st_mtime;
  106.     *sz = (Long)(scbuf.st_size);
  107. #endif
  108. }
  109. #endif
  110.  
  111. #if RISCOS                /* RISCOS needs access()       */
  112. int access(char *s, int dummy) {    /* Give 1 iff cannot access file s */
  113.     os_regset r;            /* RISCOS PRM p.850    -- JBS       */
  114.     assert(dummy == 0);
  115.     r.r[0] = 17; /* Read catalogue, no path */
  116.     r.r[1] = (int)s;
  117.     os_swi(OS_File, &r);
  118.     return r.r[0] != 1;
  119. }
  120.  
  121. int namecmp(char *filename, char *spec) /* For filename extension hacks */
  122. {
  123.   while(*spec)
  124.     if(tolower(*filename) != *spec++) return 0; else ++filename;
  125.   return *filename == '.';
  126. }
  127. #endif
  128.  
  129. /* --------------------------------------------------------------------------
  130.  * Get time/date stamp for inclusion in compiled files:
  131.  * ------------------------------------------------------------------------*/
  132.  
  133. #if MACHDEP_GOFC
  134. #include <time.h>
  135. String timeString() {            /* return time&date string       */
  136.     time_t clock;            /* must end with '\n' character       */
  137.     time(&clock);
  138.     return(ctime(&clock));
  139. }
  140. #endif
  141.  
  142. /* --------------------------------------------------------------------------
  143.  * Garbage collection notification:
  144.  * ------------------------------------------------------------------------*/
  145.  
  146. #if  (MACHDEP_GOFER | MACHDEP_GOFC)
  147. Bool gcMessages = FALSE;        /* TRUE => print GC messages       */
  148.  
  149. Void gcStarted() {            /* notify garbage collector start  */
  150.     if (gcMessages) {
  151.     printf("{{Gc");
  152.     fflush(stdout);
  153.     }
  154. }
  155.  
  156. Void gcScanning() {            /* notify garbage collector scans  */
  157.     if (gcMessages) {
  158.     putchar(':');
  159.     fflush(stdout);
  160.     }
  161. }
  162.  
  163. Void gcRecovered(recovered)        /* notify garbage collection done  */
  164. Int recovered; {
  165.     if (gcMessages) {
  166.     printf("%d}}",recovered);
  167.     fflush(stdout);
  168.     }
  169. }
  170.  
  171. Cell *CStackBase;            /* Retain start of C control stack */
  172.  
  173. #if RISCOS                /* Stack traversal for RISCOS       */
  174.  
  175. /* Warning: The following code is specific to the Acorn ARM under RISCOS
  176.    (and C4).  We must explicitly walk back through the stack frames, since
  177.    the stack is extended from the heap. (see PRM pp. 1757).  gcCStack must
  178.    not be modified, since the offset '5' assumes that only v1 is used inside
  179.    this function. Hence we do all the real work in gcARM.
  180. */
  181.           
  182. #define spreg 13 /* C3 has SP=R13 */
  183.  
  184. #define previousFrame(fp)    ((int *)((fp)[-3]))
  185. #define programCounter(fp)    ((int *)((*(fp)-12) & ~0xFC000003))
  186. #define isSubSPSP(w)        (((w)&dontCare) == doCare)
  187. #define doCare            (0xE24DD000)  /* SUB r13,r13,#0 */
  188. #define dontCare        (~0x00100FFF) /* S and # bits   */
  189. #define immediateArg(x)        ( ((x)&0xFF) << (((x)&0xF00)>>7) )
  190.  
  191. static void gcARM(int *fp) {
  192.     int si = *programCounter(fp);    /* Save instruction indicates how */
  193.                     /* many registers in this frame   */
  194.     int *regs = fp - 4;
  195.     if (si & (1<<0)) markWithoutMove(*regs--);
  196.     if (si & (1<<1)) markWithoutMove(*regs--);
  197.     if (si & (1<<2)) markWithoutMove(*regs--);
  198.     if (si & (1<<3)) markWithoutMove(*regs--);
  199.     if (si & (1<<4)) markWithoutMove(*regs--);
  200.     if (si & (1<<5)) markWithoutMove(*regs--);
  201.     if (si & (1<<6)) markWithoutMove(*regs--);
  202.     if (si & (1<<7)) markWithoutMove(*regs--);
  203.     if (si & (1<<8)) markWithoutMove(*regs--);
  204.     if (si & (1<<9)) markWithoutMove(*regs--);
  205.     if (previousFrame(fp)) {
  206.     /* The non-register stack space is for the previous frame is above
  207.        this fp, and not below the previous fp, because of the way stack
  208.        extension works. It seems the only way of discovering its size is
  209.        finding the SUB sp, sp, #? instruction by walking through the code
  210.        following the entry point.
  211.     */
  212.     int *oldpc = programCounter(previousFrame(fp));
  213.     int fsize = 0, i;
  214.     for(i = 1; i < 6; ++i)
  215.         if(isSubSPSP(oldpc[i])) fsize += immediateArg(oldpc[i]) / 4;
  216.     for(i=1; i<=fsize; ++i)
  217.         markWithoutMove(fp[i]);
  218.     }
  219. }
  220.  
  221. void gcCStack() {
  222.     int dummy;
  223.     int *fp = 5 + &dummy;
  224.     while (fp) {
  225.     gcARM(fp);
  226.     fp = previousFrame(fp);
  227.     }
  228. }
  229.  
  230. #else            /* Garbage collection for standard stack machines  */
  231.  
  232. Void gcCStack() {            /* Garbage collect elements off    */
  233.     Cell stackTop = NIL;        /* C stack               */
  234.     Cell *ptr = &stackTop;
  235. #if SMALL_GOFER
  236.     if (((long)(ptr) - (long)(CStackBase))&1)
  237.     fatal("gcCStack");
  238. #else 
  239.     if (((long)(ptr) - (long)(CStackBase))&3)
  240.     fatal("gcCStack");
  241. #endif
  242.  
  243. #define StackGrowsDown    while (ptr<=CStackBase) markWithoutMove(*ptr++)
  244. #define StackGrowsUp    while (ptr>=CStackBase) markWithoutMove(*ptr--)
  245. #define GuessDirection    if (ptr>CStackBase) StackGrowsUp; else StackGrowsDown
  246. #if HPUX
  247.     GuessDirection;
  248. #else
  249.     StackGrowsDown;
  250. #endif
  251. #undef  StackGrowsDown
  252. #undef  StackGrowsUp
  253. #undef  GuessDirection
  254. }
  255. #endif
  256. #endif
  257.  
  258. /* --------------------------------------------------------------------------
  259.  * Terminal dependent stuff:
  260.  * ------------------------------------------------------------------------*/
  261.  
  262. #if   (TERMIO_IO | SGTTY_IO)
  263.  
  264. #if TERMIO_IO
  265. #include <termio.h>
  266. typedef  struct termio   TermParams;
  267. #define  getTerminal(tp) ioctl(fileno(stdin),TCGETA,&tp)
  268. #define  setTerminal(tp) ioctl(fileno(stdin),TCSETAF,&tp)
  269. #define  noEcho(tp)      tp.c_lflag    &= ~(ICANON | ECHO); \
  270.              tp.c_cc[VMIN]  = 1;            \
  271.              tp.c_cc[VTIME] = 0;
  272. #endif
  273.  
  274. #if SGTTY_IO
  275. #include <sgtty.h>
  276. typedef  struct sgttyb   TermParams;
  277. #define  getTerminal(tp) ioctl(fileno(stdin),TIOCGETP,&tp)
  278. #define  setTerminal(tp) ioctl(fileno(stdin),TIOCSETP,&tp)
  279. #if HPUX
  280. #define  noEcho(tp)      tp.sg_flags |= RAW; tp.sg_flags &= (~ECHO);
  281. #else
  282. #define  noEcho(tp)      tp.sg_flags |= CBREAK; tp.sg_flags &= (~ECHO);
  283. #endif
  284. #endif
  285.  
  286. static Bool messedWithTerminal = FALSE;
  287. static TermParams originalSettings;
  288.  
  289. Void normalTerminal() {            /* restore terminal initial state  */
  290.     if (messedWithTerminal)
  291.     setTerminal(originalSettings);
  292. }
  293.  
  294. Void noechoTerminal() {            /* set terminal into noecho mode   */
  295.     TermParams settings;
  296.  
  297.     if (!messedWithTerminal) {
  298.     getTerminal(originalSettings);
  299.     messedWithTerminal = TRUE;
  300.     }
  301.     getTerminal(settings);
  302.     noEcho(settings);
  303.     setTerminal(settings);
  304. }
  305.  
  306. #if (MACHDEP_GOFER | MACHDEP_GOFC)
  307. Int getTerminalWidth() {        /* determine width of terminal       */
  308. #ifdef TIOCGWINSZ
  309.     static struct winsize terminalSize;
  310.     ioctl(fileno(stdout),TIOCGWINSZ,&terminalSize);
  311.     return (terminalSize.ws_col==0)? 80 : terminalSize.ws_col;
  312. #else
  313.     return 80;
  314. #endif
  315. }
  316. #endif
  317.  
  318. Int readTerminalChar() {        /* read character from terminal       */
  319.     return getchar();            /* without echo, assuming that       */
  320. }                    /* noechoTerminal() is active...   */
  321. #endif
  322.  
  323. #if DOS_IO
  324. static Bool terminalEchoReqd = TRUE;
  325.  
  326. #if (MACHDEP_GOFER | MACHDEP_GOFC)
  327. Int getTerminalWidth() {        /* PC screen is fixed 80 chars       */
  328.     return 80;
  329. }
  330. #endif
  331.  
  332. Void normalTerminal() {            /* restore terminal initial state  */
  333.     terminalEchoReqd = TRUE;
  334. }
  335.  
  336. Void noechoTerminal() {            /* turn terminal echo on/off       */
  337.     terminalEchoReqd = FALSE;
  338. }
  339.  
  340. Int readTerminalChar() {        /* read character from terminal       */
  341.     if (terminalEchoReqd)
  342.     return getchar();
  343.     else {
  344.     Int c = getch();
  345.     return c=='\r' ? '\n' : c;
  346.     }
  347. }
  348. #endif
  349.  
  350. #if RISCOS
  351. #if (MACHDEP_GOFER | MACHDEP_GOFC)
  352. Int getTerminalWidth() {
  353.     int dummy, width;
  354.     (void) os_swi3r(OS_ReadModeVariable, -1, 1, 0, &dummy, &dummy, &width);
  355.     return width+1;
  356. }
  357. #endif
  358.  
  359. Void normalTerminal() {            /* restore terminal initial state  */
  360. }                    /* (not yet implemented)       */
  361.  
  362. Void noechoTerminal() {            /* turn terminal echo on/off       */
  363. }                    /* (not yet implemented)       */
  364.  
  365. Int readTerminalChar() {        /* read character from terminal       */
  366.     return getchar();
  367. }
  368. #endif
  369.  
  370. /* --------------------------------------------------------------------------
  371.  * Interrupt handling:
  372.  * ------------------------------------------------------------------------*/
  373.  
  374. #if (MACHDEP_GOFER | MACHDEP_GOFC)    /* runtime.c provides own version  */
  375. static  Bool broken    = FALSE;
  376. static  Bool breakReqd = FALSE;
  377. static  sigProto(ignoreBreak);
  378.  
  379. Bool breakOn(reqd)            /* set break trapping on if reqd,  */
  380. Bool reqd; {                /* or off otherwise, returning old */
  381.     Bool old  = breakReqd;
  382.  
  383.     breakReqd = reqd;
  384.     if (reqd) {
  385.     if (broken) {            /* repond to break signal received */
  386.         broken = FALSE;        /* whilst break trap disabled       */
  387.         sigRaise(breakHandler);
  388.     }
  389.     ctrlbrk(breakHandler);
  390.     }
  391.     else
  392.     ctrlbrk(ignoreBreak);
  393.  
  394.     return old;
  395. }
  396.  
  397. static sigHandler(ignoreBreak) {    /* record but don't respond to break*/
  398.     ctrlbrk(ignoreBreak);
  399.     broken = TRUE;
  400.     sigResume;
  401. }
  402. #endif
  403.  
  404. /* --------------------------------------------------------------------------
  405.  * Shell escapes:
  406.  * ------------------------------------------------------------------------*/
  407.  
  408. #if MACHDEP_GOFER
  409. Int shellEsc(s)                /* run a shell command (or shell)  */
  410. String s; {
  411. #if UNIX
  412.     if (s[0]=='\0')
  413.     s = fromEnv("SHELL","/bin/sh");
  414. #endif
  415.     return system(s);
  416. }
  417.  
  418. #if RISCOS                /* RISCOS also needs a chdir()       */
  419. int chdir(char *s) {            /* RISCOS PRM p. 885    -- JBS       */
  420.     return os_swi2(OS_FSControl + XOS_Bit, 0, (int)s) != NULL;
  421. }
  422. #endif
  423. #endif
  424.  
  425. /* --------------------------------------------------------------------------
  426.  * Floating point support:
  427.  * ------------------------------------------------------------------------*/
  428.  
  429. #if HAS_FLOATS
  430. #if BREAK_FLOATS
  431. static union {
  432.     Float  flVal;
  433.     struct {
  434.     Cell flPart1,flPart2;
  435.     }       clVal;
  436. } fudgeCoerce;
  437.  
  438. Cell part1Float(fl)
  439. FloatPro fl; {
  440.     fudgeCoerce.flVal = fl;
  441.     return fudgeCoerce.clVal.flPart1;
  442. }
  443.  
  444. Cell part2Float(fl)
  445. FloatPro fl; {
  446.     fudgeCoerce.flVal = fl;
  447.     return fudgeCoerce.clVal.flPart2;
  448. }
  449.  
  450. FloatPro floatFromParts(c1,c2)
  451. Cell c1, c2; {
  452.     fudgeCoerce.clVal.flPart1 = c1;
  453.     fudgeCoerce.clVal.flPart2 = c2;
  454.     return fudgeCoerce.flVal;
  455. }
  456.  
  457. Cell mkFloat(fl)
  458. FloatPro fl; {
  459.     Cell p1,p2;
  460.     fudgeCoerce.flVal = fl;
  461.     p1 = mkInt(fudgeCoerce.clVal.flPart1);
  462.     p2 = mkInt(fudgeCoerce.clVal.flPart2);
  463.     return pair(FLOATCELL,pair(p1,p2));
  464. }
  465.  
  466. FloatPro floatOf(c)
  467. Cell c; {
  468.     fudgeCoerce.clVal.flPart1 = intOf(fst(snd(c)));
  469.     fudgeCoerce.clVal.flPart2 = intOf(snd(snd(c)));
  470.     return fudgeCoerce.flVal;
  471. }
  472.  
  473. #if MACHDEP_RUNTIME & BREAK_FLOATS
  474. Cell safeMkFloat(fl)
  475. FloatPro fl; {
  476.     fudgeCoerce.flVal = fl;
  477.     push(mkInt(fudgeCoerce.clVal.flPart2));
  478.     push(mkInt(fudgeCoerce.clVal.flPart1));
  479.     mkap();
  480.     topfun(FLOATCELL);
  481.     return pop();
  482. }
  483. #endif
  484.  
  485. #else /* !BREAK_FLOATS */
  486. static union {
  487.     Float flVal;
  488.     Cell  clVal;
  489. } fudgeCoerce;
  490.  
  491. Cell mkFloat(fl)
  492. FloatPro fl; {
  493.     fudgeCoerce.flVal = fl;
  494.     return pair(FLOATCELL,fudgeCoerce.clVal);
  495. }
  496.  
  497. FloatPro floatOf(c)
  498. Cell c; {
  499.     fudgeCoerce.clVal = snd(c);
  500.     return fudgeCoerce.flVal;
  501. }
  502. #endif
  503.  
  504. String floatToString(fl)            /* Make sure that floating   */
  505. FloatPro fl; {                    /* point values print out in */
  506.     static char buffer1[32];            /* a form in which they could*/
  507.     static char buffer2[32];            /* also be entered as floats */
  508.     Int i=0, j=0;
  509.  
  510.     sprintf(buffer1,"%g",fl);
  511.     while (buffer1[i] && strchr("eE.",buffer1[i])==0)
  512.     buffer2[j++] = buffer1[i++];
  513.     if (buffer1[i]!='.') {
  514.         buffer2[j++] = '.';
  515.     buffer2[j++] = '0';
  516.     }
  517.     while (buffer2[j++]=buffer1[i++])
  518.     ;
  519.     return buffer2;
  520. }
  521.  
  522. FloatPro stringToFloat(s)
  523. String s; {
  524.     return atof(s);
  525. }
  526. #else
  527. Cell mkFloat(fl)
  528. FloatPro fl; {
  529.     internal("mkFloat");
  530.     return 0;/*NOTREACHED*/
  531. }
  532.  
  533. FloatPro floatOf(c)
  534. Cell c; {
  535.     internal("floatOf");
  536.     return 0;/*NOTREACHED*/
  537. }
  538.  
  539. String floatToString(fl)
  540. FloatPro fl; {
  541.     internal("floatToString");
  542.     return "";/*NOTREACHED*/
  543. }
  544.  
  545. FloatPro stringToFloat(s)
  546. String s; {
  547.     internal("stringToFloat");
  548.     return 0;
  549. }
  550. #endif
  551.  
  552. /* --------------------------------------------------------------------------
  553.  * Machine dependent control:
  554.  * ------------------------------------------------------------------------*/
  555.  
  556. #if (MACHDEP_GOFER | MACHDEP_GOFC)
  557. #if UNIX
  558. static sigHandler(panic) {        /* exit in a panic, on receipt of  */
  559.     everybody(EXIT);            /* an unexpected signal           */
  560.     fprintf(stderr,"Unexpected signal\n");
  561.     exit(1);
  562.     sigResume;/*NOTREACHED*/
  563. }
  564. #endif
  565.  
  566. Void machdep(what)            /* Handle machine specific       */
  567. Int what; {                /* initialisation etc..           */
  568.     switch (what) {
  569.         case MARK    : break;
  570. #if UNIX
  571.         case INSTALL :
  572. #ifdef SIGHUP
  573.                signal(SIGHUP,panic);
  574. #endif
  575. #ifdef SIGQUIT
  576.                signal(SIGQUIT,panic);
  577. #endif
  578. #ifdef SIGTERM
  579.                signal(SIGTERM,panic);
  580. #endif
  581. #ifdef SIGSEGV
  582.                signal(SIGSEGV,panic);
  583. #endif
  584. #ifdef SIGBUS
  585.                signal(SIGBUS,panic);
  586. #endif
  587.                break;
  588. #endif
  589.         case RESET   :
  590.     case BREAK   :
  591.     case EXIT    : normalTerminal();
  592.                break;
  593.     }
  594. }
  595. #endif
  596.  
  597. /*-------------------------------------------------------------------------*/
  598.