git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Some simplifications and cleanup in runtime rep
commit9fd2ad00687b732a576998d5a3341765f4a65671 authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 3 Sep 2018, 10:43 (8 years ago) parented95a50
4 files changed+47−57
runtime/purescript.h+7−5 src/CodeGen/Cpp.hs+3−9 src/CodeGen/Cpp/Common.hs+24 src/CodeGen/Cpp/Printer.hs+13−43
runtime/purescript.h+7−5
@@ -54,8 +54,11 @@ namespace purescript {
5454 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
5555 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
5656 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
57+ boxed(const string& s) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
5758 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
59+ boxed(const array_t& l) : std::shared_ptr<void>(std::make_shared<array_t>(l)) {}
5860 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
61+ boxed(const dict_t& m) : std::shared_ptr<void>(std::make_shared<dict_t>(m)) {}
5962
6063 template <typename T,
6164 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
@@ -128,7 +131,7 @@ namespace purescript {
128131 }
129132
130133 template <typename T>
131− constexpr inline auto unbox(const boxed& b) -> const T& {
134+ constexpr auto unbox(const boxed& b) -> const T& {
132135 return *static_cast<const T*>(b.get());
133136 }
134137
@@ -146,13 +149,12 @@ namespace purescript {
146149 }
147150
148151 template <typename T>
149− constexpr inline auto peek(boxed& b) -> T& {
152+ constexpr auto peek(boxed& b) -> T& {
150153 return *static_cast<T*>(b.get());
151154 }
152155
153− template <typename T>
154− inline auto copy(const boxed& b) -> boxed {
155− return box<T>(unbox<T>(b));
156+ inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
157+ return unbox<boxed::array_t>(a).size();
156158 }
157159
158160 using fn_t = boxed::fn_t;
src/CodeGen/Cpp.hs+3−9
@@ -193,9 +193,9 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
193193 updates <- mapM (sndM valueToCpp) ps
194194 obj' <- freshName
195195 let objVar = AST.Var Nothing obj'
196− copy = AST.VariableIntroduction Nothing obj' (Just $ makeCopy obj)
196+ copy = AST.VariableIntroduction Nothing obj' (Just $ AST.App Nothing (AST.Var Nothing (unbox dictType)) [obj])
197197 assign (k, v) = AST.Assignment Nothing (accessorString k objVar) v
198− sts = copy : (assign <$> updates) ++ [AST.Return Nothing $ makeCopy objVar]
198+ sts = copy : (assign <$> updates) ++ [AST.Return Nothing objVar]
199199 return $ AST.App Nothing (AST.Function Nothing Nothing [] (AST.Block Nothing sts)) []
200200 valueToCpp (Abs _ arg val) = do
201201 ret <- valueToCpp val
@@ -387,11 +387,5 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
387387emptyAnn :: Ann
388388emptyAnn = (SourceSpan "" (SourcePos 0 0) (SourcePos 0 0), [], Nothing, Nothing)
389389
390−makeCopy :: AST -> AST
391−makeCopy obj = AST.App Nothing (AST.Var Nothing "copy<dict_t>") [obj]
392−
393390arrayLength :: AST -> AST
394−arrayLength array = applyMethod (AST.App Nothing (AST.Var Nothing "unbox<array_t>") [array]) "size"
395−
396−applyMethod :: AST -> Text -> AST
397−applyMethod obj method = (AST.Indexer Nothing (AST.ArrayLiteral Nothing [AST.Var Nothing method]) obj)
391+arrayLength a = AST.App Nothing (AST.Var Nothing arrayLengthFn) [a]
src/CodeGen/Cpp/Common.hs+24
@@ -192,5 +192,29 @@ cppLiterals =
192192 , "unbox"
193193 ]
194194
195+dictType :: Text
196+dictType = "dict_t"
197+
198+arrayType :: Text
199+arrayType = "array_t"
200+
201+int :: Text
202+int = "int"
203+
204+float :: Text
205+float = "double"
206+
207+bool :: Text
208+bool = "bool"
209+
210+string :: Text
211+string = "string"
212+
213+unbox :: Text -> Text
214+unbox t = "unbox<" <> t <> ">"
215+
216+arrayLengthFn :: Text
217+arrayLengthFn = "array_length"
218+
195219tcoLoop :: Text
196220tcoLoop = "$TCO_loop$"
src/CodeGen/Cpp/Printer.hs+13−43
@@ -106,9 +106,9 @@ literals = mkPattern' match'
106106 , Just op <- renderOp fn
107107 , Just t <- unboxType dict
108108 = mconcat <$> sequence
109− [ return $ emit $ unbox t x
109+ [ return $ emit $ unbox' t x
110110 , return $ emit op
111− , return $ emit $ unbox t y
111+ , return $ emit $ unbox' t y
112112 ]
113113 match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
114114 | fnMod == dictMod
@@ -117,7 +117,7 @@ literals = mkPattern' match'
117117 , Just t <- unboxType dict
118118 = mconcat <$> sequence
119119 [ return $ emit "-"
120− , return $ emit $ unbox t n
120+ , return $ emit $ unbox' t n
121121 ]
122122 match (App _ val args) = mconcat <$> sequence
123123 [ prettyPrintCpp' val
@@ -179,18 +179,6 @@ literals = mkPattern' match'
179179 , prettyPrintCpp' prop
180180 , return $ emit "()"
181181 ]
182− match (Indexer _ (ArrayLiteral _ [name]) val) = mconcat <$> sequence
183− [ prettyPrintCpp' val
184− , return $ emit "."
185− , prettyPrintCpp' name
186− , return $ emit "()"
187− ]
188− match (Indexer _ (NumericLiteral _ (Left index)) val) = mconcat <$> sequence
189− [ prettyPrintCpp' val
190− , return $ emit "["
191− , return . emit . T.pack $ show index
192− , return $ emit "]"
193− ]
194182 match (Indexer _ prop val) = mconcat <$> sequence
195183 [ prettyPrintCpp' val
196184 , return $ emit "["
@@ -198,7 +186,7 @@ literals = mkPattern' match'
198186 , return $ emit "]"
199187 ]
200188 match (InstanceOf _ val ty) = mconcat <$> sequence
201− [ return . emit $ unbox dictType val
189+ [ return . emit $ unbox' dictType val
202190 , return $ emit ".contains("
203191 , prettyPrintCpp' ty
204192 , return $ emit ")"
@@ -225,21 +213,21 @@ literals = mkPattern' match'
225213 ]
226214 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
227215 [ return $ emit "if ("
228− , return $ emit $ unbox "bool" cond
216+ , return $ emit $ unbox' "bool" cond
229217 , return $ emit ") "
230218 , prettyPrintCpp' thens
231219 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
232220 ]
233221 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
234222 [ return $ emit "if (!("
235− , return $ emit $ unbox "bool" cond
223+ , return $ emit $ unbox' "bool" cond
236224 , return $ emit ")) "
237225 , prettyPrintCpp' thens
238226 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
239227 ]
240228 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
241229 [ return $ emit "if ("
242− , return $ emit $ unbox t x
230+ , return $ emit $ unbox' t x
243231 , return $ emit $ " == "
244232 , prettyPrintCpp' y
245233 , return $ emit ") "
@@ -251,7 +239,7 @@ literals = mkPattern' match'
251239 | Right _ <- n = float
252240 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
253241 [ return $ emit "if ("
254− , return $ emit $ unbox string a
242+ , return $ emit $ unbox' string a
255243 , return $ emit $ " == "
256244 , prettyPrintCpp' b
257245 , return $ emit ") "
@@ -412,11 +400,11 @@ stringLiteral pss | Just s <- decodeString pss =
412400 encodeChar c = T.singleton $ c
413401stringLiteral _ = "\"\\uFFFD\""
414402
415−unbox :: Text -> AST -> Text
416−unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v
417−unbox _ v@(BooleanLiteral{}) = prettyPrintCpp1 v
418−unbox _ v@(StringLiteral{}) = prettyPrintCpp1 v
419−unbox t v = "unbox<" <> t <> ">(" <> prettyPrintCpp1 v <> ")"
403+unbox' :: Text -> AST -> Text
404+unbox' _ v@(NumericLiteral{}) = prettyPrintCpp1 v
405+unbox' _ v@(BooleanLiteral{}) = prettyPrintCpp1 v
406+unbox' _ v@(StringLiteral{}) = prettyPrintCpp1 v
407+unbox' t v = unbox t <> "(" <> prettyPrintCpp1 v <> ")"
420408
421409unboxType :: Text -> Maybe Text
422410unboxType t
@@ -490,24 +478,6 @@ implFooterSource mn foreigns =
490478 \}\n\n\
491479 \"
492480
493−dictType :: Text
494−dictType = "dict_t"
495−
496−arrayType :: Text
497−arrayType = "array_t"
498−
499−int :: Text
500−int = "int"
501−
502−float :: Text
503−float = "double"
504−
505−bool :: Text
506−bool = "bool"
507−
508−string :: Text
509−string = "string"
510−
511481varDecl :: Text
512482varDecl = "const boxed&"
513483