Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Use solely offset tables in the Haskell world #783

Merged
merged 3 commits into from
Sep 16, 2020

Conversation

gkaracha
Copy link
Member

@gkaracha gkaracha commented Sep 15, 2020

Depends on #782.

Following #782, this PR switches all symbol tables in Haskell land into offset tables; the rts still deals solely with symbol tables. Along with it comes a lot of renaming ("symbol table" => "offset table"), and type changes (Word32 used for offsets, as opposed to Int64 used for addresses).

@gkaracha gkaracha requested a review from TerrorJack September 16, 2020 07:32
@gkaracha
Copy link
Member Author

@TerrorJack merely following the types, I partitioned genExportStaticObj into genStaticsExportStaticObj and genFunctionsExportStaticObj, but looking into their structure, I find genStaticsExportStaticObj to be a little weird (given that internally it calls genExportStaticFunc). FWIW I have yet to encounter a non-empty req.staticsExportsStatic in the rts 🤔

@gkaracha gkaracha marked this pull request as ready for review September 16, 2020 07:41
@gkaracha
Copy link
Member Author

As an aside, I'd have preferred to use Word32 instead of Int32 so that all valid offsets are interpreted correctly (even things like 0x80000000), but the offset of DataSegment is a ConstI32, which takes an Int32 anyway.

@gkaracha
Copy link
Member Author

Perhaps I should switch to Word32 and use fromIntegral; when #776 lands at least we'll know about this happening at compile time.

@dpulls
Copy link

dpulls bot commented Sep 16, 2020

🎉 All dependencies have been resolved !

@gkaracha gkaracha self-assigned this Sep 16, 2020
  WIP (phase 3: tighten the types: Int32 => Word32)
    So that for example the call to fromIntegral in mkDataAddress does
    the right thing, even if the offset is 0x80000000.
  WIP (remove forgotten comment)
  WIP (phase 2: tighten the types: Int64 => Int32)
  WIP (phase 1: offsets only in Haskell land
@gkaracha gkaracha force-pushed the gkaracha/wip-part-b-offset-tables-asterius branch from c538192 to 2c276c5 Compare September 16, 2020 10:14
@TerrorJack
Copy link
Member

@gkaracha You should be able to completely remove the functionsExportsStatic field when generating the .req.mjs script in Asterius.Main. The genExportStaticObj function should only work with the data offset map, not function offset map.

This may sound unintuitive since genExportStaticObj handles Haskell function exports. But each static closure of an exported Haskell function is a data segment, so we're really dealing with data offsets here. I've no idea why I also did a functionsExportsStatic thing, but that should be redundant.

@gkaracha
Copy link
Member Author

Also, I am leaving the following renamings for last, lest GitHub treats them as file deletions and additions..

Asterius.Passes.DataSymbolTable     => Asterius.Passes.DataOffsetTable
Asterius.Passes.FunctionSymbolTable => Asterius.Passes.FunctionOffsetTable

@gkaracha
Copy link
Member Author

@gkaracha You should be able to completely remove the functionsExportsStatic field when generating the .req.mjs script in Asterius.Main. The genExportStaticObj function should only work with the data offset map, not function offset map.

This may sound unintuitive since genExportStaticObj handles Haskell function exports. But each static closure of an exported Haskell function is a data segment, so we're really dealing with data offsets here.

Wow, yes, that is unintuitive but now that you explained it it makes total sense. I'll fix that 👍

I've no idea why I also did a functionsExportsStatic thing, but that should be redundant.

Ah, that must have been me actually, when I separated the two sym_maps some time ago. I should have pointed out my confusion then I guess 😅

@TerrorJack
Copy link
Member

Also, I don't think #776 is about to land any time soon. binaryen forcibly uses overflow behavior of integers when representing literals. For instance, the C API uses signed int64_t for i64 rep, and we need to model that in Int64 in Haskell, so when handling large literals safeIntegral panics.

Maybe we can change our IR a bit, always use Word32/Word64 to represent literals. I'll still need to look a bit into GHC's FFI to see whether this will become a footgun.

@gkaracha gkaracha closed this Sep 16, 2020
@gkaracha gkaracha reopened this Sep 16, 2020
@gkaracha
Copy link
Member Author

Also, I don't think #776 is about to land any time soon. binaryen forcibly uses overflow behavior of integers when representing literals. For instance, the C API uses signed int64_t for i64 rep, and we need to model that in Int64 in Haskell, so when handling large literals safeIntegral panics.

Maybe we can change our IR a bit, always use Word32/Word64 to represent literals. I'll still need to look a bit into GHC's FFI to see whether this will become a footgun.

I see. Well, for now this PR basically would fail in these corner cases just as much as we do already, so I guess it doesn't make much of a difference (and I'd definitely leave IR changes to a separate PR). Do you have a preference for Int32 vs. Word32?

@TerrorJack
Copy link
Member

TerrorJack commented Sep 16, 2020

Let's stick with Int32 for now to keep the changes minimal.

Word32 offsets are fine since we don't stick them into ConstI32s.

@gkaracha
Copy link
Member Author

gkaracha commented Sep 16, 2020

Cool. Any other things to fix or are we just waiting for CI to finish? (I shall get started on updating #750 if you consider this one good to go)

EDIT: well, after I do the renaming also (#783 (comment)), that is.

@TerrorJack
Copy link
Member

All lgtm now, would you push the SymbolTable.hs to OffsetTable.hs renamings? Then I'd hit the merge button :)

@gkaracha
Copy link
Member Author

There, all done 🙂

@TerrorJack TerrorJack merged commit 5fc66fe into master Sep 16, 2020
@TerrorJack TerrorJack deleted the gkaracha/wip-part-b-offset-tables-asterius branch September 16, 2020 12:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants