about summary refs log tree commit diff stats
path: root/subx/034discourage_raw_hex.cc
blob: 8d5eac7de383be16c631a058f7bb59239ff781ad (plain) (blame)
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
//: Now that we have operand metadata, start warning on instructions that
//: don't use it.
//:
//: While SubX will let you write raw machine code, don't do that unless you
//: have a very good reason.

:(after "Begin Level-2 Transforms")
Transform.push_back(warn_on_raw_jumps);
:(code)
void warn_on_raw_jumps(/*const*/ program& p) {
  if (p.segments.empty()) return;
  segment& code = p.segments.at(0);
  trace(99, "transform") << "-- warn on raw hex instructions" << end();
  for (int i = 0;  i < SIZE(code.lines);  ++i) {
    line& inst = code.lines.at(i);
    if (all_hex_bytes(inst) && has_operands(inst)) {
      warn << "'" << to_string(inst) << "': using raw hex is not recommended\n" << end();
      break;
    }
  }
}

:(scenarios transform)
:(scenario warn_on_hex_bytes_without_operands)
== 0x1
bb 2a 00 00 00  # copy 0x2a (42) to EBX
+warn: 'bb 2a 00 00 00': using raw hex is not recommended

:(scenario warn_on_non_operand_metadata)
== 0x1
bb 2a 00/foo 00/bar 00  # copy 0x2a (42) to EBX
+warn: 'bb 2a 00/foo 00/bar 00': using raw hex is not recommended

:(scenario no_warn_on_instructions_without_operands)
== 0x1
55  # push EBP
-warn: '55': using raw hex is not recommended