shim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* m2compiler-V3 runtime shim (C).
  2. *
  3. * Bridges Modula-2 external procedures to the host. Memory layout
  4. * matches the compiler's: a STRING (ARRAY OF CHAR) value is a
  5. * descriptor [0] = LONGCARD element count, then the bytes; the
  6. * compiler passes the descriptor's address as an `l`.
  7. *
  8. * Linked with every V3 program image.
  9. */
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <time.h>
  15. /* Write the contents of a string descriptor to fd 1. Returns the
  16. number of bytes written. */
  17. long m2write(long *desc)
  18. {
  19. /* desc[0] is the capacity; the string is NUL-terminated. */
  20. char *s = (char *)(desc + 1);
  21. size_t n = strnlen(s, (size_t)desc[0] + 1);
  22. if (n > 0)
  23. write(1, s, n);
  24. return (long)n;
  25. }
  26. /* Write a newline to fd 1. */
  27. long m2writeln(void)
  28. {
  29. write(1, "\n", 1);
  30. return 0;
  31. }
  32. /* Read one character from stdin (0 at EOF). */
  33. long m2readchar(void)
  34. {
  35. int c = getchar();
  36. return (c == EOF) ? 0 : c;
  37. }
  38. /* Read a signed decimal integer from stdin, skipping leading
  39. whitespace. The first non-digit after the number is consumed. */
  40. long m2readint(void)
  41. {
  42. long v = 0;
  43. int c;
  44. int neg = 0;
  45. do { c = getchar(); } while (c == ' ' || c == '\t' ||
  46. c == '\n' || c == '\r');
  47. if (c == '-') { neg = 1; c = getchar(); }
  48. while (c >= '0' && c <= '9') {
  49. v = v * 10 + (c - '0');
  50. c = getchar();
  51. }
  52. return neg ? -v : v;
  53. }
  54. /* ---------------- sequential files (stdio) ---------------- */
  55. /* Copy a descriptor string into a NUL-terminated buffer. */
  56. static void m2cstr(long *desc, char *buf, size_t cap)
  57. {
  58. char *s = (char *)(desc + 1);
  59. size_t n = strnlen(s, (size_t)desc[0] + 1);
  60. if (n >= cap) n = cap - 1;
  61. if (n > 0) memcpy(buf, s, n);
  62. buf[n] = 0;
  63. }
  64. long m2fopenread(long *desc)
  65. {
  66. char buf[4096];
  67. m2cstr(desc, buf, sizeof buf);
  68. return (long)fopen(buf, "r");
  69. }
  70. long m2fopenwrite(long *desc)
  71. {
  72. char buf[4096];
  73. m2cstr(desc, buf, sizeof buf);
  74. return (long)fopen(buf, "w");
  75. }
  76. long m2fclose(long h)
  77. {
  78. if (h != 0) fclose((FILE *)h);
  79. return 0;
  80. }
  81. /* A handle of 0 means the open failed (Modula-2 NIL). Every file
  82. operation degrades to a safe no-op instead of dereferencing NULL. */
  83. long m2fgetc(long h)
  84. {
  85. int c;
  86. if (h == 0) return 0;
  87. c = fgetc((FILE *)h);
  88. return (c == EOF) ? 0 : c;
  89. }
  90. long m2fputc(long h, long c)
  91. {
  92. if (h != 0) fputc((int)c, (FILE *)h);
  93. return 0;
  94. }
  95. long m2fputs(long h, long *desc)
  96. {
  97. char *s = (char *)(desc + 1);
  98. size_t n = strnlen(s, (size_t)desc[0] + 1);
  99. if (h == 0) return 0;
  100. if (n > 0) fwrite(s, 1, n, (FILE *)h);
  101. return (long)n;
  102. }
  103. long m2freadint(long h)
  104. {
  105. long v = 0;
  106. int c, neg = 0;
  107. if (h == 0) return 0;
  108. do { c = fgetc((FILE *)h); } while (c == ' ' || c == '\t' ||
  109. c == '\n' || c == '\r');
  110. if (c == '-') { neg = 1; c = fgetc((FILE *)h); }
  111. while (c >= '0' && c <= '9') {
  112. v = v * 10 + (c - '0');
  113. c = fgetc((FILE *)h);
  114. }
  115. return neg ? -v : v;
  116. }
  117. long m2fwriteint(long h, long v)
  118. {
  119. char buf[32];
  120. int i = 0, neg = 0;
  121. unsigned long u;
  122. if (h == 0) return 0;
  123. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  124. else u = (unsigned long)v;
  125. do { buf[i++] = (char)('0' + u % 10); u /= 10; } while (u != 0);
  126. if (neg) buf[i++] = '-';
  127. while (i > 0) fputc(buf[--i], (FILE *)h);
  128. return 0;
  129. }
  130. long m2fwriteln(long h)
  131. {
  132. if (h != 0) fputc('\n', (FILE *)h);
  133. return 0;
  134. }
  135. /* Read one line (up to and including '\n', or EOF) into the
  136. descriptor's data area, dropping the newline; NUL-terminated.
  137. The descriptor's capacity header is left intact (so a caller may
  138. keep using HIGH), and the number of bytes stored is returned.
  139. `max` is declared `int`: Modula-2 CARDINAL arguments arrive in a
  140. 32-bit register (QBE `w`), so only the low 32 bits are defined. */
  141. long m2freadline(long h, long *desc, int max)
  142. {
  143. char *dst = (char *)(desc + 1);
  144. long n = 0;
  145. int c;
  146. if (max < 0) max = 0;
  147. if (h == 0) { if (max > 0) dst[0] = 0; return 0; }
  148. while (n < max) {
  149. c = fgetc((FILE *)h);
  150. if (c == EOF) break;
  151. if (c == '\n') break;
  152. if (c == '\r') continue;
  153. dst[n++] = (char)c;
  154. }
  155. dst[n] = 0;
  156. return n;
  157. }
  158. /* Bulk read: up to max bytes into the descriptor's data area; the
  159. descriptor's header is set to the number actually read. */
  160. long m2fread(long h, long *desc, long max)
  161. {
  162. long n;
  163. if (max < 0) max = 0;
  164. if (h == 0) { desc[0] = 0; return 0; }
  165. n = (long)fread((char *)(desc + 1), 1, (size_t)max, (FILE *)h);
  166. desc[0] = n;
  167. return n;
  168. }
  169. /* Write a signed integer right-justified in a field of width `wid`.
  170. wid = 0 means exactly one leading space (FileIO convention). */
  171. long m2fwriteintw(long h, long v, long wid)
  172. {
  173. char buf[64];
  174. int n;
  175. if (h == 0) return 0;
  176. if (wid == 0)
  177. n = snprintf(buf, sizeof buf, " %ld", v);
  178. else
  179. n = snprintf(buf, sizeof buf, "%*ld", (int)wid, v);
  180. if (n > 0) fwrite(buf, 1, (size_t)n, (FILE *)h);
  181. return 0;
  182. }
  183. /* Byte offset of the current position in h (-1 on error). */
  184. long m2ftell(long h)
  185. {
  186. if (h == 0) return -1;
  187. return (long)ftell((FILE *)h);
  188. }
  189. /* Seek h to absolute byte offset pos; 1 on success, 0 on failure.
  190. Rewinding also clears the stdio error/EOF state. */
  191. long m2fseek(long h, long pos)
  192. {
  193. if (h == 0) return 0;
  194. if (fseek((FILE *)h, (long)pos, SEEK_SET) != 0) return 0;
  195. clearerr((FILE *)h);
  196. return 1;
  197. }
  198. /* Standard streams as file handles. */
  199. long m2stdout(void) { return (long)stdout; }
  200. long m2stderr(void) { return (long)stderr; }
  201. long m2stdin(void) { return (long)stdin; }
  202. /* Command-line arguments, read from /proc/self/cmdline (Linux). */
  203. static char *m2_args[1024];
  204. static long m2_nargs = -1;
  205. static void m2_argv_init(void)
  206. {
  207. static char cmd[65536];
  208. FILE *f;
  209. long i, n;
  210. int c;
  211. if (m2_nargs >= 0) return;
  212. m2_nargs = 0;
  213. f = fopen("/proc/self/cmdline", "rb");
  214. if (f == NULL) return;
  215. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  216. fclose(f);
  217. if (n <= 0) return;
  218. cmd[n] = 0;
  219. i = 0;
  220. while (i < n && m2_nargs < 1024) {
  221. while (i < n && cmd[i] == 0) i++; /* skip separators */
  222. if (i >= n) break;
  223. m2_args[m2_nargs++] = &cmd[i];
  224. while (i < n && cmd[i] != 0) i++; /* to end of arg */
  225. }
  226. }
  227. long m2argc(void)
  228. {
  229. m2_argv_init();
  230. return m2_nargs;
  231. }
  232. long m2arg(long i, long *desc, long cap)
  233. {
  234. const char *a;
  235. long n;
  236. m2_argv_init();
  237. if (i < 0 || i >= m2_nargs) { ((char *)(desc + 1))[0] = 0; return 0; }
  238. a = m2_args[i];
  239. n = (long)strlen(a);
  240. if (cap > 0 && n >= cap) n = cap - 1;
  241. if (n < 0) n = 0;
  242. memcpy((char *)(desc + 1), a, (size_t)n);
  243. if (n < cap) ((char *)(desc + 1))[n] = 0; /* NUL terminator */
  244. /* desc[0] (capacity) is left intact: length is derived by scanning */
  245. return 1;
  246. }
  247. /* Set a string descriptor's logical length (header). */
  248. long m2setlen(long *desc, long n)
  249. {
  250. desc[0] = n;
  251. return n;
  252. }
  253. /* Write a single character (byte) to fd 1. */
  254. long m2writechar(long c)
  255. {
  256. char ch = (char)c;
  257. write(1, &ch, 1);
  258. return 0;
  259. }
  260. /* Write a signed decimal integer to fd 1. */
  261. long m2writeint(long v)
  262. {
  263. char buf[32];
  264. int i = 0;
  265. int neg = 0;
  266. unsigned long u;
  267. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  268. else u = (unsigned long)v;
  269. do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
  270. if (neg) buf[i++] = '-';
  271. while (i > 0)
  272. write(1, &buf[--i], 1);
  273. return 0;
  274. }
  275. /* Terminate the process. */
  276. long m2exit(long code)
  277. {
  278. exit((int)code);
  279. return 0;
  280. }
  281. /* ---------------- clock (SysClock) ---------------- */
  282. /* Wall-clock time in seconds since the Unix epoch. */
  283. long m2time(void)
  284. {
  285. return (long)time(NULL);
  286. }
  287. /* Processor time used so far, in clock ticks (CLOCKS_PER_SEC per
  288. second). The scaling is exposed as a constant by SysClock. */
  289. long m2clock(void)
  290. {
  291. return (long)clock();
  292. }
  293. /* String content equality: 1 when the NUL-terminated contents of the
  294. two descriptors match. */
  295. long m2streq(long *a, long *b)
  296. {
  297. return (strcmp((char *)(a + 1), (char *)(b + 1)) == 0) ? 1 : 0;
  298. }
  299. /* Number of characters before the NUL. */
  300. long m2strlen(long *s)
  301. {
  302. return (long)strlen((char *)(s + 1));
  303. }
  304. /* UString length: the codepoint count is the descriptor's header. */
  305. long m2ustrlen(long *s)
  306. {
  307. return s[0];
  308. }
  309. /* Build a 1-codepoint UString descriptor from a UCHAR value; returns
  310. its address. A small rotating pool lets an expression supply both
  311. operands to m2ustrcat before it reads them. */
  312. long m2ustrfrom(long cp)
  313. {
  314. static struct { long count; int cp; } pool[16];
  315. static int next = 0;
  316. long *d = (long *)&pool[next];
  317. pool[next].count = 1;
  318. pool[next].cp = (int)cp;
  319. next = (next + 1) & 15;
  320. return (long)d;
  321. }
  322. /* Concatenate two UString descriptors into a static buffer; returns
  323. the result descriptor address. The value is valid until the next
  324. m2ustrcat call. */
  325. long m2ustrcat(long *a, long *b)
  326. {
  327. static struct { long count; int cp[4096]; } buf;
  328. int *pa = (int *)(a + 1);
  329. int *pb = (int *)(b + 1);
  330. long na = a[0], nb = b[0], i;
  331. if (na < 0) na = 0;
  332. if (nb < 0) nb = 0;
  333. if (na > 4096) na = 4096;
  334. if (nb > 4096 - na) nb = 4096 - na;
  335. for (i = 0; i < na; i++) buf.cp[i] = pa[i];
  336. for (i = 0; i < nb; i++) buf.cp[na + i] = pb[i];
  337. buf.count = na + nb;
  338. return (long)&buf;
  339. }
  340. /* Copy a UString descriptor (count + 4-byte codepoints) into the
  341. UCHAR array at dst (capacity dst[0]): min(count, capacity)
  342. codepoints, followed by a 0 codepoint terminator. Returns the
  343. number of codepoints copied. */
  344. long m2uassign(long *dst, long *src)
  345. {
  346. /* Both descriptors are { l count, w codepoints... }: the header is
  347. 64-bit, each codepoint is 32-bit. */
  348. int *d = (int *)(dst + 1);
  349. int *s = (int *)(src + 1);
  350. long cap = dst[0];
  351. long n = src[0];
  352. long i;
  353. if (cap < 0) cap = 0;
  354. if (n > cap) n = cap;
  355. for (i = 0; i < n; i++) d[i] = s[i];
  356. d[n] = 0;
  357. return n;
  358. }
  359. /* Copy the content of src into the CHAR array at dst (capacity
  360. dst[0]); truncated to fit and NUL-terminated. A CHAR array has
  361. `cap` content slots plus a reserved terminator slot at index `cap`,
  362. so up to `cap` characters fit. Returns the length copied. */
  363. long m2strassign(long *dst, long *src)
  364. {
  365. char *d = (char *)(dst + 1);
  366. const char *s = (const char *)(src + 1);
  367. long cap = dst[0];
  368. size_t n;
  369. if (cap < 0) return 0;
  370. n = strlen(s);
  371. if (n > (size_t)cap) n = (size_t)cap;
  372. memmove(d, s, n);
  373. d[n] = 0;
  374. return (long)n;
  375. }
  376. /* Concatenate two string descriptors into a static buffer; returns the
  377. result descriptor address. The value is valid until the next
  378. m2strcat call (so copy it before concatenating again). */
  379. long m2strcat(long *a, long *b)
  380. {
  381. static struct { long count; char data[8192]; } buf;
  382. const char *sa = (const char *)(a + 1);
  383. const char *sb = (const char *)(b + 1);
  384. size_t na = strlen(sa), nb = strlen(sb);
  385. if (na > sizeof buf.data - 1) na = sizeof buf.data - 1;
  386. if (nb > sizeof buf.data - 1 - na)
  387. nb = sizeof buf.data - 1 - na;
  388. memmove(buf.data, sa, na);
  389. memmove(buf.data + na, sb, nb);
  390. buf.data[na + nb] = 0;
  391. buf.count = (long)(na + nb);
  392. return (long)&buf;
  393. }
  394. /* ---------------- reals (Conversions / RealIO) ---------------- */
  395. /* Format a REAL into a descriptor string (6 significant digits,
  396. scientific). desc[0] is the capacity; on return it holds the
  397. length. Returns the number of characters written. */
  398. long m2realstr(double x, long *desc)
  399. {
  400. char buf[64];
  401. char *dst = (char *)(desc + 1);
  402. long cap = desc[0];
  403. int n = snprintf(buf, sizeof buf, "%.6E", x);
  404. if (n < 0) n = 0;
  405. if (cap > 0 && n >= (int)cap) n = (int)cap - 1;
  406. if (n > 0) memcpy(dst, buf, (size_t)n);
  407. if (cap > 0) { dst[n] = 0; desc[0] = n; }
  408. return (long)n;
  409. }
  410. /* Parse a REAL from a descriptor string; *ok := 1 on success. */
  411. double m2strreal(long *desc, long *ok)
  412. {
  413. char buf[4096];
  414. char *end;
  415. double v;
  416. m2cstr(desc, buf, sizeof buf);
  417. v = strtod(buf, &end);
  418. while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++;
  419. *ok = (end != buf && *end == 0) ? 1 : 0;
  420. return v;
  421. }
  422. /* Write a REAL right-justified in a field of width `wid` (0 => one
  423. leading space), matching the integer convention. */
  424. long m2writerealf(double x, long wid)
  425. {
  426. char buf[128];
  427. int n;
  428. if (wid == 0)
  429. n = snprintf(buf, sizeof buf, "%.6E", x);
  430. else
  431. n = snprintf(buf, sizeof buf, "%*.*E", (int)wid, 6, x);
  432. if (n > 0) write(1, buf, (size_t)n);
  433. return 0;
  434. }
  435. long m2writereal(double x)
  436. {
  437. return m2writerealf(x, 0);
  438. }
  439. /* Read a REAL from stdin. */
  440. double m2readreal(void)
  441. {
  442. double v = 0.0;
  443. if (scanf("%lf", &v) != 1) v = 0.0;
  444. return v;
  445. }