From a6061b9fd2e1476172ebe67376077bd09b516e41 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 19 Nov 2018 21:16:56 -0800 Subject: 4754 - allow data segment to refer to variables --- subx/034compute_segment_address.cc | 18 +++++++++------- subx/035labels.cc | 6 +++--- subx/036global_variables.cc | 44 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 13 deletions(-) (limited to 'subx') diff --git a/subx/034compute_segment_address.cc b/subx/034compute_segment_address.cc index f6513114..b302e4d8 100644 --- a/subx/034compute_segment_address.cc +++ b/subx/034compute_segment_address.cc @@ -111,17 +111,19 @@ uint32_t size_of(const segment& s) { // Assumes all bitfields are packed. uint32_t num_bytes(const line& inst) { uint32_t sum = 0; - for (int i = 0; i < SIZE(inst.words); ++i) { - const word& curr = inst.words.at(i); - if (has_operand_metadata(curr, "disp32") || has_operand_metadata(curr, "imm32")) // only multi-byte operands - sum += 4; - // End num_bytes(curr) Special-cases - else - sum++; - } + for (int i = 0; i < SIZE(inst.words); ++i) + sum += size_of(inst.words.at(i)); return sum; } +int size_of(const word& w) { + if (has_operand_metadata(w, "disp32") || has_operand_metadata(w, "imm32")) + return 4; + // End size_of(word w) Special-cases + else + return 1; +} + //: Dependencies: //: - We'd like to compute segment addresses before setting up global variables, //: because computing addresses for global variables requires knowing where diff --git a/subx/035labels.cc b/subx/035labels.cc index 30a6082e..e07cede8 100644 --- a/subx/035labels.cc +++ b/subx/035labels.cc @@ -265,6 +265,6 @@ bar: 00 +transform: segment 1 begins at address 0x0a000079 -:(before "End num_bytes(curr) Special-cases") -else if (is_label(curr)) - ; // don't count it +:(before "End size_of(word w) Special-cases") +else if (is_label(w)) + return 0; diff --git a/subx/036global_variables.cc b/subx/036global_variables.cc index f1fdd963..afeb7427 100644 --- a/subx/036global_variables.cc +++ b/subx/036global_variables.cc @@ -38,7 +38,7 @@ void compute_addresses_for_global_variables(const segment& s, map& address) { if (p.segments.empty()) return; - segment& code = p.segments.at(0); + replace_global_variables_in_code_segment(p.segments.at(0), address); + for (int i = /*skip code*/1; i < SIZE(p.segments); ++i) + replace_global_variables_in_data_segment(p.segments.at(i), address); +} + +void replace_global_variables_in_code_segment(segment& code, const map& address) { for (int i = 0; i < SIZE(code.lines); ++i) { line& inst = code.lines.at(i); line new_inst; @@ -87,6 +92,26 @@ void replace_global_variables_with_addresses(program& p, const map& address) { + for (int i = 0; i < SIZE(data.lines); ++i) { + line& l = data.lines.at(i); + line new_l; + for (int j = 0; j < SIZE(l.words); ++j) { + const word& curr = l.words.at(j); + if (!contains_key(address, curr.data)) { + if (!looks_like_hex_int(curr.data)) + raise << "missing reference to global '" << curr.data << "'\n" << end(); + new_l.words.push_back(curr); + continue; + } + trace(99, "transform") << curr.data << " maps to " << HEXWORD << get(address, curr.data) << end(); + emit_hex_bytes(new_l, get(address, curr.data), 4); + } + l.words.swap(new_l.words); + trace(99, "transform") << "after transform: '" << data_to_string(l) << "'" << end(); + } +} + bool valid_use_of_global_variable(const word& curr) { if (has_operand_metadata(curr, "imm32")) return true; // End Valid Uses Of Global Variable(curr) @@ -148,6 +173,21 @@ x: # sub-optimal error message; should be #? +error: can't call to the data segment ('x') +:(scenario global_variable_in_data_segment) +== 0x1 +b9 x/imm32 +== 0x0a000000 +x: + y/imm32 +y: + 00 00 00 00 +# check that we loaded 'x' with the address of 'y' ++load: 0x0a000000 -> 04 ++load: 0x0a000001 -> 00 ++load: 0x0a000002 -> 00 ++load: 0x0a000003 -> 0a +$error: 0 + :(scenario disp32_data_with_modrm) == code 8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX x/disp32 -- cgit 1.4.1-2-gfad0 ef='#n59'>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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597