Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler/Decompiler] Better support for Bitfield and Enum types #374

Merged
merged 4 commits into from
Apr 22, 2021

Conversation

water111
Copy link
Collaborator

@water111 water111 commented Apr 21, 2021

This makes enums real types, like bitfields.

So far:

  • Fixed bugs where boxed arrays of enums/bitfields were broken
  • Made enum types real types in the compiler. They can be used for fields, pointers, function args/return values, locals, etc, just like bitfields. Just like bitfields, they require casting to and from int. Most of the reason for doing this was to make the type systems of the compiler/decompiler match more closely.
  • Support for decompiling bitfield enum constants that appear in code

To Do:

  • Support bitfield enum constant in static data too (should be easy)
  • Fix bad casting with (logior <bitfield-enum> <constant>). The type pass correctly determines that the result of this is also a bitfield enum, but somehow things go wrong later on.
  • Try to find some examples of non-bitfield enums and add those too.

Looks good

    lwu v1, 4(s4)             ;; (set! v1-17 (l.wu (+ arg0 4)))[37] [s4: process ] -> [v1: process-mask ]
    ori v1, v1, 512           ;; (set! v1-18 (logior v1-17 512))[38] [v1: process-mask ] -> [v1: process-mask ]
    sw v1, 4(s4)              ;; (s.w! (+ arg0 4) v1-18)[39] [v1: process-mask s4: process ] -> []

Also good:

    (set! v1-17 (-> a1-0 mask))
    (set! v1-18 (logior v1-17 512))
    (set! (-> a1-0 mask) v1-18)

Wrong:

set! (-> arg0 mask) (logior (the-as int (-> arg0 mask)) 512))

if it attempted to cast the 512 to a process-mask it would get decompiled properly.

@@ -3,20 +3,20 @@
{
"game_version":1,
// the order here matters (not sure that this is true any more...). KERNEL and GAME should go first
"dgo_names":["CGO/KERNEL.CGO","CGO/GAME.CGO",
"dgo_names_":["CGO/KERNEL.CGO","CGO/GAME.CGO",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this!

@water111
Copy link
Collaborator Author

A bunch of bitfield enum things now work properly

operations with logand and logior

;; old
(nonzero? (logand (-> arg0 mask) 256))
;; new
(nonzero? (logand (-> arg0 mask) (process-mask process-tree)))
;; old
(set! (-> pp mask) (logior (-> pp mask) 1024))
;; new
(set! (-> pp mask) (logior (-> pp mask) (process-mask going)))

operations with logand and lognot that were constant folded:

;; old
(set! (-> obj mask) (logand -961 (the-as int (-> arg0 mask))))
;; new
(set!
   (-> obj mask)
   (logand
    (lognot (process-mask sleep sleep-code process-tree heap-shrunk))
    (-> arg0 mask)
    )
   )

Setting a value:

;; old
(set! (-> a0-57 mask) (the-as uint #x4011c))
;; new 
(set! (-> a0-57 mask) (process-mask pause menu progress process-tree camera))

Still needs support for static data, and for non-bitfield enums.

@coveralls
Copy link

coveralls commented Apr 22, 2021

Pull Request Test Coverage Report for Build 775900710

  • 128 of 149 (85.91%) changed or added relevant lines in 15 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 69.305%

Changes Missing Coverage Covered Lines Changed/Added Lines %
common/type_system/Type.cpp 18 20 90.0%
common/type_system/defenum.cpp 16 18 88.89%
decompiler/util/data_decompile.cpp 26 28 92.86%
decompiler/IR2/FormExpressionAnalysis.cpp 19 34 55.88%
Totals Coverage Status
Change from base Build 775850399: 0.05%
Covered Lines: 33748
Relevant Lines: 48695

💛 - Coveralls

@water111 water111 marked this pull request as ready for review April 22, 2021 23:08
@water111 water111 merged commit 060b125 into master Apr 22, 2021
@water111 water111 deleted the w/fix-types-in-defun branch April 22, 2021 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants