//: Allow code for recipes to be pulled in from multiple places and inserted //: at special labels called 'waypoints' using two new top-level commands: //: before //: after //: Most labels are local: they must be unique to a recipe, and are invisible //: outside the recipe. However, waypoints are global: a recipe can have //: multiple of them, you can't use them as jump targets. :(before "End is_jump_target Special-cases") if (is_waypoint(label)) return false; //: Waypoints are always surrounded by '<>', e.g. . :(code) bool is_waypoint(string label) { return *label.begin() == '<' && *label.rbegin() == '>'; } :(scenario tangle_before) def main [ 1:num <- copy 0 3:num <- copy 0 ] before [ 2:num <- copy 0 ] +mem: storing 0 in location 1 +mem: storing 0 in location 2 +mem: storing 0 in location 3 # nothing else $mem: 3 //: while loading recipes, load before/after fragments :(before "End Globals") map Before_fragments, After_fragments; set Fragments_used; :(before "End Setup") Before_fragments.clear(); After_fragments.clear(); Fragments_used.clear(); :(before "End Command Handlers") else if (command == "before") { string label = next_word(in); if (label.empty()) { assert(!has_data(in)); raise << "incomplete 'before' block at end of file\n" << end(); return result; } recipe tmp; slurp_body(in, tmp); if (is_waypoint(label)) Before_fragments[label].steps.insert(Before_fragments[label].steps.end(), tmp.steps.begin(), tmp.steps.end()); else raise << "can't tangle before non-waypoint " << label << '\n' << end(); // End before Command Handler } else if (command == "after") { string label = next_word(in); if (label.empty()) { assert(!has_data(in)); raise << "incomplete 'after' block at end of file\n" << end(); return result; } recipe tmp; slurp_body(in, tmp); if (is_waypoint(label)) After_fragments[label].steps.insert(After_fragments[label].steps.begin(), tmp.steps.begin(), tmp.steps.end()); else raise << "can't tangle after non-waypoint " << label << '\n' << end(); // End after Command Handler } //: after all recipes are loaded, insert fragments at appropriate labels. :(after "Begin Instruction Inserting/Deleting Transforms") Transform.push_back(insert_fragments); // NOT idempotent //: We might need to perform multiple passes, in case inserted fragments //: include more labels that need further insertions. Track which labels we've //: already processed using an extra field. :(before "End instruction Fields") mutable bool tangle_done; :(before "End instruction Constructor") tangle_done = false; :(code) void insert_fragments(const recipe_ordinal r) { insert_fragments(get(Recipe, r)); } void insert_fragments(recipe& r) { trace(9991, "transform") << "--- insert fragments into recipe " << r.name << end(); bool made_progress = true; int pass = 0; while (made_progress) { made_progress = false; // create a new vector because insertions invalidate iterators vector result; for (int i = 0; i < SIZE(r.steps); ++i) { const instruction& inst = r.steps.at(i); if (!inst.is_label || !is_waypoint(inst.label) || inst.tangle_done) { result.push_back(inst); continue; } inst.tangle_done = true; made_progress = true; Fragments_used.insert(inst.label); ostringstream prefix; prefix << '+' << r.name << '_' << pass << '_' << i; // ok to use contains_key even though Before_fragments uses [], // because appending an empty recipe is a noop if (contains_key(Before_fragments, inst.label)) { trace(9992, "transform") << "insert fragments before label " << inst.label << end(); append_fragment(result, Before_fragments[inst.label].steps, prefix.str()); } result.push_back(inst); if (contains_key(After_fragments, inst.label)) { trace(9992, "transform") << "insert fragments after label " << inst.label << end(); append_fragment(result, After_fragments[inst.label].steps, prefix.str()); } } r.steps.swap(result); ++pass; } } void append_fragment(vector& base, const vector& patch, const string prefix) { // append 'patch' to 'base' while keeping 'base' oblivious to any new jump // targets in 'patch' oblivious to 'base' by prepending 'prefix' to them. // we might tangle the same fragment at multiple points in a single recipe, // and we need to avoid duplicate jump targets. // so we'll keep jump targets local to the specific before/after fragment // that introduces them. set jump_targets; for (int i = 0; i < SIZE(patch); ++i) { const instruction& inst = patch.at(i); if (inst.is_label && is_jump_target(inst.label)) jump_targets.insert(inst.label); } for (int i = 0; i < SIZE(patch); ++i) { instruction inst =
# Example reading commandline arguments: compute length of first arg.
#
# To run:
#   $ ./bootstrap translate init.linux apps/ex8.subx -o apps/ex8
#   $ ./bootstrap run apps/ex8 abc de fghi
# Expected result:
#   $ echo $?
#   3  # length of 'abc'
#
# At the start of a SubX program:
#   argc: *esp
#   argv[0]: *(esp+4)
#   argv[1]: *(esp+8)
#   ...
# Locals start from esp-4 downwards.

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

Entry:
    # . prologue
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # eax = ascii-length(argv[1])
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  ascii-length/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp

    # exit(eax)
    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
    e8/call  syscall_exit/disp32

ascii-length:  # s: (addr array byte) -> n/eax
    # edx = s
    8b/copy