about summary refs log blame commit diff stats
path: root/038literal_strings.cc
blob: b0b3c13f2024d6210723a54f52e07b8effc33e97 (plain) (generated by cgit-pink 1.4.1-2-gfad0 (git 2.36.2.497.gbbea4dcf42) at 2024-11-26 05:10:47 +0000
2 - support string literals directly in code' href='/akkartik/mu/commit/subx/038---literal_strings.cc?h=hlt&id=2b36eee9b13eb16fb2e4b05d7e26f6d09f431912'>2b36eee9 ^
adceed3d ^

2b36eee9 ^

c442a5ad ^
2b36eee9 ^
37c85905 ^
















2b36eee9 ^
2b36eee9 ^
37c85905 ^


2b36eee9 ^

37c85905 ^
2b36eee9 ^

37c85905 ^
2b36eee9 ^
37c85905 ^

2b36eee9 ^
37c85905 ^

2b36eee9 ^







2b36eee9 ^


4a943d4e ^

a97886ef ^
4a943d4e ^


a97886ef ^
4a943d4e ^




2b36eee9 ^
37c85905 ^














48bde7f9 ^











2b36eee9 ^







4aff81ec ^



2b36eee9 ^



02684e8d ^
2b36eee9 ^











4aff81ec ^
2b36eee9 ^
8fd7deed ^
4aff81ec ^

48bde7f9 ^




2b36eee9 ^
d2f360d8 ^
1209ddac ^
d2f360d8 ^
d2f360d8 ^
a97886ef ^
1209ddac ^



cb2344a2 ^
1209ddac ^


2b36eee9 ^

4aff81ec ^
adceed3d ^
4aff81ec ^

48f993d0 ^
4aff81ec ^









2b36eee9 ^
4aff81ec ^
8fd7deed ^
4aff81ec ^
2b36eee9 ^
4aff81ec ^




2b36eee9 ^
2b36eee9 ^






9d622b12 ^

2b36eee9 ^









fbc21293 ^






2b36eee9 ^





4a943d4e ^























































































2b36eee9 ^
2b36eee9 ^




4a943d4e ^

2b36eee9 ^

4aff81ec ^


4a943d4e ^








a97886ef ^





















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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362




                                                                             

                                      
                     
                                 
                                                  



                                                                                 
                                                               

    




                                                                              
                                               


                         

                     

                                            
                                                                              
                                 
















                                                                                                  
     
   


                                                                              

 
                                                                                           

                                                                                                         
                             
                                      

                                                                            
                           

                          







                                                                                        


                                                                                 

                                             
                                                       


                         
                                    




                                 
 














                                                                                 











                                                        







                                                                                                



                                                                                                      



                              
                              











                                                                  
                                   
                   
                                                                  

                      




                                                                
                
                        
                  
                                  
                                      
                                        



                                                                                                                    
                   


                 

                            
                                         
                                             

                       
                                                                                                        









                                                                                
     
          
                                                                    
                      
             




                                                                                                        
     






                                                                               

                                              









                                                       






                                     





                                                                        























































































                                                    
 




                                                                                    

                                   

    


                                                                               








                                              





















                                                                    
//: Allow instructions to mention literals directly.
//:
//: This layer will transparently move them to the global segment (assumed to
//: always be the second segment).

void test_transform_literal_string() {
  run(
      "== code 0x1\n"
      "b8/copy  \"test\"/imm32\n"
      "== data 0x2000\n"  // need an empty segment
  );
  CHECK_TRACE_CONTENTS(
      "transform: -- move literal strings to data segment\n"
      "transform: adding global variable '__subx_global_1' containing \"test\"\n"
      "transform: line after transform: 'b8 __subx_global_1'\n"
  );
}

//: We don't rely on any transforms running in previous layers, but this layer
//: knows about labels and global variables and will emit them for previous
//: layers to transform.
:(after "Begin Transforms")
Transform.push_back(transform_literal_strings);

:(before "End Globals")
int Next_auto_global = 1;
:(before "End Reset")
Next_auto_global = 1;
:(code)
void transform_literal_strings(program& p) {
  trace(3, "transform") << "-- move literal strings to data segment" << end();
  if (p.segments.empty()) return;
  vector<line> new_lines;
  for (int s = 0;  s < SIZE(p.segments);  ++s) {
    segment& seg = p.segments.at(s);
    trace(99, "transform") << "segment '" << seg.name << "'" << end();
    for (int i = 0;  i < SIZE(seg.lines);  ++i) {
//?       cerr << seg.name << '/' << i << '\n';
      line& line = seg.lines.at(i);
      for (int j = 0;  j < SIZE(line.words);  ++j) {
        word& curr = line.words.at(j);
        if (curr.data.at(0) != '"') continue;
        ostringstream global_name;
        global_name << "__subx_global_" << Next_auto_global;
        ++Next_auto_global;
        add_global_to_data_segment(global_name.str(), curr, new_lines);
        curr.data = global_name.str();
      }
      trace(99, "transform") << "line after transform: '" << data_to_string(line) << "'" << end();
    }
  }
  segment* data = find(p, "data");
  if (data)
    data->lines.insert(data->lines.end(), new_lines.begin(), new_lines.end());
}

void add_global_to_data_segment(const string& name, const word& value, vector<line>& out) {
  trace(99, "transform") << "adding global variable '" << name << "' containing " << value.data << end();
  // emit label
  out.push_back(label(name));
  // emit size for size-prefixed array
  out.push_back(line());
  emit_hex_bytes(out.back(), SIZE(value.data)-/*skip quotes*/2, 4/*bytes*/);
  // emit data byte by byte
  out.push_back(line());
  line& curr = out.back();
  for (int i = /*skip start quote*/1;  i < SIZE(value.data)-/*skip end quote*/1;  ++i) {
    char c = value.data.at(i);
    curr.words.push_back(word());
    curr.words.back().data = hex_byte_to_string(c);
    curr.words.back().metadata.push_back(string(1, c));
  }
}

//: Within strings, whitespace is significant. So we need to redo our instruction
//: parsing.

void test_instruction_with_string_literal() {
  parse_instruction_character_by_character(
      "a \"abc  def\" z\n"  // two spaces inside string
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: \"abc  def\"\n"
      "parse2: word: z\n"
  );
  // no other words
  CHECK_TRACE_COUNT("parse2", 3);
}

void test_string_literal_in_data_segment() {
  run(
      "== code 0x1\n"
      "b8/copy  X/imm32\n"
      "== data 0x2000\n"
      "X:\n"
      "\"test\"/imm32\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: -- move literal strings to data segment\n"
      "transform: adding global variable '__subx_global_1' containing \"test\"\n"
      "transform: line after transform: '__subx_global_1'\n"
  );
}

void test_string_literal_with_missing_quote() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "b8/copy  \"test/imm32\n"
      "== data 0x2000\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: unclosed string in: b8/copy  \"test/imm32"
  );
}

:(before "End Line Parsing Special-cases(line_data -> l)")
if (line_data.find('"') != string::npos) {  // can cause false-positives, but we can handle them
  parse_instruction_character_by_character(line_data, l);
  continue;
}

:(code)
void parse_instruction_character_by_character(const string& line_data, vector<line>& out) {
  if (line_data.find('\n') != string::npos  && line_data.find('\n') != line_data.size()-1) {
    raise << "parse_instruction_character_by_character: should receive only a single line\n" << end();
    return;
  }
  // parse literals
  istringstream in(line_data);
  in >> std::noskipws;
  line result;
  result.original = line_data;
  // add tokens (words or strings) one by one
  while (has_data(in)) {
    skip_whitespace(in);
    if (!has_data(in)) break;
    char c = in.get();
    if (c == '#') break;  // comment; drop rest of line
    if (c == ':') break;  // line metadata; skip for now
    if (c == '.') {
      if (!has_data(in)) break;  // comment token at end of line
      if (isspace(in.peek()))
        continue;  // '.' followed by space is comment token; skip
    }
    result.words.push_back(word());
    if (c == '"') {
      // string literal; slurp everything between quotes into data
      ostringstream d;
      d << c;
      while (true) {
        if (!has_data(in)) {
          raise << "unclosed string in: " << line_data << end();
          return;
        }
        in >> c;
        if (c == '\\') {
          in >> c;
          if (c == 'n') d << '\n';
          else if (c == '"') d << '"';
          else if (c == '\\') d << '\\';
          else {
            raise << "parse_instruction_character_by_character: unknown escape sequence '\\" << c << "'\n" << end();
            return;
          }
          continue;
        } else {
          d << c;
        }
        if (c == '"') break;
      }
      result.words.back().data = d.str();
      result.words.back().original = d.str();
      // slurp metadata
      ostringstream m;
      while (!isspace(in.peek()) && has_data(in)) {  // peek can sometimes trigger eof(), so do it first
        in >> c;
        if (c == '/') {
          if (!m.str().empty()) result.words.back().metadata.push_back(m.str());
          m.str("");
        }
        else {
          m << c;
        }
      }
      if (!m.str().empty()) result.words.back().metadata.push_back(m.str());
    }
    else {
      // not a string literal; slurp all characters until whitespace
      ostringstream w;
      w << c;
      while (!isspace(in.peek()) && has_data(in)) {  // peek can sometimes trigger eof(), so do it first
        in >> c;
        w << c;
      }
      parse_word(w.str(), result.words.back());
    }
    trace(99, "parse2") << "word: " << to_string(result.words.back()) << end();
  }
  if (!result.words.empty())
    out.push_back(result);
}

void skip_whitespace(istream& in) {
  while (has_data(in) && isspace(in.peek())) {
    in.get();
  }
}

void skip_comment(istream& in) {
  if (has_data(in) && in.peek() == '#') {
    in.get();
    while (has_data(in) && in.peek() != '\n') in.get();
  }
}

line label(string s) {
  line result;
  result.words.push_back(word());
  result.words.back().data = (s+":");
  return result;
}

// helper for tests
void parse_instruction_character_by_character(const string& line_data) {
  vector<line> out;
  parse_instruction_character_by_character(line_data, out);
}

void test_parse2_comment_token_in_middle() {
  parse_instruction_character_by_character(
      "a . z\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: z\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("parse2: word: .");
  // no other words
  CHECK_TRACE_COUNT("parse2", 2);
}

void test_parse2_word_starting_with_dot() {
  parse_instruction_character_by_character(
      "a .b c\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: .b\n"
      "parse2: word: c\n"
  );
}

void test_parse2_comment_token_at_start() {
  parse_instruction_character_by_character(
      ". a b\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: b\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("parse2: word: .");
}

void test_parse2_comment_token_at_end() {
  parse_instruction_character_by_character(
      "a b .\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: b\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("parse2: word: .");
}

void test_parse2_word_starting_with_dot_at_start() {
  parse_instruction_character_by_character(
      ".a b c\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: .a\n"
      "parse2: word: b\n"
      "parse2: word: c\n"
  );
}

void test_parse2_metadata() {
  parse_instruction_character_by_character(
      ".a b/c d\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: .a\n"
      "parse2: word: b /c\n"
      "parse2: word: d\n"
  );
}

void test_parse2_string_with_metadata() {
  parse_instruction_character_by_character(
      "a \"bc  def\"/disp32 g\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: \"bc  def\" /disp32\n"
      "parse2: word: g\n"
  );
}

void test_parse2_string_with_metadata_at_end() {
  parse_instruction_character_by_character(
      "a \"bc  def\"/disp32\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: a\n"
      "parse2: word: \"bc  def\" /disp32\n"
  );
}

void test_parse2_string_with_metadata_at_end_of_line_without_newline() {
  parse_instruction_character_by_character(
      "68/push \"test\"/f"  // no newline, which is how calls from parse() will look
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: 68 /push\n"
      "parse2: word: \"test\" /f\n"
  );
}

//: Make sure slashes inside strings don't trigger adding stuff from inside the
//: string to metadata.

void test_parse2_string_containing_slashes() {
  parse_instruction_character_by_character(
      "a \"bc/def\"/disp32\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: \"bc/def\" /disp32\n"
  );
}

void test_instruction_with_string_literal_with_escaped_quote() {
  parse_instruction_character_by_character(
      "\"a\\\"b\"\n"  // escaped quote inside string
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: \"a\"b\"\n"
  );
  // no other words
  CHECK_TRACE_COUNT("parse2", 1);
}

void test_instruction_with_string_literal_with_escaped_backslash() {
  parse_instruction_character_by_character(
      "\"a\\\\b\"\n"  // escaped backslash inside string
  );
  CHECK_TRACE_CONTENTS(
      "parse2: word: \"a\\b\"\n"
  );
  // no other words
  CHECK_TRACE_COUNT("parse2", 1);
}