Skip to content

Commit 3679632

Browse files
committed
Working again, now with FieldRefs
1 parent 4fcea2a commit 3679632

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

unison-runtime/src/Unison/Runtime/MCode.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Unison.Runtime.MCode
3636
Branch,
3737
RBranch,
3838
RecordFieldMappings (..),
39+
convertFieldNamesToRefs,
3940
emitCombs,
4041
emitComb,
4142
resolveCombs,
@@ -711,7 +712,7 @@ data RefNums = RN
711712
-- Map record schemas into their runtime reference
712713
recNum :: ANF.RecordSchema -> ANF.RecordRef,
713714
-- Map record field names into their runtime reference
714-
recField :: ANF.FieldName -> FieldRef
715+
recField :: RecordFieldMappings -> ANF.FieldName -> FieldRef
715716
}
716717

717718
emptyRNs :: RefNums
@@ -993,7 +994,7 @@ data RecordFieldMappings
993994
deriving stock (Show, Eq, Ord)
994995

995996
-- | Note that the Ord instance for Field Refs is arbitrary and not tied to the field name Ord instance.
996-
convertFieldNamesToRefs :: (Traversable f) => f ANF.FieldName -> Emit (f FieldRef)
997+
convertFieldNamesToRefs :: (MonadState RecordFieldMappings m, Traversable f) => f ANF.FieldName -> m (f FieldRef)
997998
convertFieldNamesToRefs names = for names \name -> do
998999
RecordFieldMappings next m <- get
9991000
case BiMap.lookupL name m of
@@ -1125,8 +1126,9 @@ emitSection _ _ grpn _ ctx (TFOp p args) =
11251126
. VArgV
11261127
$ countBlock ctx
11271128
emitSection rns grpr grpn rec ctx (TApp f args) =
1128-
emitClosures grpr grpn rec ctx args $ \ctx as ->
1129-
countCtx ctx $ emitFunction rns grpr grpn rec ctx f as
1129+
emitClosures grpr grpn rec ctx args $ \ctx as -> do
1130+
rfm <- get
1131+
countCtx ctx $ emitFunction rns rfm grpr grpn rec ctx f as
11301132
emitSection rns grpr grpn rec ctx (TLocal v bo)
11311133
| Just (i, BX) <- ctxResolve ctx v =
11321134
Ins (InLocal i)
@@ -1237,21 +1239,22 @@ emitSection _ _ _ _ _ tm =
12371239
emitFunction ::
12381240
(Var v) =>
12391241
RefNums ->
1242+
RecordFieldMappings ->
12401243
Reference ->
12411244
Word64 -> -- self combinator number
12421245
RCtx v -> -- recursive binding group
12431246
Ctx v -> -- local context
12441247
Func Reference v ->
12451248
Args ->
12461249
Section
1247-
emitFunction _ grpr grpn rec ctx (FVar v) as
1250+
emitFunction _ _rfms grpr grpn rec ctx (FVar v) as
12481251
| Just (i, BX) <- ctxResolve ctx v =
12491252
App False (Stk i) as
12501253
| Just j <- rctxResolve rec v =
12511254
let cix = CIx grpr grpn j
12521255
in App False (Env cix cix) as
12531256
| otherwise = emitSectionVErr v
1254-
emitFunction rns _grpr _ _ _ (FComb r) as
1257+
emitFunction rns _rfms _grpr _ _ _ (FComb r) as
12551258
| Just k <- anum rns r,
12561259
countArgs as == k -- exactly saturated call
12571260
=
@@ -1262,19 +1265,19 @@ emitFunction rns _grpr _ _ _ (FComb r) as
12621265
where
12631266
n = cnum rns r
12641267
cix = CIx r n 0
1265-
emitFunction rns _grpr _ _ _ (FCon r t) as =
1268+
emitFunction rns _rfms _grpr _ _ _ (FCon r t) as =
12661269
Ins (Pack r (packTags rt t) as)
12671270
. Yield
12681271
$ VArg1 0
12691272
where
12701273
rt = toEnum . fromIntegral $ dnum rns r
1271-
emitFunction rns _grpr _ _ _ (FRec rs@(ANF.RecordSchema fields)) as =
1272-
Ins (RecPack recRef (V.fromList . fmap (recField rns) $ Set.toList fields) as)
1274+
emitFunction rns rfms _grpr _ _ _ (FRec rs@(ANF.RecordSchema fields)) as =
1275+
Ins (RecPack recRef (V.fromList . fmap (recField rns rfms) $ Set.toList fields) as)
12731276
. Yield
12741277
$ VArg1 0
12751278
where
12761279
recRef = recNum rns rs
1277-
emitFunction rns _grpr _ _ _ (FReq r e) as =
1280+
emitFunction rns _rfms _grpr _ _ _ (FReq r e) as =
12781281
-- Currently implementing packed calling convention for abilities
12791282
-- TODO ct is 16 bits, but a is 48 bits. This will be a problem if we have
12801283
-- more than 2^16 types.
@@ -1284,11 +1287,11 @@ emitFunction rns _grpr _ _ _ (FReq r e) as =
12841287
where
12851288
a = dnum rns r
12861289
rt = toEnum . fromIntegral $ a
1287-
emitFunction _ _grpr _ _ ctx (FCont k) as
1290+
emitFunction _ _rfms _grpr _ _ ctx (FCont k) as
12881291
| Just (i, BX) <- ctxResolve ctx k = Jump i as
12891292
| Nothing <- ctxResolve ctx k = emitFunctionVErr k
12901293
| otherwise = internalBug [] $ "emitFunction: continuations are boxed"
1291-
emitFunction _ _grpr _ _ _ (FPrim _) _ =
1294+
emitFunction _ _rfms _grpr _ _ _ (FPrim _) _ =
12921295
internalBug [] "emitFunction: impossible"
12931296

12941297
countBlock :: Ctx v -> Int
@@ -1351,8 +1354,9 @@ emitLet rns _ grpn _ _ _ ctx (TApp (FCon r n) args) =
13511354
fmap (Ins . Pack r (packTags rt n) $ emitArgs grpn ctx args)
13521355
where
13531356
rt = toEnum . fromIntegral $ dnum rns r
1354-
emitLet rns _ grpn _ _ _ ctx (TApp (FRec rs@(ANF.RecordSchema fields)) args) =
1355-
fmap (Ins . RecPack (recNum rns rs) (V.fromList . fmap (recField rns) $ Set.toList fields) $ emitArgs grpn ctx args)
1357+
emitLet rns _ grpn _ _ _ ctx (TApp (FRec rs@(ANF.RecordSchema fields)) args) = \es -> do
1358+
rfm <- get
1359+
fmap (Ins . RecPack (recNum rns rs) (V.fromList . fmap (recField rns rfm) $ Set.toList fields) $ emitArgs grpn ctx args) es
13561360
emitLet _ _ grpn _ _ _ ctx (TApp (FPrim p) args) =
13571361
fmap (Ins . either emitPOp emitFOp p $ emitArgs grpn ctx args)
13581362
emitLet _ _ _ _ _ _ ctx (TDiscard v)

unison-runtime/src/Unison/Runtime/Machine.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,10 +1616,16 @@ cacheAdd0 recSchemas ntys0 (normalizeCodes -> termSuperGroups) sands cc = do
16161616
let newRecSchemaMap = BM.fromList $ zip (Set.toList newRecSchemas) (ANF.RecordRef <$> [nrs ..])
16171617
rtm <- updateMap (M.fromList $ zip rs [ntm ..]) (refTm cc)
16181618
rrLookup <- updateMap newRecSchemaMap (recordRefs cc)
1619-
oldRfms@(RecordFieldMappings _ rfmBM) <- readTVar (recordFieldMappings cc)
1619+
oldRfms@(RecordFieldMappings _ existingRfmsBM) <- readTVar (recordFieldMappings cc)
1620+
let recFields =
1621+
BM.toList rrLookup
1622+
<&> fst
1623+
& foldMap (\(ANF.RecordSchema flds) -> flds)
1624+
& Set.toList
1625+
let currentRFMs = flip execState oldRfms (convertFieldNamesToRefs recFields)
16201626
-- check for missing references
16211627
let arities = fmap (head . ANF.arities) int <> builtinArities
1622-
lookupRN fn = fromMaybe (error $ "cacheAdd0: missing reference for FieldName: " ++ show fn) $ BM.lookupL fn rfmBM
1628+
lookupRN (RecordFieldMappings _ rfmBM) fn = fromMaybe (error $ "cacheAdd0: missing reference for FieldName: " <> show fn <> " in map: " <> (show (rfmBM <> existingRfmsBM)) <> " and schemas: " <> show rrLookup) $ BM.lookupL fn (rfmBM <> existingRfmsBM)
16231629
rns = RN (refLookup "ty" rty) (refLookup "tm" rtm) (flip M.lookup arities) (recordRefLookup rrLookup) lookupRN
16241630
combinate :: Word64 -> (Reference, SuperGroup Reference Symbol) -> State RecordFieldMappings (Word64, EnumMap Word64 Comb)
16251631
combinate n (r, g) = (n,) <$> emitCombs rns r n g
@@ -1639,7 +1645,7 @@ cacheAdd0 recSchemas ntys0 (normalizeCodes -> termSuperGroups) sands cc = do
16391645
let (emittedCombs, newRFMs) =
16401646
zipWith combinate [ntm ..] (M.toList opt)
16411647
& sequenceA
1642-
& flip runState oldRfms
1648+
& flip runState currentRFMs
16431649
unresolvedNewCombs :: EnumMap Word64 (GCombs any CombIx)
16441650
unresolvedNewCombs =
16451651
emittedCombs

unison-runtime/src/Unison/Runtime/Machine/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ codeValidate ::
348348
codeValidate cc tml = do
349349
rty0 <- readTVarIO (refTy cc)
350350
fty <- readTVarIO (freshTy cc)
351-
(RecordFieldMappings _ rfmsBM) <- readTVarIO (recordFieldMappings cc)
351+
(RecordFieldMappings _ existingRfmsBM) <- readTVarIO (recordFieldMappings cc)
352352
recRefs <- readTVarIO (recordRefs cc)
353353
let f b r
354354
| b, M.notMember r rty0 = S.singleton r
@@ -362,7 +362,7 @@ codeValidate cc tml = do
362362
rtm0 <- readTVarIO (refTm cc)
363363
let rs = fst <$> tml
364364
rtm = rtm0 `M.union` M.fromList (zip rs [ftm ..])
365-
lookupFR fn = fromMaybe (error $ "Missing FieldRef for FieldName: " <> show fn) $ BM.lookupL fn rfmsBM
365+
lookupFR (RecordFieldMappings _ rfmsBM) fn = fromMaybe (error $ "Missing FieldRef for FieldName: " <> show fn) $ BM.lookupL fn (rfmsBM <> existingRfmsBM)
366366
rns = RN (refLookup "ty" rty) (refLookup "tm" rtm) (const Nothing) (recordRefLookup recRefs') lookupFR
367367
combinate (n, (r, g)) = evaluate $ emitCombs rns r n g
368368
(Nothing <$ traverse_ combinate (zip [ftm ..] tml))

0 commit comments

Comments
 (0)