summary refs log tree commit diff stats
path: root/compiler/sempass2.nim
Commit message (Expand)AuthorAgeFilesLines
* sink parameter inference for types that have destructors (#13544)Andreas Rumpf2020-03-041-3/+19
* fix #8312 --hints:off and --warnings:off now honored everywhere (#13489)Timothee Cour2020-02-261-7/+7
* fixes #13110 (#13197)Andreas Rumpf2020-01-191-1/+11
* fixes #13119 (#13128)Andreas Rumpf2020-01-141-2/+8
* more arc features (#13098)Andreas Rumpf2020-01-101-0/+1
* Fixes #13026 (#13028)cooldome2020-01-041-1/+1
* --exception:goto switch for deterministic exception handling (#12977)Andreas Rumpf2020-01-011-3/+1
* minor refactoringsAndreas Rumpf2019-12-271-0/+2
* ARC: cycle detector (#12823)Andreas Rumpf2019-12-171-0/+3
* Fixes #12883 (#12894)cooldome2019-12-131-0/+13
* invoke createTypeBoundOps for constructors (#12878)cooldome2019-12-111-0/+7
* Cosmetic compiler cleanup (#12718)Clyybber2019-11-281-123/+121
* ARC: ported the GC tests over to --gc:arcAraq2019-11-261-0/+1
* ARC: yet another silly bugfixAraq2019-11-221-0/+2
* more arc improvements (#12690)Andreas Rumpf2019-11-201-1/+4
* ARC: closure bugfixes (#12677)Andreas Rumpf2019-11-181-2/+5
* ARC: solves phase ordering problems (#12654)Andreas Rumpf2019-11-141-10/+16
* remove unused importsnarimiran2019-11-061-3/+0
* fixes #12281 [backport]Andreas Rumpf2019-09-301-1/+3
* Fix spellings (#12277) [backport]Federico Ceratto2019-09-271-1/+1
* gc:destructors progressAndreas Rumpf2019-09-161-0/+1
* Small ast.nim cleanup (#12156)Clyybber2019-09-091-3/+3
* fixes #11254Araq2019-08-121-1/+9
* revert changesAndrii Riabushenko2019-07-251-15/+15
* fixes #11826Andrii Riabushenko2019-07-251-15/+15
* [refactoring] remove unused imports in the compiler and in some stdlib modulesAraq2019-07-181-1/+1
* styleCheck: make the compiler and large parts of the stdlib compatible with -...Araq2019-07-101-7/+7
* fixes #11628Araq2019-07-011-0/+1
* [refactoring] liftdestructors.nim is callable from lambdalifting; refs #11217Araq2019-06-121-8/+8
* [refactoring] liftdestructors is now a module of its ownAraq2019-06-121-1/+1
* fixes #8053Araq2019-05-151-1/+2
* Replace countup(x, y) with x .. yClyybber2019-05-071-1/+1
* Replace countup(x, y-1) with x ..< yClyybber2019-05-071-1/+1
* introduce temporary <//> for 'owned' to get this compile with 0.19 (#11145)Andreas Rumpf2019-05-021-1/+1
* Fixes #11078 (#11079)Jasper Jenkins2019-04-231-1/+1
* make the CIs happyAraq2019-04-171-1/+1
* fixes #11050Araq2019-04-171-6/+12
* make tests green againAraq2019-04-101-1/+1
* koch.nim compiles with --newruntimeAraq2019-04-091-2/+2
* more efficient enumToStr implementation that works without the old typeinfo s...Andreas Rumpf2019-04-071-3/+3
* destructors: progressAndreas Rumpf2019-04-051-1/+10
* fixes destructor tuple regression #10940 (#10941)cooldome2019-04-041-1/+6
* newruntime: progressAraq2019-04-011-1/+1
* fixes #10904Araq2019-03-261-3/+6
* more destructor based changes (#10885)Andreas Rumpf2019-03-231-81/+41
* fixes #10807 (#10814)cooldome2019-03-121-1/+1
* make tests green againAndreas Rumpf2019-03-061-1/+2
* fixes a critical GC safety inference bug (#10615)Andreas Rumpf2019-03-051-9/+13
* fixes #6955Andreas Rumpf2019-02-131-3/+7
* Fix handling of reraise in effect tracking (#10582)LemonBoy2019-02-071-5/+11
Agaram <vc@akkartik.com> 2019-05-18 00:45:12 -0700 switch to new syntax for segment headers in C++' href='/akkartik/mu/commit/subx/032check_operand_bounds.cc?h=hlt&id=83c67014034bbf9072d7e4555b0e51e815a95756'>83c67014 ^
938185b3 ^







83c67014 ^
938185b3 ^









83c67014 ^
938185b3 ^







83c67014 ^
938185b3 ^





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

                                                                                              


                                  
                     





                                                         



                                    








                                        
                             
                                     

                            
                                       

                                    
       
                                                
                                                              

                                                

                                                                  




                                                                                     
                                                                                                    
                                                     
                                                                                                                    

                                  







                                                                                                              




                                                                                                     
     
   
 


                                           
                     







                                                                                  
                     








                                                                                   
                     







                                                                                   
                     









                                                                                   
                     







                                                                                 
                     









                                                                                 
                     







                                                                                  
                     





                                                                                  
//:: Check that the different operands of an instruction aren't too large for their bitfields.

void test_check_bitfield_sizes() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "01/add 4/mod 3/rm32 1/r32\n"  // add ECX to EBX
  );
  CHECK_TRACE_CONTENTS(
      "error: '4/mod' too large to fit in bitfield mod\n"
  );
}

:(before "End Globals")
map<string, uint32_t> Operand_bound;
:(before "End One-time Setup")
put_new(Operand_bound, "subop", 1<<3);
put_new(Operand_bound, "mod", 1<<2);
put_new(Operand_bound, "rm32", 1<<3);
put_new(Operand_bound, "base", 1<<3);
put_new(Operand_bound, "index", 1<<3);
put_new(Operand_bound, "scale", 1<<2);
put_new(Operand_bound, "r32", 1<<3);
put_new(Operand_bound, "disp8", 1<<8);
put_new(Operand_bound, "disp16", 1<<16);
// no bound needed for disp32
put_new(Operand_bound, "imm8", 1<<8);
// no bound needed for imm32

:(before "Pack Operands(segment code)")
check_operand_bounds(code);
if (trace_contains_errors()) return;
:(code)
void check_operand_bounds(const segment& code) {
  trace(3, "transform") << "-- check operand bounds" << end();
  for (int i = 0;  i < SIZE(code.lines);  ++i) {
    const line& inst = code.lines.at(i);
    for (int j = first_operand(inst);  j < SIZE(inst.words);  ++j)
      check_operand_bounds(inst.words.at(j));
    if (trace_contains_errors()) return;  // stop at the first mal-formed instruction
  }
}

void check_operand_bounds(const word& w) {
  for (map<string, uint32_t>::iterator p = Operand_bound.begin();  p != Operand_bound.end();  ++p) {
    if (!has_operand_metadata(w, p->first)) continue;
    if (!looks_like_hex_int(w.data)) continue;  // later transforms are on their own to do their own bounds checking
    int32_t x = parse_int(w.data);
    if (x >= 0) {
      if (p->first == "disp8" || p->first == "disp16") {
        if (static_cast<uint32_t>(x) >= p->second/2)
          raise << "'" << w.original << "' too large to fit in signed bitfield " << p->first << '\n' << end();
      }
      else {
        if (static_cast<uint32_t>(x) >= p->second)
          raise << "'" << w.original << "' too large to fit in bitfield " << p->first << '\n' << end();
      }
    }
    else {
      // hacky? assuming bound is a power of 2
      if (x < -1*static_cast<int32_t>(p->second/2))
        raise << "'" << w.original << "' too large to fit in bitfield " << p->first << '\n' << end();
    }
  }
}

void test_check_bitfield_sizes_for_imm8() {
  run(
      "== code 0x1\n"
      "c1/shift 4/subop/left 3/mod/direct 1/rm32/ECX 0xff/imm8"  // shift EBX left
  );
  CHECK(!trace_contains_errors());
}

void test_check_bitfield_sizes_for_imm8_error() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "c1/shift 4/subop/left 3/mod/direct 1/rm32/ECX 0x100/imm8"  // shift EBX left
  );
  CHECK_TRACE_CONTENTS(
      "error: '0x100/imm8' too large to fit in bitfield imm8\n"
  );
}

void test_check_bitfield_sizes_for_negative_imm8() {
  run(
      "== code 0x1\n"
      "c1/shift 4/subop/left 3/mod/direct 1/rm32/ECX -0x80/imm8"  // shift EBX left
  );
  CHECK(!trace_contains_errors());
}

void test_check_bitfield_sizes_for_negative_imm8_error() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "c1/shift 4/subop/left 3/mod/direct 1/rm32/ECX -0x81/imm8"  // shift EBX left
  );
  CHECK_TRACE_CONTENTS(
      "error: '-0x81/imm8' too large to fit in bitfield imm8\n"
  );
}

void test_check_bitfield_sizes_for_disp8() {
  // not bothering to run
  transform(
      "== code 0x1\n"
      "01/add 1/mod/*+disp8 3/rm32 1/r32 0x7f/disp8\n"  // add ECX to *(EBX+0x7f)
  );
  CHECK(!trace_contains_errors());
}

void test_check_bitfield_sizes_for_disp8_error() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "01/add 1/mod/*+disp8 3/rm32 1/r32 0x80/disp8\n"  // add ECX to *(EBX+0x80)
  );
  CHECK_TRACE_CONTENTS(
      "error: '0x80/disp8' too large to fit in signed bitfield disp8\n"
  );
}

void test_check_bitfield_sizes_for_negative_disp8() {
  // not bothering to run
  transform(
      "== code 0x1\n"
      "01/add 1/mod/*+disp8 3/rm32 1/r32 -0x80/disp8\n"  // add ECX to *(EBX-0x80)
  );
  CHECK(!trace_contains_errors());
}

void test_check_bitfield_sizes_for_negative_disp8_error() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "01/add 1/mod/*+disp8 3/rm32 1/r32 -0x81/disp8\n"  // add ECX to *(EBX-0x81)
  );
  CHECK_TRACE_CONTENTS(
      "error: '-0x81/disp8' too large to fit in bitfield disp8\n"
  );
}