about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--subx/010core.cc6
-rw-r--r--subx/021translate.cc49
2 files changed, 47 insertions, 8 deletions
diff --git a/subx/010core.cc b/subx/010core.cc
index 90359162..b21511d7 100644
--- a/subx/010core.cc
+++ b/subx/010core.cc
@@ -99,7 +99,7 @@ End_of_program = 0;
 // helper for tests: load a program into memory from a textual representation
 // of its bytes, and run it
 void run(const string& text_bytes) {
-  load_program(text_bytes);
+  load_program(text_bytes, 1);  // tests always assume a starting address of 1
   EIP = 1;  // preserve null pointer
   while (EIP < End_of_program)
     run_one_instruction();
@@ -151,9 +151,9 @@ void run_one_instruction() {
   }
 }
 
-void load_program(const string& text_bytes) {
+void load_program(const string& text_bytes, uint32_t addr) {
   istringstream in(text_bytes);
-  load_program(in, 1);
+  load_program(in, addr);
 }
 void load_program(istream& in, uint32_t addr) {
   in >> std::noskipws;
diff --git a/subx/021translate.cc b/subx/021translate.cc
index 94553ae9..f262bcd6 100644
--- a/subx/021translate.cc
+++ b/subx/021translate.cc
@@ -1,18 +1,45 @@
+//: Beginnings of a nicer way to build SubX programs.
+//: We're going to question every notion, including "Assembly language" and
+//: "compiler".
+//: Motto: Abstract nothing, check everything.
+//:
+//: Workflow: read 'source' file as a single string. Run a series of
+//: transforms on it, each converting to a new string. The final string should
+//: be just machine code and comments, suitable to pass to load_program().
+
+:(before "End Types")
+typedef void (*transform_fn)(const string& input, string& output);
+:(before "End Globals")
+vector<transform_fn> Transform;
+
 :(before "End Includes")
 const int START = 0x08048000;
 :(before "End Main")
 if (is_equal(argv[1], "translate")) {
   assert(argc > 3);
-  ifstream in(argv[2]);
-  Mem.resize(1024);
-  load_program(in, 1);  // since we're not going to run it right now, we can load it anywhere
-  dump_elf(argv[3]);
+  string program;
+  slurp(argv[2], program);
+  perform_all_transforms(program);
+  dump_elf(program, argv[3]);
 }
 
 :(code)
+void perform_all_transforms(string& program) {
+  string& in = program;
+  string out;
+  for (int t = 0;  t < SIZE(Transform);  ++t, in.swap(out), out.clear())
+    (*Transform.at(t))(in, out);
+}
+
 // write out the current Memory contents from address 1 to End_of_program to a
 // bare-bones ELF file with a single section/segment and a hard-coded origin address.
-void dump_elf(const char* filename) {
+void dump_elf(const string& program, const char* filename) {
+  Mem.resize(1024);
+  // load program into memory, filtering out comments
+  load_program(program, 1);  // Not where 'program' should be loaded for running.
+                             // But we're not going to run it right now, so we
+                             // can load it anywhere.
+  // dump contents of memory into ELF binary
   ofstream out(filename, ios::binary);
   dump_elf_header(out);
   for (size_t i = 1;  i < End_of_program;  ++i) {
@@ -93,5 +120,17 @@ void dump_elf_header(ostream& out) {
 #undef O
 }
 
+void slurp(const char* filename, string& out) {
+  ifstream fin(filename);
+  fin >> std::noskipws;
+  ostringstream fout;
+  char c = '\0';
+  while(has_data(fin)) {
+    fin >> c;
+    fout << c;
+  }
+  fout.str().swap(out);
+}
+
 :(before "End Includes")
 using std::ios;
2019-07-16 23:40:25 -0700 5408' href='/akkartik/mu/commit/subx/077slurp.subx?h=hlt&id=c2a74205d690862d4df31682aa5bbe992227fda1'>c2a74205 ^
33352536 ^

c2a74205 ^


33352536 ^



c2a74205 ^
33352536 ^

c2a74205 ^


33352536 ^
c2a74205 ^
33352536 ^


6070c23e ^
c2a74205 ^
71eb22a5 ^
33352536 ^

2a2a5b1e ^
33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^


33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^
33352536 ^




7a583220 ^
33352536 ^

c2a74205 ^









33352536 ^
13ef4258 ^
33352536 ^
7dac9ade ^
c2a74205 ^









33352536 ^
686a52bd ^
c2a74205 ^
686a52bd ^
c2a74205 ^


33352536 ^
c2a74205 ^





33352536 ^
c2a74205 ^






33352536 ^
c2a74205 ^
33352536 ^
c2a74205 ^





33352536 ^
c2a74205 ^







33352536 ^
c2a74205 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158