https://github.com/akkartik/mu/blob/master/subx/038---literal_strings.cc
  1 //: Allow instructions to mention literals directly.
  2 //:
  3 //: This layer will transparently move them to the global segment (assumed to
  4 //: always be the second segment).
  5 
  6 :(scenario transform_literal_string)
  7 == code
  8 b8/copy  "test"/imm32
  9 == data  # need to manually create this for now
 10 +transform: -- move literal strings to data segment
 11 +transform: adding global variable '__subx_global_1' containing "test"
 12 +transform: instruction after transform: 'b8 __subx_global_1'
 13 
 14 //: We don't rely on any transforms running in previous layers, but this layer
 15 //: knows about labels and global variables and will emit them for previous
 16 //: layers to transform.
 17 :(after "Begin Transforms")
 18 // Begin Level-3 Transforms
 19 Transform.push_back(transform_literal_strings);
 20 // End Level-3 Transforms
 21 
 22 :(before "End Globals")
 23 int Next_auto_global = 1;
 24 :(code)
 25 void transform_literal_strings(program& p) {
 26   trace(3, "transform") << "-- move literal strings to data segment" << end();
 27   if (p.segments.empty()) return;
 28   segment& code = p.segments.at(0);
 29   segment data;
 30   for (int i = 0;  i < SIZE(code.lines);  ++i) {
 31     line& inst = code.lines.at(i);
 32     for (int j = 0;  j < SIZE(inst.words);  ++j) {
 33       word& curr = inst.words.at(j);
 34       if (curr.data.at(0) != '"') continue;
 35       ostringstream global_name;
 36       global_name << "__subx_global_" << Next_auto_global;
 37       ++Next_auto_global;
 38       add_global_to_data_segment(global_name.str(), curr, data);
 39       curr.data = global_name.str();
 40     }
 41     trace(99, "transform") << "instruction after transform: '" << data_to_string(inst) << "'" << end();
 42   }
 43   if (data.lines.empty()) return;
 44   if (SIZE(p.segments) < 2) {
 45     p.segments.resize(2);
 46     p.segments.at(1).lines.swap(data.lines);
 47   }
 48   vector<line>& existing_data = p.segments.at(1).lines;
 49   existing_data.insert(existing_data.end(), data.lines.begin(), data.lines.end());
 50 }
 51 
 52 void add_global_to_data_segment(const string& name, const word& value, segment& data) {
 53   trace(99, "transform") << "adding global variable '" << name << "' containing " << value.data << end();
 54   // emit label
 55   data.lines.push_back(label(name));
 56   // emit size for size-prefixed array
 57   data.lines.push_back(line());
 58   emit_hex_bytes(data.lines.back(), SIZE(value.data)-/*skip quotes*/2, 4/*bytes*/);
 59   // emit data byte by byte
 60   data.lines.push_back(line());
 61   line& curr = data.lines.back();
 62   for (int i = /*skip start quote*/1;  i < SIZE(value.data)-/*skip end quote*/1;  ++i) {
 63     char c = value.data.at(i);
 64     curr.words.push_back(word());
 65     curr.words.back().data = hex_byte_to_string(c);
 66     curr.words.back().metadata.push_back(string(1, c));
 67   }
 68 }
 69 
 70 //: Within strings, whitespace is significant. So we need to redo our instruction
 71 //: parsing.
 72 
 73 :(scenarios parse_instruction_character_by_character)
 74 :(scenario instruction_with_string_literal)
 75 a "abc  def" z  # two spaces inside string
 76 +parse2: word: a
 77 +parse2: word: "abc  def"
 78 +parse2: word: z
 79 # no other words
 80 $parse2: 3
 81 
 82 :(before "End Line Parsing Special-cases(line_data -> l)")
 83 if (line_data.find('"') != string::npos) {  // can cause false-positives, but we can handle them
 84   parse_instruction_character_by_character(line_data, l);
 85   continue;
 86 }
 87 
 88 :(code)
 89 void parse_instruction_character_by_character(const string& line_data, vector<line>& out) {
 90   if (line_data.find('\n') != string::npos  && line_data.find('\n') != line_data.size()-1) {
 91     raise << "parse_instruction_character_by_character: should receive only a single line\n" << end();
 92     return;
 93   }
 94   // parse literals
 95   istringstream in(line_data);
 96   in >> std::noskipws;
 97   line result;
 98   // add tokens (words or strings) one by one
 99   while (has_data(in)) {
100     skip_whitespace(in);
101     if (!has_data(in)) break;
102     char c = in.get();
103     if (c == '#') break;  // comment; drop rest of line
104     if (c == ':') break;  // line metadata; skip for now
105     if (c == '.') {
106       if (!has_data(in)) break;  // comment token at end of line
107       if (isspace(in.peek()))
108         continue;  // '.' followed by space is comment token; skip
109     }
110     result.words.push_back(word());
111     if (c == '"') {
112       // slurp word data
113       ostringstream d;
114       d << c;
115       while (has_data(in)) {
116         in >> c;
117         d << c;
118         if (c == '"') break;
119       }
120       result.words.back().data = d.str();
121       // slurp metadata
122       ostringstream m;
123       while (!isspace(in.peek()) && has_data(in)) {
124         in >> c;
125         if (c == '/') {
126           if (!m.str().empty()) result.words.back().metadata.push_back(m.str());
127           m.str("");
128         }
129         else {
130           m << c;
131         }
132       }
133       if (!m.str().empty()) result.words.back().metadata.push_back(m.str());
134     }
135     else {
136       // slurp all characters until whitespace
137       ostringstream w;
138       w << c;
139       while (!isspace(in.peek()) && has_data(in)) {  // peek can sometimes trigger eof(), so do it first
140         in >> c;
141         w << c;
142       }
143       parse_word(w.str(), result.words.back());
144     }
145     trace(99, "parse2") << "word: " << to_string(result.words.back()) << end();
146   }
147   if (!result.words.empty())
148     out.push_back(result);
149 }
150 
151 void skip_whitespace(istream& in) {
152   while (true) {
153     if (has_data(in) && isspace(in.peek())) in.get();
154     else break;
155   }
156 }
157 
158 void skip_comment(istream& in) {
159   if (has_data(in) && in.peek() == '#') {
160     in.get();
161     while (has_data(in) && in.peek() != '\n') in.get();
162   }
163 }
164 
165 // helper for tests
166 void parse_instruction_character_by_character(const string& line_data) {
167   vector<line> out;
168   parse_instruction_character_by_character(line_data, out);
169 }
170 
171 :(scenario parse2_comment_token_in_middle)
172 a . z
173 +parse2: word: a
174 +parse2: word: z
175 -parse2: word: .
176 # no other words
177 $parse2: 2
178 
179 :(scenario parse2_word_starting_with_dot)
180 a .b c
181 +parse2: word: a
182 +parse2: word: .b
183 +parse2: word: c
184 
185 :(scenario parse2_comment_token_at_start)
186 . a b
187 +parse2: word: a
188 +parse2: word: b
189 -parse2: word: .
190 
191 :(scenario parse2_comment_token_at_end)
192 a b .
193 +parse2: word: a
194 +parse2: word: b
195 -parse2: word: .
196 
197 :(scenario parse2_word_starting_with_dot_at_start)
198 .a b c
199 +parse2: word: .a
200 +parse2: word: b
201 +parse2: word: c
202 
203 :(scenario parse2_metadata)
204 .a b/c d
205 +parse2: word: .a
206 +parse2: word: b /c
207 +parse2: word: d
208 
209 :(scenario parse2_string_with_metadata)
210 a "bc  def"/disp32 g
211 +parse2: word: a
212 +parse2: word: "bc  def" /disp32
213 +parse2: word: g
214 
215 :(scenario parse2_string_with_metadata_at_end)
216 a "bc  def"/disp32
217 +parse2: word: a
218 +parse2: word: "bc  def" /disp32
219 
220 :(code)
221 void test_parse2_string_with_metadata_at_end_of_line_without_newline() {
222   parse_instruction_character_by_character(
223       "68/push \"test\"/f"  // no newline, which is how calls from parse() will look
224   );
225   CHECK_TRACE_CONTENTS(
226       "parse2: word: 68 /push^D"
227       "parse2: word: \"test\" /f^D"
228   );
229 }
230 
231 //: Make sure slashes inside strings don't trigger adding stuff from inside the
232 //: string to metadata.
233 :(scenario parse2_string_containing_slashes)
234 a "bc/def"/disp32
235 +parse2: word: "bc/def" /disp32