2
0

SYSTEM.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #ifndef SYSTEM__h
  2. #define SYSTEM__h
  3. // 64 bit system detection
  4. #if (__SIZEOF_POINTER__ == 8) || defined (_LP64) || defined(__LP64__) || defined(_WIN64)
  5. #define o__64
  6. #endif
  7. // Declare memcpy in a way compatible with C compilers intrinsic
  8. // built in implementations.
  9. #if defined (o__64)
  10. #if defined(_WIN64)
  11. typedef unsigned long long size_t;
  12. #else
  13. typedef unsigned long size_t;
  14. #endif
  15. #else
  16. typedef unsigned int size_t;
  17. #endif
  18. #define _SIZE_T_DECLARED // For FreeBSD
  19. #define _SIZE_T_DEFINED_ // For OpenBSD
  20. void *memcpy(void *dest, const void *source, size_t size);
  21. #if defined _MSC_VER
  22. #define alloca _alloca
  23. #endif
  24. void *alloca(size_t size);
  25. // Declare fixed size versions of basic intger types
  26. #if defined (o__64) && !defined(_WIN64)
  27. // LP64
  28. typedef long INT64;
  29. typedef unsigned long UINT64;
  30. #else
  31. // ILP32 or LLP64
  32. typedef long long INT64;
  33. typedef unsigned long long UINT64;
  34. #endif
  35. typedef int INT32;
  36. typedef unsigned int UINT32;
  37. typedef short int INT16;
  38. typedef unsigned short int UINT16;
  39. typedef signed char INT8;
  40. typedef unsigned char UINT8;
  41. // The compiler uses 'import' and 'export' which translate to 'extern' and
  42. // nothing respectively.
  43. #define import extern
  44. #define export
  45. // Known constants
  46. #define NIL ((void*)0)
  47. #define __MAXEXT 16
  48. #define POINTER__typ ((ADDRESS*)(1)) // not NIL and not a valid type
  49. // Oberon types
  50. typedef INT8 BOOLEAN;
  51. typedef INT8 SYSTEM_BYTE;
  52. typedef UINT8 CHAR;
  53. typedef float REAL;
  54. typedef double LONGREAL;
  55. typedef void* SYSTEM_PTR;
  56. // 'ADDRESS' is a synonym for an integer of pointer size
  57. #if defined (o__64)
  58. #define ADDRESS INT64
  59. #else
  60. #define ADDRESS INT32
  61. #endif
  62. // ----------------------------------------------------------------------
  63. // ----------------------------------------------------------------------
  64. // OS Memory allocation interfaces are in PlatformXXX.Mod
  65. extern ADDRESS Platform_OSAllocate (ADDRESS size);
  66. extern void Platform_OSFree (ADDRESS addr);
  67. // Assertions and Halts
  68. extern void Modules_Halt(INT32 x);
  69. extern void Modules_AssertFail(INT32 x);
  70. #define __HALT(x) Modules_Halt((INT32)(x))
  71. #define __ASSERT(cond, x) if (!(cond)) Modules_AssertFail((INT32)(x))
  72. // Index checking
  73. static inline INT64 __XF(UINT64 i, UINT64 ub) {if (i >= ub) {__HALT(-2);} return i;}
  74. #define __X(i, ub) (((i)<(ub))?i:(__HALT(-2),0))
  75. // Range checking, and checked SHORT and CHR functions
  76. static inline INT64 __RF(UINT64 i, UINT64 ub) {if (i >= ub) {__HALT(-8);} return i;}
  77. #define __R(i, ub) (((i)<(ub))?i:(__HALT(-8),0))
  78. #define __SHORT(x, ub) ((int)((UINT64)(x)+(ub)<(ub)+(ub)?(x):(__HALT(-8),0)))
  79. #define __SHORTF(x, ub) ((int)(__RF((x)+(ub),(ub)+(ub))-(ub)))
  80. #define __CHR(x) ((CHAR)__R(x, 256))
  81. #define __CHRF(x) ((CHAR)__RF(x, 256))
  82. // Signal handling in SYSTEM.c
  83. #ifndef _WIN32
  84. extern void SystemSetHandler(int s, ADDRESS h);
  85. #else
  86. extern void SystemSetInterruptHandler(ADDRESS h);
  87. extern void SystemSetQuitHandler (ADDRESS h);
  88. #endif
  89. // String comparison
  90. static inline int __str_cmp(CHAR *x, CHAR *y){
  91. INT64 i = 0;
  92. CHAR ch1, ch2;
  93. do {ch1 = x[i]; ch2 = y[i]; i++;
  94. if (!ch1) return -(int)ch2;
  95. } while (ch1==ch2);
  96. return (int)ch1 - (int)ch2;
  97. }
  98. #define __STRCMP(a,b) __str_cmp((CHAR*)(a), (CHAR*)(b))
  99. // Inline string, record and array copy
  100. #define __COPY(s, d, n) {char*_a=(void*)s,*_b=(void*)d; LONGINT _i=0,_t=n-1; \
  101. while(_i<_t&&((_b[_i]=_a[_i])!=0)){_i++;};_b[_i]=0;}
  102. #define __DUPARR(v, t) v=(void*)memcpy(v##__copy,v,sizeof(t))
  103. #define __DUP(x, l, t) x=(void*)memcpy(alloca(l*sizeof(t)),x,l*sizeof(t))
  104. #define __DEL(x)
  105. /* SYSTEM ops */
  106. #define __VAL(t, x) (*(t*)&(x))
  107. #define __GET(a, x, t) x=*(t*)(ADDRESS)(a)
  108. #define __PUT(a, x, t) *(t*)(ADDRESS)(a)=x
  109. #define __LSHL(x, n, s) ((INT##s)((UINT##s)(x)<<(n)))
  110. #define __LSHR(x, n, s) ((INT##s)((UINT##s)(x)>>(n)))
  111. #define __LSH(x, n, s) ((n)>=0? __LSHL(x, n, s): __LSHR(x, -(n), s))
  112. #define __ROTL(x, n, s) ((INT##s)((UINT##s)(x)<<(n)|(UINT##s)(x)>>(s-(n))))
  113. #define __ROTR(x, n, s) ((INT##s)((UINT##s)(x)>>(n)|(UINT##s)(x)<<(s-(n))))
  114. #define __ROT(x, n, s) ((n)>=0? __ROTL(x, n, s): __ROTR(x, -(n), s))
  115. #define __ASHL(x, n) ((INT64)(x)<<(n))
  116. #define __ASHR(x, n) ((INT64)(x)>>(n))
  117. #define __ASH(x, n) ((n)>=0?__ASHL(x,n):__ASHR(x,-(n)))
  118. static inline INT64 SYSTEM_ASH(INT64 x, INT64 n) {return __ASH(x,n);}
  119. #define __ASHF(x, n) SYSTEM_ASH((INT64)(x), (INT64)(n))
  120. #define __MOVE(s, d, n) memcpy((char*)(ADDRESS)(d),(char*)(ADDRESS)(s),n)
  121. extern INT64 SYSTEM_DIV(INT64 x, INT64 y);
  122. #define __DIVF(x, y) SYSTEM_DIV(x, y)
  123. #define __DIV(x, y) (((x)>0 && (y)>0) ? (x)/(y) : __DIVF(x, y))
  124. extern INT64 SYSTEM_MOD(INT64 x, INT64 y);
  125. #define __MODF(x, y) SYSTEM_MOD(x, y)
  126. #define __MOD(x, y) (((x)>0 && (y)>0) ? (x)%(y) : __MODF(x, y))
  127. extern INT64 SYSTEM_ENTIER (double x);
  128. #define __ENTIER(x) SYSTEM_ENTIER(x)
  129. #define __ABS(x) (((x)<0)?-(x):(x))
  130. static inline INT32 SYSTEM_ABS64(INT64 i) {return i >= 0 ? i : -i;}
  131. static inline INT64 SYSTEM_ABS32(INT32 i) {return i >= 0 ? i : -i;}
  132. #define __ABSF(x) ((sizeof(x) <= 4) ? SYSTEM_ABS32(x) : SYSTEM_ABS64(x))
  133. static inline double SYSTEM_ABSD(double i) {return i >= 0.0 ? i : -i;}
  134. #define __ABSFD(x) SYSTEM_ABSD(x)
  135. #define __CAP(ch) ((CHAR)((ch)&0x5f))
  136. #define __ODD(x) ((x)&1)
  137. #define __IN(x, s, size) (((unsigned int)(x))<size && ((((UINT##size)(s))>>(x))&1))
  138. // todo tested versions of SETOF and SETRNG: check that x, l and h fit size
  139. #define __SETOF(x, size) ((UINT##size)1<<(x))
  140. #define __SETRNG(l, h, size) ((~(UINT##size)0<<(l))&~(UINT##size)0>>(size-1-(h)))
  141. #define __MASK(x, m) ((x)&~(m))
  142. #define __BIT(x, n) (*(UINT64*)(x)>>(n)&1)
  143. // Runtime checks
  144. #define __RETCHK __retchk: __HALT(-3); return 0;
  145. #define __CASECHK __HALT(-4)
  146. #define __WITHCHK __HALT(-7)
  147. #define __IS(tag, typ, level) (*(tag-(__BASEOFF-level))==(ADDRESS)typ##__typ)
  148. #define __TYPEOF(p) (*(((ADDRESS**)(p))-1))
  149. #define __ISP(p, typ, level) __IS(__TYPEOF(p),typ,level)
  150. #define __GUARDP(p, typ, level) ((typ*)(__ISP(p,typ,level)?p:(__HALT(-5),p)))
  151. #define __GUARDR(r, typ, level) (*((typ*)(__IS(r##__typ,typ,level)?r:(__HALT(-5),r))))
  152. #define __GUARDA(p, typ, level) ((struct typ*)(__IS(__TYPEOF(p),typ,level)?p:(__HALT(-5),p)))
  153. #define __GUARDEQR(p, dyntyp, typ) if(dyntyp!=typ##__typ) __HALT(-6);*(p)
  154. #define __GUARDEQP(p, typ) if(__TYPEOF(p)!=typ##__typ)__HALT(-6);*((typ*)p)
  155. // Module entry/registration/exit
  156. extern void Heap_REGCMD();
  157. extern SYSTEM_PTR Heap_REGMOD();
  158. extern void Heap_REGTYP();
  159. extern void Heap_INCREF();
  160. #define __DEFMOD static void *m; if (m!=0) {return m;}
  161. #define __REGCMD(name, cmd) Heap_REGCMD(m, (CHAR*)name, cmd)
  162. #define __REGMOD(name, enum) if (m==0) {m = Heap_REGMOD((CHAR*)name,enum);}
  163. #define __ENDMOD return m
  164. #define __MODULE_IMPORT(name) Heap_INCREF(name##__init())
  165. // Main module initialisation, registration and finalisation
  166. extern void Modules_Init(INT32 argc, ADDRESS argv);
  167. extern void Heap_FINALL();
  168. #define __INIT(argc, argv) static void *m; Modules_Init(argc, (ADDRESS)&argv);
  169. #define __REGMAIN(name, enum) m = Heap_REGMOD((CHAR*)name,enum)
  170. #define __FINI Heap_FINALL(); return 0
  171. // Memory allocation
  172. extern SYSTEM_PTR Heap_NEWBLK (ADDRESS size);
  173. extern SYSTEM_PTR Heap_NEWREC (ADDRESS tag);
  174. extern SYSTEM_PTR SYSTEM_NEWARR(ADDRESS*, ADDRESS, int, int, int, ...);
  175. #define __SYSNEW(p, len) p = Heap_NEWBLK((ADDRESS)(len))
  176. #define __NEW(p, t) p = Heap_NEWREC((ADDRESS)t##__typ)
  177. #define __NEWARR SYSTEM_NEWARR
  178. /* Type handling */
  179. extern void SYSTEM_INHERIT(ADDRESS *t, ADDRESS *t0);
  180. extern void SYSTEM_ENUMP (void *adr, ADDRESS n, void (*P)());
  181. extern void SYSTEM_ENUMR (void *adr, ADDRESS *typ, ADDRESS size, ADDRESS n, void (*P)());
  182. #define __TDESC(t, m, n) \
  183. static struct t##__desc { \
  184. ADDRESS tproc[m]; /* Proc for each ptr field */ \
  185. ADDRESS tag; \
  186. ADDRESS next; /* Module table type list points here */ \
  187. ADDRESS level; \
  188. ADDRESS module; \
  189. char name[24]; \
  190. ADDRESS basep[__MAXEXT]; /* List of bases this extends */ \
  191. ADDRESS reserved; \
  192. ADDRESS blksz; /* xxx_typ points here */ \
  193. ADDRESS ptr[n+1]; /* Offsets of ptrs up to -ve sentinel */ \
  194. } t##__desc
  195. #define __BASEOFF (__MAXEXT+1) // blksz as index to base.
  196. #define __TPROC0OFF (__BASEOFF+24/sizeof(ADDRESS)+5) // blksz as index to tproc IFF m=1.
  197. #define __EOM 1
  198. #define __TDFLDS(name, size) {__EOM}, 1, 0, 0, 0, name, {0}, 0, size
  199. #define __ENUMP(adr, n, P) SYSTEM_ENUMP(adr, (ADDRESS)(n), P)
  200. #define __ENUMR(adr, typ, size, n, P) SYSTEM_ENUMR(adr, typ, (ADDRESS)(size), (ADDRESS)(n), P)
  201. #define __INITYP(t, t0, level) \
  202. t##__typ = (ADDRESS*)&t##__desc.blksz; \
  203. memcpy(t##__desc.basep, t0##__typ - __BASEOFF, level*sizeof(ADDRESS)); \
  204. t##__desc.basep[level] = (ADDRESS)t##__typ; \
  205. t##__desc.module = (ADDRESS)m; \
  206. if(t##__desc.blksz!=sizeof(struct t)) __HALT(-15); \
  207. t##__desc.blksz = (t##__desc.blksz+5*sizeof(ADDRESS)-1)/(4*sizeof(ADDRESS))*(4*sizeof(ADDRESS)); \
  208. Heap_REGTYP(m, (ADDRESS)&t##__desc.next); \
  209. SYSTEM_INHERIT(t##__typ, t0##__typ)
  210. // Oberon-2 type bound procedures support
  211. #define __INITBP(t, proc, num) *(t##__typ-(__TPROC0OFF+num))=(ADDRESS)proc
  212. #define __SEND(typ, num, funtyp, parlist) ((funtyp)((ADDRESS)*(typ-(__TPROC0OFF+num))))parlist
  213. #endif