From e99038ea514a8703b170689d5a0730c8d2e542e7 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 18 Feb 2019 22:43:01 -0800 Subject: 4982 --- html/subx/030---operands.cc.html | 114 +++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 57 deletions(-) (limited to 'html/subx/030---operands.cc.html') diff --git a/html/subx/030---operands.cc.html b/html/subx/030---operands.cc.html index 6567df13..e0b73e02 100644 --- a/html/subx/030---operands.cc.html +++ b/html/subx/030---operands.cc.html @@ -208,17 +208,17 @@ if ('onhashchange' in window) { 148 149 :(after "Begin Transforms") 150 // Begin Level-2 Transforms -151 Transform.push_back(pack_operands); +151 Transform.push_back(pack_operands); 152 // End Level-2 Transforms 153 154 :(code) 155 void pack_operands(program& p) { -156 if (p.segments.empty()) return; -157 segment& code = p.segments.at(0); +156 if (p.segments.empty()) return; +157 segment& code = p.segments.at(0); 158 // Pack Operands(segment code) 159 trace(99, "transform") << "-- pack operands" << end(); -160 for (int i = 0; i < SIZE(code.lines); ++i) { -161 line& inst = code.lines.at(i); +160 for (int i = 0; i < SIZE(code.lines); ++i) { +161 line& inst = code.lines.at(i); 162 if (all_hex_bytes(inst)) continue; 163 trace(99, "transform") << "packing instruction '" << to_string(/*with metadata*/inst) << "'" << end(); 164 pack_operands(inst); @@ -227,76 +227,76 @@ if ('onhashchange' in window) { 167 } 168 169 void pack_operands(line& inst) { -170 line new_inst; +170 line new_inst; 171 add_opcodes(inst, new_inst); 172 add_modrm_byte(inst, new_inst); 173 add_sib_byte(inst, new_inst); 174 add_disp_bytes(inst, new_inst); 175 add_imm_bytes(inst, new_inst); -176 inst.words.swap(new_inst.words); +176 inst.words.swap(new_inst.words); 177 } 178 179 void add_opcodes(const line& in, line& out) { -180 out.words.push_back(in.words.at(0)); -181 if (in.words.at(0).data == "0f" || in.words.at(0).data == "f2" || in.words.at(0).data == "f3") -182 out.words.push_back(in.words.at(1)); -183 if (in.words.at(0).data == "f3" && in.words.at(1).data == "0f") -184 out.words.push_back(in.words.at(2)); -185 if (in.words.at(0).data == "f2" && in.words.at(1).data == "0f") -186 out.words.push_back(in.words.at(2)); +180 out.words.push_back(in.words.at(0)); +181 if (in.words.at(0).data == "0f" || in.words.at(0).data == "f2" || in.words.at(0).data == "f3") +182 out.words.push_back(in.words.at(1)); +183 if (in.words.at(0).data == "f3" && in.words.at(1).data == "0f") +184 out.words.push_back(in.words.at(2)); +185 if (in.words.at(0).data == "f2" && in.words.at(1).data == "0f") +186 out.words.push_back(in.words.at(2)); 187 } 188 189 void add_modrm_byte(const line& in, line& out) { 190 uint8_t mod=0, reg_subop=0, rm32=0; 191 bool emit = false; -192 for (int i = 0; i < SIZE(in.words); ++i) { -193 const word& curr = in.words.at(i); +192 for (int i = 0; i < SIZE(in.words); ++i) { +193 const word& curr = in.words.at(i); 194 if (has_operand_metadata(curr, "mod")) { -195 mod = hex_byte(curr.data); +195 mod = hex_byte(curr.data); 196 emit = true; 197 } 198 else if (has_operand_metadata(curr, "rm32")) { -199 rm32 = hex_byte(curr.data); +199 rm32 = hex_byte(curr.data); 200 emit = true; 201 } 202 else if (has_operand_metadata(curr, "r32")) { -203 reg_subop = hex_byte(curr.data); +203 reg_subop = hex_byte(curr.data); 204 emit = true; 205 } 206 else if (has_operand_metadata(curr, "subop")) { -207 reg_subop = hex_byte(curr.data); +207 reg_subop = hex_byte(curr.data); 208 emit = true; 209 } 210 } 211 if (emit) -212 out.words.push_back(hex_byte_text((mod << 6) | (reg_subop << 3) | rm32)); +212 out.words.push_back(hex_byte_text((mod << 6) | (reg_subop << 3) | rm32)); 213 } 214 215 void add_sib_byte(const line& in, line& out) { 216 uint8_t scale=0, index=0, base=0; 217 bool emit = false; -218 for (int i = 0; i < SIZE(in.words); ++i) { -219 const word& curr = in.words.at(i); +218 for (int i = 0; i < SIZE(in.words); ++i) { +219 const word& curr = in.words.at(i); 220 if (has_operand_metadata(curr, "scale")) { -221 scale = hex_byte(curr.data); +221 scale = hex_byte(curr.data); 222 emit = true; 223 } 224 else if (has_operand_metadata(curr, "index")) { -225 index = hex_byte(curr.data); +225 index = hex_byte(curr.data); 226 emit = true; 227 } 228 else if (has_operand_metadata(curr, "base")) { -229 base = hex_byte(curr.data); +229 base = hex_byte(curr.data); 230 emit = true; 231 } 232 } 233 if (emit) -234 out.words.push_back(hex_byte_text((scale << 6) | (index << 3) | base)); +234 out.words.push_back(hex_byte_text((scale << 6) | (index << 3) | base)); 235 } 236 237 void add_disp_bytes(const line& in, line& out) { -238 for (int i = 0; i < SIZE(in.words); ++i) { -239 const word& curr = in.words.at(i); +238 for (int i = 0; i < SIZE(in.words); ++i) { +239 const word& curr = in.words.at(i); 240 if (has_operand_metadata(curr, "disp8")) 241 emit_hex_bytes(out, curr, 1); 242 if (has_operand_metadata(curr, "disp16")) @@ -307,8 +307,8 @@ if ('onhashchange' in window) { 247 } 248 249 void add_imm_bytes(const line& in, line& out) { -250 for (int i = 0; i < SIZE(in.words); ++i) { -251 const word& curr = in.words.at(i); +250 for (int i = 0; i < SIZE(in.words); ++i) { +251 const word& curr = in.words.at(i); 252 if (has_operand_metadata(curr, "imm8")) 253 emit_hex_bytes(out, curr, 1); 254 else if (has_operand_metadata(curr, "imm32")) @@ -320,24 +320,24 @@ if ('onhashchange' in window) { 260 assert(num <= 4); 261 bool is_number = looks_like_hex_int(w.data); 262 if (num == 1 || !is_number) { -263 out.words.push_back(w); // preserve existing metadata +263 out.words.push_back(w); // preserve existing metadata 264 if (is_number) -265 out.words.back().data = hex_byte_to_string(parse_int(w.data)); +265 out.words.back().data = hex_byte_to_string(parse_int(w.data)); 266 return; 267 } -268 emit_hex_bytes(out, static_cast<uint32_t>(parse_int(w.data)), num); +268 emit_hex_bytes(out, static_cast<uint32_t>(parse_int(w.data)), num); 269 } 270 271 void emit_hex_bytes(line& out, uint32_t val, int num) { 272 assert(num <= 4); 273 for (int i = 0; i < num; ++i) { -274 out.words.push_back(hex_byte_text(val & 0xff)); +274 out.words.push_back(hex_byte_text(val & 0xff)); 275 val = val >> 8; 276 } 277 } 278 -279 word hex_byte_text(uint8_t val) { -280 word result; +279 word hex_byte_text(uint8_t val) { +280 word result; 281 result.data = hex_byte_to_string(val); 282 result.original = result.data+"/auto"; 283 return result; @@ -361,13 +361,13 @@ if ('onhashchange' in window) { 301 302 :(before "End Unit Tests") 303 void test_preserve_metadata_when_emitting_single_byte() { -304 word in; +304 word in; 305 in.data = "f0"; 306 in.original = "f0/foo"; -307 line out; +307 line out; 308 emit_hex_bytes(out, in, 1); -309 CHECK_EQ(out.words.at(0).data, "f0"); -310 CHECK_EQ(out.words.at(0).original, "f0/foo"); +309 CHECK_EQ(out.words.at(0).data, "f0"); +310 CHECK_EQ(out.words.at(0).original, "f0/foo"); 311 } 312 313 :(scenario pack_disp8) @@ -387,12 +387,12 @@ if ('onhashchange' in window) { 327 328 //: helper for scenario 329 :(code) -330 void transform(const string& text_bytes) { -331 program p; +330 void transform(const string& text_bytes) { +331 program p; 332 istringstream in(text_bytes); 333 parse(in, p); 334 if (trace_contains_errors()) return; -335 transform(p); +335 transform(p); 336 } 337 338 :(scenario pack_modrm_imm32) @@ -437,8 +437,8 @@ if ('onhashchange' in window) { 377 378 :(code) 379 bool all_hex_bytes(const line& inst) { -380 for (int i = 0; i < SIZE(inst.words); ++i) -381 if (!is_hex_byte(inst.words.at(i))) +380 for (int i = 0; i < SIZE(inst.words); ++i) +381 if (!is_hex_byte(inst.words.at(i))) 382 return false; 383 return true; 384 } @@ -453,17 +453,17 @@ if ('onhashchange' in window) { 393 return true; 394 } 395 -396 bool contains_any_operand_metadata(const word& word) { -397 for (int i = 0; i < SIZE(word.metadata); ++i) -398 if (Instruction_operands.find(word.metadata.at(i)) != Instruction_operands.end()) +396 bool contains_any_operand_metadata(const word& word) { +397 for (int i = 0; i < SIZE(word.metadata); ++i) +398 if (Instruction_operands.find(word.metadata.at(i)) != Instruction_operands.end()) 399 return true; 400 return false; 401 } 402 403 bool has_operand_metadata(const line& inst, const string& m) { 404 bool result = false; -405 for (int i = 0; i < SIZE(inst.words); ++i) { -406 if (!has_operand_metadata(inst.words.at(i), m)) continue; +405 for (int i = 0; i < SIZE(inst.words); ++i) { +406 if (!has_operand_metadata(inst.words.at(i), m)) continue; 407 if (result) { 408 raise << "'" << to_string(inst) << "' has conflicting " << m << " operands\n" << end(); 409 return false; @@ -489,10 +489,10 @@ if ('onhashchange' in window) { 429 return result; 430 } 431 -432 word metadata(const line& inst, const string& m) { -433 for (int i = 0; i < SIZE(inst.words); ++i) -434 if (has_operand_metadata(inst.words.at(i), m)) -435 return inst.words.at(i); +432 word metadata(const line& inst, const string& m) { +433 for (int i = 0; i < SIZE(inst.words); ++i) +434 if (has_operand_metadata(inst.words.at(i), m)) +435 return inst.words.at(i); 436 assert(false); 437 } 438 @@ -507,9 +507,9 @@ if ('onhashchange' in window) { 447 :(code) 448 string to_string(const line& inst) { 449 ostringstream out; -450 for (int i = 0; i < SIZE(inst.words); ++i) { +450 for (int i = 0; i < SIZE(inst.words); ++i) { 451 if (i > 0) out << ' '; -452 out << inst.words.at(i).original; +452 out << inst.words.at(i).original; 453 } 454 return out.str(); 455 } -- cgit 1.4.1-2-gfad0