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 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
|
# Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
require 'config'
require 'ast'
require 'opt'
# This file contains utilities that are useful for implementing a backend
# for RISC-like processors (ARM, MIPS, etc).
#
# Lowering of simple branch ops. For example:
#
# baddiz foo, bar, baz
#
# will become:
#
# addi foo, bar
# bz baz
#
def riscLowerSimpleBranchOps(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
annotation = node.annotation
case node.opcode
when /^b(addi|subi|ori|addp)/
op = $1
branch = "b" + $~.post_match
case op
when "addi"
op = "addis"
when "addp"
op = "addps"
when "subi"
op = "subis"
when "ori"
op = "oris"
end
newList << Instruction.new(node.codeOrigin, op, node.operands[0..-2], annotation)
newList << Instruction.new(node.codeOrigin, branch, [node.operands[-1]])
when 'bmulis', 'bmulz', 'bmulnz'
condition = $~.post_match
newList << Instruction.new(node.codeOrigin, "muli", node.operands[0..-2], annotation)
newList << Instruction.new(node.codeOrigin, "bti" + condition, [node.operands[-2], node.operands[-1]])
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowing of complex branch ops. For example:
#
# bmulio foo, bar, baz
#
# becomes:
#
# smulli foo, bar, bar, tmp1
# rshifti bar, 31, tmp2
# bineq tmp1, tmp2, baz
#
def riscLowerHardBranchOps(list)
newList = []
list.each {
| node |
if node.is_a? Instruction and node.opcode == "bmulio"
tmp1 = Tmp.new(node.codeOrigin, :gpr)
tmp2 = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "smulli", [node.operands[0], node.operands[1], node.operands[1], tmp1], node.annotation)
newList << Instruction.new(node.codeOrigin, "rshifti", [node.operands[-2], Immediate.new(node.codeOrigin, 31), tmp2])
newList << Instruction.new(node.codeOrigin, "bineq", [tmp1, tmp2, node.operands[-1]])
else
newList << node
end
}
newList
end
#
# Lowering of shift ops. For example:
#
# lshifti foo, bar
#
# will become:
#
# andi foo, 31, tmp
# lshifti tmp, bar
#
def riscSanitizeShift(operand, list)
return operand if operand.immediate?
tmp = Tmp.new(operand.codeOrigin, :gpr)
list << Instruction.new(operand.codeOrigin, "andi", [operand, Immediate.new(operand.codeOrigin, 31), tmp])
tmp
end
def riscLowerShiftOps(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "lshifti", "rshifti", "urshifti", "lshiftp", "rshiftp", "urshiftp"
if node.operands.size == 2
newList << Instruction.new(node.codeOrigin, node.opcode, [riscSanitizeShift(node.operands[0], newList), node.operands[1]], node.annotation)
else
newList << Instruction.new(node.codeOrigin, node.opcode, [node.operands[0], riscSanitizeShift(node.operands[1], newList), node.operands[2]], node.annotation)
raise "Wrong number of operands for shift at #{node.codeOriginString}" unless node.operands.size == 3
end
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of malformed addresses. For example:
#
# loadp 10000[foo], bar
#
# will become:
#
# move 10000, tmp
# addp foo, tmp
# loadp 0[tmp], bar
#
# Note that you use this lowering phase by passing it a block that returns true
# if you don't want to lower the address, or false if you do. For example to get
# the effect of the example above, the block would have to say something like:
#
# riscLowerMalformedAddresses(thingy) {
# | node, address |
# if address.is_a? Address
# address.offset.value > 1000
# else
# true # don't lower anything else, in this example
# end
# }
#
# See arm.rb for a different example, in which we lower all BaseIndex addresses
# that have non-zero offset, all Address addresses that have large offsets, and
# all other addresses (like AbsoluteAddress).
#
class Node
def riscLowerMalformedAddressesRecurse(list, topLevelNode, &block)
mapChildren {
| subNode |
subNode.riscLowerMalformedAddressesRecurse(list, topLevelNode, &block)
}
end
end
class Address
def riscLowerMalformedAddressesRecurse(list, node, &block)
return self if yield node, self
tmp = Tmp.new(codeOrigin, :gpr)
list << Instruction.new(codeOrigin, "move", [offset, tmp])
list << Instruction.new(codeOrigin, "addp", [base, tmp])
Address.new(codeOrigin, tmp, Immediate.new(codeOrigin, 0))
end
end
class BaseIndex
def riscLowerMalformedAddressesRecurse(list, node, &block)
return self if yield node, self
tmp = Tmp.new(codeOrigin, :gpr)
list << Instruction.new(codeOrigin, "leap", [BaseIndex.new(codeOrigin, base, index, scale, Immediate.new(codeOrigin, 0)), tmp])
Address.new(codeOrigin, tmp, offset).riscLowerMalformedAddressesRecurse(list, node, &block)
end
end
class AbsoluteAddress
def riscLowerMalformedAddressesRecurse(list, node, &block)
return self if yield node, self
tmp = Tmp.new(codeOrigin, :gpr)
list << Instruction.new(codeOrigin, "move", [address, tmp])
Address.new(codeOrigin, tmp, Immediate.new(codeOrigin, 0))
end
end
def riscLowerMalformedAddresses(list, &block)
newList = []
list.each {
| node |
newList << node.riscLowerMalformedAddressesRecurse(newList, node, &block)
}
newList
end
#
# Lowering of malformed addresses in double loads and stores. For example:
#
# loadd [foo, bar, 8], baz
#
# becomes:
#
# leap [foo, bar, 8], tmp
# loadd [tmp], baz
#
class Node
def riscDoubleAddress(list)
self
end
end
class BaseIndex
def riscDoubleAddress(list)
tmp = Tmp.new(codeOrigin, :gpr)
list << Instruction.new(codeOrigin, "leap", [self, tmp])
Address.new(codeOrigin, tmp, Immediate.new(codeOrigin, 0))
end
end
def riscLowerMalformedAddressesDouble(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "loadd"
newList << Instruction.new(node.codeOrigin, "loadd", [node.operands[0].riscDoubleAddress(newList), node.operands[1]], node.annotation)
when "stored"
newList << Instruction.new(node.codeOrigin, "stored", [node.operands[0], node.operands[1].riscDoubleAddress(newList)], node.annotation)
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of misplaced immediates for opcodes in opcodeList. For example, if storei is in opcodeList:
#
# storei 0, [foo]
#
# will become:
#
# move 0, tmp
# storei tmp, [foo]
#
def riscLowerMisplacedImmediates(list, opcodeList)
newList = []
list.each {
| node |
if node.is_a? Instruction
if opcodeList.include? node.opcode
operands = node.operands
newOperands = []
operands.each {
| operand |
if operand.is_a? Immediate
tmp = Tmp.new(operand.codeOrigin, :gpr)
newList << Instruction.new(operand.codeOrigin, "move", [operand, tmp])
newOperands << tmp
else
newOperands << operand
end
}
newList << Instruction.new(node.codeOrigin, node.opcode, newOperands, node.annotation)
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of malformed immediates except when used in a "move" instruction.
# For example:
#
# addp 642641, foo
#
# will become:
#
# move 642641, tmp
# addp tmp, foo
#
class Node
def riscLowerMalformedImmediatesRecurse(list, validImmediates)
mapChildren {
| node |
node.riscLowerMalformedImmediatesRecurse(list, validImmediates)
}
end
end
class Address
def riscLowerMalformedImmediatesRecurse(list, validImmediates)
self
end
end
class BaseIndex
def riscLowerMalformedImmediatesRecurse(list, validImmediates)
self
end
end
class AbsoluteAddress
def riscLowerMalformedImmediatesRecurse(list, validImmediates)
self
end
end
class Immediate
def riscLowerMalformedImmediatesRecurse(list, validImmediates)
unless validImmediates.include? value
tmp = Tmp.new(codeOrigin, :gpr)
list << Instruction.new(codeOrigin, "move", [self, tmp])
tmp
else
self
end
end
end
def riscLowerMalformedImmediates(list, validImmediates)
newList = []
list.each {
| node |
if node.is_a? Instruction
annotation = node.annotation
case node.opcode
when "move"
newList << node
when "addi", "addp", "addis", "subi", "subp", "subis"
if node.operands[0].is_a? Immediate and
(not validImmediates.include? node.operands[0].value) and
validImmediates.include? -node.operands[0].value
node.operands.size == 2
if node.opcode =~ /add/
newOpcode = "sub" + $~.post_match
else
newOpcode = "add" + $~.post_match
end
newList << Instruction.new(node.codeOrigin, newOpcode,
[Immediate.new(node.codeOrigin, -node.operands[0].value)] + node.operands[1..-1],
annotation)
else
newList << node.riscLowerMalformedImmediatesRecurse(newList, validImmediates)
end
when "muli", "mulp"
if node.operands[0].is_a? Immediate
tmp = Tmp.new(codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "move", [node.operands[0], tmp], annotation)
newList << Instruction.new(node.codeOrigin, node.opcode, [tmp] + node.operands[1..-1])
else
newList << node.riscLowerMalformedImmediatesRecurse(newList, validImmediates)
end
else
newList << node.riscLowerMalformedImmediatesRecurse(newList, validImmediates)
end
else
newList << node
end
}
newList
end
#
# Lowering of misplaced addresses. For example:
#
# addi foo, [bar]
#
# will become:
#
# loadi [bar], tmp
# addi foo, tmp
# storei tmp, [bar]
#
# Another example:
#
# addi [foo], bar
#
# will become:
#
# loadi [foo], tmp
# addi tmp, bar
#
def riscAsRegister(preList, postList, operand, suffix, needStore)
return operand unless operand.address?
tmp = Tmp.new(operand.codeOrigin, if suffix == "d" then :fpr else :gpr end)
preList << Instruction.new(operand.codeOrigin, "load" + suffix, [operand, tmp])
if needStore
postList << Instruction.new(operand.codeOrigin, "store" + suffix, [tmp, operand])
end
tmp
end
def riscAsRegisters(preList, postList, operands, suffix)
newOperands = []
operands.each_with_index {
| operand, index |
newOperands << riscAsRegister(preList, postList, operand, suffix, index == operands.size - 1)
}
newOperands
end
def riscLowerMisplacedAddresses(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
postInstructions = []
annotation = node.annotation
case node.opcode
when "addi", "addis", "andi", "lshifti", "muli", "negi", "noti", "ori", "oris",
"rshifti", "urshifti", "subi", "subis", "xori", /^bi/, /^bti/, /^ci/, /^ti/
newList << Instruction.new(node.codeOrigin,
node.opcode,
riscAsRegisters(newList, postInstructions, node.operands, "i"),
annotation)
when "addp", "andp", "lshiftp", "mulp", "negp", "orp", "rshiftp", "urshiftp",
"subp", "xorp", /^bp/, /^btp/, /^cp/
newList << Instruction.new(node.codeOrigin,
node.opcode,
riscAsRegisters(newList, postInstructions, node.operands, "p"),
annotation)
when "bbeq", "bbneq", "bba", "bbaeq", "bbb", "bbbeq", "btbz", "btbnz", "tbz", "tbnz",
"cbeq", "cbneq", "cba", "cbaeq", "cbb", "cbbeq"
newList << Instruction.new(node.codeOrigin,
node.opcode,
riscAsRegisters(newList, postInstructions, node.operands, "b"),
annotation)
when "bbgt", "bbgteq", "bblt", "bblteq", "btbs", "tbs", "cbgt", "cbgteq", "cblt", "cblteq"
newList << Instruction.new(node.codeOrigin,
node.opcode,
riscAsRegisters(newList, postInstructions, node.operands, "bs"),
annotation)
when "addd", "divd", "subd", "muld", "sqrtd", /^bd/
newList << Instruction.new(node.codeOrigin,
node.opcode,
riscAsRegisters(newList, postInstructions, node.operands, "d"),
annotation)
when "jmp", "call"
newList << Instruction.new(node.codeOrigin,
node.opcode,
[riscAsRegister(newList, postInstructions, node.operands[0], "p", false)],
annotation)
else
newList << node
end
newList += postInstructions
else
newList << node
end
}
newList
end
#
# Lowering of register reuse in compare instructions. For example:
#
# cieq t0, t1, t0
#
# will become:
#
# mov tmp, t0
# cieq tmp, t1, t0
#
def riscLowerRegisterReuse(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
annotation = node.annotation
case node.opcode
when "cieq", "cineq", "cia", "ciaeq", "cib", "cibeq", "cigt", "cigteq", "cilt", "cilteq",
"cpeq", "cpneq", "cpa", "cpaeq", "cpb", "cpbeq", "cpgt", "cpgteq", "cplt", "cplteq",
"tis", "tiz", "tinz", "tbs", "tbz", "tbnz", "tps", "tpz", "tpnz", "cbeq", "cbneq",
"cba", "cbaeq", "cbb", "cbbeq", "cbgt", "cbgteq", "cblt", "cblteq"
if node.operands.size == 2
if node.operands[0] == node.operands[1]
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "move", [node.operands[0], tmp], annotation)
newList << Instruction.new(node.codeOrigin, node.opcode, [tmp, node.operands[1]])
else
newList << node
end
else
raise "Wrong number of arguments at #{node.codeOriginString}" unless node.operands.size == 3
if node.operands[0] == node.operands[2]
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "move", [node.operands[0], tmp], annotation)
newList << Instruction.new(node.codeOrigin, node.opcode, [tmp, node.operands[1], node.operands[2]])
elsif node.operands[1] == node.operands[2]
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "move", [node.operands[1], tmp], annotation)
newList << Instruction.new(node.codeOrigin, node.opcode, [node.operands[0], tmp, node.operands[2]])
else
newList << node
end
end
else
newList << node
end
else
newList << node
end
}
newList
end
|