/compiler/nativeGen/RegAlloc/Linear/StackMap.hs

https://github.com/pepeiborra/ghc · Haskell · 72 lines · 32 code · 18 blank · 22 comment · 3 complexity · 48db6cca04dd836956536f3282d4c26f MD5 · raw file

  1. -- | The assignment of virtual registers to stack slots
  2. -- We have lots of stack slots. Memory-to-memory moves are a pain on most
  3. -- architectures. Therefore, we avoid having to generate memory-to-memory moves
  4. -- by simply giving every virtual register its own stack slot.
  5. -- The StackMap stack map keeps track of virtual register - stack slot
  6. -- associations and of which stack slots are still free. Once it has been
  7. -- associated, a stack slot is never "freed" or removed from the StackMap again,
  8. -- it remains associated until we are done with the current CmmProc.
  9. --
  10. module RegAlloc.Linear.StackMap (
  11. StackSlot,
  12. StackMap(..),
  13. emptyStackMap,
  14. getStackSlotFor
  15. )
  16. where
  17. import RegAlloc.Linear.FreeRegs
  18. import Outputable
  19. import Platform
  20. import UniqFM
  21. import Unique
  22. -- | Identifier for a stack slot.
  23. type StackSlot = Int
  24. data StackMap
  25. = StackMap
  26. { -- | The slots that are still available to be allocated.
  27. stackMapFreeSlots :: [StackSlot]
  28. -- | Assignment of vregs to stack slots.
  29. , stackMapAssignment :: UniqFM StackSlot }
  30. -- | An empty stack map, with all slots available.
  31. emptyStackMap :: Platform -> StackMap
  32. emptyStackMap platform = StackMap [0 .. maxSpillSlots platform] emptyUFM
  33. -- | If this vreg unique already has a stack assignment then return the slot number,
  34. -- otherwise allocate a new slot, and update the map.
  35. --
  36. getStackSlotFor :: StackMap -> Unique -> (StackMap, Int)
  37. getStackSlotFor (StackMap [] _) _
  38. -- This happens all the time when trying to compile darcs' SHA1.hs, see Track #1993
  39. -- SHA1.lhs has also been added to the Crypto library on Hackage,
  40. -- so we see this all the time.
  41. --
  42. -- It would be better to automatically invoke the graph allocator, or do something
  43. -- else besides panicing, but that's a job for a different day. -- BL 2009/02
  44. --
  45. = panic $ "RegAllocLinear.getStackSlotFor: out of stack slots\n"
  46. ++ " If you are trying to compile SHA1.hs from the crypto library then this\n"
  47. ++ " is a known limitation in the linear allocator.\n"
  48. ++ "\n"
  49. ++ " Try enabling the graph colouring allocator with -fregs-graph instead."
  50. ++ " You can still file a bug report if you like.\n"
  51. getStackSlotFor fs@(StackMap (freeSlot:stack') reserved) reg =
  52. case lookupUFM reserved reg of
  53. Just slot -> (fs, slot)
  54. Nothing -> (StackMap stack' (addToUFM reserved reg freeSlot), freeSlot)