diff --git a/2011/richards/Makefile b/2011/richards/Makefile index 493115069..f7164801f 100644 --- a/2011/richards/Makefile +++ b/2011/richards/Makefile @@ -61,7 +61,7 @@ CDEFINE= # Include files that are needed to compile # -CINCLUDE= +CINCLUDE= -include stdlib.h -include string.h -include stdio.h -include ctype.h # Optimization # diff --git a/2011/richards/README.md b/2011/richards/README.md index 66262cd3c..28fbef457 100644 --- a/2011/richards/README.md +++ b/2011/richards/README.md @@ -1,7 +1,7 @@ # Most surprisingly portable - Gregor Richards - +Gregor Richards + ## To build: @@ -21,6 +21,9 @@ make echo '10 9 8 7 6 5 4 3 2 1*p*p*p*p*p*p*p*p*p' | ./richards ``` +NOTE: this entry does not work in macOS. See [bugs.md](/bugs.md) for more +details. + ## Judges' remarks: Oh no, yet another dc-style calculator? (see 2000/dlowe using an embedded Perl @@ -34,7 +37,7 @@ What would you do to prove or disprove his claim without attempting to unravel t This is an implementation of the classic UNIX dc command, the reverse-polish calculator. Actually it's a pretty bad implementation, since it only implements 32-bit integers (well, sizeof(int)-sized integers). It only implements the -commands +, -, *, /, p (only for numbers), register stacks, macros and +commands `+`, `-`, `*`, `/`, `p` (only for numbers), register stacks, macros and comparisons. Wow, you'd think I could've done better in 2K of code. OH! I forgot to mention! It's a JIT. @@ -48,19 +51,19 @@ integers are at least 32-bit. You will probably get warnings while compiling the code. In particular: * If your system requires the inclusion of alloca.h to use alloca, use the - compile flag -DAH - * If your system cannot use mmap, use the compile flag -DNM + compile flag `-DAH`. + * If your system cannot use mmap, use the compile flag `-DNM`. * I don't include any headers I don't strictly need to, so you'll get warnings - about (at least) memcpy, isdigit, atoi, calloc, malloc and alloca if you - didn't use -DAH. - * GCC likes to complain about while (a = b) (assignments as a condition). - * I don't return from main, even though it's declared to return int. + about (at least) `memcpy()`, `isdigit()`, `atoi()`, `calloc()`, `malloc()` + and `alloca()` if you didn't use `-DAH`. + * GCC likes to complain about `while (a = b)` (assignments as a condition). + * I don't return from `main()`, even though it's declared to return int. Platforms on which all known/tested optimization levels work with no additional flags: - * gcc 4.6.2 + x86[_64] + GNU/Linux, Mac OS X or Windows - * clang 2.9-16 + x86[_64] + GNU/Linux or Mac OS X + * gcc 4.6.2 + x86[\_64] + GNU/Linux, Mac OS X or Windows + * clang 2.9-16 + x86[\_64] + GNU/Linux or Mac OS X * tcc 0.9.25 * gcc 4.4.5 + alpha, arm, mips[el], powerpc[64], s390x (IBM zSeries) * gcc 4.1.3 + vax + NetBSD @@ -68,7 +71,7 @@ flags: Platforms which work but not at all optimization levels or needing special flags: - * owcc 1.9 + x86 + Windows: Needs -DNM -DAH -fno-stack-check + * owcc 1.9 + x86 + Windows: Needs `-DNM -DAH -fno-stack-check` * gcc 4.4.5 + sh4: -O0 works, higher optimization levels don't work for unknown reasons. ## Copyright and CC BY-SA 4.0 License: diff --git a/2011/richards/richards.c b/2011/richards/richards.c index cc74c5a6b..40a4fddb8 100644 --- a/2011/richards/richards.c +++ b/2011/richards/richards.c @@ -192,7 +192,7 @@ u main() #if NM malloc(H); #else - mmap(0, H, -1, MAP_PRIVATE | MAP_ANON, -1, 0); + mmap(NULL, H, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); #endif while (b = fgets(malloc(999), 999, stdin)) ((f) K(b, d)) (d); diff --git a/bugs.md b/bugs.md index 88f57f4d7..c79202063 100644 --- a/bugs.md +++ b/bugs.md @@ -1829,6 +1829,33 @@ instead being something else entirely. The Internet Wayback Machine, although it archived it, did not load scripts. Do you know if the domain was moved? Do you have an archive or mirror? Please provide us it! Thank you. +## [2011/richards](2011/richards/richards.c) ([README.md](2011/richards/README.md)) +## STATUS: doesn't work with some platforms - please help us fix + +This does not appear to work with macOS. We're not sure if this is to do with +the Apple silicon chip or not. At a glance it appears to be that it might be the +function pointers. In particular it seems to crash at: + +```c + + W(1) while ((c = *p)) { +``` + +in the `K` function. It's called like this, from main(): + +```c + while (b = fgets(malloc(999), 999, stdin)) + ((f) K(b, d)) (d); +``` + +But what is curious is that it seems to have different problems depending on the +macros defined. In an attempt to get it to work the `mmap()` call was changed to +allow for exec and read and write (which might be a problem in such a +combination) but this only works in linux (it already did work in linux, +however, so this doesn't help much). + +Do you have a fix? We welcome it! + # 2012