home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  4.6 KB  |  190 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * unix.c ---    Words making sense in UNIX-like systems only.
  31.  *        This file exports a word set system_words. So should do
  32.  *        any alternative files you might create for your OS.
  33.  *
  34.  * (duz 24Feb94)
  35.  */
  36.  
  37. #include "forth.h"
  38. #include "support.h"
  39.  
  40. #include <time.h>
  41.  
  42. #include "nonansi.h"
  43. #include "missing.h"
  44.  
  45.  
  46.  
  47. #if defined (Linux) && !defined (__cplusplus)
  48. /************************************************************************/
  49. /* Linux shared library calls -- KAH 930824                */
  50. /************************************************************************/
  51.  
  52. /* ================ */
  53. /* Dynload macros   */
  54. /* ================ */
  55.  
  56. typedef struct
  57.   {
  58.     unsigned nargs    :4;    /* # of arguments */
  59.     unsigned restype    :2;    /* result type */
  60.     unsigned        :2;    /* reserved -- complex res */
  61.     unsigned argtype    :2;    /* double/long arg flags */
  62.     unsigned        :22;    /* other argtypes shifted */
  63.                 /* from here */
  64.   }
  65. control_word;
  66.  
  67. #define DYN_INTEGER    0
  68. #define DYN_LONGINT    1
  69. #define DYN_FLOAT    2
  70. #define DYN_LONGFLOAT    3
  71.  
  72. #define exec(sub,resulthi,resultlo)    \
  73.     __asm__ __volatile__ ("call %2;movl %%edx, %0;movl %%eax, %1":    \
  74.     "=g" (resulthi), "=g" (resultlo):"g" (sub): "eax", "edx");
  75. #define cpush(x)    \
  76.     __asm__ __volatile__ ("pushl %0;"::"g" (x));
  77.  
  78. /* not sure if this float stuff is right.  Are singles and 
  79.    doubles same length?  Are these stored in a different forth stack?
  80.    How do I copy things back and forth from int/pointer stack to
  81.    float stack? */
  82.  
  83. #define lowfresult(x)    __asm__ __volatile__ ("fstpl %0;":"=g" (x));
  84. #define highfresult(x)    lowfresult(x);
  85.  
  86.  
  87. Code (uselibrary)
  88. {
  89.   *sp = uselib ((char *) *sp);
  90. }
  91.  
  92. static void
  93. call_c (pCode * sub)
  94. {
  95.   int i, high, low, result_type;
  96.   udCell *tmp;
  97.   control_word x = *(control_word *) sp++;
  98.  
  99.   i = x.nargs;
  100.   result_type = x.restype;
  101.   tmp = (udCell *) fp;
  102.   while (i--)
  103.     {
  104.       switch (x.argtype)
  105.     {
  106.     case DYN_LONGINT:
  107.       cpush (*sp++);
  108.     case DYN_INTEGER:
  109.       cpush (*sp++);
  110.       break;
  111.     case DYN_LONGFLOAT:
  112.       cpush (tmp->hi);
  113.     case DYN_FLOAT:
  114.       cpush (tmp++->lo);
  115.     }
  116.       *(uCell *) & x >>= 2;
  117.     }
  118.   exec (sub, high, low);
  119.   switch (result_type)
  120.     {
  121.     case DYN_INTEGER:
  122.       *--sp = low;
  123.       break;
  124.     case DYN_LONGINT:
  125.       *--sp = low;
  126.       *--sp = high;
  127.       break;
  128.     case DYN_FLOAT:
  129.       lowfresult (*--tmp);
  130.       break;
  131.     case DYN_LONGFLOAT:
  132.       lowfresult (*--tmp);
  133.       highfresult (*--tmp);
  134.     }
  135. }
  136.  
  137. Code (call_c)
  138. {
  139.   call_c ((pCode *) * sp++);
  140. }
  141.  
  142. #endif /* Linux shared library calls */
  143.  
  144.  
  145. #if !(defined(EMX) || defined(WATCOM))
  146.  
  147. /* defining `#!' to support forth scripts executed by the unix kernel: */
  148. #define ignore_line_ (pCode)refill
  149.  
  150. Code (termcap)            /* display what we got from termcap */
  151. {
  152.   extern void show_control_strings (int (*) (const char *, ...));
  153.   extern void show_rawkey_strings (int (*) (const char *, ...));
  154.   show_control_strings (outf);
  155.   show_rawkey_strings (outf);
  156. }
  157. #endif
  158.  
  159. Code (clock)            /* CLOCK ( --- ticks )    return clock() */
  160. {
  161.   *--sp = (Cell)clock ();
  162. }
  163.  
  164.  
  165. LISTWORDS (system) =
  166. {
  167. #if defined Linux && !defined __cplusplus
  168.   /* shared libraries */
  169.   CO ("CALL-C",        call_c),
  170.   CO ("USELIBRARY",    uselibrary),
  171. #endif
  172.  
  173. #if !(defined(EMX) || defined(WATCOM))
  174.   CO ("#!",        ignore_line),
  175.   CO ("TERMCAP",    termcap),
  176. #endif
  177.  
  178.   CO ("CLOCK",        clock),
  179. #if defined AIX1
  180.   OC ("CLK_TCK",    1000000),
  181. #elif defined CLOCKS_PER_SEC
  182.   OC ("CLK_TCK",    CLOCKS_PER_SEC),
  183. #elif defined CLK_TCK
  184.   OC ("CLK_TCK",    CLK_TCK),
  185. #else
  186.   OC ("CLK_TCK",    1000000), /* just a guess :-) */
  187. #endif
  188. };
  189. COUNTWORDS (system, "Unix words");
  190.