2015-04-24 03:35:31 +00:00
|
|
|
//: Everything this project/binary supports.
|
|
|
|
//: This should give you a sense for what to look forward to in later layers.
|
|
|
|
|
2015-04-22 20:29:02 +00:00
|
|
|
:(before "End Commandline Parsing")
|
|
|
|
if (argc <= 1 || is_equal(argv[1], "--help")) {
|
2016-12-12 18:01:12 +00:00
|
|
|
//: this is the functionality later layers will provide
|
2015-04-22 20:29:02 +00:00
|
|
|
// currently no automated tests for commandline arg parsing
|
2019-07-27 23:01:55 +00:00
|
|
|
cerr << get(Help, "usage");
|
2015-04-22 20:29:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-12 22:53:48 +00:00
|
|
|
//: Support for option parsing.
|
|
|
|
//: Options always begin with '--' and are always the first arguments. An
|
|
|
|
//: option will never follow a non-option.
|
|
|
|
char** arg = &argv[1];
|
|
|
|
while (argc > 1 && starts_with(*arg, "--")) {
|
|
|
|
if (false)
|
|
|
|
; // no-op branch just so any further additions can consistently always start with 'else'
|
|
|
|
// End Commandline Options(*arg)
|
|
|
|
else
|
|
|
|
cerr << "skipping unknown option " << *arg << '\n';
|
2016-10-20 05:10:35 +00:00
|
|
|
--argc; ++argv; ++arg;
|
2016-08-12 22:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 23:01:55 +00:00
|
|
|
if (is_equal(argv[1], "help")) {
|
|
|
|
if (argc == 2) {
|
|
|
|
cerr << "help on what?\n";
|
|
|
|
help_contents();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
string key(argv[2]);
|
|
|
|
// End Help Special-cases(key)
|
|
|
|
if (contains_key(Help, key)) {
|
|
|
|
cerr << get(Help, key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cerr << "No help found for '" << key << "'\n";
|
|
|
|
help_contents();
|
|
|
|
cerr << "Please check your command for typos.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:(code)
|
|
|
|
void help_contents() {
|
|
|
|
cerr << "Available top-level topics:\n";
|
|
|
|
cerr << " usage\n";
|
|
|
|
// End Help Contents
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Globals")
|
|
|
|
map<string, string> Help;
|
|
|
|
:(before "End Includes")
|
|
|
|
#include <map>
|
|
|
|
using std::map;
|
|
|
|
:(before "End One-time Setup")
|
|
|
|
init_help();
|
|
|
|
:(code)
|
|
|
|
void init_help() {
|
|
|
|
put(Help, "usage",
|
2020-01-02 10:14:48 +00:00
|
|
|
"bootstrap: the bootstrap translator for SubX.\n"
|
|
|
|
"This program also wraps some miscellaneous useful functionality:\n"
|
|
|
|
" - an x86 emulator: `bootstrap run`\n"
|
|
|
|
" - online help: `bootstrap help`\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"\n"
|
2020-01-02 09:41:55 +00:00
|
|
|
"== Ways to invoke bootstrap\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"- See this message:\n"
|
2020-01-02 09:41:55 +00:00
|
|
|
" bootstrap --help\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"- Convert a textual SubX program into a standard ELF binary that you can\n"
|
|
|
|
" run on your computer:\n"
|
2020-01-02 09:41:55 +00:00
|
|
|
" bootstrap translate input1.subx input2.subx ... -o <output ELF binary>\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"- Run a SubX binary using SubX itself (for better error messages):\n"
|
2020-01-02 09:41:55 +00:00
|
|
|
" bootstrap run <ELF binary>\n"
|
2020-01-02 23:11:10 +00:00
|
|
|
"- Run all bootstrap's unit tests:\n"
|
|
|
|
" bootstrap test\n"
|
|
|
|
"- Run a single unit test:\n"
|
|
|
|
" bootstrap test <test name>\n"
|
|
|
|
" e.g. bootstrap test test_copy_imm32_to_EAX\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"\n"
|
|
|
|
"== Debugging aids\n"
|
2019-11-27 05:42:42 +00:00
|
|
|
"- Add '--trace' to any of these commands to save a trace to disk at the end.\n"
|
|
|
|
" This can run out of memory for long-running commands.\n"
|
2020-04-09 07:45:36 +00:00
|
|
|
"- Add '--debug' to emit additional debug information during translation.\n"
|
|
|
|
" 'bootstrap --debug translate' will save metadata to disk that\n"
|
|
|
|
" 'bootstrap --trace run' uses to make traces more informative.\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
"\n"
|
|
|
|
"Options starting with '--' must always come before any other arguments.\n"
|
|
|
|
"\n"
|
|
|
|
"To start learning how to write SubX programs, see Readme.md (particularly\n"
|
|
|
|
"the section on the x86 instruction set) and then run:\n"
|
2020-01-02 09:41:55 +00:00
|
|
|
" bootstrap help\n"
|
2019-07-27 23:01:55 +00:00
|
|
|
);
|
|
|
|
// End Help Texts
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:29:02 +00:00
|
|
|
:(code)
|
|
|
|
bool is_equal(char* s, const char* lit) {
|
|
|
|
return strncmp(s, lit, strlen(lit)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-08-13 00:26:22 +00:00
|
|
|
bool starts_with(const string& s, const string& pat) {
|
2016-09-25 02:55:48 +00:00
|
|
|
string::const_iterator a=s.begin(), b=pat.begin();
|
2016-10-20 05:10:35 +00:00
|
|
|
for (/*nada*/; a!=s.end() && b!=pat.end(); ++a, ++b)
|
2016-09-25 02:55:48 +00:00
|
|
|
if (*a != *b) return false;
|
|
|
|
return b == pat.end();
|
2016-08-13 00:26:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 23:47:49 +00:00
|
|
|
//: I'll throw some style conventions here for want of a better place for them.
|
|
|
|
//: As a rule I hate style guides. Do what you want, that's my motto. But since
|
|
|
|
//: we're dealing with C/C++, the one big thing we want to avoid is undefined
|
|
|
|
//: behavior. If a compiler ever encounters undefined behavior it can make
|
|
|
|
//: your program do anything it wants.
|
|
|
|
//:
|
|
|
|
//: For reference, my checklist of undefined behaviors to watch out for:
|
|
|
|
//: out-of-bounds access
|
|
|
|
//: uninitialized variables
|
|
|
|
//: use after free
|
|
|
|
//: dereferencing invalid pointers: null, a new of size 0, others
|
|
|
|
//:
|
|
|
|
//: casting a large number to a type too small to hold it
|
|
|
|
//:
|
|
|
|
//: integer overflow
|
|
|
|
//: division by zero and other undefined expressions
|
|
|
|
//: left-shift by negative count
|
|
|
|
//: shifting values by more than or equal to the number of bits they contain
|
|
|
|
//: bitwise operations on signed numbers
|
|
|
|
//:
|
|
|
|
//: Converting pointers to types of different alignment requirements
|
|
|
|
//: T* -> void* -> T*: defined
|
|
|
|
//: T* -> U* -> T*: defined if non-function pointers and alignment requirements are same
|
|
|
|
//: function pointers may be cast to other function pointers
|
|
|
|
//:
|
|
|
|
//: Casting a numeric value into a value that can't be represented by the target type (either directly or via static_cast)
|
|
|
|
//:
|
|
|
|
//: To guard against these, some conventions:
|
|
|
|
//:
|
|
|
|
//: 0. Initialize all primitive variables in functions and constructors.
|
|
|
|
//:
|
|
|
|
//: 1. Minimize use of pointers and pointer arithmetic. Avoid 'new' and
|
|
|
|
//: 'delete' as far as possible. Rely on STL to perform memory management to
|
|
|
|
//: avoid use-after-free issues (and memory leaks).
|
|
|
|
//:
|
|
|
|
//: 2. Avoid naked arrays to avoid out-of-bounds access. Never use operator[]
|
|
|
|
//: except with map. Use at() with STL vectors and so on.
|
|
|
|
//:
|
|
|
|
//: 3. Valgrind all the things.
|
|
|
|
//:
|
|
|
|
//: 4. Avoid unsigned numbers. Not strictly an undefined-behavior issue, but
|
|
|
|
//: the extra range doesn't matter, and it's one less confusing category of
|
|
|
|
//: interaction gotchas to worry about.
|
|
|
|
//:
|
|
|
|
//: Corollary: don't use the size() method on containers, since it returns an
|
|
|
|
//: unsigned and that'll cause warnings about mixing signed and unsigned,
|
|
|
|
//: yadda-yadda. Instead use this macro below to perform an unsafe cast to
|
|
|
|
//: signed. We'll just give up immediately if a container's ever too large.
|
2016-05-08 20:12:39 +00:00
|
|
|
//: Basically, Mu is not concerned about this being a little slower than it
|
|
|
|
//: could be. (https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7)
|
2016-03-14 03:26:47 +00:00
|
|
|
//:
|
2016-05-08 20:12:39 +00:00
|
|
|
//: Addendum to corollary: We're going to uniformly use int everywhere, to
|
|
|
|
//: indicate that we're oblivious to number size, and since Clang on 32-bit
|
|
|
|
//: platforms doesn't yet support multiplication over 64-bit integers, and
|
|
|
|
//: since multiplying two integers seems like a more common situation to end
|
|
|
|
//: up in than integer overflow.
|
2015-05-17 09:22:41 +00:00
|
|
|
:(before "End Includes")
|
2016-03-14 03:26:47 +00:00
|
|
|
#define SIZE(X) (assert((X).size() < (1LL<<(sizeof(int)*8-2))), static_cast<int>((X).size()))
|
2016-06-02 20:39:45 +00:00
|
|
|
|
2016-06-02 16:15:40 +00:00
|
|
|
//: 5. Integer overflow is guarded against at runtime using the -ftrapv flag
|
2016-06-02 22:46:17 +00:00
|
|
|
//: to the compiler, supported by Clang (GCC version only works sometimes:
|
|
|
|
//: http://stackoverflow.com/questions/20851061/how-to-make-gcc-ftrapv-work).
|
2017-07-09 21:25:48 +00:00
|
|
|
:(before "atexit(reset)")
|
2016-06-02 22:46:17 +00:00
|
|
|
initialize_signal_handlers(); // not always necessary, but doesn't hurt
|
|
|
|
//? cerr << INT_MAX+1 << '\n'; // test overflow
|
|
|
|
//? assert(false); // test SIGABRT
|
|
|
|
:(code)
|
|
|
|
// based on https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c
|
|
|
|
void initialize_signal_handlers() {
|
|
|
|
struct sigaction action;
|
2016-06-02 23:24:27 +00:00
|
|
|
bzero(&action, sizeof(action));
|
2016-06-02 22:46:17 +00:00
|
|
|
action.sa_sigaction = dump_and_exit;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaction(SIGABRT, &action, NULL); // assert() failure or integer overflow on linux (with -ftrapv)
|
|
|
|
sigaction(SIGILL, &action, NULL); // integer overflow on OS X (with -ftrapv)
|
|
|
|
}
|
2018-07-26 03:47:41 +00:00
|
|
|
void dump_and_exit(int sig, siginfo_t* /*unused*/, void* /*unused*/) {
|
2016-06-02 22:46:17 +00:00
|
|
|
switch (sig) {
|
|
|
|
case SIGABRT:
|
|
|
|
#ifndef __APPLE__
|
2019-07-27 23:01:55 +00:00
|
|
|
cerr << "SIGABRT: might be an integer overflow if it wasn't an assert() failure\n";
|
2016-06-02 22:46:17 +00:00
|
|
|
_Exit(1);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case SIGILL:
|
|
|
|
#ifdef __APPLE__
|
|
|
|
cerr << "SIGILL: most likely caused by integer overflow\n";
|
|
|
|
_Exit(1);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:(before "End Includes")
|
|
|
|
#include <signal.h>
|
2016-06-02 20:39:45 +00:00
|
|
|
|
2015-11-06 19:06:58 +00:00
|
|
|
//: 6. Map's operator[] being non-const is fucking evil.
|
|
|
|
:(before "Globals") // can't generate prototypes for these
|
|
|
|
// from http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map
|
|
|
|
template<typename T> typename T::mapped_type& get(T& map, typename T::key_type const& key) {
|
|
|
|
typename T::iterator iter(map.find(key));
|
2019-07-27 23:01:55 +00:00
|
|
|
if (iter == map.end()) {
|
|
|
|
cerr << "get couldn't find key '" << key << "'\n";
|
|
|
|
assert(iter != map.end());
|
|
|
|
}
|
2015-11-06 19:06:58 +00:00
|
|
|
return iter->second;
|
|
|
|
}
|
2015-11-08 06:56:06 +00:00
|
|
|
template<typename T> typename T::mapped_type const& get(const T& map, typename T::key_type const& key) {
|
|
|
|
typename T::const_iterator iter(map.find(key));
|
2019-07-27 23:01:55 +00:00
|
|
|
if (iter == map.end()) {
|
|
|
|
cerr << "get couldn't find key '" << key << "'\n";
|
|
|
|
assert(iter != map.end());
|
|
|
|
}
|
2015-11-08 06:56:06 +00:00
|
|
|
return iter->second;
|
|
|
|
}
|
2015-11-06 19:06:58 +00:00
|
|
|
template<typename T> typename T::mapped_type const& put(T& map, typename T::key_type const& key, typename T::mapped_type const& value) {
|
2019-07-27 23:01:55 +00:00
|
|
|
map[key] = value;
|
|
|
|
return map[key];
|
2015-11-06 19:06:58 +00:00
|
|
|
}
|
2015-11-06 21:22:16 +00:00
|
|
|
template<typename T> bool contains_key(T& map, typename T::key_type const& key) {
|
|
|
|
return map.find(key) != map.end();
|
|
|
|
}
|
|
|
|
template<typename T> typename T::mapped_type& get_or_insert(T& map, typename T::key_type const& key) {
|
|
|
|
return map[key];
|
|
|
|
}
|
2019-07-27 23:01:55 +00:00
|
|
|
template<typename T> typename T::mapped_type const& put_new(T& map, typename T::key_type const& key, typename T::mapped_type const& value) {
|
|
|
|
assert(map.find(key) == map.end());
|
|
|
|
map[key] = value;
|
|
|
|
return map[key];
|
|
|
|
}
|
2015-11-06 21:22:16 +00:00
|
|
|
//: The contract: any container that relies on get_or_insert should never call
|
|
|
|
//: contains_key.
|
2016-06-02 20:39:45 +00:00
|
|
|
|
2015-11-17 09:21:00 +00:00
|
|
|
//: 7. istreams are a royal pain in the arse. You have to be careful about
|
|
|
|
//: what subclass you try to putback into. You have to watch out for the pesky
|
|
|
|
//: failbit and badbit. Just avoid eof() and use this helper instead.
|
2016-06-02 20:39:45 +00:00
|
|
|
:(code)
|
2015-11-17 09:21:00 +00:00
|
|
|
bool has_data(istream& in) {
|
|
|
|
return in && !in.eof();
|
|
|
|
}
|
2015-05-17 09:22:41 +00:00
|
|
|
|
2015-04-22 20:29:02 +00:00
|
|
|
:(before "End Includes")
|
2016-06-02 17:40:06 +00:00
|
|
|
#include <assert.h>
|
2015-05-06 01:15:09 +00:00
|
|
|
|
2016-06-02 17:40:06 +00:00
|
|
|
#include <iostream>
|
2015-04-22 20:29:02 +00:00
|
|
|
using std::istream;
|
|
|
|
using std::ostream;
|
|
|
|
using std::iostream;
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
2016-06-02 17:40:06 +00:00
|
|
|
#include <iomanip>
|
2015-04-22 20:29:02 +00:00
|
|
|
|
2016-08-20 06:07:06 +00:00
|
|
|
#include <string.h>
|
2016-06-02 17:40:06 +00:00
|
|
|
#include <string>
|
2015-04-22 20:29:02 +00:00
|
|
|
using std::string;
|
2016-06-02 23:20:43 +00:00
|
|
|
|
4089
Clean up how we reclaim local scopes.
It used to work like this (commit 3216):
1. Update refcounts of products after every instruction, EXCEPT:
a) when instruction is a non-primitive and the callee starts with
'local-scope' (because it's already not decremented in 'return')
OR:
b) when instruction is primitive 'next-ingredient' or
'next-ingredient-without-typechecking', and its result is saved to a
variable in the default space (because it's already incremented at
the time of the call)
2. If a function starts with 'local-scope', force it to be reclaimed
before each return. However, since locals may be returned, *very
carefully* don't reclaim those. (See the logic in the old `escaping`
and `should_update_refcount` functions.)
However, this approach had issues. We needed two separate commands for
'local-scope' (reclaim locals on exit) and 'new-default-space'
(programmer takes charge of reclaiming locals). The hard-coded
reclamation duplicated refcounting logic. In addition to adding
complexity, this implementation failed to work if a function overwrites
default-space after setting up a local-scope (the old default-space is
leaked). It also fails in the presence of continuations. Calling a
continuation more than once was guaranteed to corrupt memory (commit
3986).
After this commit, reclaiming local scopes now works like this:
Update refcounts of products for every PRIMITIVE instruction.
For non-primitive instructions, all the work happens in the `return`
instruction:
increment refcount of ingredients to `return`
(unless -- one last bit of ugliness -- they aren't saved in the
caller)
decrement the refcount of the default-space
use existing infrastructure for reclaiming as necessary
if reclaiming default-space, first decrement refcount of each
local
again, use existing infrastructure for reclaiming as necessary
This commit (finally!) completes the bulk[1] of step 2 of the plan in
commit 3991. It was very hard until I gave up trying to tweak the
existing implementation and just test-drove layer 43 from scratch.
[1] There's still potential for memory corruption if we abuse
`default-space`. I should probably try to add warnings about that at
some point (todo in layer 45).
2017-10-23 06:14:19 +00:00
|
|
|
#include <algorithm>
|
|
|
|
using std::min;
|
|
|
|
using std::max;
|