From 9b8b39f4449e324ca283f52ea4a09709205b4bff Mon Sep 17 00:00:00 2001 From: Bayu Aldi Yansyah Date: Thu, 28 Jan 2016 11:22:27 +0700 Subject: [PATCH] samples: add Terra samples source: https://github.com/zdevito/terra/tree/master/tests --- samples/Terra/abouttocompile.t | 22 ++ samples/Terra/addlanguage1.t | 17 + samples/Terra/aggregatearr.t | 18 + samples/Terra/ainline.t | 24 ++ samples/Terra/alignment.t | 7 + samples/Terra/and.t | 9 + samples/Terra/anon.t | 26 ++ samples/Terra/anon3.t | 18 + samples/Terra/anonstruct.t | 12 + samples/Terra/anonstruct2.t | 7 + samples/Terra/antiquote1.t | 12 + samples/Terra/antiquote2.t | 13 + samples/Terra/antiquote3.t | 16 + samples/Terra/antiquote4.t | 11 + samples/Terra/antiquote5.t | 21 + samples/Terra/arith.t | 114 ++++++ samples/Terra/array.t | 60 +++ samples/Terra/arraylit.t | 29 ++ samples/Terra/arrayt.t | 75 ++++ samples/Terra/arrayt2.t | 82 ++++ samples/Terra/arrptr.t | 5 + samples/Terra/asm.t | 28 ++ samples/Terra/atoi.t | 7 + samples/Terra/avxhadd.t | 32 ++ samples/Terra/badname.t | 4 + samples/Terra/benchmark_fannkuchredux.t | 148 +++++++ samples/Terra/benchmark_nbody.t | 150 ++++++++ samples/Terra/bf.t | 60 +++ samples/Terra/blankexp.t | 7 + samples/Terra/blockescape.t | 23 ++ samples/Terra/blocking.t | 57 +++ samples/Terra/blocking2-fixed.t | 68 ++++ samples/Terra/blocking2.t | 81 ++++ samples/Terra/blocking3.t | 85 +++++ samples/Terra/bounce.t | 23 ++ samples/Terra/bug.t | 20 + samples/Terra/bug2.t | 20 + samples/Terra/bug3.t | 15 + samples/Terra/bug4.t | 14 + samples/Terra/calc.t | 33 ++ samples/Terra/call.t | 53 +++ samples/Terra/callbackcache.t | 9 + samples/Terra/canon.t | 45 +++ samples/Terra/canon2.t | 41 ++ samples/Terra/cast.t | 11 + samples/Terra/cbool.t | 13 + samples/Terra/cconv.t | 488 ++++++++++++++++++++++++ samples/Terra/cfgbug.t | 14 + samples/Terra/clanginfo.t | 1 + samples/Terra/class.t | 235 ++++++++++++ samples/Terra/class2.t | 45 +++ samples/Terra/class3.t | 36 ++ samples/Terra/class4.t | 22 ++ samples/Terra/class5.t | 54 +++ samples/Terra/class6.t | 66 ++++ samples/Terra/classifyfloatstructs.t | 137 +++++++ samples/Terra/clean.t | 17 + samples/Terra/cnames.t | 25 ++ samples/Terra/cnamespace.t | 24 ++ samples/Terra/cnamespaces.t | 32 ++ samples/Terra/compilecallback.t | 16 + samples/Terra/completec.t | 13 + samples/Terra/conflict.t | 12 + samples/Terra/constant.t | 35 ++ samples/Terra/constant2.t | 21 + samples/Terra/constructor.t | 12 + samples/Terra/coverage.t | 47 +++ samples/Terra/coverage2.t | 209 ++++++++++ samples/Terra/coverage3.t | 107 ++++++ samples/Terra/crash1.t | 1 + samples/Terra/crash2.t | 1 + samples/Terra/cstruct.t | 20 + samples/Terra/cstruct2.t | 8 + samples/Terra/cudaagg.t | 56 +++ samples/Terra/cudaaggregate.t | 45 +++ samples/Terra/cudaatomic.t | 39 ++ samples/Terra/cudaconst2.t | 14 + samples/Terra/cudaglobal.t | 48 +++ samples/Terra/cudahello.t | 30 ++ samples/Terra/cudaoffline.t | 65 ++++ samples/Terra/cudaoo.t | 20 + samples/Terra/cudaprintf.t | 50 +++ samples/Terra/cudashared.t | 47 +++ samples/Terra/cudatest.t | 47 +++ samples/Terra/cudatex.t | 59 +++ samples/Terra/cunion.t | 34 ++ samples/Terra/cunion2.t | 35 ++ samples/Terra/customline.t | 10 + samples/Terra/customtable.t | 10 + samples/Terra/cvar.t | 9 + samples/Terra/debugcallback.t | 23 ++ samples/Terra/declerrors.t | 29 ++ samples/Terra/decltwice.t | 18 + samples/Terra/def1.t | 16 + samples/Terra/defaultoperator.t | 9 + samples/Terra/defer.t | 111 ++++++ samples/Terra/deferbreak.t | 35 ++ samples/Terra/defercond.t | 82 ++++ samples/Terra/defergoto.t | 35 ++ samples/Terra/dgemm.t | 143 +++++++ samples/Terra/dgemm2.t | 163 ++++++++ samples/Terra/dgemm3.t | 213 +++++++++++ samples/Terra/dgemmpaper.t | 75 ++++ samples/Terra/diffuse.t | 237 ++++++++++++ samples/Terra/dynlib.t | 47 +++ samples/Terra/eager.t | 10 + samples/Terra/empty.t | 0 samples/Terra/emptycalls.t | 31 ++ samples/Terra/emptyname.t | 5 + samples/Terra/emptystruct.t | 30 ++ samples/Terra/enumc.t | 14 + samples/Terra/evenodd.t | 20 + samples/Terra/examplecompiler1.t | 41 ++ samples/Terra/examplelanguage1.t | 7 + samples/Terra/exampleparser1.t | 22 ++ samples/Terra/exittest.t | 9 + samples/Terra/explicitcast.t | 11 + samples/Terra/exportdynamic.t | 9 + samples/Terra/expvec.t | 34 ++ samples/Terra/f2.t | 17 + samples/Terra/fact.t | 16 + samples/Terra/fakeasm.t | 31 ++ samples/Terra/falsespec.t | 8 + samples/Terra/fastcall.t | 24 ++ samples/Terra/fib.t | 26 ++ samples/Terra/fib2.t | 27 ++ samples/Terra/fnames.t | 5 + samples/Terra/fncalltest.t | 16 + samples/Terra/fnpointer.t | 24 ++ samples/Terra/fnptr.t | 7 + samples/Terra/fnptrc.t | 25 ++ samples/Terra/foo.t | 1 + samples/Terra/for.t | 31 ++ samples/Terra/for2.t | 31 ++ samples/Terra/forbreak.t | 17 + samples/Terra/forlist.t | 24 ++ samples/Terra/forlist2.t | 37 ++ samples/Terra/forp.t | 21 + samples/Terra/forsym.t | 24 ++ samples/Terra/forwardtodef.t | 7 + samples/Terra/functionnoproto.t | 6 + samples/Terra/gctest.t | 18 + samples/Terra/gemm.t | 207 ++++++++++ samples/Terra/getmethod.t | 19 + samples/Terra/gettype.t | 18 + samples/Terra/globals.t | 26 ++ samples/Terra/goto.t | 16 + samples/Terra/goto2.t | 18 + samples/Terra/gvarfault.t | 24 ++ samples/Terra/hasbeenfrozen.t | 33 ++ samples/Terra/hello.t | 8 + samples/Terra/hello2.t | 10 + samples/Terra/hexf.t | 9 + samples/Terra/huge.t | 7 + samples/Terra/ifelse.t | 17 + samples/Terra/includec.t | 42 ++ samples/Terra/includetwice.t | 21 + samples/Terra/incomplete.t | 32 ++ samples/Terra/incomplete2.t | 25 ++ samples/Terra/incomplete3.t | 29 ++ samples/Terra/incomplete4.t | 22 ++ samples/Terra/incomplete5.t | 65 ++++ samples/Terra/incompletetypetest.t | 13 + samples/Terra/incompletetypetest2.t | 13 + samples/Terra/incompletetypetest3.t | 13 + samples/Terra/indexing64.t | 10 + samples/Terra/indexingbug.t | 31 ++ samples/Terra/interface.t | 33 ++ samples/Terra/interface2.t | 38 ++ samples/Terra/intrinsic.t | 21 + samples/Terra/isvolatile.t | 7 + samples/Terra/labelbug.t | 5 + samples/Terra/latelink.t | 24 ++ samples/Terra/lazycstring.t | 21 + samples/Terra/lazylog.t | 61 +++ samples/Terra/leaktest.t | 27 ++ samples/Terra/let1.t | 15 + samples/Terra/let2.t | 64 ++++ samples/Terra/linkllvm.t | 13 + samples/Terra/localenv.t | 46 +++ samples/Terra/localenv2.t | 7 + samples/Terra/logical.t | 33 ++ samples/Terra/luabridge.t | 18 + samples/Terra/luabridge2.t | 92 +++++ samples/Terra/luabridgefn.t | 23 ++ samples/Terra/luabridgerec.t | 10 + samples/Terra/luabridgeunion.t | 14 + samples/Terra/luaterramethod.t | 40 ++ samples/Terra/lvaluepointer.t | 10 + samples/Terra/lvaluequote.t | 22 ++ samples/Terra/lvaluetreelist.t | 9 + samples/Terra/macro.t | 50 +++ samples/Terra/macro2.t | 14 + samples/Terra/macro3.t | 14 + samples/Terra/macrokey.t | 9 + samples/Terra/macrolet.t | 17 + samples/Terra/macroselect.t | 11 + samples/Terra/macrotest.t | 9 + samples/Terra/malloc.t | 23 ++ samples/Terra/mathlib.t | 9 + samples/Terra/metatype.t | 16 + samples/Terra/method.t | 42 ++ samples/Terra/methodantiquote.t | 18 + samples/Terra/methodmissing.t | 17 + samples/Terra/methodrvalue.t | 18 + samples/Terra/methodsugar.t | 15 + samples/Terra/missingfields.t | 14 + samples/Terra/mixed.t | 13 + samples/Terra/multiconstructor.t | 17 + samples/Terra/multimacro.t | 17 + samples/Terra/multiterra.t | 79 ++++ samples/Terra/names.t | 16 + samples/Terra/nestedcalls.t | 11 + samples/Terra/nestextract.t | 40 ++ samples/Terra/nestnoerror.t | 17 + samples/Terra/new.t | 14 + samples/Terra/nillocal.t | 6 + samples/Terra/niltype.t | 9 + samples/Terra/nojit.t | 20 + samples/Terra/nolengthop.t | 2 + samples/Terra/nonprototypec.t | 12 + samples/Terra/nontemporal.t | 7 + samples/Terra/numliteral.t | 14 + samples/Terra/objc.t | 25 ++ samples/Terra/objc2.t | 42 ++ samples/Terra/objtest.t | 45 +++ samples/Terra/offsetcalc.t | 33 ++ samples/Terra/opaquealloc.t | 19 + samples/Terra/or.t | 9 + samples/Terra/ordercomplete.t | 25 ++ samples/Terra/output.t | 16 + samples/Terra/overload.t | 17 + samples/Terra/overload2.t | 15 + samples/Terra/overload3.t | 15 + samples/Terra/overloadcall.t | 12 + samples/Terra/overloadmethod.t | 24 ++ samples/Terra/overloadmethod2.t | 17 + samples/Terra/overloadmethod3.t | 18 + samples/Terra/overloadproduct.t | 65 ++++ samples/Terra/overloadrecv.t | 20 + samples/Terra/painfulrecstruct.t | 22 ++ samples/Terra/paren.t | 5 + samples/Terra/parsecrash.t | 7 + samples/Terra/parsefail.t | 35 ++ samples/Terra/parsefail2.t | 5 + samples/Terra/pattern.t | 43 +++ samples/Terra/point.t | 13 + samples/Terra/pointerarith.t | 31 ++ samples/Terra/pointerlike.t | 7 + samples/Terra/pow.t | 28 ++ samples/Terra/ppltalk.t | 18 + samples/Terra/ppnil.t | 4 + samples/Terra/pratttest1.t | 4 + samples/Terra/prec.t | 4 + samples/Terra/prec2.t | 2 + samples/Terra/pretty.t | 69 ++++ samples/Terra/printd.t | 5 + samples/Terra/printfarray.t | 12 + samples/Terra/printfloat.t | 7 + samples/Terra/proxystruct.t | 19 + samples/Terra/pt.t | 18 + samples/Terra/pthreads.t | 47 +++ samples/Terra/quote.t | 10 + samples/Terra/quote10.t | 13 + samples/Terra/quote2.t | 9 + samples/Terra/quote3.t | 13 + samples/Terra/quote4.t | 9 + samples/Terra/quote5.t | 20 + samples/Terra/quote6.t | 12 + samples/Terra/quote7.t | 23 ++ samples/Terra/quote8.t | 19 + samples/Terra/quote9.t | 19 + samples/Terra/quoteblock.t | 12 + samples/Terra/quoteenv.t | 25 ++ samples/Terra/quoteselect.t | 12 + samples/Terra/rd.t | 1 + samples/Terra/receivercasts.t | 22 ++ samples/Terra/recfn.t | 10 + samples/Terra/recoverfromerror.t | 15 + samples/Terra/recstruct.t | 33 ++ samples/Terra/recstruct2.t | 17 + samples/Terra/rename.t | 8 + samples/Terra/renaming.t | 21 + samples/Terra/requiretwice.t | 3 + samples/Terra/rvaluerecv.t | 30 ++ samples/Terra/scope.t | 13 + samples/Terra/selectoverload.t | 25 ++ samples/Terra/setname.t | 7 + samples/Terra/setter.t | 39 ++ samples/Terra/sgemm-old.t | 205 ++++++++++ samples/Terra/sgemm.t | 162 ++++++++ samples/Terra/sgemm3.t | 124 ++++++ samples/Terra/sgemmkernel.t | 164 ++++++++ samples/Terra/shallowfreeze.t | 24 ++ samples/Terra/sharedlib.t | 22 ++ samples/Terra/shift.t | 7 + samples/Terra/signext.t | 12 + samples/Terra/simple.t | 6 + samples/Terra/simpleadd.t | 15 + samples/Terra/simpleapply.t | 19 + samples/Terra/simpleglobal.t | 19 + samples/Terra/simplerec.t | 25 ++ samples/Terra/simplestruct.t | 10 + samples/Terra/simplevec.t | 12 + samples/Terra/sintable.t | 17 + samples/Terra/special.t | 61 +++ samples/Terra/speed.t | 30 ++ samples/Terra/splitprimary.t | 19 + samples/Terra/ssimple.t | 477 +++++++++++++++++++++++ samples/Terra/staticmethod.t | 9 + samples/Terra/stattest.t | 13 + samples/Terra/stencil.t | 134 +++++++ samples/Terra/strerror.t | 9 + samples/Terra/string.t | 15 + samples/Terra/struct.t | 50 +++ samples/Terra/structarg.t | 9 + samples/Terra/structcast.t | 38 ++ samples/Terra/structconstructor.t | 9 + samples/Terra/structrvalue.t | 16 + samples/Terra/structsyntax.t | 14 + samples/Terra/sugar.t | 15 + samples/Terra/sumlanguage1.t | 2 + samples/Terra/sumlanguage2.t | 4 + samples/Terra/symbolmangling.t | 4 + samples/Terra/symbolvar.t | 11 + samples/Terra/symbolvar2.t | 16 + samples/Terra/symbolvar3.t | 16 + samples/Terra/symbolvar4.t | 16 + samples/Terra/symbolvar5.t | 16 + samples/Terra/symbolvar6.t | 16 + samples/Terra/symbolvar7.t | 15 + samples/Terra/symparam.t | 9 + samples/Terra/symparam2.t | 9 + samples/Terra/symparam3.t | 15 + samples/Terra/template.t | 6 + samples/Terra/terracast.t | 15 + samples/Terra/terralua.t | 32 ++ samples/Terra/terraluamethod.t | 16 + samples/Terra/terranew.t | 19 + samples/Terra/testdebug.t | 57 +++ samples/Terra/testimport.t | 6 + samples/Terra/testlang1.t | 21 + samples/Terra/testlang2.t | 4 + samples/Terra/testlog.t | 8 + samples/Terra/testrequire.t | 14 + samples/Terra/teststd.t | 45 +++ samples/Terra/testvector.t | 57 +++ samples/Terra/torturechain.t | 67 ++++ samples/Terra/toterraexpression.t | 8 + samples/Terra/twolang.t | 23 ++ samples/Terra/typeexp.t | 9 + samples/Terra/union.t | 26 ++ samples/Terra/unm.t | 20 + samples/Terra/unpacktuple.t | 30 ++ samples/Terra/unsafesym.t | 14 + samples/Terra/unstrict.t | 5 + samples/Terra/usercast.t | 24 ++ samples/Terra/varargcstring.t | 2 + samples/Terra/vars.t | 23 ++ samples/Terra/vars2.t | 17 + samples/Terra/vec.t | 27 ++ samples/Terra/vecarith.t | 138 +++++++ samples/Terra/veclit.t | 29 ++ samples/Terra/vecobj.t | 54 +++ samples/Terra/vecptr.t | 4 + samples/Terra/vecsize.t | 5 + samples/Terra/vtablerec.t | 30 ++ samples/Terra/zeroargs.t | 5 + samples/Terra/zeroreturn.t | 18 + samples/Terra/zeroreturn2.t | 17 + 370 files changed, 11921 insertions(+) create mode 100644 samples/Terra/abouttocompile.t create mode 100755 samples/Terra/addlanguage1.t create mode 100644 samples/Terra/aggregatearr.t create mode 100644 samples/Terra/ainline.t create mode 100644 samples/Terra/alignment.t create mode 100644 samples/Terra/and.t create mode 100644 samples/Terra/anon.t create mode 100644 samples/Terra/anon3.t create mode 100644 samples/Terra/anonstruct.t create mode 100644 samples/Terra/anonstruct2.t create mode 100644 samples/Terra/antiquote1.t create mode 100644 samples/Terra/antiquote2.t create mode 100644 samples/Terra/antiquote3.t create mode 100644 samples/Terra/antiquote4.t create mode 100644 samples/Terra/antiquote5.t create mode 100644 samples/Terra/arith.t create mode 100644 samples/Terra/array.t create mode 100644 samples/Terra/arraylit.t create mode 100644 samples/Terra/arrayt.t create mode 100644 samples/Terra/arrayt2.t create mode 100644 samples/Terra/arrptr.t create mode 100644 samples/Terra/asm.t create mode 100644 samples/Terra/atoi.t create mode 100644 samples/Terra/avxhadd.t create mode 100644 samples/Terra/badname.t create mode 100644 samples/Terra/benchmark_fannkuchredux.t create mode 100644 samples/Terra/benchmark_nbody.t create mode 100644 samples/Terra/bf.t create mode 100644 samples/Terra/blankexp.t create mode 100644 samples/Terra/blockescape.t create mode 100644 samples/Terra/blocking.t create mode 100644 samples/Terra/blocking2-fixed.t create mode 100644 samples/Terra/blocking2.t create mode 100644 samples/Terra/blocking3.t create mode 100644 samples/Terra/bounce.t create mode 100644 samples/Terra/bug.t create mode 100644 samples/Terra/bug2.t create mode 100644 samples/Terra/bug3.t create mode 100644 samples/Terra/bug4.t create mode 100644 samples/Terra/calc.t create mode 100644 samples/Terra/call.t create mode 100644 samples/Terra/callbackcache.t create mode 100644 samples/Terra/canon.t create mode 100644 samples/Terra/canon2.t create mode 100644 samples/Terra/cast.t create mode 100644 samples/Terra/cbool.t create mode 100644 samples/Terra/cconv.t create mode 100644 samples/Terra/cfgbug.t create mode 100644 samples/Terra/clanginfo.t create mode 100644 samples/Terra/class.t create mode 100644 samples/Terra/class2.t create mode 100644 samples/Terra/class3.t create mode 100644 samples/Terra/class4.t create mode 100644 samples/Terra/class5.t create mode 100644 samples/Terra/class6.t create mode 100644 samples/Terra/classifyfloatstructs.t create mode 100644 samples/Terra/clean.t create mode 100644 samples/Terra/cnames.t create mode 100644 samples/Terra/cnamespace.t create mode 100644 samples/Terra/cnamespaces.t create mode 100644 samples/Terra/compilecallback.t create mode 100644 samples/Terra/completec.t create mode 100644 samples/Terra/conflict.t create mode 100644 samples/Terra/constant.t create mode 100644 samples/Terra/constant2.t create mode 100644 samples/Terra/constructor.t create mode 100644 samples/Terra/coverage.t create mode 100644 samples/Terra/coverage2.t create mode 100644 samples/Terra/coverage3.t create mode 100644 samples/Terra/crash1.t create mode 100644 samples/Terra/crash2.t create mode 100644 samples/Terra/cstruct.t create mode 100644 samples/Terra/cstruct2.t create mode 100644 samples/Terra/cudaagg.t create mode 100644 samples/Terra/cudaaggregate.t create mode 100644 samples/Terra/cudaatomic.t create mode 100644 samples/Terra/cudaconst2.t create mode 100644 samples/Terra/cudaglobal.t create mode 100644 samples/Terra/cudahello.t create mode 100644 samples/Terra/cudaoffline.t create mode 100644 samples/Terra/cudaoo.t create mode 100644 samples/Terra/cudaprintf.t create mode 100644 samples/Terra/cudashared.t create mode 100644 samples/Terra/cudatest.t create mode 100644 samples/Terra/cudatex.t create mode 100644 samples/Terra/cunion.t create mode 100644 samples/Terra/cunion2.t create mode 100644 samples/Terra/customline.t create mode 100644 samples/Terra/customtable.t create mode 100644 samples/Terra/cvar.t create mode 100644 samples/Terra/debugcallback.t create mode 100644 samples/Terra/declerrors.t create mode 100644 samples/Terra/decltwice.t create mode 100755 samples/Terra/def1.t create mode 100644 samples/Terra/defaultoperator.t create mode 100644 samples/Terra/defer.t create mode 100644 samples/Terra/deferbreak.t create mode 100644 samples/Terra/defercond.t create mode 100644 samples/Terra/defergoto.t create mode 100644 samples/Terra/dgemm.t create mode 100644 samples/Terra/dgemm2.t create mode 100644 samples/Terra/dgemm3.t create mode 100644 samples/Terra/dgemmpaper.t create mode 100644 samples/Terra/diffuse.t create mode 100644 samples/Terra/dynlib.t create mode 100644 samples/Terra/eager.t create mode 100644 samples/Terra/empty.t create mode 100644 samples/Terra/emptycalls.t create mode 100644 samples/Terra/emptyname.t create mode 100644 samples/Terra/emptystruct.t create mode 100644 samples/Terra/enumc.t create mode 100644 samples/Terra/evenodd.t create mode 100755 samples/Terra/examplecompiler1.t create mode 100755 samples/Terra/examplelanguage1.t create mode 100644 samples/Terra/exampleparser1.t create mode 100644 samples/Terra/exittest.t create mode 100644 samples/Terra/explicitcast.t create mode 100644 samples/Terra/exportdynamic.t create mode 100644 samples/Terra/expvec.t create mode 100644 samples/Terra/f2.t create mode 100644 samples/Terra/fact.t create mode 100644 samples/Terra/fakeasm.t create mode 100644 samples/Terra/falsespec.t create mode 100644 samples/Terra/fastcall.t create mode 100644 samples/Terra/fib.t create mode 100644 samples/Terra/fib2.t create mode 100644 samples/Terra/fnames.t create mode 100644 samples/Terra/fncalltest.t create mode 100644 samples/Terra/fnpointer.t create mode 100644 samples/Terra/fnptr.t create mode 100644 samples/Terra/fnptrc.t create mode 100644 samples/Terra/foo.t create mode 100644 samples/Terra/for.t create mode 100644 samples/Terra/for2.t create mode 100644 samples/Terra/forbreak.t create mode 100644 samples/Terra/forlist.t create mode 100644 samples/Terra/forlist2.t create mode 100644 samples/Terra/forp.t create mode 100644 samples/Terra/forsym.t create mode 100644 samples/Terra/forwardtodef.t create mode 100644 samples/Terra/functionnoproto.t create mode 100644 samples/Terra/gctest.t create mode 100644 samples/Terra/gemm.t create mode 100644 samples/Terra/getmethod.t create mode 100644 samples/Terra/gettype.t create mode 100644 samples/Terra/globals.t create mode 100644 samples/Terra/goto.t create mode 100644 samples/Terra/goto2.t create mode 100644 samples/Terra/gvarfault.t create mode 100644 samples/Terra/hasbeenfrozen.t create mode 100644 samples/Terra/hello.t create mode 100644 samples/Terra/hello2.t create mode 100644 samples/Terra/hexf.t create mode 100644 samples/Terra/huge.t create mode 100644 samples/Terra/ifelse.t create mode 100644 samples/Terra/includec.t create mode 100644 samples/Terra/includetwice.t create mode 100644 samples/Terra/incomplete.t create mode 100644 samples/Terra/incomplete2.t create mode 100644 samples/Terra/incomplete3.t create mode 100644 samples/Terra/incomplete4.t create mode 100644 samples/Terra/incomplete5.t create mode 100644 samples/Terra/incompletetypetest.t create mode 100644 samples/Terra/incompletetypetest2.t create mode 100644 samples/Terra/incompletetypetest3.t create mode 100644 samples/Terra/indexing64.t create mode 100644 samples/Terra/indexingbug.t create mode 100644 samples/Terra/interface.t create mode 100644 samples/Terra/interface2.t create mode 100644 samples/Terra/intrinsic.t create mode 100644 samples/Terra/isvolatile.t create mode 100644 samples/Terra/labelbug.t create mode 100644 samples/Terra/latelink.t create mode 100644 samples/Terra/lazycstring.t create mode 100644 samples/Terra/lazylog.t create mode 100644 samples/Terra/leaktest.t create mode 100644 samples/Terra/let1.t create mode 100644 samples/Terra/let2.t create mode 100644 samples/Terra/linkllvm.t create mode 100644 samples/Terra/localenv.t create mode 100644 samples/Terra/localenv2.t create mode 100644 samples/Terra/logical.t create mode 100644 samples/Terra/luabridge.t create mode 100644 samples/Terra/luabridge2.t create mode 100644 samples/Terra/luabridgefn.t create mode 100644 samples/Terra/luabridgerec.t create mode 100644 samples/Terra/luabridgeunion.t create mode 100644 samples/Terra/luaterramethod.t create mode 100644 samples/Terra/lvaluepointer.t create mode 100644 samples/Terra/lvaluequote.t create mode 100644 samples/Terra/lvaluetreelist.t create mode 100644 samples/Terra/macro.t create mode 100644 samples/Terra/macro2.t create mode 100644 samples/Terra/macro3.t create mode 100644 samples/Terra/macrokey.t create mode 100644 samples/Terra/macrolet.t create mode 100644 samples/Terra/macroselect.t create mode 100644 samples/Terra/macrotest.t create mode 100644 samples/Terra/malloc.t create mode 100644 samples/Terra/mathlib.t create mode 100644 samples/Terra/metatype.t create mode 100644 samples/Terra/method.t create mode 100644 samples/Terra/methodantiquote.t create mode 100644 samples/Terra/methodmissing.t create mode 100644 samples/Terra/methodrvalue.t create mode 100644 samples/Terra/methodsugar.t create mode 100644 samples/Terra/missingfields.t create mode 100644 samples/Terra/mixed.t create mode 100644 samples/Terra/multiconstructor.t create mode 100644 samples/Terra/multimacro.t create mode 100644 samples/Terra/multiterra.t create mode 100644 samples/Terra/names.t create mode 100644 samples/Terra/nestedcalls.t create mode 100644 samples/Terra/nestextract.t create mode 100644 samples/Terra/nestnoerror.t create mode 100644 samples/Terra/new.t create mode 100644 samples/Terra/nillocal.t create mode 100644 samples/Terra/niltype.t create mode 100644 samples/Terra/nojit.t create mode 100644 samples/Terra/nolengthop.t create mode 100644 samples/Terra/nonprototypec.t create mode 100644 samples/Terra/nontemporal.t create mode 100644 samples/Terra/numliteral.t create mode 100644 samples/Terra/objc.t create mode 100644 samples/Terra/objc2.t create mode 100644 samples/Terra/objtest.t create mode 100644 samples/Terra/offsetcalc.t create mode 100644 samples/Terra/opaquealloc.t create mode 100644 samples/Terra/or.t create mode 100644 samples/Terra/ordercomplete.t create mode 100644 samples/Terra/output.t create mode 100644 samples/Terra/overload.t create mode 100644 samples/Terra/overload2.t create mode 100644 samples/Terra/overload3.t create mode 100644 samples/Terra/overloadcall.t create mode 100644 samples/Terra/overloadmethod.t create mode 100644 samples/Terra/overloadmethod2.t create mode 100644 samples/Terra/overloadmethod3.t create mode 100644 samples/Terra/overloadproduct.t create mode 100644 samples/Terra/overloadrecv.t create mode 100644 samples/Terra/painfulrecstruct.t create mode 100644 samples/Terra/paren.t create mode 100644 samples/Terra/parsecrash.t create mode 100644 samples/Terra/parsefail.t create mode 100644 samples/Terra/parsefail2.t create mode 100644 samples/Terra/pattern.t create mode 100644 samples/Terra/point.t create mode 100644 samples/Terra/pointerarith.t create mode 100644 samples/Terra/pointerlike.t create mode 100644 samples/Terra/pow.t create mode 100644 samples/Terra/ppltalk.t create mode 100644 samples/Terra/ppnil.t create mode 100755 samples/Terra/pratttest1.t create mode 100644 samples/Terra/prec.t create mode 100644 samples/Terra/prec2.t create mode 100644 samples/Terra/pretty.t create mode 100644 samples/Terra/printd.t create mode 100644 samples/Terra/printfarray.t create mode 100644 samples/Terra/printfloat.t create mode 100644 samples/Terra/proxystruct.t create mode 100644 samples/Terra/pt.t create mode 100644 samples/Terra/pthreads.t create mode 100644 samples/Terra/quote.t create mode 100644 samples/Terra/quote10.t create mode 100644 samples/Terra/quote2.t create mode 100644 samples/Terra/quote3.t create mode 100644 samples/Terra/quote4.t create mode 100644 samples/Terra/quote5.t create mode 100644 samples/Terra/quote6.t create mode 100644 samples/Terra/quote7.t create mode 100644 samples/Terra/quote8.t create mode 100644 samples/Terra/quote9.t create mode 100644 samples/Terra/quoteblock.t create mode 100644 samples/Terra/quoteenv.t create mode 100644 samples/Terra/quoteselect.t create mode 100644 samples/Terra/rd.t create mode 100644 samples/Terra/receivercasts.t create mode 100644 samples/Terra/recfn.t create mode 100644 samples/Terra/recoverfromerror.t create mode 100644 samples/Terra/recstruct.t create mode 100644 samples/Terra/recstruct2.t create mode 100644 samples/Terra/rename.t create mode 100644 samples/Terra/renaming.t create mode 100644 samples/Terra/requiretwice.t create mode 100644 samples/Terra/rvaluerecv.t create mode 100644 samples/Terra/scope.t create mode 100644 samples/Terra/selectoverload.t create mode 100644 samples/Terra/setname.t create mode 100644 samples/Terra/setter.t create mode 100644 samples/Terra/sgemm-old.t create mode 100644 samples/Terra/sgemm.t create mode 100644 samples/Terra/sgemm3.t create mode 100644 samples/Terra/sgemmkernel.t create mode 100644 samples/Terra/shallowfreeze.t create mode 100644 samples/Terra/sharedlib.t create mode 100644 samples/Terra/shift.t create mode 100644 samples/Terra/signext.t create mode 100644 samples/Terra/simple.t create mode 100644 samples/Terra/simpleadd.t create mode 100644 samples/Terra/simpleapply.t create mode 100644 samples/Terra/simpleglobal.t create mode 100644 samples/Terra/simplerec.t create mode 100644 samples/Terra/simplestruct.t create mode 100644 samples/Terra/simplevec.t create mode 100644 samples/Terra/sintable.t create mode 100644 samples/Terra/special.t create mode 100644 samples/Terra/speed.t create mode 100644 samples/Terra/splitprimary.t create mode 100644 samples/Terra/ssimple.t create mode 100644 samples/Terra/staticmethod.t create mode 100644 samples/Terra/stattest.t create mode 100644 samples/Terra/stencil.t create mode 100644 samples/Terra/strerror.t create mode 100644 samples/Terra/string.t create mode 100644 samples/Terra/struct.t create mode 100644 samples/Terra/structarg.t create mode 100644 samples/Terra/structcast.t create mode 100644 samples/Terra/structconstructor.t create mode 100644 samples/Terra/structrvalue.t create mode 100644 samples/Terra/structsyntax.t create mode 100644 samples/Terra/sugar.t create mode 100755 samples/Terra/sumlanguage1.t create mode 100755 samples/Terra/sumlanguage2.t create mode 100644 samples/Terra/symbolmangling.t create mode 100644 samples/Terra/symbolvar.t create mode 100644 samples/Terra/symbolvar2.t create mode 100644 samples/Terra/symbolvar3.t create mode 100644 samples/Terra/symbolvar4.t create mode 100644 samples/Terra/symbolvar5.t create mode 100644 samples/Terra/symbolvar6.t create mode 100644 samples/Terra/symbolvar7.t create mode 100644 samples/Terra/symparam.t create mode 100644 samples/Terra/symparam2.t create mode 100644 samples/Terra/symparam3.t create mode 100644 samples/Terra/template.t create mode 100644 samples/Terra/terracast.t create mode 100644 samples/Terra/terralua.t create mode 100644 samples/Terra/terraluamethod.t create mode 100644 samples/Terra/terranew.t create mode 100644 samples/Terra/testdebug.t create mode 100644 samples/Terra/testimport.t create mode 100755 samples/Terra/testlang1.t create mode 100755 samples/Terra/testlang2.t create mode 100644 samples/Terra/testlog.t create mode 100644 samples/Terra/testrequire.t create mode 100644 samples/Terra/teststd.t create mode 100644 samples/Terra/testvector.t create mode 100644 samples/Terra/torturechain.t create mode 100644 samples/Terra/toterraexpression.t create mode 100644 samples/Terra/twolang.t create mode 100644 samples/Terra/typeexp.t create mode 100644 samples/Terra/union.t create mode 100644 samples/Terra/unm.t create mode 100644 samples/Terra/unpacktuple.t create mode 100644 samples/Terra/unsafesym.t create mode 100644 samples/Terra/unstrict.t create mode 100644 samples/Terra/usercast.t create mode 100644 samples/Terra/varargcstring.t create mode 100644 samples/Terra/vars.t create mode 100644 samples/Terra/vars2.t create mode 100644 samples/Terra/vec.t create mode 100644 samples/Terra/vecarith.t create mode 100644 samples/Terra/veclit.t create mode 100644 samples/Terra/vecobj.t create mode 100644 samples/Terra/vecptr.t create mode 100644 samples/Terra/vecsize.t create mode 100644 samples/Terra/vtablerec.t create mode 100644 samples/Terra/zeroargs.t create mode 100644 samples/Terra/zeroreturn.t create mode 100644 samples/Terra/zeroreturn2.t diff --git a/samples/Terra/abouttocompile.t b/samples/Terra/abouttocompile.t new file mode 100644 index 00000000..f6f9f6c8 --- /dev/null +++ b/samples/Terra/abouttocompile.t @@ -0,0 +1,22 @@ +struct A { + a : int; + b : int; +} + +function A.metamethods.__getentries(self) + print("GET ENTRIES") + for i,e in ipairs(self.entries) do + e.field = "foo"..e.field + end + return self.entries +end + +terra foo() + var a : A + a.fooa = 3 + a.foob = 4 + return a.fooa + a.foob +end + + +assert(foo() == 7) \ No newline at end of file diff --git a/samples/Terra/addlanguage1.t b/samples/Terra/addlanguage1.t new file mode 100755 index 00000000..ad383d9e --- /dev/null +++ b/samples/Terra/addlanguage1.t @@ -0,0 +1,17 @@ +do +import "lib/addlanguage" +local a = 4 +local b = add a,3,4+5,(function() terra one() return a end return one() end)() end +local inception = add add 3,4,a end,4 end +local test = require("test") + + +test.eq(b,20) +test.eq(inception,15) +end + +do +import "lib/addlanguage" +local c = add 4,5 end +assert(9 == c) +end \ No newline at end of file diff --git a/samples/Terra/aggregatearr.t b/samples/Terra/aggregatearr.t new file mode 100644 index 00000000..b54f3dc3 --- /dev/null +++ b/samples/Terra/aggregatearr.t @@ -0,0 +1,18 @@ + + +a = terralib.new(int[4]) + +terra foo() + a[3] = 4 +end + +foo() +assert(4 == a[3]) + + +terra bar() + a = array(5,6,7,8) +end + +bar() +assert(a[3] == 8) diff --git a/samples/Terra/ainline.t b/samples/Terra/ainline.t new file mode 100644 index 00000000..4f6dcabe --- /dev/null +++ b/samples/Terra/ainline.t @@ -0,0 +1,24 @@ + +C = terralib.includec("stdio.h") +terra foo() + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") + C.printf("hello, world\n") +end + +foo:getdefinitions()[1]:setinlined(true) + +terra bar() + foo() + return 4 +end + +bar:compile() +bar:disas() diff --git a/samples/Terra/alignment.t b/samples/Terra/alignment.t new file mode 100644 index 00000000..08b648f8 --- /dev/null +++ b/samples/Terra/alignment.t @@ -0,0 +1,7 @@ +local alignment = 16 +local aligned = terralib.aligned +terra foobar(a : &float) + terralib.attrstore(a,terralib.attrload(a+3,{ align = alignment }), { align = alignment }) +end + +foobar:disas() \ No newline at end of file diff --git a/samples/Terra/and.t b/samples/Terra/and.t new file mode 100644 index 00000000..87ea0284 --- /dev/null +++ b/samples/Terra/and.t @@ -0,0 +1,9 @@ +local test = require("test") + +terra foo(a : double, b : double, c : double) : bool + return a < b and b < c +end + +test.eq(foo(1,2,3),true) +test.eq(foo(1,2,1),false) +test.eq(foo(2,1,2),false) \ No newline at end of file diff --git a/samples/Terra/anon.t b/samples/Terra/anon.t new file mode 100644 index 00000000..c5d3315f --- /dev/null +++ b/samples/Terra/anon.t @@ -0,0 +1,26 @@ +local test = require("test") + +local Num = int + +local fib = +terra(a : Num) : Num + var i,c,p = 0,1,1 + while i < a do + c,p = c + p,c + i = i + 1 + end + return c +end + +function fib2(a) + local i,c,p = 0,1,1 + while i < a do + c,p = c + p,c + i = i + 1 + end + return c +end +for i = 0,10 do + print(fib(i)) + test.eq(fib(i),fib2(i)) +end \ No newline at end of file diff --git a/samples/Terra/anon3.t b/samples/Terra/anon3.t new file mode 100644 index 00000000..5d37c7c7 --- /dev/null +++ b/samples/Terra/anon3.t @@ -0,0 +1,18 @@ +C,T = terralib.includecstring [[ + struct D { + struct { int a; struct { int c; } c; } b; + } c; + typedef struct {int c;} B; + typedef struct { + struct { int a; } a; + struct { int b; } b; + B c; + } A; +]] + +terra foo(a : &C.A) + var c = a.a + var b = a.b.b + return c.a + b + a.c.c; +end +foo:compile() \ No newline at end of file diff --git a/samples/Terra/anonstruct.t b/samples/Terra/anonstruct.t new file mode 100644 index 00000000..5f661249 --- /dev/null +++ b/samples/Terra/anonstruct.t @@ -0,0 +1,12 @@ + +local C = tuple(int,tuple(int,int)) +terra anon() + var c : tuple(int,tuple(int,int)) = { 1, {2,3} } + var d : C = c + d._0 = 2 + return d._0 + c._1._0 + c._1._1 +end + +test = require("test") + +test.eq(anon(),7) \ No newline at end of file diff --git a/samples/Terra/anonstruct2.t b/samples/Terra/anonstruct2.t new file mode 100644 index 00000000..156e2a8b --- /dev/null +++ b/samples/Terra/anonstruct2.t @@ -0,0 +1,7 @@ + +struct A { a : &A } + +terra foo() + var a : A +end +foo() \ No newline at end of file diff --git a/samples/Terra/antiquote1.t b/samples/Terra/antiquote1.t new file mode 100644 index 00000000..8bddcf3e --- /dev/null +++ b/samples/Terra/antiquote1.t @@ -0,0 +1,12 @@ + + +function omgfunc() + return 2 +end + +terra foo() + return 1 + [{omgfunc()}] + [omgfunc()] +end + +local test = require("test") +test.eq(foo(),5) \ No newline at end of file diff --git a/samples/Terra/antiquote2.t b/samples/Terra/antiquote2.t new file mode 100644 index 00000000..7ad4f86d --- /dev/null +++ b/samples/Terra/antiquote2.t @@ -0,0 +1,13 @@ + + +function omgfunc() + return 4 +end + +local what = `[ {2,3} ] +terra foo() + return [{3,`[ {2,3} ]}] +end + +local test = require("test") +test.meq({3,2,3},foo()) diff --git a/samples/Terra/antiquote3.t b/samples/Terra/antiquote3.t new file mode 100644 index 00000000..1d34ef7e --- /dev/null +++ b/samples/Terra/antiquote3.t @@ -0,0 +1,16 @@ + + +function omgfunc() + return 2 +end + +a = global(0) + +terra foo() + [quote a = a + 1 end]; + [{quote a = a + 1 end,quote a = a + 1 end}] + return a +end + +local test = require("test") +test.eq(foo(),3) \ No newline at end of file diff --git a/samples/Terra/antiquote4.t b/samples/Terra/antiquote4.t new file mode 100644 index 00000000..d6585edc --- /dev/null +++ b/samples/Terra/antiquote4.t @@ -0,0 +1,11 @@ + + +terra foo() + var a = 4 + return [a] +end + +foo:printpretty() + +local test = require("test") +test.eq(foo(),4) \ No newline at end of file diff --git a/samples/Terra/antiquote5.t b/samples/Terra/antiquote5.t new file mode 100644 index 00000000..9ebbbed4 --- /dev/null +++ b/samples/Terra/antiquote5.t @@ -0,0 +1,21 @@ + + +function makeloop(N,body) + return quote + for i = 0, N do + body + end + end +end + + +terra stuff() + var a = 0; + [makeloop(10,quote + a = a + 1 + end)] + return a +end + + +print(stuff()) \ No newline at end of file diff --git a/samples/Terra/arith.t b/samples/Terra/arith.t new file mode 100644 index 00000000..d73a46da --- /dev/null +++ b/samples/Terra/arith.t @@ -0,0 +1,114 @@ +--[[ + +- ++ + +* +/ +% + +^ +and +or +~= == < > >= <= +<< >> + +]] + + + + +terra test0() + var a = 1 + 3 + var b = 1LL + 2LL + var c = 1ULL + 2ULL + var d = 1.f + 2.f + var e = 1.0 + 2.0 + return a + b + c + d + e +end +terra test1() + var a = 1 - 3 + var b = 1LL - 2LL + var c = 1ULL - 2ULL + var d = 1.f - 2.f + var e = 1.0 - 2.0 + return a - b - c - d - e +end +terra test2() + var a = 2 * 3 + var b = 3LL * 2LL + var c = 3ULL * 2ULL + var d = 1.f * 2.f + var e = 3.0 * 2.0 + return a * b * c * d * e +end + +terra test3() + var a = 2 / 3 + 1 + var b = 3LL / 2LL + 1 + var c = 3ULL / 2ULL + var d = 1.f / 2.f + var e = 3.0 / 2.0 + return a * b * c * d * e +end + +terra test4() + var a = 2 % 3 + var b = 3LL % 4LL + var c = 3ULL % 2ULL + var d = 1.f % 2.f + var e = 3.75 % 2.0 + return (a == 2) and (b == 3LL) and (d == 1.f) and (e == 1.75) +end + +terra test5() + var a = 2 ^ 3 + var b = 3LL ^ 4LL + var c = 3ULL ^ 2ULL + return (a == 1) and (b == 7LL) and (c == 1ULL) +end + +terra test6() + var a = 2 and 3 + var b = 3LL and 4LL + var c = 3ULL and 2ULL + return (a == 2) and (b == 0LL) and (c == 2ULL) +end + +terra test7() + var a = 2 or 3 + var b = 3LL or 4LL + var c = 3ULL or 2ULL + return (a == 3) and (b == 7LL) and (c == 3ULL) +end + + +terra test8() + var a0,a1 = 2 ~= 3, 2 == 3 + var b0,b1 = 2 < 3, 2 >= 3 + var c0,c1 = 2 > 3, 2 <= 3 + + return a0 and not a1 and b0 and not b1 and not c0 and c1 +end + +terra test9() + var a0, a1 = 8 >> 1, 8 << 1 + var b0, b1 = -8 >> 1, -8 << 1 + return a0 == 4 and a1 == 16 and b0 == -4 and b1 == -16 +end + + +local test = require("test") + +test.eq(test0(),16) +test.eq(test1(),2) +test.eq(test2(),2592) +test.eq(test3(),1.5) + +test.eq(test4(),true) + +test.eq(test5(),true) +test.eq(test6(),true) +test.eq(test7(),true) +test.eq(test8(),true) +test.eq(test9(),true) \ No newline at end of file diff --git a/samples/Terra/array.t b/samples/Terra/array.t new file mode 100644 index 00000000..28040709 --- /dev/null +++ b/samples/Terra/array.t @@ -0,0 +1,60 @@ + +terra bar() + var a : int[2] + a[0] = 1 + a[1] = 2 + return a +end + +terra foo() + var a : int[4] + a[1], a[2] = 4,2 + return a[1] + a[2] +end + +terra foo2() + var b = bar() + return b[0] + b[1] +end + +terra foo3() + return (bar())[0] +end + +terra foo4() + var a : int[4] + a[3] = 7 + var b = &a[0] + b[2] = 8 + return b[2] + b[3] +end + +terra bar2(a : &int) + a[1] = 100 + return a[0] +end + +terra foo5() + var a : int[4] + bar2(a) + return a[1] +end + +terra foo6() + return bar2(bar()) +end + + +terra foo7() + var a = array(1,2,3,4) + return a[1]+a[2] +end +local test = require("test") + +test.eq(6,foo()) +test.eq(3,foo2()) +test.eq(1,foo3()) +test.eq(15,foo4()) +test.eq(100,foo5()) +test.eq(1,foo6()) +test.eq(5,foo7()) \ No newline at end of file diff --git a/samples/Terra/arraylit.t b/samples/Terra/arraylit.t new file mode 100644 index 00000000..3731a367 --- /dev/null +++ b/samples/Terra/arraylit.t @@ -0,0 +1,29 @@ + +terra foo() + var a = array(1,2,3) + return a[0] + a[1] + a[2] +end + +terra foo2() + var a = array(1,2.5,3) + return a[1] +end +terra what() + return 3,4.5 +end +local expand = macro(function(a) return {`a._0,`a._1} end) +terra foo3() + var w = what() + var a = array(1,2.5,expand(w)) + return a[3] +end +terra foo4() + var a = array("what","magic","is","this") + return a[1][1] +end + +local test = require("test") +test.eq(foo(),6) +test.eq(foo2(),2.5) +test.eq(foo3(),4.5) +test.eq(foo4(),97) \ No newline at end of file diff --git a/samples/Terra/arrayt.t b/samples/Terra/arrayt.t new file mode 100644 index 00000000..29fca5b4 --- /dev/null +++ b/samples/Terra/arrayt.t @@ -0,0 +1,75 @@ +C = terralib.includecstring [[ + #include + #include +]] +local arraytypes = {} +function Array(T) + local struct ArrayImpl { + data : &T; + N : int; + } + function ArrayImpl.metamethods.__typename(self) + return "Array("..tostring(T)..")" + end + arraytypes[ArrayImpl] = true + terra ArrayImpl:init(N : int) + self.data = [&T](C.malloc(N*sizeof(T))) + self.N = N + end + terra ArrayImpl:free() + C.free(self.data) + end + ArrayImpl.metamethods.__apply = macro(function(self,idx) + return `self.data[idx] + end) + ArrayImpl.metamethods.__methodmissing = macro(function(methodname,selfexp,...) + local args = terralib.newlist {...} + local i = symbol(int) + local promotedargs = args:map(function(a) + if arraytypes[a:gettype()] then + return `a(i) + else + return a + end + end) + return quote + var self = selfexp + var r : ArrayImpl + r:init(self.N) + for [i] = 0,r.N do + r.data[i] = self.data[i]:[methodname](promotedargs) + end + in + r + end + end) + return ArrayImpl +end + +struct Complex { + real : float; + imag : float; +} + +terra Complex:add(c : Complex) + return Complex { self.real + c.real, self.imag + c.imag } +end + +ComplexArray = Array(Complex) +N = 10 +terra testit() + var ca : ComplexArray + ca:init(N) + for i = 0,N do + ca(i) = Complex { i, i + 1 } + end + var ra = ca:add(ca) + return ra +end +local r = testit() +assert(r.N == N) +for i = 0,N-1 do + assert(r.data[i].real == 2*i) + assert(r.data[i].imag == 2*(i+1)) +end +assert(tostring(Array(int)) == "Array(int32)") \ No newline at end of file diff --git a/samples/Terra/arrayt2.t b/samples/Terra/arrayt2.t new file mode 100644 index 00000000..8b6c9f2f --- /dev/null +++ b/samples/Terra/arrayt2.t @@ -0,0 +1,82 @@ +local f = assert(io.popen("uname", 'r')) +local s = assert(f:read('*a')) +f:close() + +if s~="Darwin\n" then + print("Warning, not running test b/c this isn't a mac") + return +end + +C = terralib.includecstring [[ + #include + #include +]] +local arraytypes = {} +function Array(T) + if arraytypes[T] then return arraytypes[T] end + local struct ArrayImpl { + data : &T; + N : int; + } + arraytypes[T] = ArrayImpl + terra ArrayImpl:init(N : int) + self.data = [&T](C.malloc(N*sizeof(T))) + self.N = N + end + terra ArrayImpl:free() + C.free(self.data) + end + ArrayImpl.metamethods.__apply = macro(function(self,idx) + return `self.data[idx] + end) + ArrayImpl.metamethods.__methodmissing = macro(function(methodname,selfexp,...) + local args = terralib.newlist {...} + local params = args:map(function(a) return symbol(a:gettype()) end) + local terra elemfn(a : &T, [params]) + return a:[methodname](params) + end + local RT = elemfn:gettype().returntype + return quote + var self = selfexp + var r : Array(RT) + r:init(self.N) + for i = 0,r.N do + r.data[i] = elemfn(&self.data[i],args) + end + in + r + end + end) + return ArrayImpl +end + +local OC = require("lib/objc") +local IO = terralib.includec("stdio.h") + +struct Rect { + a : double, + b : double, + c : double, + d : double +} + +terra str(data : &uint8) + return OC.NSString:stringWithUTF8String(data) +end + +terra main() + OC.NSAutoreleasePool:new() + var app = OC.NSApplication:sharedApplication() + var rec = Rect {0,0,200,200} + var windows : Array(OC.ID) + windows:init(2) + windows(0) = OC.NSWindow + windows(1) = OC.NSWindow + windows = windows:alloc():initWithContentRect_styleMask_backing_defer(rec,1,2,false) + windows:makeKeyAndOrderFront(nil) + IO.printf("entering run loop\n") + app:run() +end + +terralib.linklibrary("/System/Library/Frameworks/Cocoa.framework/Cocoa") +main:compile() diff --git a/samples/Terra/arrptr.t b/samples/Terra/arrptr.t new file mode 100644 index 00000000..55b6a918 --- /dev/null +++ b/samples/Terra/arrptr.t @@ -0,0 +1,5 @@ +a = &int[4] +a = (&int)[4] +local test = require("test") +test.eq(false,a:ispointer()) +test.eq(true,a:isarray()) \ No newline at end of file diff --git a/samples/Terra/asm.t b/samples/Terra/asm.t new file mode 100644 index 00000000..803b6882 --- /dev/null +++ b/samples/Terra/asm.t @@ -0,0 +1,28 @@ +local a = ... +if not a then + --force MCJIT + os.execute("../terra -m asm.t true") + return +end + +C = terralib.includec("stdio.h") + +struct Vendor { + maxV : int; + b : int; + c : int; + d : int; +} + +terra foo() + var r = terralib.asm(Vendor,"cpuid","={eax},={ebx},={ecx},={edx},{eax},~{dirflag},~{fpsr},~{flags}",false,0) + r.c,r.d = r.d,r.c + C.printf("%.12s\n", &r.b) +end +foo() + + +terra addone(a : int) + return terralib.asm(int,"addl $$1,$1","=r,0",true,a) +end +assert(addone(3) == 4) \ No newline at end of file diff --git a/samples/Terra/atoi.t b/samples/Terra/atoi.t new file mode 100644 index 00000000..929f9672 --- /dev/null +++ b/samples/Terra/atoi.t @@ -0,0 +1,7 @@ +c = terralib.includec("stdlib.h") + +terra what() + return c.atoi("52") +end + +print(what()) diff --git a/samples/Terra/avxhadd.t b/samples/Terra/avxhadd.t new file mode 100644 index 00000000..d1e762b7 --- /dev/null +++ b/samples/Terra/avxhadd.t @@ -0,0 +1,32 @@ +local haddavx = terralib.intrinsic("llvm.x86.avx.hadd.ps.256", { vector(float,8), vector(float,8) } -> vector(float,8)) + +terra hadd(v : vector(float,8)) + var v1 = haddavx(v,v) + var v2 = haddavx(v1,v1) + return v2[0] + v2[4] +end + +ffi = require("ffi") + +local stdio = terralib.includec("stdio.h") + +terra foobar(a : &float) + return hadd(@[&vector(float,8)](a)) +end + +dat = ffi.new("float[?] __attribute__((aligned(32)))",8) + +for i = 1,8 do + dat[i-1] = i +end + +if terralib.llvmversion == 31 then + print("ignoring...") +else + foobar:compile() + local test = require("test") + test.eq(foobar(dat),36) +end + +--terralib.saveobj("avxhadd",{main = foobar}) +--os.execute("./avxhadd") \ No newline at end of file diff --git a/samples/Terra/badname.t b/samples/Terra/badname.t new file mode 100644 index 00000000..a2436c12 --- /dev/null +++ b/samples/Terra/badname.t @@ -0,0 +1,4 @@ +--tostring on a type should never error, so we assign it its original name if it does +struct A {} +A.metamethods.__typename = error +print(A) \ No newline at end of file diff --git a/samples/Terra/benchmark_fannkuchredux.t b/samples/Terra/benchmark_fannkuchredux.t new file mode 100644 index 00000000..2ec27e6e --- /dev/null +++ b/samples/Terra/benchmark_fannkuchredux.t @@ -0,0 +1,148 @@ +--[[ + The Computer Language Benchmarks Game + http://shootout.alioth.debian.org/ + + contributed by Ledrug Katz + + ]] + + +local C = { + printf = terralib.externfunction("printf", terralib.types.funcpointer(rawstring,int,true)), + exit = terralib.externfunction("exit", int -> {}), + atoi = terralib.externfunction("atoi", rawstring -> int) +} + +-- this depends highly on the platform. It might be faster to use +-- char type on 32-bit systems; it might be faster to use unsigned. + +elem = int + +s = global(elem[16]) +t = global(elem[16]) + +maxflips = global(int) +max_n = global(int) +odd = global(bool) +checksum = global(int) + +terra flip() + var i = max_n + var x : &elem = t + var y : &elem = s + var c : elem + + while i > 0 do + i = i - 1 + @x = @y + x = x + 1 + y = y + 1 + end + + i = 1 + + repeat + x = t + y = t + t[0] + while x < y do + c = @x + @x = @y + x = x + 1 + @y = c + y = y - 1 + end + i = i + 1 + until t[t[0]] == 0 + --C.printf("flip %d\n",i); + return i +end + + +terra rotate(n : int) + var c = s[0] + for i = 0,n do + s[i] = s[i+1] + end + s[n] = c + --C.printf("rotate(%d) %d\n",n,c); +end + + +terra tk(n : int) + var i = 0 + var f : int + var c : elem[16] + + for i = 0,16 do + c[i] = 0 + end + + while i < n do + rotate(i) + if c[i] >= i then + c[i] = 0 + i = i + 1 + goto continue + end + + c[i] = c[i] + 1 + i = 1 + odd = not odd + if s[0] ~= 0 then + if s[s[0]] ~= 0 then + f = flip() + else + f = 1 + end + if f > maxflips then + maxflips = f + end + --C.printf("f = %d\n",f) + if odd then + checksum = checksum - f + else + checksum = checksum + f + end + end + ::continue:: + end + +end + + +terra doit(N : int) + maxflips = 0 + odd = false + checksum = 0 + max_n = N + if max_n < 3 or max_n > 15 then + C.printf("range: must be 3 <= n <= 12\n") + C.exit(1) + end + + for i = 0,max_n do + s[i] = i + end + tk(max_n) + C.printf("%d\nPfannkuchen(%d) = %d\n", checksum, max_n, maxflips) + return checksum +end + +terra main(argc : int, v : &&int8) + if argc < 2 then + C.printf("usage: %s number\n", v[0]) + C.exit(1); + end + + doit(C.atoi(v[1])) + return 0 +end + +local test = require("test") + +doit:compile() +print(test.time(function() + test.eq(doit(10),73196) +end)) + +terralib.saveobj("benchmark_fannkuchredux", { main = main } ) diff --git a/samples/Terra/benchmark_nbody.t b/samples/Terra/benchmark_nbody.t new file mode 100644 index 00000000..8b007193 --- /dev/null +++ b/samples/Terra/benchmark_nbody.t @@ -0,0 +1,150 @@ +local C = terralib.includecstring[[ + +#include +#include +#include + +]] + +pi = 3.141592653589793 +solar_mass = (4 * pi * pi) +days_per_year = 365.24 + +struct planet { + x : double; + y : double; + z : double; + vx : double; + vy : double; + vz : double; + mass : double; +} + +terra advance(nbodies : int, bodies : &planet, dt : double) + + for i = 0, nbodies do + var b = &bodies[i] + for j = i + 1, nbodies do + var b2 = &bodies[j] + + var dx = b.x - b2.x; + var dy = b.y - b2.y; + var dz = b.z - b2.z; + var distance = C.sqrt(dx * dx + dy * dy + dz * dz); + var mag = dt / (distance * distance * distance); + --C.printf("%f %f %f %f %f\n",dx,dy,dz,distance,mag); + b.vx = b.vx - dx * b2.mass * mag; + b.vy = b.vy - dy * b2.mass * mag; + b.vz = b.vz - dz * b2.mass * mag; + b2.vx = b2.vx + dx * b.mass * mag; + b2.vy = b2.vy + dy * b.mass * mag; + b2.vz = b2.vz + dz * b.mass * mag; + --C.printf("%f %f %f %f %f %f\n",b.vx,b.vy,b.vz,b2.vx,b2.vy,b2.vz) + end + end + for i = 0,nbodies do + var b = &bodies[i] + b.x = b.x + dt * b.vx; + b.y = b.y + dt * b.vy; + b.z = b.z + dt * b.vz; + end +end + +terra energy(nbodies : int, bodies : &planet) + var e = 0.0 + for i = 0, nbodies do + var b = &bodies[i] + e = e + 0.5 * b.mass * (b.vx * b.vx + b.vy * b.vy + b.vz * b.vz); + for j = i + 1, nbodies do + var b2 = &bodies[j] + var dx = b.x - b2.x + var dy = b.y - b2.y + var dz = b.z - b2.z + var distance = C.sqrt(dx * dx + dy * dy + dz * dz) + e = e - (b.mass * b2.mass) / distance + end + end + return e +end + +terra offset_momentum(nbodies : int, bodies : &planet) + var px,py,pz = 0.0,0.0,0.0 + + for i = 0,nbodies do + px = px + bodies[i].vx * bodies[i].mass + py = py + bodies[i].vy * bodies[i].mass + pz = pz + bodies[i].vz * bodies[i].mass + end + bodies[0].vx = - px / solar_mass + bodies[0].vy = - py / solar_mass + bodies[0].vz = - pz / solar_mass +end + +NBODIES = 5 + +terra main(argc : int, argv : &&int8) + var bodies = array( + planet { -- sun */ + 0, 0, 0, 0, 0, 0, solar_mass + }, + planet { -- jupiter */ + 4.84143144246472090e+00, + -1.16032004402742839e+00, + -1.03622044471123109e-01, + 1.66007664274403694e-03 * days_per_year, + 7.69901118419740425e-03 * days_per_year, + -6.90460016972063023e-05 * days_per_year, + 9.54791938424326609e-04 * solar_mass + }, + planet { -- saturn */ + 8.34336671824457987e+00, + 4.12479856412430479e+00, + -4.03523417114321381e-01, + -2.76742510726862411e-03 * days_per_year, + 4.99852801234917238e-03 * days_per_year, + 2.30417297573763929e-05 * days_per_year, + 2.85885980666130812e-04 * solar_mass + }, + planet { -- uranus */ + 1.28943695621391310e+01, + -1.51111514016986312e+01, + -2.23307578892655734e-01, + 2.96460137564761618e-03 * days_per_year, + 2.37847173959480950e-03 * days_per_year, + -2.96589568540237556e-05 * days_per_year, + 4.36624404335156298e-05 * solar_mass + }, + planet { -- neptune */ + 1.53796971148509165e+01, + -2.59193146099879641e+01, + 1.79258772950371181e-01, + 2.68067772490389322e-03 * days_per_year, + 1.62824170038242295e-03 * days_per_year, + -9.51592254519715870e-05 * days_per_year, + 5.15138902046611451e-05 * solar_mass + } + ) + var n = C.atoi(argv[1]) + offset_momentum(NBODIES, bodies) + C.printf ("%.9f\n", energy(NBODIES, bodies)) + for i = 0,n do + advance(NBODIES, bodies, 0.01) + end + C.printf ("%.9f\n", energy(NBODIES, bodies)); + return 0 +end + +terra run() + main(2,array("what","1000000")) +end + +--run:compile() + + +--local test = require("test") + +--print(test.time(run)) + +terralib.saveobj("benchmark_nbody",{ main = main } ) +energy:disas() +energy:printpretty() \ No newline at end of file diff --git a/samples/Terra/bf.t b/samples/Terra/bf.t new file mode 100644 index 00000000..84b99b5a --- /dev/null +++ b/samples/Terra/bf.t @@ -0,0 +1,60 @@ + +local C = terralib.includec("stdio.h") + +local function compile(code,N) + local function body(data,ptr) + local stmts = terralib.newlist() + local jumpstack = {} + for i = 1,#code do + local c = code:sub(i,i) + local stmt + if c == ">" then + stmt = quote ptr = ptr + 1 end + elseif c == "<" then + stmt = quote ptr = ptr - 1 end + elseif c == "+" then + stmt = quote data[ptr] = data[ptr] + 1 end + elseif c == "-" then + stmt = quote data[ptr] = data[ptr] - 1 end + elseif c == "." then + stmt = quote C.putchar(data[ptr]) end + elseif c == "," then + stmt = quote data[ptr] = C.getchar() end + elseif c == "[" then + local target = { before = symbol(), after = symbol() } + table.insert(jumpstack,target) + stmt = quote + ::[target.before]:: + if data[ptr] == 0 then + goto [target.after] + end + end + elseif c == "]" then + local target = table.remove(jumpstack) + assert(target) + stmt = quote + goto [target.before] + :: [target.after] :: + end + else + error("unknown character "..c) + end + stmts:insert(stmt) + end + return stmts + end + return terra() + var data : int[N] + for i = 0, N do + data[i] = 0 + end + var ptr = 0; + [ body(data,ptr) ] + end +end + +local helloworld = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." + +local fn = compile(helloworld,256) + +fn() \ No newline at end of file diff --git a/samples/Terra/blankexp.t b/samples/Terra/blankexp.t new file mode 100644 index 00000000..e53242c1 --- /dev/null +++ b/samples/Terra/blankexp.t @@ -0,0 +1,7 @@ +local a = quote + while false do end +end +terra foo() + return a +end +foo() \ No newline at end of file diff --git a/samples/Terra/blockescape.t b/samples/Terra/blockescape.t new file mode 100644 index 00000000..eb475932 --- /dev/null +++ b/samples/Terra/blockescape.t @@ -0,0 +1,23 @@ +terra foo() + var c = 3 + escape + local a = 1 + for i = 1,10 do + emit quote c = c + a end + end + end + return c +end + +assert(foo() == 13) + +terra foo2() + return escape emit(1) end +end + +assert(1 == foo2()) + +a = terralib.newlist() +a:insert quote + foo() +end \ No newline at end of file diff --git a/samples/Terra/blocking.t b/samples/Terra/blocking.t new file mode 100644 index 00000000..8ee19e3e --- /dev/null +++ b/samples/Terra/blocking.t @@ -0,0 +1,57 @@ +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 1,I do + r[i] = symmat(name..tostring(i),...) + end + return r +end + +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end + +function blockedloop(bounds,sizes,bodyfn) + local indexes = symmat("i",#sizes,#bounds) + --local makeloop --bug local function doesn't add to set of live variables... + local function makeloop(s,b) + if s > #sizes then + return bodyfn(unpack(indexes[#sizes])) + elseif b > #bounds then + return makeloop(s + 1, 1) + else + local topbound = bounds[b] + local blocksize = sizes[s] + local begin,bound + if s == 1 then + begin,bound = 0, topbound + else + begin,bound = indexes[s-1][b], sizes[s-1] + end + local step = sizes[s] + return quote + for [indexes[s][b]] = begin,min(begin+bound,topbound),step do + [ makeloop(s,b+1) ] + end + end + end + end + return makeloop(1,1) +end + +IO = terralib.includec("stdio.h") + +terra main() + + var M,N = 30,40; + + [blockedloop({M,N}, {10,1}, function(m,n) + return quote + IO.printf("%d %d\n",m,n) + end + end)] + +end + +main:printpretty() +main() \ No newline at end of file diff --git a/samples/Terra/blocking2-fixed.t b/samples/Terra/blocking2-fixed.t new file mode 100644 index 00000000..10ff5a6d --- /dev/null +++ b/samples/Terra/blocking2-fixed.t @@ -0,0 +1,68 @@ +C = terralib.includec("stdio.h") + +terra min(a: int, b: int) : int + if a < b then return a + else return b end +end +std = terralib.includec("stdlib.h") +function Image(PixelType) + local struct ImageImpl { + data : &PixelType, + N : int + } + terra ImageImpl:init(N: int): {} --returns nothing + self.data = [&PixelType]( + std.malloc(N*N*sizeof(PixelType))) + self.N = N + end + terra ImageImpl:get(x: int, y: int) : PixelType + return self.data[x*self.N + y] + end + + + terra ImageImpl:set(x: int, y: int, v : PixelType) + self.data[x*self.N + y] = v + end + terra ImageImpl:save(i : rawstring) + for i = 0, 2 do for j = 0, 2 do + C.printf("%d %d %f\n", i,j,self:get(i,j)) + end end + end + terra ImageImpl:load(i : rawstring) self:init(16) + for i = 0, 4 do for j = 0, 4 do + self:set(i,j,(i*4+j)%3) + --C.printf("%f\n",self:get(i,j)) + end end + end + terra ImageImpl:free() end + + --omitted methods for: set, save, load, free + return ImageImpl +end +GreyscaleImage = Image(float) +terra laplace(img: &GreyscaleImage, + out: &GreyscaleImage) : {} + --shrink result, do not calculate boundaries + var newN = img.N - 2 + out:init(newN) + for i = 0,newN do + for j = 0,newN do + var v = img:get(i+0,j+1) + img:get(i+2,j+1) + + img:get(i+1,j+2) + img:get(i+1,j+0) + - 4 * img:get(i+1,j+1) + out:set(i,j,v) + end + end +end +terra runlaplace(input: rawstring, + output: rawstring) : {} + var i: GreyscaleImage, o : GreyscaleImage + i:load(input) + laplace(&i,&o) + o:save(output) + i:free(); o:free() +end + +runlaplace("myinput","myoutput") + terralib.saveobj("runlaplace.o", + {runlaplace = runlaplace}) diff --git a/samples/Terra/blocking2.t b/samples/Terra/blocking2.t new file mode 100644 index 00000000..2b0b3b71 --- /dev/null +++ b/samples/Terra/blocking2.t @@ -0,0 +1,81 @@ +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end +function blockedloop(N,blocksizes,bodyfn) + local function generatelevel(n,ii,jj,bb) + if n > #blocksizes then + return bodyfn(ii,jj) + end + local blocksize = blocksizes[n] + return quote + for i = ii,min(ii+bb,N),blocksize do + for j = jj,min(jj+bb,N),blocksize do + [ generatelevel(n+1,i,j,blocksize) ] + end + end + end + end + return generatelevel(1,0,0,N) +end + +IO = terralib.includec("stdio.h") +stdlib = terralib.includec("stdlib.h") + +terra main() + + var a : int[8][8] + var c = 0 + var N = 8 + [blockedloop(N, {4,2,1}, function(i,j) + return quote + --IO.printf("%d %d\n",i,j) + a[i][j] = c + c = c + 1 + end + end)] + for i = 0,N do + for j = 0,N do + IO.printf("%d\t",a[i][j]) + end + IO.printf("\n") + end +end + +main() + + + +function Image(Spectrum) + local struct ImageImpl { + data : &Spectrum, + N : int + } + terra ImageImpl:init(N : int) + self.data = [&float](stdlib.malloc(N*N*sizeof(Spectrum))) + self.N = N + end + ImageImpl.methods.pixel = macro(function(self,x,y) + return `self.data[x*self.N + y] + end) + return ImageImpl +end + +GreyScaleImage = Image(float) + +terra laplace(input : &GreyScaleImage, output : &GreyScaleImage) + var newN = input.N - 2 --shrink result since we do not calculate boundaries + output:init(newN); + [blockedloop(newN,{32,1},function(i,j) + return quote + output:pixel(i,j) = + input:pixel(i+0,j+1) + + input:pixel(i+2,j+1) + + input:pixel(i+1,j+2) + + input:pixel(i+1,j+0) + - 4 * input:pixel(i+1,j+1) + end + end)] +end + +laplace:compile() +laplace:printpretty() \ No newline at end of file diff --git a/samples/Terra/blocking3.t b/samples/Terra/blocking3.t new file mode 100644 index 00000000..c7b99b98 --- /dev/null +++ b/samples/Terra/blocking3.t @@ -0,0 +1,85 @@ +C = terralib.includec("stdio.h") + +function blockedloop(N,blocksizes,bodyfn) + local function generatelevel(n,ii,jj,bb) + if n > #blocksizes then + return bodyfn(ii,jj) + end + local blocksize = blocksizes[n] + return quote + for i = ii,min(ii+bb,N),blocksize do + for j = jj,min(jj+bb,N),blocksize do + [ generatelevel(n+1,i,j,blocksize) ] + end + end + end + end + return generatelevel(1,0,0,N) +end + +terra min(a: int, b: int) : int + if a < b then return a + else return b end +end +std = terralib.includec("stdlib.h") +function Image(PixelType) + local struct ImageImpl { + data : &PixelType, + N : int + } + terra ImageImpl:init(N: int): {} --returns nothing + self.data = + [&PixelType](std.malloc(N*N*sizeof(PixelType))) + self.N = N + end + terra ImageImpl:get(x: int, y: int) : PixelType + return self.data[x*self.N + y] + end + + + terra ImageImpl:set(x: int, y: int, v : PixelType) + self.data[x*self.N + y] = v + end + terra ImageImpl:save(i : rawstring) + for i = 0, 8 do for j = 0, 8 do + C.printf("%d %d %f\n", i,j,self:get(i,j)) + end end + end + terra ImageImpl:load(i : rawstring) self:init(16) + for i = 0, 10 do for j = 0, 10 do + self:set(i,j,(i*4+j)%3) + --C.printf("%f\n",self:get(i,j)) + end end + end + terra ImageImpl:free() end + + --omitted methods for: set, save, load, free + return ImageImpl +end +GreyscaleImage = Image(float) +terra laplace(img: &GreyscaleImage, + out: &GreyscaleImage) : {} + --shrink result, do not calculate boundaries + var newN = img.N - 2 + out:init(newN); + [blockedloop(newN,{4,2,1}, function(i,j) + return quote + var v = img:get(i+0,j+1) + img:get(i+2,j+1) + + img:get(i+1,j+2) + img:get(i+1,j+0) + - 4 * img:get(i+1,j+1) + out:set(i,j,v) + end + end)] +end +terra runlaplace(input: rawstring, + output: rawstring) : {} + var i: GreyscaleImage, o : GreyscaleImage + i:load(input) + laplace(&i,&o) + o:save(output) + i:free(); o:free() +end + +runlaplace("myinput","myoutput") + terralib.saveobj("runlaplace.o", + {runlaplace = runlaplace}) diff --git a/samples/Terra/bounce.t b/samples/Terra/bounce.t new file mode 100644 index 00000000..85e7400c --- /dev/null +++ b/samples/Terra/bounce.t @@ -0,0 +1,23 @@ +local io = terralib.includec("stdio.h") + +struct Count { value : int } +function luafn(a) + print("lua:",a.value) + a.value = a.value + 1 + terrafn(a) +end +terra terrafn(a : &Count) + io.printf("terra: %d\n",a.value) + if a.value < 100 then + luafn(a) + end + return a.value +end + +terra begin() + var c = Count {0} + return terrafn(&c) +end + +local test = require("test") +test.eq(begin(),100) \ No newline at end of file diff --git a/samples/Terra/bug.t b/samples/Terra/bug.t new file mode 100644 index 00000000..5d2f860d --- /dev/null +++ b/samples/Terra/bug.t @@ -0,0 +1,20 @@ +local f = assert(io.popen("uname", 'r')) +local s = assert(f:read('*a')) +f:close() + +if s~="Darwin\n" then + print("Warning, not running test b/c this isn't a mac") +else + + +local OC = require("lib/objc") +local OCR = terralib.includec("objc/runtime.h") + +terra main() + var nsobject = OC.NSObject + OCR.objc_allocateClassPair([&OCR.objc_class](nsobject.data),nil,0) +end + +main:compile() + +end \ No newline at end of file diff --git a/samples/Terra/bug2.t b/samples/Terra/bug2.t new file mode 100644 index 00000000..91b77cbd --- /dev/null +++ b/samples/Terra/bug2.t @@ -0,0 +1,20 @@ +ffi = require("ffi") +cstdio = terralib.includec("stdio.h") + +local vec4 = &vector(float,4) + +local align = terralib.aligned +terra lol( w : &float, out : &float) + var a = terralib.attrload(vec4(w),{align = 4}) + terralib.attrstore(vec4(out), a, {align = 4}) +end + +dat = ffi.new("float[?]",32) +for i=0,31 do dat[i]=i end +datO = ffi.new("float[?]",32) + +lol:compile() +lol:disas() + +lol(dat, datO) + diff --git a/samples/Terra/bug3.t b/samples/Terra/bug3.t new file mode 100644 index 00000000..e58be1df --- /dev/null +++ b/samples/Terra/bug3.t @@ -0,0 +1,15 @@ + + +local S = terralib.types.newstruct("mystruct") + +struct A { + v : int +} + +S.entries:insert( { type = A, field = "what" } ) +terra foo() + var v : S + return v.what.v +end + +foo:compile() diff --git a/samples/Terra/bug4.t b/samples/Terra/bug4.t new file mode 100644 index 00000000..2690b028 --- /dev/null +++ b/samples/Terra/bug4.t @@ -0,0 +1,14 @@ +c = terralib.includecstring [[ + #include + #include +]] +struct exception { slug : int8[60]; code : int; msg : int8[960]; } +EXC_INFO = terralib.new(exception) +terra bar() c.memcpy(EXC_INFO.slug + 0, 'foobar', 7); c.printf('%s\n', EXC_INFO.slug + 0); end +bar() + +terra zoo() EXC_INFO.slug[0] = 65; EXC_INFO.slug[1] = 0; c.printf('%s\n', EXC_INFO.slug + 0); end +zoo() + +terra zoo2() EXC_INFO.slug[0] = 65; EXC_INFO.slug[1] = 0; return EXC_INFO.slug[0] end +assert(zoo2() == 65) \ No newline at end of file diff --git a/samples/Terra/calc.t b/samples/Terra/calc.t new file mode 100644 index 00000000..2167f2b8 --- /dev/null +++ b/samples/Terra/calc.t @@ -0,0 +1,33 @@ +function makecalcfn(inst) + local stk = {} + for i,v in ipairs(inst) do + if type(v) == "number" then + table.insert(stk,`v) + else + local b = table.remove(stk) + local a = table.remove(stk) + + if v == "+" then + table.insert(stk,`a + b) + elseif v == "-" then + table.insert(stk,`a - b) + elseif v == "*" then + table.insert(stk,`a * b) + elseif v == "/" then + table.insert(stk,`a / b) + end + end + end + + local result = table.remove(stk) + + local terra wrapper() + return result + end + + return wrapper +end + +local calcfn = makecalcfn({5,4,"*",5,"-",3,"+"}) +local test = require("test") +test.eq(calcfn(),18) diff --git a/samples/Terra/call.t b/samples/Terra/call.t new file mode 100644 index 00000000..4bf49c62 --- /dev/null +++ b/samples/Terra/call.t @@ -0,0 +1,53 @@ + +terra bar(a : int, b : int) : int + return a + b +end +terra foo(a : int,b : int) : int + return bar(a,b) + 1 +end + +terra baz(a : int, b : int) : int + var f,c,d = 4,baz2(a,b) + return f + c + d +end and +terra baz2(a : int, b : int) : {int, int} + return a + 1, b + 2 +end + +terra two(a : int, b : int) : int + return a + b +end +terra baz3() : int + var a,b = baz2(1,2) + return two(a,b) +end +terra baz4() : int + return two(5,(baz2(1,2))._0) +end + +terra baz5() + var a,b = baz2(0,0) + return 1,2,a,b +end + +terra baz6() + var a,b,c,d = baz5() + var e = (baz5()._0) + return a + b + c + d + e +end + +terra baz7(a : int) + if a < 3 then + return 1,(baz5())._0 + else + return 100,2 + end +end +local test = require("test") + +test.eq(foo(2,3),6) +test.eq(baz(1,2),10) +test.eq(baz3(),6) +test.eq(baz4(),7) +test.eq(baz6(),7) +test.meq({100,2},baz7(10)) \ No newline at end of file diff --git a/samples/Terra/callbackcache.t b/samples/Terra/callbackcache.t new file mode 100644 index 00000000..06450cf3 --- /dev/null +++ b/samples/Terra/callbackcache.t @@ -0,0 +1,9 @@ + + +for i = 1,10 do + local terra doprint() + print(1,2) + print(3) + end + doprint() +end \ No newline at end of file diff --git a/samples/Terra/canon.t b/samples/Terra/canon.t new file mode 100644 index 00000000..c07ffaae --- /dev/null +++ b/samples/Terra/canon.t @@ -0,0 +1,45 @@ + +struct B { + a : A +} and +struct A { + b : &B +} + + + + +struct C { + i : int +} +local U = struct { c : C } +local UP = &U + + +local FP = UP -> int +local FP2 = UP -> int + +local FI = int -> int +local FI2 = int -> int +terra anon() + var b : B + b.a.b = &b + return 4 +end + +terra anon2() + var u = U { C {3} } + + var fp : FP, fi : FI + var fi2 : FI2 = fi + var fp2 : FP2 = fp + var up : UP = &u + return up.c.i +end + + + +test = require("test") + +test.eq(anon(),4) +test.eq(anon2(),3) \ No newline at end of file diff --git a/samples/Terra/canon2.t b/samples/Terra/canon2.t new file mode 100644 index 00000000..a73b8577 --- /dev/null +++ b/samples/Terra/canon2.t @@ -0,0 +1,41 @@ +local C = terralib.includecstring [[ + typedef union { + float v[2]; + struct { + float x; + float y; + }; + } float2; + static float2 a; + void doit() { + a.x = 4; + a.y = 5; + } + float2* what() { return &a; } +]] + +C.float2:printpretty() + +local anonstructgetter = macro(function(name,self) + for i,e in pairs(self:gettype():getfields()) do + if e.key:match("_%d+") and e.type:getfield(name) then + return `self.[e.key].[name] + end + end + error("no field "..name.." in struct of type "..tostring(T)) +end) + +C.float2.metamethods.__entrymissing = anonstructgetter + +terra foo(pa : &C.float2) + var a = @C.what() + return a.v[0],a.v[1],a.x,a.y +end + +C.doit() + +local a = foo(C.what()) +assert(4 == a._0) +assert(5 == a._1) +assert(4 == a._2) +assert(5 == a._3) \ No newline at end of file diff --git a/samples/Terra/cast.t b/samples/Terra/cast.t new file mode 100644 index 00000000..6de7f79d --- /dev/null +++ b/samples/Terra/cast.t @@ -0,0 +1,11 @@ +terra bar(a : int) : int + return a + 1 +end +terra foo(a : int) : int + + return a +end + +local test = require("test") +test.eq(foo(2),2) +test.eq(bar(2),3) diff --git a/samples/Terra/cbool.t b/samples/Terra/cbool.t new file mode 100644 index 00000000..cfa7cd4a --- /dev/null +++ b/samples/Terra/cbool.t @@ -0,0 +1,13 @@ + + +C = terralib.includecstring [[ + _Bool And(_Bool a, _Bool b) { return a && b; } +]] + + +terra foobar(a : bool, b : bool) + return C.And(a,b) +end + +assert(foobar(false,true) == false) +assert(foobar(true,true) == true) \ No newline at end of file diff --git a/samples/Terra/cconv.t b/samples/Terra/cconv.t new file mode 100644 index 00000000..ba12b5e9 --- /dev/null +++ b/samples/Terra/cconv.t @@ -0,0 +1,488 @@ + +local test = require("test") + +terra f1(a : int) + return a +end + +terra c1() + return 1 + f1(4),f1(4) +end + +terra f2(a : int, b : float) + return a + b +end + +terra c2() + return f2(3,4) + 1, f2(3,4) +end + + +terra f3() + return 3,4 +end + +terra c3() + var r = f3() + return f3()._0 + 1,unpackstruct(r) +end + +terra f4() : {float,float} + return 3.25,4.25 +end + +terra c4() + var r = f4() + return f4()._0 + 1, unpackstruct(r) +end + + +terra f5(a : int) : {uint8,uint8,uint8,uint8} + return 0,1,a,3 +end + +terra c5() + var r = f5(8) + return f5(8)._0 + 1, unpackstruct(r) +end + +terra f6() : {double, double, int} + return 3.25, 4.25, 3 +end + +terra c6() + var r = f6() + return f6()._0 + 1, unpackstruct(r) +end +test.meq({4.25, 3.25,4.25,3},c6()) + +terra f7(a : int) : {double, double, int} + return 3.25, 4.25, a +end + +terra c7() + var r = f7(4) + return f7(4)._0 + 1, unpackstruct(r) +end +test.meq({4.25,3.25,4.25,4},c7()) + +terra f8() : {double, double} + return 3.25, 4.25 +end + +terra c8() + var r= f8() + return f8()._0 + 1, unpackstruct(r) +end + +test.meq({4.25,3.25,4.25},c8()) + +struct S1 { + a : int; + b : int; +} + +terra f9(a : S1) + return a.a+1,a.b+2 +end + +terra c9() + var a = S1 { 4, 5} + var r = f9(a) + return f9(a)._0 + 1, unpackstruct(r) +end +test.meq({6,5,7},c9()) + +struct S2 { + a : int; + b : double; + c : double; +} + +terra f10(a : S2) + return a.a, a.b, a.c +end + +terra c10() + var s2 = S2 { 4,5,6 } + var r = f10(s2) + return f10(s2)._0 + 1, unpackstruct(r) +end + +test.meq({5,4,5,6},c10()) + +C = terralib.includec("stdio.h") + +terra f11(a : int) + C.printf("f11 %d\n",a) +end + + +terra c11() + f11(7) +end +c11() + +struct S3 { + a : vector(float,2); + b : double; +} + +struct S4 { + b : double; + a : vector(float,2); +} + +struct S5 { + a : vector(uint8,4); + b : int; +} + +terra f12a(a : S3) + return a.a[0] + a.a[1], a.b +end + +terra c12a() + var a = S3 { vector(2.25f, 3.25f), 4 } + var r = f12a(a) + return f12a(a)._0 + 1, unpackstruct(r) +end +test.meq({6.5,5.5,4},c12a()) + +terra f12b(a : S4) + return a.a[0] + a.a[1], a.b +end + +terra c12b() + var a = S4 { 4, vector(2.25f, 3.25f) } + var r = f12b(a) + return f12b(a)._0 + 1, unpackstruct(r) +end +test.meq({6.5,5.5,4},c12b()) + + +terra f12() + var a = S3 { vector(8.f,2.f), 3.0 } + var b = S4 { 3.0, vector(8.f,2.f) } + var c,d = f12a(a) + var e,f = f12b(b) + return c,d,e,f +end + +terra f13a(a : S5) + return a.a[0] + a.a[1] + a.a[2] + a.a[3], a.b +end + +terra f13() + var a = S5 { vectorof(int8, 1,2,3,4), 5 } + return f13a(a) +end + + +struct S6 { + a : float; + aa : float; + b : float +} + +struct S7a { + a : int; + b : int; +} +struct S7 { + a : int; + b : S7a; + c : int; +} + +terra f14(a : S6) + return a.a,a.aa,a.b +end + +terra c14() + var a = S6 { 4,2,3} + var r = f14(a) + return f14(a)._0 + 1, unpackstruct(r) +end +test.meq({5,4,2,3},c14()) + +terra f15(a : S7) + return a.a, a.b.a, a.b.b, a.c +end + +terra c15() + var a = S7 {1, S7a { 2,3 }, 4} + var r = f15(a) + return f15(a)._0 + 1, unpackstruct(r) +end + +test.meq({2,1,2,3,4}, c15()) + +struct S8 { + a : uint8[7]; +} + +terra f16(a : S8) + return a.a[0],a.a[6] +end + +terra c16() + var a = S8 { arrayof(uint8, 1,2,3,4,5,6,7) } + var r = f16(a) + return f16(a)._0 + 1, unpackstruct(r) +end + +test.meq({2,1,7},c16()) + +struct S9 { + a : uint8[9]; +} + +terra f17(a : S9) + return a.a[0],a.a[8] +end + +terra c17() + var a = S9 { arrayof(uint8, 1,2,3,4,5,6,7,8,9) } + var r = f17(a) + return f17(a)._0 + 1, unpackstruct(r) +end + + +test.meq({2,1,9},c17()) + + +struct S10 { + a : double; + b : int64 +} + + +terra f18a(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a5 : int, a : S10) + return a.a, a.b +end + +terra c18a() + var r = f18a(1,2,3,4,5,6,S10{7,8}) + return f18a(1,2,3,4,5,6,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8},c18a()) + + +terra f18b(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a : S10) + return a.a, a.b +end + +terra c18b() + var r = f18b(1,2,3,4,5,S10{7,8}) + return f18b(1,2,3,4,5,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8},c18b()) + +terra f18c(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a : S10) + return a.a, a.b, a0, a1, a2 +end +terra c18c() + var r = f18c(1,2,3,4,5,S10 {7,8}) + return f18c(1,2,3,4,5,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8,1,2,3},c18c()) + +struct S11 { + a : float; + b : int; +} + +terra f18d(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a5 : int, a : S11) + return a.a, a.b +end +terra c18d() + var r = f18d(1,2,3,4,5,6,S11{7,8}) + return f18d(1,2,3,4,5,6,S11{7,8})._0 + 1, unpackstruct(r) +end +test.meq({8,7,8},c18d()) + + +terra f18e(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a : S11) + return a.a, a.b +end + +terra c18e() + var r = f18e(1,2,3,4,5,S11{7,8}) + return f18e(1,2,3,4,5,S11{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8},c18e()) + +terra f18f(a0 : int, a1 : int, a2 : int, a3 : int, a4: int, a : S11) + return a.a, a.b, a0, a1, a2 +end + +terra c18f() + var r = f18f(1,2,3,4,5,S11{7,8}) + return f18f(1,2,3,4,5,S11{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8,1,2,3},c18f()) + + +terra f18g(a0 : float, a1 : float, a2 : float, a3 : float, a4: float, a5 : float, a6 : float, a7 : float, a : S10) + return a.a, a.b +end + +terra c18g() + var r = f18g(1,2,3,4,5,6,9,10,S10{7,8}) + return f18g(1,2,3,4,5,6,9,10,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8},c18g()) + +terra f18h(a0 : float, a1 : float, a2 : float, a3 : float, a4: float, a5 : float, a6 : float, a : S10) + return a.a, a.b +end + +terra c18h() + var r = f18h(1,2,3,4,5,6,9,S10{7,8}) + return f18h(1,2,3,4,5,6,9,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8},c18h()) + +terra f18i(a0 : float, a1 : float, a2 : float, a3 : float, a4: float, a5 : float, a6 : float, a : S10) + return a.a, a.b, a0, a1, a2 +end + +terra c18i() + var r = f18i(1,2,3,4,5,6,9,S10{7,8}) + return f18i(1,2,3,4,5,6,9,S10{7,8})._0 + 1, unpackstruct(r) +end + +test.meq({8,7,8,1,2,3},c18i()) + +struct S12 { + a : float; + b : int; +} + +terra f19(a : S12) + return a.a, a.b +end +terra c19() + var a = S12 { 3,5 } + var r = f19(a) + return f19(a)._0 + 1, unpackstruct(r) +end +test.meq({4,3,5},c19()) + + + +terra f20(a : S10, b : int) + return a.a,a.b,b +end +terra c20() + var r = f20(S10{1,2},3) + return f20(S10{1,2},3)._0 + 1, unpackstruct(r) +end +test.meq({2,1,2,3},c20()) + + +terra f21() + return +end +f21() + +terra f22() + return S12 { 3, 4} +end +terra c22() + return f22().a, f22() +end +local s22_0, s22_1 = terralib.unpackstruct(c22()) +test.eq(s22_0,3) +test.eq(s22_1.a,3) +test.eq(s22_1.b,4) + +terra f23() + return S10 { 1, 2} +end + +terra c23() + return f23().a, f23() +end +local s23_0, s23_1 = terralib.unpackstruct(c23()) +test.eq(s23_0,1) +test.eq(s23_1.a,1) +test.eq(s23_1.b,2) + +terra f24() + return S2 { 1,2,3} +end + +terra c24() + return f24().a, f24() +end +local s24_0, s24_1 = terralib.unpackstruct(c24()) +test.eq(s24_0,1) +test.eq(s24_1.a,1) +test.eq(s24_1.b,2) +test.eq(s24_1.c,3) + + +local s22 = f22() +test.eq(s22.a,3) +test.eq(s22.b,4) + +local s23 = f23() +test.eq(s23.a,1) +test.eq(s23.b,2) + + +local s24 = f24() + +test.eq(s24.a,1) +test.eq(s24.b,2) +test.eq(s24.c,3) + + +test.meq({1,2,3},f20({1,2},3)) + + +test.eq(f1(3),3) +test.eq(f2(4,5),9) +test.meq({3,4},f3()) +test.meq({3.25,4.25},f4()) +test.meq({0,1,2,3},f5(2)) +test.meq({3.25,4.25,3},f6()) +test.meq({3.25,4.25,4},f7(4)) +test.meq({3.25,4.25},f8()) +test.meq({3,5},f9({2,3})) +test.meq({1,2.5,3.5},f10({1,2.5,3.5})) +f11(3) +test.meq({10,3,10,3},f12()) +test.meq({10,5},f13()) +test.meq({4,5,6},f14({4,5,6})) +test.meq({1,2,3,4},f15({1,{2,3},4})) +test.meq({1,7},f16({{1,2,3,4,5,6,7}})) +test.meq({1,9},f17({{1,2,3,4,5,6,7,8,9}})) +test.meq({7,8},f18a(1,2,3,4,5,6,{7,8})) +test.meq({7,8},f18b(1,2,3,4,5,{7,8})) +test.meq({7,8,1,2,3},f18c(1,2,3,4,5,{7,8})) + +test.meq({7,8},f18d(1,2,3,4,5,6,{7,8})) +test.meq({7,8},f18e(1,2,3,4,5,{7,8})) +test.meq({7,8,1,2,3},f18f(1,2,3,4,5,{7,8})) + +test.meq({9,10},f18g(1,2,3,4,5,6,7,8,{9,10})) +test.meq({9,10},f18h(1,2,3,4,5,6,7,{9,10})) +test.meq({9,10,1,2,3},f18i(1,2,3,4,5,6,7,{9,10})) + +test.meq({4,5}, f19({4,5})) + +test.meq({5,4},c1()) +test.meq({8,7},c2()) +test.meq({4,3,4},c3()) +test.meq({4.25,3.25,4.25},c4()) +test.meq({1,0,1,8,3},c5()) \ No newline at end of file diff --git a/samples/Terra/cfgbug.t b/samples/Terra/cfgbug.t new file mode 100644 index 00000000..b1f9b31c --- /dev/null +++ b/samples/Terra/cfgbug.t @@ -0,0 +1,14 @@ +local struct Type { + field: int +} + +local terra foo(obj: Type) end +foo:setinlined(false) + +local terra bar() + var obj: Type + defer foo(obj) + return +end + +bar() \ No newline at end of file diff --git a/samples/Terra/clanginfo.t b/samples/Terra/clanginfo.t new file mode 100644 index 00000000..b1d27a70 --- /dev/null +++ b/samples/Terra/clanginfo.t @@ -0,0 +1 @@ +terralib.includec("stdio.h",{"-v"}) diff --git a/samples/Terra/class.t b/samples/Terra/class.t new file mode 100644 index 00000000..e02546d9 --- /dev/null +++ b/samples/Terra/class.t @@ -0,0 +1,235 @@ + +IO = terralib.includec("stdio.h") +local Class = require("lib/javalike") + +struct A { + a : int +} +terra A:times2() : int + return self.a*2 +end + +struct B { + b : int +} +Class.extends(B,A) + +terra B:combine(a : int) : int + return self.b + self.a + a +end + + +struct C { + c : double +} +Class.extends(C,B) + +terra C:combine(a : int) : int + return self.c + self.a + self.b + a +end +terra C:times2() : double + return self.a * 4 +end + +terra doubleAnA(a : &A) + return a:times2() +end + +terra combineAB(b : &B) + return b:combine(3) +end + +terra returnA(a : A) + return a +end +terra foobar1() + var c = C.alloc() + c.a,c.b,c.c = 1,2,3.5 + return c:times2() +end + +assert(foobar1() == 4) + +terra foobar() + + var a = A.alloc() + a.a = 1 + var b = B.alloc() + b.a,b.b = 1,2 + + var c = C.alloc() + c.a,c.b,c.c = 1,2,3.5 + + var r = doubleAnA(a) + doubleAnA(b) + doubleAnA(c) + combineAB(b) + combineAB(c) + + a:free() + b:free() + c:free() + + return r +end + +assert(23 == foobar()) + +Doubles = Class.interface { times2 = {} -> int } + +Adds = Class.interface { add = int -> int } + +struct D { + data : int +} +Class.implements(D,Doubles) +Class.implements(D,Adds) + + +terra D:times2() : int + return self.data * 2 +end + +terra D:add(a : int) : int + return self.data + a +end + + +terra aDoubles(a : &Doubles) + return a:times2() +end + +terra aAdds(a : &Adds) + return a:add(3) +end + +terra foobar2() + var a : D + a:init() + a.data = 3 + return aDoubles(&a) + aAdds(&a) +end + +assert(12 == foobar2()) + + +local IO = terralib.includec("stdio.h") +struct Animal { + data : int +} +terra Animal:speak() : {} + IO.printf("... %d\n",self.data) +end + +struct Dog { +} +Class.extends(Dog,Animal) +terra Dog:speak() : {} + IO.printf("woof! %d\n",self.data) +end + +struct Cat { +} + +Class.extends(Cat,Animal) + +terra Cat:speak() : {} + IO.printf("meow! %d\n",self.data) +end + +terra dospeak(a : &Animal) + a:speak() +end + +terra barnyard() + var c : Cat + var d : Dog + c:init() + d:init() + c.data,d.data = 0,1 + + dospeak(&c) + dospeak(&d) +end +barnyard() + + +local Add = Class.interface { add = int -> int } + +local Sub = Class.interface { sub = int -> int } + +local struct P { + data : int +} +Class.implements(P,Add) + +local struct C { + data2 : int +} +Class.extends(C,P) +Class.implements(C,Sub) + +terra P:add(b : int) : int + self.data = self.data + b + return self.data +end + +terra C:sub(b : int) : int + return self.data2 - b +end + +terra doadd(a : &Add) + return a:add(1) +end + +terra dopstuff(p : &P) + return p:add(2) + doadd(p) +end + +terra dosubstuff(s : &Sub) + return s:sub(1) +end + +terra dotests() + var p : P + p:init() + var c : C + c:init() + p.data = 1 + c.data = 1 + c.data2 = 2 + return dopstuff(&p) + dopstuff(&c) + dosubstuff(&c) +end + +assert(dotests() == 15) + +terra timeadd(a :&P, N : int) + IO.printf("%p\n",a) + for i = 0, N,10 do + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + a:add(1) + end + return a +end + +local a = global(C) + +terra doinit() : &P + a:init() + a.data = 0 + return &a +end + +local v = doinit() +timeadd:compile() + +local b = terralib.currenttimeinseconds() +--timeadd(v,100000000) +local e = terralib.currenttimeinseconds() +print(e - b) +print(v.data) + diff --git a/samples/Terra/class2.t b/samples/Terra/class2.t new file mode 100644 index 00000000..1af0da25 --- /dev/null +++ b/samples/Terra/class2.t @@ -0,0 +1,45 @@ +local Interface = require("lib/golike") + +local I = Interface.create { + get = {} -> int; + set = int -> {}; +} + +struct A { + data : int +} + +terra A:get() + return self.data +end + +terra A:set(a : int) + self.data = a +end + +struct B { + data : int +} + +terra B:get() + return self.data + 1 +end + +terra B:set(a : int) + self.data = self.data + a +end + + +terra anInterface(a : I) + a:set(3) + return a:get() +end + +terra foo() + var a = A { 0 } + var b = B { 2 } + return anInterface(&a) + anInterface(&b) +end + +local test = require("test") +test.eq(foo(),9) \ No newline at end of file diff --git a/samples/Terra/class3.t b/samples/Terra/class3.t new file mode 100644 index 00000000..8ecc6963 --- /dev/null +++ b/samples/Terra/class3.t @@ -0,0 +1,36 @@ + +C = terralib.includec("stdio.h") + +local Class = require("lib/javalike") + + +local Prints = Class.interface { print = {} -> {} } + +struct Leaf { + data : int +} +Class.implements(Leaf,Prints) + +terra Leaf:print() : {} + C.printf("%d\n",self.data) +end + + +struct Node { + next : &Leaf +} +Class.extends(Node,Leaf) + +terra Node:print() : {} + C.printf("%d\n",self.data) + self.next:print() +end + +terra test() + var a,b = Leaf.alloc(), Node.alloc() + a.data,b.data,b.next = 1,2,a + var p : &Prints = b + p:print() +end + +test() \ No newline at end of file diff --git a/samples/Terra/class4.t b/samples/Terra/class4.t new file mode 100644 index 00000000..f369adba --- /dev/null +++ b/samples/Terra/class4.t @@ -0,0 +1,22 @@ +J = require("lib/javalike") + +struct Shape { + foo : int +} + +Drawable = J.interface { draw = {} -> {} } + +struct Square { + length : int +} +J.extends(Square,Shape) +J.implements(Square,Drawable) + +terra Square:draw() : {} end + +terra bar() + var a : &Square = Square.alloc() + a:draw() +end + +bar() \ No newline at end of file diff --git a/samples/Terra/class5.t b/samples/Terra/class5.t new file mode 100644 index 00000000..ab021c59 --- /dev/null +++ b/samples/Terra/class5.t @@ -0,0 +1,54 @@ + +IO = terralib.includec("stdio.h") +local Class = require("lib/javalike") + +struct A { + a : int; + bb : &B +} and struct B { + b : int; + aa : &A +} +Class.extends(B,A) + + +terra A:times2() : int + return self.a*2 +end + +terra B:combine(a : int) : int + return self.b + self.a + a +end + + +struct C { + c : double +} +Class.extends(C,B) + +terra C:combine(a : int) : int + return self.c + self.a + self.b + a +end +terra C:times2() : double + return self.a * 4 +end + +terra doubleAnA(a : &A) + return a:times2() +end + +terra combineAB(b : &B) + return b:combine(3) +end + +terra returnA(a : A) + return a +end +terra foobar1() + var c = C.alloc() + c.a,c.b,c.c = 1,2,3.5 + return c:times2() +end + +assert(foobar1() == 4) + diff --git a/samples/Terra/class6.t b/samples/Terra/class6.t new file mode 100644 index 00000000..008610db --- /dev/null +++ b/samples/Terra/class6.t @@ -0,0 +1,66 @@ + +IO = terralib.includec("stdio.h") +local Class = require("lib/javalikesimple") + +struct A { + a : int +} +terra A:times2() : int + return self.a*2 +end + +struct B { + b : int +} +Class.extends(B,A) + +terra B:combine(a : int) : int + return self.b + self.a + a +end + +struct C { + c : double +} +Class.extends(C,B) + +terra C:combine(a : int) : int + return self.c + self.a + self.b + a +end +terra C:times2() : double + return self.a * 4 +end + +terra doubleAnA(a : &A) + return a:times2() +end + +terra combineAB(b : &B) + return b:combine(3) +end + +terra foobar1() + var c : C + c:init() + c.a,c.b,c.c = 1,2,3.5 + return c:times2() +end + +assert(foobar1() == 4) + +terra foobar() + + var a : A, b : B, c : C + a:init(); b:init(); c:init() + + a.a = 1 + b.a,b.b = 1,2 + c.a,c.b,c.c = 1,2,3.5 + + var r = b:times2() + doubleAnA(&a) + doubleAnA(&b) + doubleAnA(&c) + combineAB(&b) + combineAB(&c) + + return r +end + + +assert(25 == foobar()) + diff --git a/samples/Terra/classifyfloatstructs.t b/samples/Terra/classifyfloatstructs.t new file mode 100644 index 00000000..e9c4d66b --- /dev/null +++ b/samples/Terra/classifyfloatstructs.t @@ -0,0 +1,137 @@ +-- reading and returning by reference produces far more compact assembly +-- in terra than reading and returning by value, even when a by-value function +-- is embedded in a by-reference wrapper. Also, optimization behavior varies +-- wildly. Here's a demonstration: + +local float4 = tuple(float,float,float,float) + +-- first, comparing assembly code size of stand-alone functions which all do +-- the same operation, in different styles. This is after aggressive +-- optimization by the LLVM backend +-------------------------------------------------------------------------------- + +-- read by value, return by value +-- 26 instructions, 120 bytes +local terra add4_val_val(a : float4, b : float4) : float4 + return { + a._0+b._0, + a._1+b._1, + a._2+b._2, + a._3+b._3 + } +end + +-- read by value, return by reference +-- 24 instructions, 103 bytes +local terra add4_val_ref(c : &float4, a : float4, b: float4) : {} + c._0 = a._0 + b._0 + c._1 = a._1 + b._1 + c._2 = a._2 + b._2 + c._3 = a._3 + b._3 +end + +-- read by reference, return by value +-- 14 instructions, 74 bytes +local terra add4_ref_val(a : &float4, b : &float4) : float4 + return { + a._0+b._0, + a._1+b._1, + a._2+b._2, + a._3+b._3 + } +end + +-- read by reference, return by reference +-- 12 instructions, 57 bytes +local terra add4_ref_ref(c : &float4, a : &float4, b: &float4) : {} + c._0 = a._0 + b._0 + c._1 = a._1 + b._1 + c._2 = a._2 + b._2 + c._3 = a._3 + b._3 +end + +-- read by reference, return by reference, BUT use temporary variables +-- 4 instructions, 12 bytes (!!!) +-- what happens here is that the tempvars are understood and treated +-- as a single SIMD register, and so only one addition is executed. +-- this is already reflected in the bytecode passed to LLVM, so I suppose +-- terra does this optimization. +local terra add4_ref_ref_tempvar(c : &float4, a : &float4, b: &float4) : {} + var x = a._0 + b._0 + var y = a._1 + b._1 + var z = a._2 + b._2 + var w = a._3 + b._3 + c._0 = x + c._1 = y + c._2 = z + c._3 = w +end + +-- turn on always-inline for later +add4_val_val:setinlined(true) +add4_val_ref:setinlined(true) +add4_ref_val:setinlined(true) +add4_ref_ref:setinlined(true) + +-- uncomment to look at individual LLVM bytecode & disassembly +add4_val_val:disas() +-- add4_val_ref:disas() +-- add4_ref_val:disas() +-- add4_ref_ref:disas() +-- add4_ref_ref_tempvar:disas() + + +if terralib.lookupsymbol and require("ffi").os ~= "Windows" then + terra sizecheck() + var si : terralib.SymbolInfo + terralib.lookupsymbol(add4_val_val,&si) + return si.size + end + assert(sizecheck() < 16) +end +-------------------------------------------------------------------------------- + +-- up to this point, one could argue that functions are always inlined into +-- other functions, and that the instructions dissolve in the greater scheme +-- of things. + +-- if that is true, let's attempt to convert one style into another and see +-- if the optimizations catch up. + +-- read by value, return by value -> read by reference, return by reference +-- the clunky solution to the slim interface +-- 38 instructions (!), 193 bytes (!!), so it gets *worse* +local terra add4_val_val_to_ref_ref(c : &float4, a : &float4, b: &float4) : {} + @c = add4_val_val(@a, @b) +end + +-- read by reference, return by reference -> read by value, return by value +-- the slim solution to the clunky interface +-- 13 instructions, 61 bytes with tempvar -- wow, that's better than +-- the original function with the actual code in it! +local terra add4_ref_ref_to_val_val(a : float4, b : float4) : float4 + var c : float4 + add4_ref_ref_tempvar(&c, &a, &b) + return c +end + +-- so what happens if we do a conversion back to the by-reference interface? +-- 41 instructions, 194 bytes +local terra add4_ref_ref_to_val_val_to_ref_ref(c : &float4, a : &float4, b: &float4) : {} + @c = add4_ref_ref_to_val_val(@a, @b) +end + +-- and nest it once more, back to the by-value interface +-- 47 instructions, 208 bytes +-- so once we pass structs by-value, we'll never recover. +local terra add4_ref_ref_to_val_val_to_ref_ref_to_val_val(a : float4, b : float4) : float4 + var c : float4 + add4_ref_ref_to_val_val_to_ref_ref(&c, &a, &b) + return c +end + +-- uncomment to look at individual disassembly +-- add4_val_val_to_ref_ref:disas() +-- add4_ref_ref_to_val_val:disas() +-- add4_ref_ref_to_val_val_to_ref_ref:disas() +-- add4_ref_ref_to_val_val_to_ref_ref_to_val_val:disas() \ No newline at end of file diff --git a/samples/Terra/clean.t b/samples/Terra/clean.t new file mode 100644 index 00000000..078a3963 --- /dev/null +++ b/samples/Terra/clean.t @@ -0,0 +1,17 @@ +local c = terralib.includec("stdio.h") + +iamclean = macro(function(arg) + return quote + var a = 3 + return a,arg + end +end) + +terra doit() + var a = 4 + iamclean(a) +end + +local a = doit() +local test = require("test") +test.meq({3,4}, a) \ No newline at end of file diff --git a/samples/Terra/cnames.t b/samples/Terra/cnames.t new file mode 100644 index 00000000..5a487ce2 --- /dev/null +++ b/samples/Terra/cnames.t @@ -0,0 +1,25 @@ +struct Foo { + c : float; +} +Foo.displayname = "struct.Foo" +terra useFoo() + var a : Foo + a.c = 4.5 + return a.c +end +assert(4.5 == useFoo()) + +C = terralib.includecstring [[ +typedef struct { int a; int b; } Foo; +]] + +terra stuff() + var a : Foo + var b : C.Foo + b.a = 1 + b.b = 2 + a.c = 4.5 + return b.a + b.b + a.c +end + +assert(7.5 == stuff()) diff --git a/samples/Terra/cnamespace.t b/samples/Terra/cnamespace.t new file mode 100644 index 00000000..dc3c6cc2 --- /dev/null +++ b/samples/Terra/cnamespace.t @@ -0,0 +1,24 @@ +G,T = terralib.includecstring [[ + typedef struct { + double c; + } A; + struct A { + int b; + }; +]] + +G2,T2 = terralib.includecstring [[ + struct A; + typedef struct C A; +]] + +assert(T2.A == T.A) +assert(G2.A ~= T2.A and G2.A ~= G.A) +terra foo() + var a : G.A + var b : T.A + a.c = 4.5 + b.b = 4.5 + return a.c + b.b +end +assert(8.5 == foo()) \ No newline at end of file diff --git a/samples/Terra/cnamespaces.t b/samples/Terra/cnamespaces.t new file mode 100644 index 00000000..54ad3cd0 --- /dev/null +++ b/samples/Terra/cnamespaces.t @@ -0,0 +1,32 @@ +C,T = terralib.includecstring [[ + + struct Foo { + int a; + }; + typedef int Foo; + typedef struct Foo * Bar; + typedef Foo * Bar2; + Bar bar(struct Foo * a, Bar2 b) { return (Bar)0; } +]] + +terra what() + var f = T.Foo { 3 } + var a : C.Bar = &f + return a.a +end + +assert(3 == what()) + +C,T = terralib.includecstring [[ + typedef struct { int a; } Foo; + typedef Foo * FooPtr; + int returna(FooPtr a) { return a->a; } +]] + +terra what2() + var a : C.Foo = C.Foo { 3 } + var ap : C.FooPtr = &a + return C.returna(ap) +end + +assert(3 == what2()) diff --git a/samples/Terra/compilecallback.t b/samples/Terra/compilecallback.t new file mode 100644 index 00000000..147fdb0e --- /dev/null +++ b/samples/Terra/compilecallback.t @@ -0,0 +1,16 @@ + +local a = 0 +local foo = macro(function(arg) + bar:gettype(function() + a = bar() + end) + a = 1 + return 3 +end) + +terra bar() + return foo() +end + +assert(bar() == 3) +assert(a == 3) \ No newline at end of file diff --git a/samples/Terra/completec.t b/samples/Terra/completec.t new file mode 100644 index 00000000..0b16091e --- /dev/null +++ b/samples/Terra/completec.t @@ -0,0 +1,13 @@ +C = terralib.includecstring [[ + struct A { + int a; + }; + static struct A foo; + struct A * getfoo() { + foo.a = 4; + return &foo; + } +]] + +assert(C.getfoo().a == 4) + diff --git a/samples/Terra/conflict.t b/samples/Terra/conflict.t new file mode 100644 index 00000000..ac871735 --- /dev/null +++ b/samples/Terra/conflict.t @@ -0,0 +1,12 @@ + +terra foo() +end + +foo:compile() +terralib.dumpmodule() + +terralib.includecstring [[ + int foo() { + return 4; + } +]] \ No newline at end of file diff --git a/samples/Terra/constant.t b/samples/Terra/constant.t new file mode 100644 index 00000000..6f44ee87 --- /dev/null +++ b/samples/Terra/constant.t @@ -0,0 +1,35 @@ + + +local foo = terralib.constant(terralib.new(int[4],{1,2,3,4})) + +struct A { + a : int; + b : float +} + +local mystr = terralib.new(A,{3,4.5}) +local const = constant(mystr) + + +terra bar() + return foo[3] + mystr.a +end + +terra bar2() + return foo[1] + mystr.b +end + +function wrapper(a) + return a + 1 +end + +local p1 = terralib.constant(int -> int, wrapper) + +terra doit() + return p1(3) +end + +local test = require("test") +test.eq(bar(),7) +test.eq(bar2(),6.5) +test.eq(doit(),4) diff --git a/samples/Terra/constant2.t b/samples/Terra/constant2.t new file mode 100644 index 00000000..bc18b2ca --- /dev/null +++ b/samples/Terra/constant2.t @@ -0,0 +1,21 @@ + + +local a = terralib.new(int,3) +local b = terralib.new(int8,4) +local c = terralib.new(int64,5) +local d = terralib.new(float,3.25) +local e = terralib.new(double,4.25) + +f = global(4) +terra foo() + return &f +end + +local pf = foo() + +terra bar() + return a + b + c + d + e + @pf +end + +local test = require("test") +test.eq(bar(),23.5) \ No newline at end of file diff --git a/samples/Terra/constructor.t b/samples/Terra/constructor.t new file mode 100644 index 00000000..87161fe9 --- /dev/null +++ b/samples/Terra/constructor.t @@ -0,0 +1,12 @@ + +local b = 1 +local dd = "d" +local c = symbol() + +terra foo() + var a = { _0 = [b], [c] = 2, [dd] = 3, r = 4} + return a._0 + a.[c] + a.d + a.r +end + +local test = require("test") +test.eq(foo(),10) \ No newline at end of file diff --git a/samples/Terra/coverage.t b/samples/Terra/coverage.t new file mode 100644 index 00000000..e865d664 --- /dev/null +++ b/samples/Terra/coverage.t @@ -0,0 +1,47 @@ +function failit(match,fn) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,match) then + error("failed wrong: "..msg,2) + end +end +local test = require("test") +local erd = "Errors reported during" + +local terra f1() + return test +end + +failit(erd,function() +f1:compile() +end) +failit("referencing a function which failed to compile",function() +f1:compile() +end) +failit(erd,function() + local terra foo() + f1() + end + foo() +end) + + +local struct A { + a : int +} + +A.metamethods.__getentries = function(self) + error("I AM BAD") +end + +failit(erd,function() + A:complete() +end) + +failit(erd,function() + local terra foo() + var a : A + end + foo() +end) diff --git a/samples/Terra/coverage2.t b/samples/Terra/coverage2.t new file mode 100644 index 00000000..b74377f7 --- /dev/null +++ b/samples/Terra/coverage2.t @@ -0,0 +1,209 @@ +function failit(match,fn) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,match) then + error("failed wrong: "..msg,2) + end +end +local test = require("test") +local erd = "Errors reported during" + +failit(erd,function() +local aglobal = 5 +local terra foo() + return [ (function() aglobal = 4; return 3 end)() ] +end +foo() +end) + +A = terralib.types.newstruct() +A.entries:insert{ field = "a", type = int[2] } + +A.metamethods.__getentries = function() error("NOPE") end + +failit(erd,function() + A:complete() +end) +local terra foo() + var a : int[2] + return 3 +end +foo:compile() +local a = 0 +foo:gettype(function() + a = a + 1 +end) +assert(a == 1) + +local terra errored +failit(erd,function() + terra errored() + return A + end + errored:compile() +end) +failit("referencing a function which failed to compile",function() + errored() +end) + +local terra ol(a : int) return a end +terra ol(a : int, b : int) return a + b end + +assert(ol(3) == 3) +assert(ol(3,4) == 7) + +failit("bad argument #1",function() + ol("a") +end) + +ol:printstats() +NSE = terralib.types.newstruct() + +failit(erd,function() + NSE.entries:insert { field = "a", type = "b" } + NSE:complete() +end) + +SICS = terralib.types.newstruct() +SICS.entries:insert { field = symbol(), type = int } +a = 1 +SICS.metamethods.__staticinitialize = function() a = a + 1 end +print(terralib.new(SICS,{3})) + +NSF = terralib.types.newstruct() +NSF.entries:insert { type = int , field = 3 } + +failit(erd,function() + NSF:complete() +end) +SICS:complete() +assert(a == 2) +struct SF { + a : SF2 +} and struct SF2 { + a : int +} +SF2.metamethods.__getentries = function(self) SF:complete() end +failit(erd,function() +SF:complete() +end) +failit("Attempting to get a property of a type that previously resulted in an error.",function() +SF:complete() +end) + +failit(erd,function() + struct SF { b : int } +end) + + +struct C { + a : int +} + +C.metamethods.__cast = function() return error("CAST ERROR") end + +local terra casttest() + return int(C { 3 }) +end +failit(erd,function() +casttest() +end) + +local terra shiftcheck() + var r = 1 << vector(1,2,3,4) + var r2 = vector(1,2,3,4) << 1 + return r[0],r[1],r[2],r[3],r2[0],r2[1],r2[2],r2[3] +end + +test.meq({2,4,8,16,2,4,6,8},shiftcheck()) + +failit(erd,function() + local terra foo() + return terralib.select(3,4,5) + end + foo() +end) +failit(erd,function() + local terra foo() + return (4):foo() + end + foo() +end) +failit(erd,function() + local terra foo() + return (C {3}):foo() + end + foo() +end) +failit(erd,function() + local a = { a = 4} + local terra foo() + return a() + end + foo() +end) +local saveit +local foom = macro(function(arg) saveit = arg; arg:astype(); end) +failit(erd,function() + local terra foo() + return foom(4) + end + foo() +end) + +failit(erd,function() + local terra foo() + return saveit + end + foo() +end) +failit(erd,function() + local struct A { + a : 3 + } +end) + +failit(erd,function() + local terra foo + local bar = macro(function() foo:compile() end) + terra foo() + return bar() + end + foo() +end) + +struct ATF { + a : int +} + +ATF.metamethods.__getentries = function(self) + local terra foo() + var a : ATF + return a.a + end + foo:compile() +end + +failit(erd,function() + ATF:complete() +end) + +struct FA { + a : &FA2 +} and struct FA2 { + a : int +} + +FA.metamethods.__staticinitialize = function() a = a + 1 end + +FA2.metamethods.__staticinitialize = function(self) + FA:complete() +end + +FA:complete() + +assert(a == 3) +--[[ +freezing asynchronus needs to be called +]] \ No newline at end of file diff --git a/samples/Terra/coverage3.t b/samples/Terra/coverage3.t new file mode 100644 index 00000000..495d6251 --- /dev/null +++ b/samples/Terra/coverage3.t @@ -0,0 +1,107 @@ +function failit(match,fn) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,match) then + error("failed wrong: "..msg,2) + end +end +local test = require("test") +local erd = "Errors reported during" + +terra foo() +end +foo:compile() +failit("inlining",function() foo:setinlined(false) end) + +terra bar +failit("attempting to call",function() +bar:compile() end) + +failit("expected a name",function() + terralib.intrinsic("far","far") +end) + +print((&int)[4]) + +struct A { + a : int[4]; + b : ∫ + union { c : int; d : int}; + e : int -> int; +} +A:printpretty() + +terra rv() return vector(1,2,3) end + +terra varg(a : vector(int,3)) + + return a[0] + a[1] + a[2] +end + +assert (6 == varg(rv())) + +local something = nil +failit(erd,function() +local terra a([something]) +end +end) + +failit(erd,function() +local terra what() + var a = A { ee = 4 } +end +what:compile() +end) + +A.metamethods.__getmethod = function(self,methodname) + return 1 +end + +failit(erd,function() +local terra what() + var a : A + A.something() +end +what:compile() +end) + +C = terralib.includec("stdio.h") + +--TODO: __sputc isn't consistent across architectures, so this is a bad test +--failit("cannot import",function() +--local a = C.__sputc +--end) + +failit("not found",function() +local a = C.nothing +end) + +terra up() return unpackstruct(3) end +assert(3 == up()) + +failit(erd,function() +local terra aloads(a : &int) + return terralib.attrload(a),terralib.attrload(nil,{3}),terralib.attrstore(a,nil) +end +aloads:compile() +end) + +terra f() end +terra f(a:int) end +failit("overloaded", function() +terralib.saveobj("err.o", { f = f}) +end) + +terra nop() end +terra nilcall(a : tuple(), b : int) + return b,a +end +terra nilstuff() + var a = nop() + var c,b = nilcall(a,4) + return c +end +assert(4 == nilstuff()) + +nilcall:disas() \ No newline at end of file diff --git a/samples/Terra/crash1.t b/samples/Terra/crash1.t new file mode 100644 index 00000000..91732c22 --- /dev/null +++ b/samples/Terra/crash1.t @@ -0,0 +1 @@ +a = &int \ No newline at end of file diff --git a/samples/Terra/crash2.t b/samples/Terra/crash2.t new file mode 100644 index 00000000..789ad7ac --- /dev/null +++ b/samples/Terra/crash2.t @@ -0,0 +1 @@ +a = global( struct { a : int } ) \ No newline at end of file diff --git a/samples/Terra/cstruct.t b/samples/Terra/cstruct.t new file mode 100644 index 00000000..6f4f0d9d --- /dev/null +++ b/samples/Terra/cstruct.t @@ -0,0 +1,20 @@ +local C = terralib.includecstring [[ + +struct teststruct { + int idata; + float fdata; +}; + +void makeitlive(struct teststruct * s) {} + +]] + + +terra foo() + var a : C.teststruct + a.idata = 3 + a.fdata = 3.5 + return a.idata + a.fdata +end + +assert(foo() == 6.5) \ No newline at end of file diff --git a/samples/Terra/cstruct2.t b/samples/Terra/cstruct2.t new file mode 100644 index 00000000..8b557c8a --- /dev/null +++ b/samples/Terra/cstruct2.t @@ -0,0 +1,8 @@ + + +C = terralib.includecstring [[ + typedef struct { int x; int y; } Point; + Point mkpoint() { Point p; p.x = p.y = 3; return p; } +]] + +assert(C.mkpoint().x == 3) \ No newline at end of file diff --git a/samples/Terra/cudaagg.t b/samples/Terra/cudaagg.t new file mode 100644 index 00000000..12fcf4ed --- /dev/null +++ b/samples/Terra/cudaagg.t @@ -0,0 +1,56 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) +local ntid = cudalib.nvvm_read_ptx_sreg_ntid_x -- terralib.intrinsic("llvm.nvvm.read.ptx.sreg.ntid.x",{} -> int) + + +struct OneFloat { + a : int; +} + +fn = terra(result : &float, bar : int[5]) + result[tid()] = bar[0] + bar[1] + bar[2] + bar[3] + bar[4] +end +fn:setinlined(false) + +--our very simple cuda kernel +--more work needs to be done to expose the right CUDA intrinsics +--to do more complicated things +foo = terra(result : &float) + fn(result, array(tid(),tid()+1,tid()+2,tid()+3,tid()+4) ) +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] +local R = terralib.cudacompile({ bar = foo }) + +terra doit(N : int) + var data : &float + C.cudaMalloc([&&opaque](&data),sizeof(float)*N) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,data) + var results : &float = [&float](C.malloc(sizeof(float)*N)) + C.cudaMemcpy(results,data,sizeof(float)*N,2) + var result = 0.f + for i = 0,N do + result = result + results[i] + end + return result +end + +local test = require("test") +local N = 16 + +function s(n) return (n - 1) * n / 2 end +function ex(i) return s(N+i) - s(i) end +local expected = ex(0) + ex(1) + ex(2) + ex(3) + ex(4) +test.eq(doit(N),expected) + diff --git a/samples/Terra/cudaaggregate.t b/samples/Terra/cudaaggregate.t new file mode 100644 index 00000000..9d6070d7 --- /dev/null +++ b/samples/Terra/cudaaggregate.t @@ -0,0 +1,45 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) +local ntid = cudalib.nvvm_read_ptx_sreg_ntid_x -- terralib.intrinsic("llvm.nvvm.read.ptx.sreg.ntid.x",{} -> int) + +struct A { + a : int + b : int + c : int[2] +} +terra foo(result : &float,a : A, c : int, d : int[2]) + var t = tid() + result[t] = t + a.a + a.b + c + a.c[0] + a.c[1] + d[0] + d[1] +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] +local R = terralib.cudacompile({ bar = foo }) + +terra doit(N : int) + var data : &float + C.cudaMalloc([&&opaque](&data),sizeof(float)*N) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,data, A { 1,2, array(3,4) },5,array(6,7)) + var results : &float = [&float](C.malloc(sizeof(float)*N)) + C.cudaMemcpy(results,data,sizeof(float)*N,2) + var result = 0.f + for i = 0,N do + result = result + results[i] + end + return result +end + +local test = require("test") +local N = 16 +local expected = (N - 1)*N/2 + N*(1 + 2 + 3 + 4 + 5 + 6 + 7) +test.eq(doit(N),expected) diff --git a/samples/Terra/cudaatomic.t b/samples/Terra/cudaatomic.t new file mode 100644 index 00000000..ebe3425d --- /dev/null +++ b/samples/Terra/cudaatomic.t @@ -0,0 +1,39 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) + +foo = terra(result : &int) + var t = tid() + terralib.asm(terralib.types.unit,"red.global.max.u32 [$0], $1;","l,r",true,result,t) +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] + +sync = terralib.externfunction("cudaThreadSynchronize", {} -> int) + +local R = terralib.cudacompile({ bar = foo },true) + +terra doit(N : int) + var data = 0 + var location : &int + C.cudaMalloc([&&opaque](&location),sizeof(int)) + C.cudaMemcpy(location,&data,sizeof(int),1) + + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,location) + var data2 = -1 + C.cudaMemcpy(&data2,location,sizeof(int),2) + return data2 +end + +assert(doit(32) == 31) diff --git a/samples/Terra/cudaconst2.t b/samples/Terra/cudaconst2.t new file mode 100644 index 00000000..6ca8eead --- /dev/null +++ b/samples/Terra/cudaconst2.t @@ -0,0 +1,14 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local const = cudalib.constantmemory(float, 1) + +local terra kernel(data: &float) +end + +local M = terralib.cudacompile({ + kernel = kernel, + const = const +}) diff --git a/samples/Terra/cudaglobal.t b/samples/Terra/cudaglobal.t new file mode 100644 index 00000000..53564f4f --- /dev/null +++ b/samples/Terra/cudaglobal.t @@ -0,0 +1,48 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) +local ntid = cudalib.nvvm_read_ptx_sreg_ntid_x -- terralib.intrinsic("llvm.nvvm.read.ptx.sreg.ntid.x",{} -> int) + + +theone = global(0) + +theconst = cudalib.constantmemory(int,1) + +terra foo(result : &float) + result[tid()] = tid() + theone + theconst[0] +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] +local R = terralib.cudacompile({ foo = foo, aone = theone, theconst = theconst }) + +terra doit(N : int) + var data : &float + C.cudaMalloc([&&opaque](&data),sizeof(float)*N) + var one = 1 + var two = 2 + C.cudaMemcpy(R.aone,&one,sizeof(int),1) + C.cudaMemcpy(R.theconst,&two,sizeof(int),1) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.foo(&launch,data) + var results : &float = [&float](C.malloc(sizeof(float)*N)) + C.cudaMemcpy(results,data,sizeof(float)*N,2) + var result = 0.f + for i = 0,N do + result = result + results[i] + end + return result +end + +local test = require("test") +local N = 16 +local expected = (N - 1)*N/2 + 3*N +test.eq(doit(N),expected) diff --git a/samples/Terra/cudahello.t b/samples/Terra/cudahello.t new file mode 100644 index 00000000..96b68462 --- /dev/null +++ b/samples/Terra/cudahello.t @@ -0,0 +1,30 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) + +C = terralib.includec("stdio.h") +vprintf = terralib.externfunction("cudart:vprintf", {&int8,&int8} -> int) + +foo = terra(result : &float) + var t = tid() + vprintf("%d\n",[&int8](&t)) +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + + +sync = terralib.externfunction("cudaThreadSynchronize", {} -> int) + +local R = terralib.cudacompile({ bar = foo }) + +terra doit(N : int) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,nil) + sync() + C.printf("and were done\n") +end + +doit(3) diff --git a/samples/Terra/cudaoffline.t b/samples/Terra/cudaoffline.t new file mode 100644 index 00000000..f750027b --- /dev/null +++ b/samples/Terra/cudaoffline.t @@ -0,0 +1,65 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x +local ntid = cudalib.nvvm_read_ptx_sreg_ntid_x + +theone = global(0) + +theconst = cudalib.constantmemory(int,1) + +terra foo(result : &float) + result[tid()] = tid() + theone + theconst[0] +end + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] + +local R,L = terralib.cudacompile({ foo = foo, aone = theone, theconst = theconst },nil,nil,false) + +terra doit(N : int) + var data : &float + C.cudaMalloc([&&opaque](&data),sizeof(float)*N) + var one = 1 + var two = 2 + C.cudaMemcpy(R.aone,&one,sizeof(int),1) + C.cudaMemcpy(R.theconst,&two,sizeof(int),1) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.foo(&launch,data) + var results : &float = [&float](C.malloc(sizeof(float)*N)) + C.cudaMemcpy(results,data,sizeof(float)*N,2) + var result = 0.f + for i = 0,N do + result = result + results[i] + end + return result +end + + +terra main() : int + if L(nil,nil,nil,0) ~= 0 then + C.printf("WHAT\n") + end + var N = 16 + var expected = (N - 1)*N/2 + 3*N + return terralib.select(doit(N) == expected,0,1) +end + + +local ffi = require 'ffi' +local path = ({ OSX = "/lib", Linux = "/lib64", Windows = "\\lib\\x64" })[ffi.os] +path = terralib.cudahome..path + + +local args = ffi.os == "Windows" and {path.."\\cuda.lib", path.."\\cudart.lib"} + or {"-L"..path, "-Wl,-rpath,"..path, "-lcuda", "-lcudart"} + +local name = ffi.os == "Windows" and ".\\cudaoffline.exe" or "./cudaoffline" +terralib.saveobj(name,{ main = main },args) +assert(os.execute(name) == 0) + diff --git a/samples/Terra/cudaoo.t b/samples/Terra/cudaoo.t new file mode 100644 index 00000000..65b16688 --- /dev/null +++ b/samples/Terra/cudaoo.t @@ -0,0 +1,20 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end +C = terralib.includec("cuda_runtime.h") + +cudalib.linkruntime() + +terra foo() + var stuff : &opaque + C.cudaMalloc(&stuff,sizeof(int)) + return stuff +end + +local a = foo() + +terra blank() end +terralib.cudacompile { blank = blank } + +assert(0 == C.cudaMemset(a,0,4)) diff --git a/samples/Terra/cudaprintf.t b/samples/Terra/cudaprintf.t new file mode 100644 index 00000000..b9ef9b73 --- /dev/null +++ b/samples/Terra/cudaprintf.t @@ -0,0 +1,50 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) + +vprintf = terralib.externfunction("cudart:vprintf", {&int8,&int8} -> int) +local function createbuffer(args) + local Buf = terralib.types.newstruct() + return quote + var buf : Buf + escape + for i,e in ipairs(args) do + local typ = e:gettype() + local field = "_"..tonumber(i) + typ = typ == float and double or typ + table.insert(Buf.entries,{field,typ}) + emit quote + buf.[field] = e + end + end + end + in + [&int8](&buf) + end +end +printf = macro(function(fmt,...) + local buf = createbuffer({...}) + return `vprintf(fmt,buf) +end) + +foo = terra(result : &float) + var t = tid() + printf("a = %d, b = %f, c = %d\n",t,1.0 + t,t + 2) +end + +sync = terralib.externfunction("cuStreamSynchronize", {&opaque} -> int) + +annotations = { {"maxntidx",43}, {"minctasm",8}} -- example of annotating cuda kernel with launch bounds +local R = terralib.cudacompile({ bar = { kernel = foo, annotations = annotations }}) + +terra doit(N : int) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,nil) + sync(nil) +end + +doit(3) + diff --git a/samples/Terra/cudashared.t b/samples/Terra/cudashared.t new file mode 100644 index 00000000..cde12737 --- /dev/null +++ b/samples/Terra/cudashared.t @@ -0,0 +1,47 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) + +N = 1024 + +somedata = cudalib.sharedmemory(int,N) + +terra bar(result : &int) + var t = tid() + somedata[t] = t + cudalib.nvvm_barrier0() + result[t] = somedata[N - 1 - t] +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] + +local R = terralib.cudacompile({ bar = bar },true) + + +terra doit(N : int) + var data : &int + C.cudaMalloc([&&opaque](&data),sizeof(int)*N) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,data) + var results : &int = [&int](C.malloc(sizeof(int)*N)) + C.cudaMemcpy(results,data,sizeof(int)*N,2) + var result = 0 + for i = 0,N do + --C.printf("result = %d\n",results[i]) + result = result + results[i] + end + return result +end + +local test = require("test") +local expected = (N - 1)*N/2 +test.eq(doit(N),expected) diff --git a/samples/Terra/cudatest.t b/samples/Terra/cudatest.t new file mode 100644 index 00000000..a15d54fc --- /dev/null +++ b/samples/Terra/cudatest.t @@ -0,0 +1,47 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) +local ntid = cudalib.nvvm_read_ptx_sreg_ntid_x -- terralib.intrinsic("llvm.nvvm.read.ptx.sreg.ntid.x",{} -> int) + +fn = terra(result : &float) + var t = tid() + result[t] = t +end +fn:setinlined(false) +--our very simple cuda kernel +--more work needs to be done to expose the right CUDA intrinsics +--to do more compilicated things +foo = terra(result : &float) + fn(result) +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +local C = terralib.includecstring [[ +#include "cuda_runtime.h" +#include +#include +]] +local R = terralib.cudacompile({ bar = foo }) + +terra doit(N : int) + var data : &float + C.cudaMalloc([&&opaque](&data),sizeof(float)*N) + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.bar(&launch,data) + var results : &float = [&float](C.malloc(sizeof(float)*N)) + C.cudaMemcpy(results,data,sizeof(float)*N,2) + var result = 0.f + for i = 0,N do + result = result + results[i] + end + return result +end + +local test = require("test") +local N = 16 +local expected = (N - 1)*N/2 +test.eq(doit(N),expected) diff --git a/samples/Terra/cudatex.t b/samples/Terra/cudatex.t new file mode 100644 index 00000000..cebb6079 --- /dev/null +++ b/samples/Terra/cudatex.t @@ -0,0 +1,59 @@ +if not terralib.cudacompile then + print("CUDA not enabled, not performing test...") + return +end + +local tid = cudalib.nvvm_read_ptx_sreg_tid_x--terralib.intrinsic("llvm.nvvm.read.ptx.sreg.tid.x",{} -> int) + +C = terralib.includecstring [[ +#include +#include +#include +#include +]] + +vprintf = terralib.externfunction("cudart:vprintf", {&int8,&int8} -> int) + +foo = terra(result : C.cudaTextureObject_t) + var t = tid() + var r = terralib.asm([tuple(float,float,float,float)], + "tex.1d.v4.f32.s32 {$0,$1,$2,$3}, [$4, {$5}];", + "=f,=f,=f,=f,l,r",false,result,t) + var rr : double = r._0 + vprintf("%f\n",[&int8](&rr)) +end + +terralib.includepath = terralib.includepath..";/usr/local/cuda/include" + +sync = terralib.externfunction("cudaThreadSynchronize", {} -> int) + +local R = terralib.cudacompile({ foo = foo }) + +terra doit(N : int) + var d_buffer : &double + C.cudaMalloc([&&opaque](&d_buffer),N*sizeof(float)) + + var h_buffer = arrayof(float,0,1,2,3,4,5,6,7,8,9,10) + C.cudaMemcpy(d_buffer,&h_buffer[0],sizeof(float)*N, C.cudaMemcpyHostToDevice) + + var resDesc : C.cudaResourceDesc + C.memset(&resDesc,0,sizeof(C.cudaResourceDesc)) + resDesc.resType = C.cudaResourceTypeLinear; + resDesc.res.linear.devPtr = d_buffer; + resDesc.res.linear.desc.f = C.cudaChannelFormatKindFloat; + resDesc.res.linear.desc.x = 32; -- bits per channel + resDesc.res.linear.sizeInBytes = N*sizeof(float); + var texDesc : C.cudaTextureDesc + C.memset(&texDesc, 0, sizeof(C.cudaTextureDesc)); + texDesc.readMode = C.cudaReadModeElementType; + + var tex : C.cudaTextureObject_t + C.cudaCreateTextureObject(&tex, &resDesc, &texDesc, nil); + + var launch = terralib.CUDAParams { 1,1,1, N,1,1, 0, nil } + R.foo(&launch,tex) + sync() + C.printf("and were done\n") +end + +doit(10) diff --git a/samples/Terra/cunion.t b/samples/Terra/cunion.t new file mode 100644 index 00000000..7077d4d0 --- /dev/null +++ b/samples/Terra/cunion.t @@ -0,0 +1,34 @@ +local C = terralib.includecstring [[ + +union testunion { + int idata; + float fdata; +}; + +typedef + union { + int a; + float b; + } S; + +void setUnion(union testunion * u, S * s){ + u->idata = 3; +} +int getUnionInt(union testunion * u){ + return u->idata; +} + +]] + +terra foo() : int + var u : C.testunion + C.setUnion(&u,nil) + var s : C.S + return C.getUnionInt(&u) +end + + +terralib.tree.printraw(C.S) + +local test = require("test") +test.eq(foo(),3) \ No newline at end of file diff --git a/samples/Terra/cunion2.t b/samples/Terra/cunion2.t new file mode 100644 index 00000000..08a4e3e5 --- /dev/null +++ b/samples/Terra/cunion2.t @@ -0,0 +1,35 @@ +local C = terralib.includecstring [[ + +union testunion { + int idata; + float fdata; +}; + +void setUnionI(union testunion * u){ + u->idata = 3; +} +void setUnionF(union testunion * u){ + u->fdata = 4.f; +} + +]] + +terra foo() : int + var u : C.testunion + C.setUnionI(&u) + var f = u.idata + C.setUnionF(&u) + var f2 = u.fdata + return f + f2 +end + +terra foo2() + var a : C.testunion + a.fdata = -3.0 + a.idata = a.idata and not (1 << 31) + return a.fdata +end + +local test = require("test") +test.eq(foo(),7) +test.eq(foo2(),3) \ No newline at end of file diff --git a/samples/Terra/customline.t b/samples/Terra/customline.t new file mode 100644 index 00000000..c70ecff6 --- /dev/null +++ b/samples/Terra/customline.t @@ -0,0 +1,10 @@ +terra foo(c : int, b : int) + terralib.debuginfo("../src/terralib.lua",300) + var a = b + c + terralib.debuginfo("../src/terralib.lua",301) + return a + a +end + +foo(3,4) + +--terralib.dumpmodule() \ No newline at end of file diff --git a/samples/Terra/customtable.t b/samples/Terra/customtable.t new file mode 100644 index 00000000..18649347 --- /dev/null +++ b/samples/Terra/customtable.t @@ -0,0 +1,10 @@ + + +struct A { +} + +A.metamethods.__luametatable = { __index = function(self,idx) return 4 end } +A.methods.foo = function() return 5 end +a = terralib.new(A) + +assert(a.foo == 4) \ No newline at end of file diff --git a/samples/Terra/cvar.t b/samples/Terra/cvar.t new file mode 100644 index 00000000..aaa3398f --- /dev/null +++ b/samples/Terra/cvar.t @@ -0,0 +1,9 @@ +C = terralib.includecstring [[ + int foo = 4; + const int foo2 = 5; +]] +terra t() + C.foo = C.foo + 1; + return C.foo + C.foo2 +end +assert(t() == 10) \ No newline at end of file diff --git a/samples/Terra/debugcallback.t b/samples/Terra/debugcallback.t new file mode 100644 index 00000000..2e236650 --- /dev/null +++ b/samples/Terra/debugcallback.t @@ -0,0 +1,23 @@ + +terralib.settypeerrordebugcallback(function(o) + o:printpretty() +end) + + +local function dosomecomplicatedstuff(a) + return `@a +end + +local s,e = pcall(function() + + local terra what(a : &opaque) + var b = [dosomecomplicatedstuff(a)] + return b + end + what:compile() + +end) + +assert(not s) +print(e) +assert(e:match("Errors reported during")) \ No newline at end of file diff --git a/samples/Terra/declerrors.t b/samples/Terra/declerrors.t new file mode 100644 index 00000000..58d24477 --- /dev/null +++ b/samples/Terra/declerrors.t @@ -0,0 +1,29 @@ +function failit(fn,err) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,err or "Errors reported during") then + error("failed wrong: "..msg,2) + end +end + + +failit(function() +local A = {} +terra A:foo() end +end,"expected a struct") + + +failit(function() +terra A.b() end +end,"failed attempting to index field") + +failit(function() +terra A.b.c() end +end,"failed attempting to index field") + + +failit(function() +local A = 4 +struct A.b {} +end,"failed attempting to index field") \ No newline at end of file diff --git a/samples/Terra/decltwice.t b/samples/Terra/decltwice.t new file mode 100644 index 00000000..b56eab60 --- /dev/null +++ b/samples/Terra/decltwice.t @@ -0,0 +1,18 @@ + +terra mything + +terra mybar() + return mything(4) +end + +terra mything(a : int) + return a +end + +terra mything + +terra mybar2() + return mything(4) +end + +assert(mybar() == mybar2()) \ No newline at end of file diff --git a/samples/Terra/def1.t b/samples/Terra/def1.t new file mode 100755 index 00000000..2ce3ba06 --- /dev/null +++ b/samples/Terra/def1.t @@ -0,0 +1,16 @@ +import "lib/def" + +local c = 3 +local a = 4 +local b = def(a) a + c +local d = b(10) + +def e(a) a + c +local def f(a) a + c + +print(d, e(10), f(10)) + +local test = require("test") +test.eq(d,13) +test.eq(e(10),13) +test.eq(f(10),13) diff --git a/samples/Terra/defaultoperator.t b/samples/Terra/defaultoperator.t new file mode 100644 index 00000000..fe1f6c79 --- /dev/null +++ b/samples/Terra/defaultoperator.t @@ -0,0 +1,9 @@ + +local a = "+" +local b = "__sub" +local ops = {4,5} +terra foobar() + return operator(b,operator(a,3,operator("+",ops))) +end + +assert(foobar() == -12) diff --git a/samples/Terra/defer.t b/samples/Terra/defer.t new file mode 100644 index 00000000..4d8c7a84 --- /dev/null +++ b/samples/Terra/defer.t @@ -0,0 +1,111 @@ +C = terralib.includec("stdio.h") + + +cur = global(int) + +terra d(a : int) + if cur ~= a then + C.printf("found %d while expecting %d\n",a,cur) + error(cur) + end + cur = cur + 1 + --C.printf("d%d\n",a) +end +d:setinlined(false) + +terra side(a : int, b : {}) + d(a) +end + +terra doit() + d(0) + defer side(3,d(1)) + d(2) +end +cur:set(0) +doit() + + +terra doit2() + d(0) + var i = 100 + defer d(203) + repeat + defer d(2*(100-i)+2) + d(2*(100-i)+1) + i = i - 1 + until i == 0 + defer d(202) + do defer d(201) end +end +cur:set(0) +doit2() + +terra doit3() + d(0) + defer d(11) + for i = 0,10 do + defer d(i+1) + end +end + +cur:set(0) +doit3() + +terra doit4() + d(0) + defer d(5) + if true then + defer d(2) + d(1) + end + if false then + else + defer d(3) + end + d(4) +end +cur:set(0) +doit4() + +struct A { +} + +A.methods.stackalloc = macro(function() + return quote + var a : A + defer a:free() + in + &a + end +end) +terra A:free() + d(1) + --C.printf("freeing A (%p)\n",self) +end +terra A:print() + d(0) + --C.printf("printing A (%p)\n",self) +end +terra doit5() + var a = A.stackalloc() + a:print() +end +cur:set(0) +doit5() + +terra doit6(a : int) + defer d(2) + d(0) + if a == 0 then + defer d(1) + return 4 + end + d(1) + return 3 +end +cur:set(0) +doit6(0) + +cur:set(0) +doit6(1) diff --git a/samples/Terra/deferbreak.t b/samples/Terra/deferbreak.t new file mode 100644 index 00000000..eeac802d --- /dev/null +++ b/samples/Terra/deferbreak.t @@ -0,0 +1,35 @@ +C = terralib.includec("stdio.h") + +cur = global(int) + +terra d(a : int) + if cur ~= a then + C.printf("found %d while expecting %d\n",a,cur) + error(cur) + end + cur = cur + 1 + C.printf("d%d\n",a) +end +d:setinlined(false) + +terra side(a : int, b : {}) + d(a) +end + +terra doit() + d(0) + for i = 0,10 do + defer d(2*i+2) + if true then + defer d(2*i + 1) + if i == 8 then + break + end + end + end + d(19) +end +cur:set(0) +doit() +doit:printpretty() + diff --git a/samples/Terra/defercond.t b/samples/Terra/defercond.t new file mode 100644 index 00000000..8de1901c --- /dev/null +++ b/samples/Terra/defercond.t @@ -0,0 +1,82 @@ +c = global(int,0) + +terra up() + c = c + 1 +end + +terra foo() + var a = 0 + while([quote do defer up() end in a < 10 end]) do + a = a + 1 + end +end + +foo() +assert(c:get() == 11) + +terra foo2() + var a = 0 + while(a < 10 and [quote do defer up() end in true end]) do + a = a + 1 + end +end + +foo2() +assert(c:get() == 21) + +terra foo3() + var a = 0 + while true do + var r = a < 10 and [quote do defer up() end in true end] + if not r then break end + a = a + 1 + end +end +foo3() +assert(c:get() == 31) + +function failit(match,fn) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,match) then + error("failed wrong: "..msg,2) + end +end +local df = "defer statements are not allowed in conditional expressions" +failit(df,function() + local terra foo() + if [quote defer up() in true end] then end + end + foo() +end) +failit(df,function() + local terra foo() + while [quote defer up() in true end] do end + end + foo() +end) +failit(df,function() + local terra foo() + repeat until [quote defer up() in true end] do end + end + foo() +end) +failit(df,function() + local terra foo() + var a = true or [quote defer up() in true end] + end + foo() +end) + +failit(df,function() + local terra foo() + var a = [quote defer up() in true end] and true + end + foo() +end) + local terra foo() + var a = [quote defer up() in 1 end] and 2 + end + foo() + assert(c:get() == 32) \ No newline at end of file diff --git a/samples/Terra/defergoto.t b/samples/Terra/defergoto.t new file mode 100644 index 00000000..5f73670f --- /dev/null +++ b/samples/Terra/defergoto.t @@ -0,0 +1,35 @@ +C = terralib.includec("stdio.h") +cur = global(int) + +terra d(a : int) + if cur ~= a then + C.printf("found %d while expecting %d\n",a,cur) + error(cur) + end + cur = cur + 1 + C.printf("d%d\n",a) +end +d:setinlined(false) + +terra foo() + d(0) + defer d(14) + var a = 0 + ::begin:: + defer d(terralib.select(a == 10,13,a + 1)) + if a >= 10 then + defer d(11) + goto theend + end + a = a + 1 + goto begin + ::theend:: + defer d(12) + return a +end + +foo:printpretty() +cur:set(0) + +test = require("test") +test.eq(foo(),10) \ No newline at end of file diff --git a/samples/Terra/dgemm.t b/samples/Terra/dgemm.t new file mode 100644 index 00000000..4cf577b9 --- /dev/null +++ b/samples/Terra/dgemm.t @@ -0,0 +1,143 @@ + +local IO = terralib.includec("stdio.h") +local stdlib = terralib.includec("stdlib.h") + +local function isinteger(x) return math.floor(x) == x end + + + +local NB = 48 +terra naivel1matmul(A : &double, B : &double, C : &double, lda : int, ldb : int, ldc : int, alpha : double) + + for m = 0, NB do + for n = 0, NB do + C[m*ldc + n] = alpha * C[m*ldc + n] + for k = 0, NB do + C[m*ldc + n] = C[m*ldc + n] + A[m*lda + k] * B[k*ldb + n] + end + end + end + +end + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + +function genl1matmul(NB, NK, RM, RN, V,prefetch) + + assert(isinteger(NB / (RN*V))) + assert(isinteger(NB / RM)) + + local VP = &vector(double,V) + local terra vecload(data : &double, idx : int) + var addr = &data[idx] + return @VP(addr) + end + local terra vecstore(data : &double, idx : int, v : vector(double,V)) + var addr = &data[idx] + @VP(addr) = v + end + + local A,B,C,mm,nn, alpha = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn"),symbol("alpha") + local lda,ldb,ldc = symbol("lda"),symbol("ldb"), symbol("ldc") + local a,b,c = symmat("a",NB/V,RM), symmat("b",NB,RN), symmat("c",RM,RN) + local kk = symbol("kk") + + local loadc,storec = terralib.newlist(),terralib.newlist() + + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [c[m][n]] = alpha * vecload(C,(mm+m)*ldc + nn + n*V) + end) + storec:insert(quote + vecstore(C,(mm+m)*ldc + nn + n*V,[c[m][n]]) + end) + end + end + + local calcc = terralib.newlist() + + for kb = 0, NK/V-1 do + local kbV = kb*V + for m = 0, RM-1 do + calcc:insert(quote + var [a[kb][m]] = vecload(A,(mm+m)*lda + kk + kbV) + end) + end + for v = 0, V-1 do + local k = kbV+v + if not prefetch or (v == 0 and kb == 0) then + for n = 0, RN-1 do + calcc:insert(quote + var [b[k][n]] = vecload(B,(kk + k)*ldb + nn + n*V) + end) + end + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[kb][m]][v] * [b[k][n]] + end) + if prefetch and not (v == V-1 and kb == NK/V-1) and m == RM-1 then --prefetch the next b + calcc:insert(quote + var [b[k+1][n]] = vecload(B,(kk + k + 1)*ldb + nn + n*V) + end) + end + end + end + end + end + + return terra([A] : &double, [B] : &double, [C] : &double, [lda] : int, [ldb] : int, [ldc] : int, [alpha] : double) + for [mm] = 0, NB, RM do + for [nn] = 0, NB,RN*V do + [loadc]; + for [kk] = 0, NB, NK do + [calcc]; + end + [storec]; + end + end + end +end + + + +local NB2 = 8 * NB +local l1matmul = genl1matmul(NB,4, 3, 2, 4) + + + +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end + +terra my_dgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : double, A : &double, lda : int, B : &double, ldb : int, + beta : double, C : &double, ldc : int) + for mm = 0,M,NB2 do + for nn = 0,N,NB2 do + for kk = 0,K, NB2 do + for m = mm,min(mm+NB2,M),NB do + for n = nn,min(nn+NB2,N),NB do + for k = kk,min(kk+NB2,K),NB do + l1matmul(A + m*lda + k, + B + k*ldb + n, + C + m*ldc + n, + lda,ldb,ldc, terralib.select(k == 0,0,1)) + end + end + end + end + end + end +end + +terralib.saveobj("my_dgemm.o", {my_dgemm = my_dgemm}) \ No newline at end of file diff --git a/samples/Terra/dgemm2.t b/samples/Terra/dgemm2.t new file mode 100644 index 00000000..55ab3a92 --- /dev/null +++ b/samples/Terra/dgemm2.t @@ -0,0 +1,163 @@ + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + +function genkernel(NB, RM, RN, V,alpha) + + local terra vecload(data : &double, idx : int) + var addr = &data[idx] + return @addr:as(&vector(double,V)) + end + local terra vecstore(data : &double, idx : int, v : vector(double,V)) + var addr = &data[idx] + @addr:as(&vector(double,V)) = v + end + + local A,B,C,mm,nn = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn") + local lda,ldb,ldc = NB,NB,NB + local a,b,c,caddr = symmat("a",RM), symmat("b",RN), symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(double,V) + local VP = &VT + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + (mm+m)*ldc + nn + n*V + var [c[m][n]] = alpha * @VP([caddr[m][n]]) + end) + storec:insert(quote + @VP([caddr[m][n]]) = [c[m][n]] + end) + end + end + + local calcc = terralib.newlist() + + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = @VP(&B[k*ldb + nn + n*V]) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[(mm+m)*lda + k]) + end) + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end + end + + + return terra([A] : &double, [B] : &double, [C] : &double) + for [mm] = 0, NB, RM do + for [nn] = 0, NB,RN*V do + [loadc]; + for [k] = 0, NB do + [calcc]; + end + [storec]; + end + end + end +end + +local NB = 48 +local NB2 = 8 * NB + +local V = 4 + +l1dgemm0 = genkernel(NB,2,4,V,0) +l1dgemm1 = genkernel(NB,2,4,V,1) + +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end + +local stdlib = terralib.includec("stdlib.h") +local IO = terralib.includec("stdio.h") +local VP = &vector(double,V) + +terra my_dgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : double, A : &double, lda : int, B : &double, ldb : int, + beta : double, C : &double, ldc : int) + + var AA = [&double](stdlib.malloc(sizeof(double)*M*K)) + var BB = [&double](stdlib.malloc(sizeof(double)*K*N)) + var CC = [&double](stdlib.malloc(sizeof(double)*M*N)) + + var i = 0 + for mm = 0,M,NB do + for kk = 0,K,NB do + for m = mm,mm+NB do + for k = kk,kk+NB,V do + @VP(&AA[i]) = @VP(&A[m*lda + k]) + i = i + V + end + end + end + end + i = 0 + for kk = 0,K,NB do + for nn = 0,N,NB do + for k = kk,kk+NB do + for n = nn,nn+NB,V do + @VP(&BB[i]) = @VP(&B[k*ldb + n]) + i = i + V + end + end + end + end + + for mm = 0,M,NB2 do + for nn = 0,N,NB2 do + for kk = 0,K, NB2 do + for m = mm,min(mm+NB2,M),NB do + for n = nn,min(nn+NB2,N),NB do + for k = kk,min(kk+NB2,K),NB do + --IO.printf("%d %d starting at %d\n",m,k,m*lda + NB*k) + if k == 0 then + l1dgemm0(AA + (m*lda + NB*k), + BB + (k*ldb + NB*n), + CC + (m*ldc + NB*n)) + else + l1dgemm1(AA + (m*lda + NB*k), + BB + (k*ldb + NB*n), + CC + (m*ldc + NB*n)) + end + end + end + end + end + end + end + + i = 0 + for mm = 0,M,NB do + for nn = 0,N,NB do + for m = mm,mm+NB do + for n = nn,nn+NB,V do + @VP(&C[m*ldc + n]) = @VP(&CC[i]) + i = i + V + end + end + end + end + + stdlib.free(AA) + stdlib.free(BB) + stdlib.free(CC) +end + +terralib.saveobj("my_dgemm.o", { my_dgemm = my_dgemm }) \ No newline at end of file diff --git a/samples/Terra/dgemm3.t b/samples/Terra/dgemm3.t new file mode 100644 index 00000000..29319e84 --- /dev/null +++ b/samples/Terra/dgemm3.t @@ -0,0 +1,213 @@ + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + +local function isinteger(x) return math.floor(x) == x end + +llvmprefetch = terralib.intrinsic("llvm.prefetch",{&opaque,int,int,int} -> {}) + +local function alignedload(addr) + return `terralib.attrload(addr, { align = 8 }) +end +local function alignedstore(addr,v) + return `terralib.attrstore(addr,v, { align = 8 }) +end +alignedload,alignedstore = macro(alignedload),macro(alignedstore) +function genkernel(NB, RM, RN, V,alpha,boundary) + + local M,N,K, boundaryargs + if boundary then + M,N,K = symbol(int64,"M"),symbol(int64,"N"),symbol(int64,"K") + boundaryargs = terralib.newlist({M,N,K}) + else + boundaryargs = terralib.newlist() + M,N,K = NB,NB,NB + end + + local A,B,C,mm,nn,ld = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn"),symbol("ld") + local lda,ldb,ldc = symbol("lda"),symbol("ldb"),symbol("ldc") + local a,b,c,caddr = symmat("a",RM), symmat("b",RN), symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(double,V) + local VP = &VT + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + m*ldc + n*V + var [c[m][n]] = alpha * alignedload(VP([caddr[m][n]])) + end) + storec:insert(quote + alignedstore(VP([caddr[m][n]]),[c[m][n]]) + end) + end + end + + local calcc = terralib.newlist() + + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = alignedload(VP(&B[n*V])) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[m*lda]) + end) + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end + end + + + local result = terra([A] : &double, [B] : &double, [C] : &double, [lda] : int64,[ldb] : int64,[ldc] : int64,[boundaryargs]) + for [mm] = 0, M, RM do + for [nn] = 0, N,RN*V do + [loadc]; + for [k] = 0, K do + llvmprefetch(B + 4*ldb,0,3,1); + [calcc]; + B = B + ldb + A = A + 1 + end + [storec]; + A = A - K + B = B - ldb*K + RN*V + C = C + RN*V + end + C = C + RM * ldb - N + B = B - N + A = A + lda*RM + end + end + return result +end + + +local stdlib = terralib.includec("stdlib.h") +local IO = terralib.includec("stdio.h") + + +function generatedgemm(NB,NBF,RM,RN,V) + + if not isinteger(NB/(RN*V)) then + return false + end + if not isinteger(NB/RM) then + return false + end + + local NB2 = NBF * NB + local l1dgemm0 = genkernel(NB,RM,RN,V,0,false) + local l1dgemm1 = genkernel(NB,RM,RN,V,1,false) + + + local l1dgemm0b = genkernel(NB,1,1,1,0,true) + local l1dgemm1b = genkernel(NB,1,1,1,1,true) + + local terra min(a : int, b : int) + return terralib.select(a < b, a, b) + end + + return terra(gettime : {} -> double, M : int, N : int, K : int, alpha : double, A : &double, lda : int, B : &double, ldb : int, + beta : double, C : &double, ldc : int) + for mm = 0,M,NB2 do + for nn = 0,N,NB2 do + for kk = 0,K, NB2 do + for m = mm,min(mm+NB2,M),NB do + for n = nn,min(nn+NB2,N),NB do + for k = kk,min(kk+NB2,K),NB do + --IO.printf("%d %d starting at %d\n",m,k,m*lda + NB*k) + var MM,NN,KK = min(M-m,NB),min(N-n,NB),min(K-k,NB) + var isboundary = MM < NB or NN < NB or KK < NB + var AA,BB,CC = A + (m*lda + k),B + (k*ldb + n),C + (m*ldc + n) + if k == 0 then + if isboundary then + --IO.printf("b0 %d %d %d\n",MM,NN,KK) + l1dgemm0b(AA,BB,CC,lda,ldb,ldc,MM,NN,KK) + + --IO.printf("be %d %d %d\n",MM,NN,KK) + else + l1dgemm0(AA,BB,CC,lda,ldb,ldc) + end + else + if isboundary then + + --IO.printf("b %d %d %d\n",MM,NN,KK) + l1dgemm1b(AA,BB,CC,lda,ldb,ldc,MM,NN,KK) + + --IO.printf("be %d %d %d\n",MM,NN,KK) + else + l1dgemm1(AA,BB,CC,lda,ldb,ldc) + end + end + end + end + end + end + end + end + end +end + +-- + +local blocksizes = {16,24,32,40,48,56,64} +local regblocks = {1,2,4} +local vectors = {1,2,4,8} + +--local best = { gflops = 0, b = 56, rm = 4, rn = 1, v = 8 } +local best = { gflops = 0, b = 40, rm = 4, rn = 2, v = 4 } + + +if false then + local tunefor = 1024 + local harness = require("lib/matrixtestharness") + for _,b in ipairs(blocksizes) do + for _,rm in ipairs(regblocks) do + for _,rn in ipairs(regblocks) do + for _,v in ipairs(vectors) do + local my_dgemm = generatedgemm(b,5,rm,rn,v) + if my_dgemm then + print(b,rm,rn,v) + my_dgemm:compile() + local i = math.floor(tunefor / b) * b + local avg = 0 + local s, times = harness.timefunctions("double",i,i,i,function(M,K,N,A,B,C) + my_dgemm(nil,M,N,K,1.0,A,K,B,N,0.0,C,N) + end) + if not s then + print("") + break + end + print(i,unpack(times)) + local avg = times[1] + if best.gflops < avg then + best = { gflops = avg, b = b, rm = rm, rn = rn, v = v } + terralib.tree.printraw(best) + end + end + end + end + end + end +end + +terralib.tree.printraw(best) + +local my_dgemm = generatedgemm(best.b, 5, best.rm, best.rn, best.v) + +--my_dgemm:disas() +terralib.saveobj("my_dgemm.o", { my_dgemm = my_dgemm }) \ No newline at end of file diff --git a/samples/Terra/dgemmpaper.t b/samples/Terra/dgemmpaper.t new file mode 100644 index 00000000..e415236c --- /dev/null +++ b/samples/Terra/dgemmpaper.t @@ -0,0 +1,75 @@ + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end +prefetch = terralib.intrinsic("llvm.prefetch",{&opaque,int,int,int} -> {}) + + +function genkernel(NB, RM, RN, V,alpha) + local A,B,C = symbol("A"),symbol("B"),symbol("C") + local mm,nn = symbol("mn"),symbol("nn") + local lda,ldb,ldc = symbol("lda"),symbol("ldb"),symbol("ldc") + local a,b = symmat("a",RM), symmat("b",RN) + local c,caddr = symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(double,V) + local VP = &VT + for m = 0, RM-1 do for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + m*ldc + n*V + var [c[m][n]] = + alpha * @VP([caddr[m][n]]) + end) + storec:insert(quote + @VP([caddr[m][n]]) = [c[m][n]] + end) + end end + local calcc = terralib.newlist() + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = @VP(&B[n*V]) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[m*lda]) + end) + end + for m = 0, RM-1 do for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end end + return terra([A] : &double, [B] : &double, [C] : &double, + [lda] : int64,[ldb] : int64,[ldc] : int64) + for [mm] = 0, NB, RM do + for [nn] = 0, NB, RN*V do + [loadc]; + for [k] = 0, NB do + prefetch(B + 4*ldb,0,3,1); + [calcc]; + B,A = B + ldb,A + 1 + end + [storec]; + A,B,C = A - NB,B - ldb*NB + RN*V,C + RN*V + end + A,B,C = A + lda*RM, B - NB, C + RM * ldb - NB + end + end +end + +local a = genkernel(40,4,2,8,1) +a:compile() +a:printpretty() + +terra short_saxpy(a : float, + x : vector(float,4), y : vector(float,4)) + return a*x + y +end +short_saxpy:printpretty() \ No newline at end of file diff --git a/samples/Terra/diffuse.t b/samples/Terra/diffuse.t new file mode 100644 index 00000000..ddf33dbe --- /dev/null +++ b/samples/Terra/diffuse.t @@ -0,0 +1,237 @@ + +local C = terralib.includecstring [[ +#include +#include +#ifndef _WIN32 +#include +static double CurrentTimeInSeconds() { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec + tv.tv_usec / 1000000.0; +} +#else +#include +static double CurrentTimeInSeconds() { + return time(NULL); +} +#endif + +int CalcTime(int * times, double * start) { + if(*times == 0) { + *start = CurrentTimeInSeconds(); + } else { + double elapsed = CurrentTimeInSeconds() - *start; + if(elapsed > 0.1f && *times >= 1) { + *start = elapsed / *times; + return 0; + } + } + (*times)++; + return 1; +} +]] + +alignedloads = global(0) +loads = global(0) +NI,NJ = 1,1 +V = 4 +local VP = &vector(float,V) +terra uload(d : &float) + --if d:as(uint64) % 16 == 0 then + -- alignedloads = alignedloads + 1 + --end + --loads = loads + 1 + return terralib.attrload(VP(d),{ align = 4 }) +end +terra ustore(d : &float, v : vector(float,V)) + terralib.attrstore(VP(d),v, { align = 4 }) +end + +terra diffuse_reference(output : &float, N : int, M : int, stride : int, x : &float, x0 : &float, a : float) + var invD = 1.f / (1 + 4.f*a) + var nextline = stride - N + for i = 0, M do + for j = 0,N do + var r = + (x0[0] + + (x[ - stride] + + x[stride] + + x[ - 1] + + x[1] )*a) * invD + output[0] = r + x0 = x0 + 1 + x = x + 1 + output = output + 1 + end + x0 = x0 + nextline --- stride + x = x + nextline --- stride + output = output + nextline --- stride + end + +end + +terra diffuse(output : &float, N : int, M : int, stride : int, x : &float, x0 : &float, a : float) + var invD = 1.f / (1 + 4.f*a) + var nextline = stride - N + for i = 0, M do + + for j = 0,N,V do + var r = + (uload(x0) + + (uload(x - stride) + + uload(x + stride) + + uload(x - 1) + + uload(x + 1) )*a) * invD + --C.printf("%d %d\n",i,j) + --llvmprefetch(x + 4 * stride,0,3,1) + --llvmprefetch(x0 + 3 * stride,0,3,1) + + ustore(output,r) + x0 = x0 + V + x = x + V + output = output + V + end + + x0 = x0 + nextline --- stride + x = x + nextline --- stride + output = output + nextline --- stride + end + +end + +llvmprefetch = terralib.intrinsic("llvm.prefetch",{&uint8,int,int,int} -> {}) + +terra diffuse2(output : &float, N : int, M : int, stride : int, x : &float, x0 : &float, a : float,xi : &float) + var invD = 1.f / (1 + 4.f*a) + var nextline = stride - N + var xi0,xi1,xi2 = xi - stride, xi, xi + stride + var x02 = x0 + for i = 0, M + 1 do + + if i < M then + for j = 0,N,V do + var r = + (uload(x0) + + (uload(x - stride) + + uload(x + stride) + + uload(x - 1) + + uload(x + 1) )*a) * invD + --C.printf("%d %d\n",i,j) + --llvmprefetch(x + 2 * stride,0,3,1) + ustore(xi,r) + x0 = x0 + V + x = x + V + xi = xi + V + end + + x0 = x0 + nextline --- stride + x = x + nextline --- stride + xi = xi + nextline --- stride + if i % 3 == 1 then + xi = xi - stride*3 + end + end + + if i >= 1 then + for j = 0,N,V do + var r = + (uload(x02) + + (uload(xi0) + + uload(xi2) + + uload(xi1 - 1) + + uload(xi1 + 1) )*a) * invD + --C.printf("%d %d\n",i,j) + ustore(output,r) + x02 = x02 + V + xi0 = xi0 + V + xi1 = xi1 + V + xi2 = xi2 + V + output = output + V + end + + x02 = x02 + nextline --- stride + xi0,xi1,xi2 = xi1 - N,xi2 - N, xi0 - N + output = output + nextline --- stride + end + end + +end + + +terra doit(thea : float) + var enlarge = 1 + var N = 1024/8 + var M = 1024*8 + var N1 = (N + 2*enlarge) + var M1 = (M + 2*enlarge) + + var NA = N1 + var x = [&float](C.malloc(NA*M1*sizeof(float))) + var x0 = [&float](C.malloc(NA*M1*sizeof(float))) + + var ro1 = [&float](C.malloc(NA*M1*sizeof(float))) + var ro2 = [&float](C.malloc(NA*M1*sizeof(float))) + + var to1 = [&float](C.malloc(NA*M1*sizeof(float))) + var to2 = [&float](C.malloc(NA*M1*sizeof(float))) + + + for i = 0, N1 do + for j = 0, N1 do + x[i*N1+j] = C.rand() % 10 + x0[i*N1+j] = C.rand() % 10 + ro1[i*N1+j] = -42 + ro2[i*N1+j] = -43 + to1[i*N1+j] = -44 + to2[i*N1+j] = -45 + end + end + var stride = N1 + var start = enlarge*stride + enlarge + + diffuse_reference(ro1 + start,N,M,stride,x + start,x0 + start,thea) + diffuse_reference(ro2 + start,N,M,stride,ro1 + start,x0 + start,thea) + + + + var times = 0 + var mytime : double + + var fuse = true + while C.CalcTime(×,&mytime) ~= 0 do + if not fuse then + diffuse(to1 + start,N,M,stride,x + start,x0 + start,thea) + diffuse(to2 + start,N,M,stride,to1 + start,x0 + start,thea) + else + diffuse2(to2 + start,N,M,stride,x + start,x0 + start,thea,to1 + start) + end + end + + C.printf("times = %d\n",times) + for i = enlarge+1, M1-enlarge-1 do + for j = enlarge+1,N1-enlarge-1 do + if ro2[i*stride+j] ~= to2[i*stride+j] then + C.printf("wrong! %d %d %f %f\n",i,j,ro2[i*stride+j],to2[i*stride+j]) + goto out + else + --C.printf("right! %d %d %f %f\n",i,j,ro1[i*N1+j],to1[i*N1+j]) + end + end + end + ::out:: + var togiga = 1.0/(1024*1024*1024) + var pixels = N*M + C.printf("%f %f %f\n", mytime, 2*pixels*4*3 *togiga / mytime, 2*6*pixels * togiga / mytime) + +end + +terra getloads() + return alignedloads,loads +end + +doit(1) +diffuse:disas() +doit(1) + +local al,loads = terralib.unpackstruct(getloads()) +print(al/loads) \ No newline at end of file diff --git a/samples/Terra/dynlib.t b/samples/Terra/dynlib.t new file mode 100644 index 00000000..2d67bbab --- /dev/null +++ b/samples/Terra/dynlib.t @@ -0,0 +1,47 @@ +local ffi = require 'ffi' +-- test that the dynamic library for terra was built correctly +-- by compiling a new program that links against it and running it +terralib.includepath = terralib.terrahome.."/include/terra" +C = terralib.includecstring [[ +#include +#include "terra.h" +]] + +local libpath = terralib.terrahome.."/lib" + +terra doerror(L : &C.lua_State) + C.printf("%s\n",C.luaL_checklstring(L,-1,nil)) +end + +local thecode = "terra foo() return 1 end print(foo())" + +terra main(argc : int, argv : &rawstring) + var L = C.luaL_newstate(); + C.luaL_openlibs(L); + if C.terra_init(L) ~= 0 then + doerror(L) + end + if C.terra_loadstring(L,thecode) ~= 0 or C.lua_pcall(L, 0, -1, 0) ~= 0 then + doerror(L) + end + return 0; +end + +if ffi.os ~= "Windows" then + print(libpath) + local flags = terralib.newlist {"-L", libpath,"-Wl,-rpath,"..libpath,"-lterra_dynamic"} + if require("ffi").os == "OSX" then + flags:insertall {"-pagezero_size","10000", "-image_base", "100000000"} + end + + terralib.saveobj("dynlib",{main = main},flags) + + assert(0 == os.execute("./dynlib")) + +else + local putenv = terralib.externfunction("_putenv", rawstring -> int) + local flags = {libpath.."\\terra.lib",libpath.."\\lua51.lib"} + terralib.saveobj("dynlib.exe",{main = main},flags) + putenv("Path="..os.getenv("Path")..";"..terralib.terrahome.."\\bin") --make dll search happy + assert(0 == os.execute(".\\dynlib.exe")) +end \ No newline at end of file diff --git a/samples/Terra/eager.t b/samples/Terra/eager.t new file mode 100644 index 00000000..7218d811 --- /dev/null +++ b/samples/Terra/eager.t @@ -0,0 +1,10 @@ + + +local fn = terra() return 0 end + +for i = 1,10 do + fn = terra() return fn() + 1 end +end + +local test = require("test") +test.eq(fn(),10) \ No newline at end of file diff --git a/samples/Terra/empty.t b/samples/Terra/empty.t new file mode 100644 index 00000000..e69de29b diff --git a/samples/Terra/emptycalls.t b/samples/Terra/emptycalls.t new file mode 100644 index 00000000..2c7f8b34 --- /dev/null +++ b/samples/Terra/emptycalls.t @@ -0,0 +1,31 @@ +terra f(x : {}) + return x +end + + +print(f({})) + +terra f2(x : {}) + return x,x +end + +print(f2({})._0) + +terra f3(x : {}, a : int) + return a + 1 +end + +assert(f3({},3) == 4) + +terra f4(x : {}) + return x,4 +end + +a = f4({}) +assert(a._1 == 4) + +terra f5() + return f({}) +end + +f5() \ No newline at end of file diff --git a/samples/Terra/emptyname.t b/samples/Terra/emptyname.t new file mode 100644 index 00000000..b6f200aa --- /dev/null +++ b/samples/Terra/emptyname.t @@ -0,0 +1,5 @@ +struct A {} +A.metamethods.__typename = function() return "" end +struct B {} +B.metamethods.__typename = function() return "" end +print(A:cstring(),B:cstring()) \ No newline at end of file diff --git a/samples/Terra/emptystruct.t b/samples/Terra/emptystruct.t new file mode 100644 index 00000000..5dc75028 --- /dev/null +++ b/samples/Terra/emptystruct.t @@ -0,0 +1,30 @@ +terra f(x : {}) + return x +end + +print(f({})) + +terra f2(x : {}) + return x,x +end + +print(f2({})) + +terra f3(x : {}, a : int) + return a + 1 +end + +assert(f3({},3) == 4) + +terra f4(x : {}) + return x,4 +end + +a = f4({}) +assert(a._1 == 4) + +terra f5() + return f({}) +end + +f5() \ No newline at end of file diff --git a/samples/Terra/enumc.t b/samples/Terra/enumc.t new file mode 100644 index 00000000..575e8c13 --- /dev/null +++ b/samples/Terra/enumc.t @@ -0,0 +1,14 @@ +C = terralib.includecstring [[ + enum Foo { + A_VALUE = -1, + B_VALUE = 4, + C_VALUE = 2 << 1, + D_VALUE = (1 << 31) - 1, + E_VALUE = (1 << 44) + }; +]] +assert(C.A_VALUE == -1) +assert(C.B_VALUE == 4) +assert(C.C_VALUE == 4) +assert(C.D_VALUE == 2147483647) +assert(C.E_VALUE == -2147483648) \ No newline at end of file diff --git a/samples/Terra/evenodd.t b/samples/Terra/evenodd.t new file mode 100644 index 00000000..f226c17a --- /dev/null +++ b/samples/Terra/evenodd.t @@ -0,0 +1,20 @@ +terra iseven(a : uint) : bool + if a == 0 then + return true + else + return isodd(a - 1) + end +end and +terra isodd(a : uint) : bool + if a == 0 then + return false + else + return iseven(a - 1) + end +end + +local test = require("test") +test.eq(iseven(3),false) +test.eq(iseven(2),true) +test.eq(isodd(3),true) +test.eq(isodd(2),false) diff --git a/samples/Terra/examplecompiler1.t b/samples/Terra/examplecompiler1.t new file mode 100755 index 00000000..e98d3dcc --- /dev/null +++ b/samples/Terra/examplecompiler1.t @@ -0,0 +1,41 @@ +import "lib/examplecompiler" +foo = {} + + +local def foo2(b : eg.number) : eg.number return 2 + b end + + +assert(foo2(3) == 5) + +def foo.bar(a : eg.number) : eg.number + return 1 + -2 * 3 + 1 + a - foo2(a) +end + +assert(-6 == foo.bar(4)) + +local c = def(c : eg.number) : eg.number return 4 end + +assert(4 == c(1)) + +def sign(a : eg.number) : eg.number + if a < 0 then + return -1 + else + return 1 + end +end + +def usev(a : eg.number) : eg.number + var b = 1 + return a + b +end + +def what(a : eg.number) : eg.number + var c = usev + return c(a) +end + +assert(usev(4) == 5) +assert(sign(4) == 1) +assert(sign(-3) == -1) +assert(what(4) == 5) \ No newline at end of file diff --git a/samples/Terra/examplelanguage1.t b/samples/Terra/examplelanguage1.t new file mode 100755 index 00000000..f9f46373 --- /dev/null +++ b/samples/Terra/examplelanguage1.t @@ -0,0 +1,7 @@ +import "lib/examplelanguage" + +local a,b = 1,2 +c = 3 +local d = sum a,b,c done + +print(d) \ No newline at end of file diff --git a/samples/Terra/exampleparser1.t b/samples/Terra/exampleparser1.t new file mode 100644 index 00000000..98882fbb --- /dev/null +++ b/samples/Terra/exampleparser1.t @@ -0,0 +1,22 @@ +import "lib.exampleparser" + +P exp -1 + 1 * (2 - 3) +P exp not true or false and 3 +P exp a.b.c[d]()(a,b,c) +P exp (a + 3)(4) +P exp {a,3,4, d = 4, [1+3] = 3} +P exp [ 3 + 4 ] +P statement var a,b,c:int = 3 +P statement do + if a then b else c end + if a then b end + if a then b elseif c then d end + while 3 + 3 do end + repeat + until a + b + for a,b,c:int = 1,3 do end + for a,b,c:int in 1,3 do end + a = b + a + a,c = d,e +end \ No newline at end of file diff --git a/samples/Terra/exittest.t b/samples/Terra/exittest.t new file mode 100644 index 00000000..0f24e5ce --- /dev/null +++ b/samples/Terra/exittest.t @@ -0,0 +1,9 @@ + +C = { exit = terralib.externfunction("exit", int -> {}) } + + +terra moo() + C.exit(0) +end + +moo() \ No newline at end of file diff --git a/samples/Terra/explicitcast.t b/samples/Terra/explicitcast.t new file mode 100644 index 00000000..15d7aab2 --- /dev/null +++ b/samples/Terra/explicitcast.t @@ -0,0 +1,11 @@ + +terra foo() + var a : int64 = 256+3 + var c = intptr(&a) + var d = [&int64](c) + var fi = int(true) + var what = @[&int8](&a) + return @d,fi,what +end +local test = require("test") +test.meq({256+3,1,3},foo()) \ No newline at end of file diff --git a/samples/Terra/exportdynamic.t b/samples/Terra/exportdynamic.t new file mode 100644 index 00000000..3bbd63b3 --- /dev/null +++ b/samples/Terra/exportdynamic.t @@ -0,0 +1,9 @@ +terralib.includepath = terralib.terrahome.."/include/terra" +C = terralib.includecstring [[ + #include "lua.h" + #include "lauxlib.h" +]] + +local s = C.luaL_newstate() +assert(C.lua_gettop(s) == 0) + diff --git a/samples/Terra/expvec.t b/samples/Terra/expvec.t new file mode 100644 index 00000000..94154777 --- /dev/null +++ b/samples/Terra/expvec.t @@ -0,0 +1,34 @@ + +if require("ffi").os == "Windows" then + print("Not consistent on windows") + return +end +local C = terralib.includecstring[[ + #include + void dostuff(__m128 what) {} + void dostuff2() { __m128 a; dostuff(a); } +]] + +local V = 1 +local exp = terralib.intrinsic(("llvm.exp.v%df32"):format(V),vector(float,V) -> vector(float,V)) + +terra foo(N : int) + + var d = vector(0.f,1.f,3.f,4.f) + + C.dostuff(d) + + --var v = vector(0.f,1.f,2.f,3.f,4.f,5.f,6.f,7.f) + var v = vector(0.f) + for i = 0, N do + v = exp(v) + end + return v[0],v[1],v[2],v[3] +end + +C.dostuff:printpretty() + +local begin = terralib.currenttimeinseconds() +print(foo(100000000)) +local endd = terralib.currenttimeinseconds() +print(endd - begin) \ No newline at end of file diff --git a/samples/Terra/f2.t b/samples/Terra/f2.t new file mode 100644 index 00000000..a7507f08 --- /dev/null +++ b/samples/Terra/f2.t @@ -0,0 +1,17 @@ +local C = terralib.includecstring [[ + typedef struct { + int a; + int b; + } Foo; +]] +for i,e in ipairs(C.Foo.entries) do + print(e.field,e.type) +end + +terra useFoo() + var f : C.Foo + f.a = 1 + f.b = 2 +end + +useFoo() \ No newline at end of file diff --git a/samples/Terra/fact.t b/samples/Terra/fact.t new file mode 100644 index 00000000..284bb512 --- /dev/null +++ b/samples/Terra/fact.t @@ -0,0 +1,16 @@ +local test = require("test") + +local Num = int +local terra fact(a : Num) : Num + var c,i = 1,1 + while i <= a do + c = c * i + i = i + 1 + end + return c +end + + +test.eq(fact(1),1) +test.eq(fact(2),2) +test.eq(fact(3),6) \ No newline at end of file diff --git a/samples/Terra/fakeasm.t b/samples/Terra/fakeasm.t new file mode 100644 index 00000000..09998825 --- /dev/null +++ b/samples/Terra/fakeasm.t @@ -0,0 +1,31 @@ + + +local op = setmetatable({},{ __index = function(self,idx) + return idx +end }) +C = terralib.includec("stdio.h") +local function emit(buf,...) + local str = "" + local operands = {} + for i = 1,select("#",...) do + local v = select(i,...) + local cv,e = v:asvalue() + str = str .. (e and "%d" or tostring(cv)) .. " " + if e then table.insert(operands,v) end + end + str = str.."\n" + return `C.sprintf(buf,str,operands) +end +emit = macro(emit) + +local c = "as28" +terra what(buf : rawstring) + var b = 3 + emit(buf,1,3,4,3+3,op.what,b + 3) +end + +local buf = terralib.new(int8[128]) +what(buf) +local ffi = require("ffi") +local s = ffi.string(buf) +assert("1 3 4 6 what 6 \n" == s) diff --git a/samples/Terra/falsespec.t b/samples/Terra/falsespec.t new file mode 100644 index 00000000..970a9695 --- /dev/null +++ b/samples/Terra/falsespec.t @@ -0,0 +1,8 @@ + + +terra derp() + var a = [false] + return a +end + +assert(derp() == false) \ No newline at end of file diff --git a/samples/Terra/fastcall.t b/samples/Terra/fastcall.t new file mode 100644 index 00000000..1cef49bb --- /dev/null +++ b/samples/Terra/fastcall.t @@ -0,0 +1,24 @@ + +terra foo() + return 1 +end + + +assert(1 == foo()) +assert(rawget(foo,"fastcall") == foo:getdefinitions()[1].ffiwrapper) +assert(1 == foo()) + + +terra foo2() + return 1,2 +end +local a,b = terralib.unpackstruct(foo2()) +assert(a == 1 and b == 2) +assert(rawget(foo2,"fastcall") == foo2:getpointer()) +local a,b = unpackstruct(foo2()) +assert(a == 1 and b == 2) + +terra foo(a : int) +end + +assert(rawget(foo,"fastcall") == nil) \ No newline at end of file diff --git a/samples/Terra/fib.t b/samples/Terra/fib.t new file mode 100644 index 00000000..c2fb5fe2 --- /dev/null +++ b/samples/Terra/fib.t @@ -0,0 +1,26 @@ +local test = require("test") + +local Num = int + +local base = {} +terra base.fib(a : Num) : Num + var i,c,p = 0,1,1 + while i < a do + c,p = c + p,c + i = i + 1 + end + return c +end + +function fib2(a) + local i,c,p = 0,1,1 + while i < a do + c,p = c + p,c + i = i + 1 + end + return c +end +for i = 0,10 do + print(base.fib(i)) + test.eq(base.fib(i),fib2(i)) +end \ No newline at end of file diff --git a/samples/Terra/fib2.t b/samples/Terra/fib2.t new file mode 100644 index 00000000..29d8127e --- /dev/null +++ b/samples/Terra/fib2.t @@ -0,0 +1,27 @@ +local test = require("test") + +local Num = int + +terra fib(a : Num) : Num + if a == 0 then + return 1 + elseif a == 1 then + return 1 + else + return fib(a - 1) + fib(a - 2) + end +end + +function fib2(a) + if a == 0 then + return 1 + elseif a == 1 then + return 1 + else + return fib2(a - 1) + fib2(a - 2) + end +end +for i = 0,10 do + print(fib(i)) + test.eq(fib(i),fib2(i)) +end diff --git a/samples/Terra/fnames.t b/samples/Terra/fnames.t new file mode 100644 index 00000000..befb5e4d --- /dev/null +++ b/samples/Terra/fnames.t @@ -0,0 +1,5 @@ +foo = terra(a : int) end +foo:setname("bar") + +assert(foo:getname() == "bar") +foo:disas() \ No newline at end of file diff --git a/samples/Terra/fncalltest.t b/samples/Terra/fncalltest.t new file mode 100644 index 00000000..dcf18907 --- /dev/null +++ b/samples/Terra/fncalltest.t @@ -0,0 +1,16 @@ + + + +terra foo() + +end + + +terra bar() + foo() + var what = foo + --what() +end + +bar:compile() + diff --git a/samples/Terra/fnpointer.t b/samples/Terra/fnpointer.t new file mode 100644 index 00000000..042af355 --- /dev/null +++ b/samples/Terra/fnpointer.t @@ -0,0 +1,24 @@ + +terra foo() + return 4LL +end + +terra bar() + return 5LL +end + +terra bar3(fn: {} -> int64) + return fn() +end + +terra baz(a : int64) + var afn = foo + if a > 2 then + afn = bar + end + return bar3(afn) +end + +local test = require("test") +test.eq(baz(1),4) +test.eq(baz(3),5) \ No newline at end of file diff --git a/samples/Terra/fnptr.t b/samples/Terra/fnptr.t new file mode 100644 index 00000000..baaad41a --- /dev/null +++ b/samples/Terra/fnptr.t @@ -0,0 +1,7 @@ +terra baz() return 1 end +bp = baz:getpointer() +terra foo() + return bp() +end + +assert(foo() == 1) \ No newline at end of file diff --git a/samples/Terra/fnptrc.t b/samples/Terra/fnptrc.t new file mode 100644 index 00000000..935c3341 --- /dev/null +++ b/samples/Terra/fnptrc.t @@ -0,0 +1,25 @@ + +C = terralib.includecstring [[ + struct Foo { + int (*bar)(int); + }; + int runptr(int (*bar)(int), int a) { + return bar(a); + } +]] + +terra add1(a : int) return a + 1 end + +terra what0() + return C.runptr(add1,1) +end + +assert(what0() == 2) + +terra what() + var c : C.Foo + c.bar = add1 + return c.bar(1) +end + +assert(what() == 2) \ No newline at end of file diff --git a/samples/Terra/foo.t b/samples/Terra/foo.t new file mode 100644 index 00000000..0aa5252f --- /dev/null +++ b/samples/Terra/foo.t @@ -0,0 +1 @@ +a = global( struct { real : float, imag : float }) \ No newline at end of file diff --git a/samples/Terra/for.t b/samples/Terra/for.t new file mode 100644 index 00000000..41e4d893 --- /dev/null +++ b/samples/Terra/for.t @@ -0,0 +1,31 @@ + +terra foo(a : int, s : int) + var r = 0 + for i = 0,a,s do + r = r + i + end + return r +end + +terra foo2(a : int) + var r = 0 + for i = 0,a do + r = r + i + end + return r +end + +terra foo3(a : int) + var r = 0 + for i = a,0,-1 do + r = r + i + end + return r +end + +local test = require("test") +test.eq(foo(10,1),45) +test.eq(foo(10,2),20) +test.eq(foo(0,1),0) +test.eq(foo2(10),45) +test.eq(foo3(10),55) \ No newline at end of file diff --git a/samples/Terra/for2.t b/samples/Terra/for2.t new file mode 100644 index 00000000..3bd8f374 --- /dev/null +++ b/samples/Terra/for2.t @@ -0,0 +1,31 @@ + +terra foo(a : uint64, s : uint64) + var r = 0 + for i : uint64 = 0,a,s do + r = r + i + end + return r +end + +terra foo2(a : int) + var r = 0 + for i : int = 0.f,a do + r = r + i + end + return r +end +terra foo3(a : int) + var r = 0 + for i = a,0,-1 do + r = r + i + end + return r +end + + +local test = require("test") +test.eq(foo(10,1),45) +test.eq(foo(10,2),20) +test.eq(foo(0,1),0) +test.eq(foo2(10),45) +test.eq(foo3(10),55) diff --git a/samples/Terra/forbreak.t b/samples/Terra/forbreak.t new file mode 100644 index 00000000..d14b056f --- /dev/null +++ b/samples/Terra/forbreak.t @@ -0,0 +1,17 @@ + +terra foo(a : int, s : int) + var r = 0 + for i = 0,a,s do + if i == 8 then + break + end + r = r + i + + end + return r +end + +local test = require("test") +test.eq(foo(10,1),28) +test.eq(foo(10,2),12) +test.eq(foo(0,1),0) \ No newline at end of file diff --git a/samples/Terra/forlist.t b/samples/Terra/forlist.t new file mode 100644 index 00000000..da427475 --- /dev/null +++ b/samples/Terra/forlist.t @@ -0,0 +1,24 @@ + + +struct Range { + a : int; + b : int; +} +Range.metamethods.__for = function(syms,iter,body) + return syms, quote + var it = iter + for [syms[1]] = it.a,it.b do + body + end + end +end + +terra foo() + var a = 0 + for i in Range {0,10} do + a = a + i + end + return a +end + +assert(foo() == 10*9/2) \ No newline at end of file diff --git a/samples/Terra/forlist2.t b/samples/Terra/forlist2.t new file mode 100644 index 00000000..a09b6d73 --- /dev/null +++ b/samples/Terra/forlist2.t @@ -0,0 +1,37 @@ + + +struct Array { + data : int[3]; +} +Array.metamethods.__for = function(syms,iter,body) + local pe = symbol() + local e = `@pe + return {e}, quote + var it = &iter + for i = 0,3 do + var [pe] = &it.data[i] + body + end + end +end + +terra foo() + var a = Array{ array(1,2,3) } + for i in a do + i = i + 1 + end + return a.data[0] + a.data[1] + a.data[2] +end + +terra foo2() + var a = Array{ array(1,2,3) } + var s = 0 + for i : double in a do + s = s + i + end + return s +end + +assert(foo() == 9) + +assert(foo2() == 6) \ No newline at end of file diff --git a/samples/Terra/forp.t b/samples/Terra/forp.t new file mode 100644 index 00000000..2db2c3e9 --- /dev/null +++ b/samples/Terra/forp.t @@ -0,0 +1,21 @@ +struct Range { + a : int; + b : int; +} +Range.metamethods.__for = function(syms,iter,body) + return syms, quote + var it = iter + for [syms[1]] = it.a,it.b do + body + end + end +end + +terra foo() + var v = Range { 0, 3 } + var vp = &v + var i = 0 + for e in vp do i = i + e end + return i +end +assert(3 == foo()) \ No newline at end of file diff --git a/samples/Terra/forsym.t b/samples/Terra/forsym.t new file mode 100644 index 00000000..10857a48 --- /dev/null +++ b/samples/Terra/forsym.t @@ -0,0 +1,24 @@ + +terra foo(a : int, s : int) + var r = 0 + for i = 0,a,s do + r = r + i + end + return r +end + +local i = symbol() + +terra foo2(a : int) + var r = 0 + for [i] = 0,a do + r = r + [i] + end + return r +end + +local test = require("test") +test.eq(foo(10,1),45) +test.eq(foo(10,2),20) +test.eq(foo(0,1),0) +test.eq(foo2(10),45) \ No newline at end of file diff --git a/samples/Terra/forwardtodef.t b/samples/Terra/forwardtodef.t new file mode 100644 index 00000000..228ad86a --- /dev/null +++ b/samples/Terra/forwardtodef.t @@ -0,0 +1,7 @@ + + +terra foo() + return 1 +end +foo:setinlined(true) +foo:disas() \ No newline at end of file diff --git a/samples/Terra/functionnoproto.t b/samples/Terra/functionnoproto.t new file mode 100644 index 00000000..1ce9bad8 --- /dev/null +++ b/samples/Terra/functionnoproto.t @@ -0,0 +1,6 @@ +C = terralib.includecstring [[ + typedef int (*PROC)(); + PROC what() { return 0; } +]] + +assert(C.what() == nil) \ No newline at end of file diff --git a/samples/Terra/gctest.t b/samples/Terra/gctest.t new file mode 100644 index 00000000..12342798 --- /dev/null +++ b/samples/Terra/gctest.t @@ -0,0 +1,18 @@ + + + + +local terra foo() +end + +local terra bar() +end + +foo:compile() +bar:compile() +foo = nil + +collectgarbage() +collectgarbage() + +print("HERE") \ No newline at end of file diff --git a/samples/Terra/gemm.t b/samples/Terra/gemm.t new file mode 100644 index 00000000..5993ae22 --- /dev/null +++ b/samples/Terra/gemm.t @@ -0,0 +1,207 @@ +local number = double +local alignment = 8 +local dotune = false + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + +local function isinteger(x) return math.floor(x) == x end + +llvmprefetch = terralib.intrinsic("llvm.prefetch",{&opaque,int,int,int} -> {}) +local function unalignedload(addr) + return `terralib.attrload(addr, { align = alignment }) +end +local function unalignedstore(addr,v) + return `terralib.attrstore(addr,v, { align = alignment }) +end + +unalignedload,unalignedstore = macro(unalignedload),macro(unalignedstore) + +function genkernel(NB, RM, RN, V,alpha,boundary) + + local M,N,K, boundaryargs + if boundary then + M,N,K = symbol(int64,"M"),symbol(int64,"N"),symbol(int64,"K") + boundaryargs = terralib.newlist({M,N,K}) + else + boundaryargs = terralib.newlist() + M,N,K = NB,NB,NB + end + + local A,B,C,mm,nn,ld = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn"),symbol("ld") + local lda,ldb,ldc = symbol("lda"),symbol("ldb"),symbol("ldc") + local a,b,c,caddr = symmat("a",RM), symmat("b",RN), symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(number,V) + local VP = &VT + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + m*ldc + n*V + var [c[m][n]] = alpha * unalignedload(VP([caddr[m][n]])) + end) + storec:insert(quote + unalignedstore(VP([caddr[m][n]]),[c[m][n]]) + end) + end + end + + local calcc = terralib.newlist() + + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = unalignedload(VP(&B[n*V])) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[m*lda]) + end) + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end + end + + local result = terra([A] : &number, [B] : &number, [C] : &number, [lda] : int64,[ldb] : int64,[ldc] : int64,[boundaryargs]) + for [mm] = 0, M, RM do + for [nn] = 0, N,RN*V do + [loadc]; + for [k] = 0, K do + llvmprefetch(B + 4*ldb,0,3,1); + [calcc]; + B = B + ldb + A = A + 1 + end + [storec]; + A = A - K + B = B - ldb*K + RN*V + C = C + RN*V + end + C = C + RM * ldb - N + B = B - N + A = A + lda*RM + end + end + return result +end + +local stdlib = terralib.includec("stdlib.h") +local IO = terralib.includec("stdio.h") + +local terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end +function blockedloop(N,M,K,blocksizes,bodyfn) + local function generatelevel(n,ii,jj,kk,bb0,bb1,bb2) + if n > #blocksizes then + return bodyfn(ii,jj,kk) + end + local blocksize = blocksizes[n] + return quote for i = ii,min(ii+bb0,N),blocksize do + for j = jj,min(jj+bb1,M),blocksize do + for k = kk,min(kk+bb2,K),blocksize do + [ generatelevel(n+1,i,j,k,blocksize,blocksize,blocksize) ] + end end end end + end + return generatelevel(1,0,0,0,N,M,K) +end + +function generatedgemm(NB,NBF,RM,RN,V) + if not isinteger(NB/(RN*V)) or not isinteger(NB/RM) then + return false + end + + local NB2 = NBF * NB + local l1dgemm0 = genkernel(NB,RM,RN,V,0,false) + local l1dgemm1 = genkernel(NB,RM,RN,V,1,false) + local l1dgemm0b = genkernel(NB,1,1,1,0,true) + local l1dgemm1b = genkernel(NB,1,1,1,1,true) + + return terra(gettime : {} -> double, M : int, N : int, K : int, alpha : number, A : &number, lda : int, B : &number, ldb : int, + beta : number, C : &number, ldc : int) + [ blockedloop(N,M,K,{NB2,NB},function(m,n,k) return quote + var MM,NN,KK = min(M-m,NB),min(N-n,NB),min(K-k,NB) + var isboundary = MM < NB or NN < NB or KK < NB + var AA,BB,CC = A + (m*lda + k),B + (k*ldb + n),C + (m*ldc + n) + if k == 0 then + if isboundary then + --IO.printf("b0 %d %d %d\n",MM,NN,KK) + l1dgemm0b(AA,BB,CC,lda,ldb,ldc,MM,NN,KK) + + --IO.printf("be %d %d %d\n",MM,NN,KK) + else + l1dgemm0(AA,BB,CC,lda,ldb,ldc) + end + else + if isboundary then + + --IO.printf("b %d %d %d\n",MM,NN,KK) + l1dgemm1b(AA,BB,CC,lda,ldb,ldc,MM,NN,KK) + + --IO.printf("be %d %d %d\n",MM,NN,KK) + else + l1dgemm1(AA,BB,CC,lda,ldb,ldc) + end + end + end end) ] + end +end + +local blocksizes = {16,24,32,40,48,56,64} +local regblocks = {1,2,4} +local vectors = {1,2,4,8,16} +--local best = { gflops = 0, b = 56, rm = 4, rn = 1, v = 8 } +local best = { gflops = 0, b = 40, rm = 4, rn = 2, v = 4 } +--local best = { gflops = 0, b = 40, rm = 1, rn = 1, v = 1 } + +if dotune then + local tunefor = 1024 + local harness = require("lib/matrixtestharness") + for _,b in ipairs(blocksizes) do + for _,rm in ipairs(regblocks) do + for _,rn in ipairs(regblocks) do + for _,v in ipairs(vectors) do + local my_dgemm = generatedgemm(b,5,rm,rn,v) + if my_dgemm then + print(b,rm,rn,v) + my_dgemm:compile() + local i = math.floor(tunefor / b) * b + local avg = 0 + local ctyp + local s, times = harness.timefunctions(tostring(number),i,i,i,function(M,K,N,A,B,C) + my_dgemm(nil,M,N,K,1.0,A,K,B,N,0.0,C,N) + end) + if not s then + print("") + break + end + print(i,unpack(times)) + local avg = times[1] + if best.gflops < avg then + best = { gflops = avg, b = b, rm = rm, rn = rn, v = v } + terralib.tree.printraw(best) + end + end + end + end + end + end +end +local my_dgemm = generatedgemm(best.b, 5, best.rm, best.rn, best.v) +if number == double then + terralib.saveobj("my_dgemm.o", { my_dgemm = my_dgemm }) +else + terralib.saveobj("my_sgemm.o", { my_sgemm = my_dgemm }) +end \ No newline at end of file diff --git a/samples/Terra/getmethod.t b/samples/Terra/getmethod.t new file mode 100644 index 00000000..5d93531a --- /dev/null +++ b/samples/Terra/getmethod.t @@ -0,0 +1,19 @@ + + +struct A {} + + +A.metamethods.__getmethod = function(self,methodname) + local c = methodname:sub(1,1):byte() + return terra(a : &A) + return c + end +end + + +terra foo() + var a : A + return a:a() + a:b() +end + +assert(foo() == ("a"):byte() + ("b"):byte()) \ No newline at end of file diff --git a/samples/Terra/gettype.t b/samples/Terra/gettype.t new file mode 100644 index 00000000..dacd2b39 --- /dev/null +++ b/samples/Terra/gettype.t @@ -0,0 +1,18 @@ + + + +function makestuff(a) + local T = a:gettype() + local struct ST { + a : T + } + return `ST { a } +end + +makestuff = macro(makestuff) + +terra useit() + return makestuff(true).a,makestuff(3.0).a +end +local test = require("test") +test.meq({true,3},useit()) \ No newline at end of file diff --git a/samples/Terra/globals.t b/samples/Terra/globals.t new file mode 100644 index 00000000..9dc6d6ba --- /dev/null +++ b/samples/Terra/globals.t @@ -0,0 +1,26 @@ + +struct A { a : int } +local a = terralib.global(A) +a:set({3}) +local b = terralib.global(true) +local c = terralib.global(int[3]) +local d = terralib.global(3.5) + +terra foobar() + c[0] = 3 + return a.a + d +end + +e = global(int) +terra doihaveaname() + e = 10 + return e +end + +assert(doihaveaname() == 10) +assert(6.5 == foobar()) +print("HERE") +assert(true == b:get()) +assert(3 == c:get()[0]) + +print("TODO - fix handling of initializers for array-like things and literal strings") \ No newline at end of file diff --git a/samples/Terra/goto.t b/samples/Terra/goto.t new file mode 100644 index 00000000..241adf0c --- /dev/null +++ b/samples/Terra/goto.t @@ -0,0 +1,16 @@ + + +terra foo() + var a = 0 + ::begin:: + if a >= 10 then + goto theend + end + a = a + 1 + goto begin + ::theend:: + return a +end + +test = require("test") +test.eq(foo(),10) \ No newline at end of file diff --git a/samples/Terra/goto2.t b/samples/Terra/goto2.t new file mode 100644 index 00000000..f8f430da --- /dev/null +++ b/samples/Terra/goto2.t @@ -0,0 +1,18 @@ + +local begin = symbol() +local theend = symbol() + +terra foo() + var a = 0 + ::[begin]:: + if a >= 10 then + goto [theend] + end + a = a + 1 + goto [begin] + ::[theend]:: + return a +end + +test = require("test") +test.eq(foo(),10) \ No newline at end of file diff --git a/samples/Terra/gvarfault.t b/samples/Terra/gvarfault.t new file mode 100644 index 00000000..6702563b --- /dev/null +++ b/samples/Terra/gvarfault.t @@ -0,0 +1,24 @@ +if require("ffi").os == "Windows" then + print("Disabled on windows (uses unistd.h)") + return +end +local C = terralib.includecstring( +[[ +#include +#include +#include +#include +#include +]] +) + +local gstr = global(&int8, "some text") + +terra fun1() + C.printf("gstr: %s \n", gstr) + return 0 +end + +fun1:printpretty() + +fun1() \ No newline at end of file diff --git a/samples/Terra/hasbeenfrozen.t b/samples/Terra/hasbeenfrozen.t new file mode 100644 index 00000000..c1c40866 --- /dev/null +++ b/samples/Terra/hasbeenfrozen.t @@ -0,0 +1,33 @@ +struct A { + a : int; + b : int; + c : &B +} and struct B { + a : &A +} + +local a = global(A) + + +terra foo() + var b : B + return a.a + a.b +end + +function A.metamethods.__staticinitialize(self) + print("A") + assert(A:iscomplete()) + a:get().a = 4 +end + +function B.metamethods.__staticinitialize(self) + print("B") + assert(B:iscomplete()) + a:get().b = 3 + foo:gettype(function() + assert(foo() == 7) + a:get().a = a:get().a + 1 + end) +end + +assert(foo() == 8) \ No newline at end of file diff --git a/samples/Terra/hello.t b/samples/Terra/hello.t new file mode 100644 index 00000000..81af8917 --- /dev/null +++ b/samples/Terra/hello.t @@ -0,0 +1,8 @@ +local c = terralib.includec("stdio.h") + +terra hello() + c.printf("hello, world\n") +end + +terralib.saveobj("hello",{main = hello}) +hello() diff --git a/samples/Terra/hello2.t b/samples/Terra/hello2.t new file mode 100644 index 00000000..638b5e15 --- /dev/null +++ b/samples/Terra/hello2.t @@ -0,0 +1,10 @@ + +local c = terralib.includec("stdio.h") + +terralib.tree.printraw(getmetatable(c).errors) + +terra main() + c.printf("hello, world\n") +end + +main() \ No newline at end of file diff --git a/samples/Terra/hexf.t b/samples/Terra/hexf.t new file mode 100644 index 00000000..6b767b49 --- /dev/null +++ b/samples/Terra/hexf.t @@ -0,0 +1,9 @@ + + +assert(0xf == 15) + +terra foo() + return 0xf +end + +assert(foo() == 15) \ No newline at end of file diff --git a/samples/Terra/huge.t b/samples/Terra/huge.t new file mode 100644 index 00000000..e52733db --- /dev/null +++ b/samples/Terra/huge.t @@ -0,0 +1,7 @@ + + +terra returnhuge() + return math.huge +end + +assert(returnhuge() == math.huge) \ No newline at end of file diff --git a/samples/Terra/ifelse.t b/samples/Terra/ifelse.t new file mode 100644 index 00000000..2f3d83a0 --- /dev/null +++ b/samples/Terra/ifelse.t @@ -0,0 +1,17 @@ + +terra foo(a : int) + return terralib.select(a > 0, 1,-1) +end + + +terra foo2(a : int) + var c = vector(a > 0, a > 1, a > 2) + var d = terralib.select(c,vector(1,2,3),vector(4,5,6)) + return d[0], d[1], d[2] +end +local test = require("test") + +test.eq(foo(1),1) +test.eq(foo(-1),-1) + +test.meq({1,5,6},foo2(1)) \ No newline at end of file diff --git a/samples/Terra/includec.t b/samples/Terra/includec.t new file mode 100644 index 00000000..3ebb9f4d --- /dev/null +++ b/samples/Terra/includec.t @@ -0,0 +1,42 @@ + +local c = terralib.includec("mytest.h") + +terra foo() + var a : int = 3 + return c.myfoobarthing(1,2,3.5,&a) + a +end + +terra bar() + return c.myotherthing(4,5) +end + +terra bar2() + return c.myfnptr(bar) +end + +terra bar3() + var a : c.size_t = 3 + return a +end + +terra bar4() + var opaquething : c.MyStruct2 + var my = c.MyStruct { 3, 4} + return my.a + my.b +end + +terra writefile() + c.printf("%f %f %f\n",3.0,4.0,5.0) + var f = c.fopen("afile.txt","w") + c.fputs("a string\n",f) + c.fclose(f) +end + +local test = require("test") + +test.eq(foo(),15) +test.eq(c.myotherthing(1,2),3) +test.eq(bar2(),9) +test.eq(bar3(),3) +test.eq(bar4(),7) +writefile() diff --git a/samples/Terra/includetwice.t b/samples/Terra/includetwice.t new file mode 100644 index 00000000..eeba70d4 --- /dev/null +++ b/samples/Terra/includetwice.t @@ -0,0 +1,21 @@ + + +local C1 = terralib.includecstring [[ + typedef struct { int a; } A; + int geta(A* a) { return a->a; } +]] +local C2 = terralib.includecstring [[ + typedef struct { int a; } A; + void seta(A* a, int v) { a->a = v; } +]] + +assert(C1.A == C2.A) + + +terra usethem() + var a = C1.A { 1 } + C2.seta(&a,C1.geta(&a) + 3) + return a.a +end + +assert(usethem() == 4) \ No newline at end of file diff --git a/samples/Terra/incomplete.t b/samples/Terra/incomplete.t new file mode 100644 index 00000000..8b1d4662 --- /dev/null +++ b/samples/Terra/incomplete.t @@ -0,0 +1,32 @@ + + +struct A + +terra foo(a : &A) + return a +end + +assert(nil == foo(nil)) +assert(false == A:iscomplete()) + + +struct A { + b : int +} + +terra foo2(a : &A) + a.b = 6 + return @a +end + +foo2:compile() +assert(true == A:iscomplete()) + +local ptr = terralib.new(A) + +ptr.b = 4 + +local r = foo2(ptr) + +assert(r.b == 6) +assert(ptr.b == 6) \ No newline at end of file diff --git a/samples/Terra/incomplete2.t b/samples/Terra/incomplete2.t new file mode 100644 index 00000000..17223680 --- /dev/null +++ b/samples/Terra/incomplete2.t @@ -0,0 +1,25 @@ + +struct A { + b : &B +} and +struct B + + +terra foo() + var a : A + a.b = nil + return a +end + +local a = foo() + + +struct B { + a : int +} + +terra foo2(a : &A) + a.b = a.b + 1 +end + +foo2(a) diff --git a/samples/Terra/incomplete3.t b/samples/Terra/incomplete3.t new file mode 100644 index 00000000..c23bb9be --- /dev/null +++ b/samples/Terra/incomplete3.t @@ -0,0 +1,29 @@ + +struct A { + b : B -> C +} and +struct B and struct C + + +terra foo() + var a : A + a.b = nil + return a +end + +local a = foo() + + +struct B { + a : int +} +struct C { + b : int +} + +terra foo2(a : &A) + var ptrb : &B + var c = a.b(@ptrb) +end + +foo2:compile() \ No newline at end of file diff --git a/samples/Terra/incomplete4.t b/samples/Terra/incomplete4.t new file mode 100644 index 00000000..765eadc1 --- /dev/null +++ b/samples/Terra/incomplete4.t @@ -0,0 +1,22 @@ + + +struct A + +terra foo(a : &A) + return a +end + +assert(nil == foo(nil)) +assert(false == A:iscomplete()) + + +struct A { + b : int +} + +terra foo2(a : &A) + return a.b +end + +foo2:compile() +assert(true == A:iscomplete()) diff --git a/samples/Terra/incomplete5.t b/samples/Terra/incomplete5.t new file mode 100644 index 00000000..444b451e --- /dev/null +++ b/samples/Terra/incomplete5.t @@ -0,0 +1,65 @@ + + + +--terra things that should complete the type +local ct = {} + +--terra things that should not complete the type +local dct = {} + + + +function ct.a(A) + return terra(a : A) return a end +end + +function ct.b(A) + return terra() var a : A end +end + +function ct.c(A) + return terra(a : &A) return a + 1 end +end +function ct.d(A) + local at = A[3] + return terra(a : A[3]) return a[0] end +end +function ct.e(A) + local struct B { a : A } + return terra(a : B) end +end + +function dct.c(A) + local at = A[3] + return terra(a : &A[3]) return a end +end + +function dct.b(A) + return terra(a : &A) return a end +end + +function dct.d(A) + local struct B { + a : &A; + b : int + } + return terra(a : &B) return a.b end +end + + +local function testthem(stuff,completestype) + for k,v in pairs(stuff) do + local struct A + if completestype then + struct A { + a : int + } + end + local result = v(A) + result:compile() + assert(A:iscomplete() == completestype) + end +end + +testthem(ct,true) +testthem(dct,false) \ No newline at end of file diff --git a/samples/Terra/incompletetypetest.t b/samples/Terra/incompletetypetest.t new file mode 100644 index 00000000..e35cfd90 --- /dev/null +++ b/samples/Terra/incompletetypetest.t @@ -0,0 +1,13 @@ + + +struct A { + a : int +} + + +terra what() + var a : &A = nil + return &a[4] +end + +what:compile() \ No newline at end of file diff --git a/samples/Terra/incompletetypetest2.t b/samples/Terra/incompletetypetest2.t new file mode 100644 index 00000000..9f48c53e --- /dev/null +++ b/samples/Terra/incompletetypetest2.t @@ -0,0 +1,13 @@ + + +struct A { + a : int +} + + +terra what() + var a : &A = nil + return a + 4 +end + +what:compile() \ No newline at end of file diff --git a/samples/Terra/incompletetypetest3.t b/samples/Terra/incompletetypetest3.t new file mode 100644 index 00000000..bae8aa2f --- /dev/null +++ b/samples/Terra/incompletetypetest3.t @@ -0,0 +1,13 @@ + + +struct A { + a : int +} + + +terra what() + var a : &A = nil + return a - a +end + +what:compile() \ No newline at end of file diff --git a/samples/Terra/indexing64.t b/samples/Terra/indexing64.t new file mode 100644 index 00000000..4c87ff73 --- /dev/null +++ b/samples/Terra/indexing64.t @@ -0,0 +1,10 @@ + + +terra foo() + var a : int[5] + a[2] = 2 + var b = [int64](2) + return a[b] +end + +assert(foo() == 2) \ No newline at end of file diff --git a/samples/Terra/indexingbug.t b/samples/Terra/indexingbug.t new file mode 100644 index 00000000..0f9cee13 --- /dev/null +++ b/samples/Terra/indexingbug.t @@ -0,0 +1,31 @@ +arr = terralib.new(int[256]) + +arr[252] = 12 + +terra foo(arr : &int) + var idx = [uint8](252) + return arr[idx] +end + +terra foo2(arr : &int) + var idx = [uint8](252) + return @(arr + idx) +end + +terra foo3(arr : &int) + var idx = [int8](-3) + arr = arr + 255 + return @(arr + idx) +end + + +terra foo4(arr : &int) + var idx = [int8](-3) + arr = arr + 255 + return arr[idx] +end + +assert(foo2(arr) == 12) +assert(foo(arr) == 12) +assert(foo3(arr) == 12) +assert(foo4(arr) == 12) \ No newline at end of file diff --git a/samples/Terra/interface.t b/samples/Terra/interface.t new file mode 100644 index 00000000..d837aa60 --- /dev/null +++ b/samples/Terra/interface.t @@ -0,0 +1,33 @@ + +IO = terralib.includec("stdio.h") +local Class = require("lib/javalike") + +struct A { + a : int +} + +terra A:foo(a : int) : int + return self.a + a +end + +local m = A.methods.foo:getdefinitions()[1] + +HasFoo = Class.interface({ foo = int -> int }) + +Class.implements(A,HasFoo) + +terra hasfoo(a : &HasFoo) + return a:foo(3) +end + +terra testit() + var a = A.alloc() + a.a = 4 + return hasfoo(a) + a:foo(5) +end + +print(m:getpointer()) +testit:compile() +print("DONE") + +assert(testit() == 16) \ No newline at end of file diff --git a/samples/Terra/interface2.t b/samples/Terra/interface2.t new file mode 100644 index 00000000..bdb9a02d --- /dev/null +++ b/samples/Terra/interface2.t @@ -0,0 +1,38 @@ + +IO = terralib.includec("stdio.h") +local Class = require("lib/javalike") + +HasFoo = Class.interface({ foo = int -> int }) + +struct A { + a : int +} + +terra A:foo(a : int) : int + return 1 +end +Class.implements(A,HasFoo) + +struct B {} +Class.extends(B,A) + + +struct C {} +Class.extends(C,B) + +terra C:foo(a : int) : int + return 3 +end + +terra hasfoo(a : &HasFoo) + return a:foo(3) +end + +terra testit() + var a = A.alloc() + var b = B.alloc() + var c = C.alloc() + return hasfoo(a) + hasfoo(b) + hasfoo(c) +end + +assert(testit() == 5) \ No newline at end of file diff --git a/samples/Terra/intrinsic.t b/samples/Terra/intrinsic.t new file mode 100644 index 00000000..8617ce5c --- /dev/null +++ b/samples/Terra/intrinsic.t @@ -0,0 +1,21 @@ +local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) +local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) + +local sqrt3 = terralib.intrinsic(function(types) + if #types == 1 and types[1]:isvector() and types[1].type == float then + local N = types[1].N + return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) + elseif #types == 1 and types[1] == float then + return "llvm.sqrt.f32",float -> float + end +end) + +terra foo() + var v = vector(1.f,2.f,3.f,4.f) + var c = sqrt2(vector(1,2,3,4)) + return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) +end + +local test = require("test") + +test.eq(foo(),8) \ No newline at end of file diff --git a/samples/Terra/isvolatile.t b/samples/Terra/isvolatile.t new file mode 100644 index 00000000..f293fb96 --- /dev/null +++ b/samples/Terra/isvolatile.t @@ -0,0 +1,7 @@ + + +terra foobar(a : &vector(float,4),b : vector(int,4)) + terralib.attrstore(a,b,{ isvolatile = true }) +end + +foobar:disas() diff --git a/samples/Terra/labelbug.t b/samples/Terra/labelbug.t new file mode 100644 index 00000000..4d996e4f --- /dev/null +++ b/samples/Terra/labelbug.t @@ -0,0 +1,5 @@ + +terra foo() + ::alabel:: ;; + return 0 +end \ No newline at end of file diff --git a/samples/Terra/latelink.t b/samples/Terra/latelink.t new file mode 100644 index 00000000..43a64765 --- /dev/null +++ b/samples/Terra/latelink.t @@ -0,0 +1,24 @@ +C = terralib.includecstring [[ + typedef struct { + int a; + } S; + typedef struct { + S a; + } A; +]] +C2 = terralib.includecstring [[ + typedef struct { + int a; + } S; + typedef struct { + S a; + } B; +]] + +terra second() + var a : C2.B + var b : C.A + + a.a = b.a +end +second:disas() \ No newline at end of file diff --git a/samples/Terra/lazycstring.t b/samples/Terra/lazycstring.t new file mode 100644 index 00000000..3b3270a1 --- /dev/null +++ b/samples/Terra/lazycstring.t @@ -0,0 +1,21 @@ + +struct A { + a : int +} + + +print(A:cstring()) + +terra foo() + return A { 1 } +end + +struct B { + a : int +} + +terra foo2() + return B { 1 } +end + +assert(foo().a == foo2().a) \ No newline at end of file diff --git a/samples/Terra/lazylog.t b/samples/Terra/lazylog.t new file mode 100644 index 00000000..84bba0fa --- /dev/null +++ b/samples/Terra/lazylog.t @@ -0,0 +1,61 @@ +local function gentrees(depth) + local function treetostring(tree) + if type(tree) == "string" then return tree + elseif tree.rhs then return "(" .. treetostring(tree.lhs) .. " " .. tree.op .. " " .. treetostring(tree.rhs) .. ")" + else return "(" .. tree.op .. " " .. treetostring(tree.lhs) .. ")" end + end + local function gendepth(depth) + local trees = {} + table.insert(trees, "%s") + if depth == 0 then return trees end + local subtrees = gendepth(depth - 1) + for _,t0 in ipairs(subtrees) do + table.insert(trees, { lhs = t0, op = "not" }) + for _,t1 in ipairs(subtrees) do + table.insert(trees, { lhs = t0, rhs = t1, op = "and" }) + table.insert(trees, { lhs = t0, rhs = t1, op = "or" }) + end + end + return trees + end + local trees = gendepth(depth) + local nargs = 2 ^ depth + local argassign = "local i0" + local targs = "return terra(i0 : bool" + local args = {"i0"} + for i = 1, nargs - 1 do + local arg = "i"..tostring(i) + table.insert(args,arg) + argassign = argassign .. ", " .. arg + targs = targs .. ", " .. arg .. " : bool" + end + argassign = argassign .. " = ..." + targs = targs .. ")" + + argassign = argassign .. "\nreturn " + targs = targs .. "\nreturn " + for treeno,t in ipairs(trees) do + local line = string.format(treetostring(t),unpack(args)) + io.write(string.format("\r%3d%% %s",treeno/#trees * 100,line)) + local lcode = argassign .. line + local tcode = targs .. line .. " end" + local lfn = assert(loadstring(lcode)) + local tfn = assert(terralib.loadstring(tcode))() + tfn:compile() + local states = 2 ^ nargs + local actuals = {} + local band = bit.band + local blshift = bit.lshift + for i = 0, states - 1 do + for s = 1, nargs do + actuals[s] = 0 ~= band(i,blshift(1,s-1)) + end + local result = lfn(unpack(actuals)) + local result2 = tfn(unpack(actuals)) + assert(result == result2) + end + end + io.write("\n") +end + +gentrees(2) \ No newline at end of file diff --git a/samples/Terra/leaktest.t b/samples/Terra/leaktest.t new file mode 100644 index 00000000..d49ff6bb --- /dev/null +++ b/samples/Terra/leaktest.t @@ -0,0 +1,27 @@ +local leak = require "lib.leak" + +function make() + local terra a(x:int) + return x+1 + end + + local terra b(x:int) + return a(x) + end + + return b(12),leak.track(b) +end +nan = 0/0 +res,t=make() +assert(res == 13) +local gcd, path = leak.find(t) +if gcd then + print(path) + assert(false) +end + +l = leak.track(leak) +foo = 0/0 +local f,p = leak.find(l) +assert(f) +assert(p == "$locals.leak") \ No newline at end of file diff --git a/samples/Terra/let1.t b/samples/Terra/let1.t new file mode 100644 index 00000000..2363cc7f --- /dev/null +++ b/samples/Terra/let1.t @@ -0,0 +1,15 @@ + + +terra foo() + return (1),(2) +end + +terra bar() + var c = [quote var a = 3 in a end] + var d = 5 + c = c + 10 + return [quote var a = 3 in [quote var b = 4 in a + b + c + d end] end] +end +local test = require("test") +test.meq({1,2},foo()) +assert(25 == bar()) diff --git a/samples/Terra/let2.t b/samples/Terra/let2.t new file mode 100644 index 00000000..273984a2 --- /dev/null +++ b/samples/Terra/let2.t @@ -0,0 +1,64 @@ + + +local a = +quote + var b = 1 +in + b + 0, b + 1 +end + +terra f0() + return (a) +end +terra f1() + return a +end +terra f2() + a +end +local test = require("test") +test.meq({1,2},f0()) +test.meq({1,2},f1()) + +local c = symbol() +local b = +quote + var [c] = 3 +in + c,c+1 +end + +terra f3() + b + return ([c]) +end +assert(f3() == 3) + +a = global(1) +local emptyexp = quote + a = a + 1 +end + +local emptystmts = {4,3} + +local both = quote +in 4,[quote a = a + 1 in 3 end] +end + +terra bar(a : int, b : int) + return a + b +end +terra bar2(a : int, b : int, c : {}) + return a + b +end +terra f4() + return bar(emptystmts) + bar2(1,2,emptyexp) + a +end + +assert(f4() == 12) + +terra f5() + return bar(both._0,both._0) + a +end + +assert(f5() == 12) \ No newline at end of file diff --git a/samples/Terra/linkllvm.t b/samples/Terra/linkllvm.t new file mode 100644 index 00000000..852f503f --- /dev/null +++ b/samples/Terra/linkllvm.t @@ -0,0 +1,13 @@ + + +terra add(a : int, b : int) + return a + b +end + + +terralib.saveobj("add.bc",{ add = add }) + +local addlib = terralib.linkllvm("add.bc") +add2 = addlib:extern("add", {int,int} -> int) + +assert(add2(3,4) == 7) \ No newline at end of file diff --git a/samples/Terra/localenv.t b/samples/Terra/localenv.t new file mode 100644 index 00000000..da37d91a --- /dev/null +++ b/samples/Terra/localenv.t @@ -0,0 +1,46 @@ + +local a = 4 +b = 3 +local terra foo() + return a + b +end +local terra bar() + return foo() +end +local test = require("test") + +test.eq(foo(),7) +test.eq(bar(),7) + +terra nested() + return [ (function(a) return a + b end)(8) ] +end +terra nested2() + return [ a + b ] +end +local c = {} +local d = {} +local vv = {} +local b = 3 +local e = 8 +g = 1 +local dd = 8 + aa = global(10) +terra nested3() + var luav = 8 + return [ (function() + local b = 5 + function c.b() return 7 end + local f = 7 + vv.bb = global(aa:get()) + terra d.c() return e + f + g + vv.bb + [ `dd ] end + return b + a + end)() ] +end + +test.eq(nested(),11) + +test.eq(nested2(),7) +test.eq(nested3(),9) +test.eq(c.b(),7) +test.eq(d.c(),34) \ No newline at end of file diff --git a/samples/Terra/localenv2.t b/samples/Terra/localenv2.t new file mode 100644 index 00000000..355602ba --- /dev/null +++ b/samples/Terra/localenv2.t @@ -0,0 +1,7 @@ + +local dd = 8 +terra nested3() + return [ `dd ] +end +local test = require("test") +test.eq(nested3(),8) \ No newline at end of file diff --git a/samples/Terra/logical.t b/samples/Terra/logical.t new file mode 100644 index 00000000..7e4f7523 --- /dev/null +++ b/samples/Terra/logical.t @@ -0,0 +1,33 @@ + + +local T = true +local F = false + +terra check() + var a = true + return not true == false +end + +assert(check() == true) + +terra check2() + return not T == F +end + +assert(check2() == true) + +terra check3() + return not T == not not T +end + +assert(check3() == false) + +terra check4() + return not not T == T and not not F == F and true == T and false == F +end + +assert(check4() == true) + +terra foo() return not false end + +assert(foo() == true) \ No newline at end of file diff --git a/samples/Terra/luabridge.t b/samples/Terra/luabridge.t new file mode 100644 index 00000000..d7fcb285 --- /dev/null +++ b/samples/Terra/luabridge.t @@ -0,0 +1,18 @@ +struct A { a : int, b : double } + +terra foo(a : A) + return a.a + a.b +end + + +terra foo2(a : A) + return a.a + a.b,a.a +end + +local test = require("test") + +test.eq( foo( {a = 1,b = 2.3} ), 3.3 ) +test.eq( foo( {1,2.3} ), 3.3 ) +test.eq( foo( {b = 1, a = 2.3} ),3 ) + +test.meq({3.3,1},foo2( { a = 1, b = 2.3} )) \ No newline at end of file diff --git a/samples/Terra/luabridge2.t b/samples/Terra/luabridge2.t new file mode 100644 index 00000000..f4d86412 --- /dev/null +++ b/samples/Terra/luabridge2.t @@ -0,0 +1,92 @@ +struct A { a : int, b : double } + +terra returnstruct() + var a = A { 1, 2.5} + return a +end +terra returnstruct2() + var a = A { 1, 2.5} + var b = A { 2, 3.5} + return a,b +end + +terra returnarray() + var a : int[4] = array(1,2,3,4) + return a +end +terra returnarray2() + var a : int[4] = array(1,2,3,4) + var b : int[4] = array(5,6,7,8) + return a,b +end + +terra returnaos() + var a : A[2] = arrayof(A, {1,2.5}, {2,3.5} ) + return a +end +struct B { a : int[4] } +terra returnsoa() + var a = B { array(1,2,3,4) } + return a +end + +global_a = global(A) +global_a:set( {3, 4.5}) + +terra retstructptr() + return &global_a +end +terra structptr(a : &A) + return a.a + a.b +end + +global_arr = global(int[2]) +global_arr:set({3,4}) + +terra retarrptr() + return &global_arr +end +terra arrptr(a : &int[2]) + return (@a)[0] + a[0][1] +end + +local test = require("test") +local a0 = returnstruct() +test.eq(a0.a,1) +test.eq(a0.b,2.5) +local a1,b1 = terralib.unpackstruct(returnstruct2()) +test.eq(a1.a,1) +test.eq(a1.b,2.5) +test.eq(b1.a,2) +test.eq(b1.b,3.5) +--[[ C doesn't actually allow you to return arrays directly +local arr = returnarray() +test.eq(arr[0],1) +test.eq(arr[1],2) +test.eq(arr[2],3) +test.eq(arr[3],4) +local arr2,arr3 = returnarray2() +test.eq(arr2[0],1) +test.eq(arr2[1],2) +test.eq(arr2[2],3) +test.eq(arr2[3],4) +test.eq(arr3[0],5) +test.eq(arr3[1],6) +test.eq(arr3[2],7) +test.eq(arr3[3],8) +local arr4 = returnaos() +test.eq(arr4[0].a,1) +test.eq(arr4[1].b,3.5) +]] + +local arr5 = returnsoa() +test.eq(arr5.a[0],1) +test.eq(arr5.a[3],4) +local ptrret = retstructptr() +test.eq(ptrret[0].a,3) +test.eq(ptrret[0].b,4.5) +test.eq(structptr(ptrret),7.5) +local ptrarr = retarrptr() +test.eq(ptrarr[0][0],3) +test.eq(ptrarr[0][1],4) +test.eq(arrptr(ptrarr),7) \ No newline at end of file diff --git a/samples/Terra/luabridgefn.t b/samples/Terra/luabridgefn.t new file mode 100644 index 00000000..488e4f9e --- /dev/null +++ b/samples/Terra/luabridgefn.t @@ -0,0 +1,23 @@ + +terra plus1(a : int) + return a + int(1) +end + +terra afn() : int -> int + return plus1 +end + +terra doit(a : int, b : int -> int) + return b(a) +end + +local test = require("test") + +local foobar = afn() +test.eq(foobar(3),4) +test.eq(doit(4,foobar),5) +local function whatwhat(a) + print("I GOT A ", a) + return a + 3 +end +test.eq(doit(5,whatwhat),8) diff --git a/samples/Terra/luabridgerec.t b/samples/Terra/luabridgerec.t new file mode 100644 index 00000000..b5a5ad23 --- /dev/null +++ b/samples/Terra/luabridgerec.t @@ -0,0 +1,10 @@ + +struct A { a : &A, b : int } + +terra foo(a : A) + return a.b +end + +local test = require("test") + +test.eq(foo({b = 5}),5) diff --git a/samples/Terra/luabridgeunion.t b/samples/Terra/luabridgeunion.t new file mode 100644 index 00000000..8866866f --- /dev/null +++ b/samples/Terra/luabridgeunion.t @@ -0,0 +1,14 @@ +struct A { union {a : int, b : double}, c : int } + +terra foo(a : A) + return a.a + a.c +end +terra foo2(a : A) + return a.b + a.c +end + + +local test = require("test") + +test.eq( foo({a = 4, c = 6}), 10) +test.eq( foo2({b = 4.4, c = 6}), 10.4) \ No newline at end of file diff --git a/samples/Terra/luaterramethod.t b/samples/Terra/luaterramethod.t new file mode 100644 index 00000000..8cad2f52 --- /dev/null +++ b/samples/Terra/luaterramethod.t @@ -0,0 +1,40 @@ + + +struct Foo { + a : int; + b : int; +} + +terra Foo:bar() + self.a = self.a + 1 + return self.a + self.b +end + +Foo.methods.baz = function(self) print(self.a,self.b) end +Foo.methods.mac = macro(function() return `1 end) + +terra usemethod() + var a = Foo { 3, 4} + a:baz() + return a:bar() +end +assert(8 == usemethod()) + +obj = terralib.new(Foo[1], {{3,4}}) + +assert(8 == obj[0]:bar()) +assert(9 == obj[0]:bar()) +obj[0]:baz() + +local a,b = pcall(function() +obj[0]:mac() +end) +assert(not a) +assert(string.match(b,"not supported")) + +local a,b = pcall(function() +obj[0]:maz() +end) + +assert(not a) +assert(string.match(b,"attempt to call method")) \ No newline at end of file diff --git a/samples/Terra/lvaluepointer.t b/samples/Terra/lvaluepointer.t new file mode 100644 index 00000000..1e3855ba --- /dev/null +++ b/samples/Terra/lvaluepointer.t @@ -0,0 +1,10 @@ + + +terra foo() + var a : int = 5 + var pa = &a + @pa = 4 + @[&float](pa) = 5 +end + +foo() \ No newline at end of file diff --git a/samples/Terra/lvaluequote.t b/samples/Terra/lvaluequote.t new file mode 100644 index 00000000..56f4536e --- /dev/null +++ b/samples/Terra/lvaluequote.t @@ -0,0 +1,22 @@ + + +checkl = macro(function(a) + assert(a:islvalue()) + return a +end) + +checkr = macro(function(a) + assert(not a:islvalue()) + return a +end) + + +terra testit() + + var a = checkr(4) + var b = checkr(checkl(a) + 3) + var c = checkl([quote var d = 5 in d end]) + return a + b + c +end + +assert(4+7+5 == testit()) diff --git a/samples/Terra/lvaluetreelist.t b/samples/Terra/lvaluetreelist.t new file mode 100644 index 00000000..cfc01a57 --- /dev/null +++ b/samples/Terra/lvaluetreelist.t @@ -0,0 +1,9 @@ + + +terra foobar() + var a = 4 + @&[quote in a end] = 5 + return a +end + +assert(foobar() == 5) \ No newline at end of file diff --git a/samples/Terra/macro.t b/samples/Terra/macro.t new file mode 100644 index 00000000..3c8afcf5 --- /dev/null +++ b/samples/Terra/macro.t @@ -0,0 +1,50 @@ + +local bar = terralib.internalmacro(function(ctx,tree,typ) + return terralib.newtree(typ.tree, { kind = terralib.kinds.literal, type = double, value = 4.0 }) + +end) + +local bar2 = macro(function(typ) + return typ + +end) + + +local bar3 = macro(function(a,b) + return {a.tree,b.tree} +end) + +terra up(v : &int) + @v = @v + 1 +end + +local bar4 = macro(function() + local terra myfn() + return 42 + end + return myfn +end) + +moo = global(int,3) + +local bar4 = macro(function() + local terra myfn() + return 42 + end + return myfn +end) + +local bar5 = macro(function() + return moo +end) + +terra foo() : int + var a : int = bar(int,int16,int32) + bar2(a) = bar2(a) + 5 + bar3(up(&a),up(&a)) + bar5() = bar5() + 1 + return a + bar4()() + moo +end + +local test = require("test") +test.eq(57,foo()) diff --git a/samples/Terra/macro2.t b/samples/Terra/macro2.t new file mode 100644 index 00000000..c910bb14 --- /dev/null +++ b/samples/Terra/macro2.t @@ -0,0 +1,14 @@ + +local bar2 = macro(function(typ) + return typ + +end) + +terra foo() : int + var a = 3 + bar2(a) = bar2(a) + 5 + return a +end + +local test = require("test") +test.eq(8,foo()) diff --git a/samples/Terra/macro3.t b/samples/Terra/macro3.t new file mode 100644 index 00000000..9b1015c8 --- /dev/null +++ b/samples/Terra/macro3.t @@ -0,0 +1,14 @@ + + +foo = macro(function() + local terra bar() + return 3 + end + return bar +end) + +terra baz() + return foo()() +end + +assert(baz() == 3) \ No newline at end of file diff --git a/samples/Terra/macrokey.t b/samples/Terra/macrokey.t new file mode 100644 index 00000000..329b92aa --- /dev/null +++ b/samples/Terra/macrokey.t @@ -0,0 +1,9 @@ + +what = function() return "abcd" end + +terra foo() + var a = { [what()] = 4 } + return a.abcd +end +local test = require("test") +test.eq(foo(),4) \ No newline at end of file diff --git a/samples/Terra/macrolet.t b/samples/Terra/macrolet.t new file mode 100644 index 00000000..f6343823 --- /dev/null +++ b/samples/Terra/macrolet.t @@ -0,0 +1,17 @@ + + +twice = macro(function(a) + return quote + var v = a + in + v + v + end +end) + + +terra foobar() + var what = 1 + return twice([quote what = what + 1 in what end]) + what +end + +assert(6 == foobar()) \ No newline at end of file diff --git a/samples/Terra/macroselect.t b/samples/Terra/macroselect.t new file mode 100644 index 00000000..31c18f6c --- /dev/null +++ b/samples/Terra/macroselect.t @@ -0,0 +1,11 @@ + +struct A { a : int, b : int } + +local c = "b" +terra foo() + var a = A {1,2} + return a.[c] +end + +local test = require("test") +test.eq(foo(),2) \ No newline at end of file diff --git a/samples/Terra/macrotest.t b/samples/Terra/macrotest.t new file mode 100644 index 00000000..707e6b6e --- /dev/null +++ b/samples/Terra/macrotest.t @@ -0,0 +1,9 @@ +C = terralib.includecstring [[ +#define foo 1 +#define foo2 -3.4 +#undef foo +#define foo 3 +]] + +assert(C.foo == 3) +assert(C.foo2 == -3.4) \ No newline at end of file diff --git a/samples/Terra/malloc.t b/samples/Terra/malloc.t new file mode 100644 index 00000000..6672bfdd --- /dev/null +++ b/samples/Terra/malloc.t @@ -0,0 +1,23 @@ +local c = terralib.includecstring [[ + #include + #include +]] + +local N = 10 +terra foo() + c.printf("size = %d\n",int(sizeof(int))) + var list = [&int](c.malloc(sizeof(int)*N)) + for i = 0,N do + list[i] = i + 1 + end + var result = 0 + for i = 0,N do + c.printf("%d = %d\n",int(i),list[i]) + result = result + list[i] + end + c.free([&uint8](list)) + return result +end + +local test = require("test") +test.eq(foo(),N*(1 + N)/2) diff --git a/samples/Terra/mathlib.t b/samples/Terra/mathlib.t new file mode 100644 index 00000000..26af6cc8 --- /dev/null +++ b/samples/Terra/mathlib.t @@ -0,0 +1,9 @@ +local c = terralib.includec("math.h") + +terra mysqrt(a : float) + return c.sqrtf(a) +end + +local test = require("test") +test.eq(mysqrt(4),2) +test.eq(mysqrt(9),3) \ No newline at end of file diff --git a/samples/Terra/metatype.t b/samples/Terra/metatype.t new file mode 100644 index 00000000..bd8cdd64 --- /dev/null +++ b/samples/Terra/metatype.t @@ -0,0 +1,16 @@ +local function foo(T) + terra T:what() + return self.a + self.b + end +end +struct A(foo) { + a : int; + b : int; +} + +terra test() + var a = A {1,2} + return a:what() +end + +assert(test() == 3) \ No newline at end of file diff --git a/samples/Terra/method.t b/samples/Terra/method.t new file mode 100644 index 00000000..7e38fa45 --- /dev/null +++ b/samples/Terra/method.t @@ -0,0 +1,42 @@ +--the zero line +struct A { b : B } and +struct B {a : int, b : int} + +B.methods.foo = terra(b : B) + return b.a +end +terra bar() + var b = B { 1,2 } + return b:foo() +end + + +B.methods.foo2 = terra(b : &B) + b.a = 6 +end + +terra bar2() + var b = B { 1,2 } + b:foo2() + return b.a +end + +B.methods.foo3 = terra(b : B) + return b.a +end + +terra bar3() + var b = B { 1,2 } + return (&b):foo3() +end +terra bar4() + var b = B { 1,2 } + (&b):foo2() + return b.a +end + +test = require("test") +test.eq(bar(),1) +test.eq(bar2(),6) +test.eq(bar3(),1) +test.eq(bar4(),6) \ No newline at end of file diff --git a/samples/Terra/methodantiquote.t b/samples/Terra/methodantiquote.t new file mode 100644 index 00000000..6fbb79ad --- /dev/null +++ b/samples/Terra/methodantiquote.t @@ -0,0 +1,18 @@ +--the zero line +M = {} +struct M.B {a : int, b : int} + +terra M.B:foo(a : int) + return self.a + a +end + + +local avar = "foo" + +terra bar() + var b = M.B { 1,2 } + return b:[avar](3) +end + +test = require("test") +test.eq(bar(),4) diff --git a/samples/Terra/methodmissing.t b/samples/Terra/methodmissing.t new file mode 100644 index 00000000..b60cd5cf --- /dev/null +++ b/samples/Terra/methodmissing.t @@ -0,0 +1,17 @@ + + +struct A { + a : int +} + +A.metamethods.__methodmissing = macro(function(methodname,obj,anarg) + print(methodname) + return `anarg + [string.byte(methodname,1,1)] +end) + +terra foobar() + var a : A + return a:a(3) + a:b(4) +end + +assert(foobar() == 202) \ No newline at end of file diff --git a/samples/Terra/methodrvalue.t b/samples/Terra/methodrvalue.t new file mode 100644 index 00000000..13513b98 --- /dev/null +++ b/samples/Terra/methodrvalue.t @@ -0,0 +1,18 @@ +--the zero line +M = {} +struct M.B {a : int, b : int} + +terra M.B:foo(a : int) + return self.a + a +end + +terra rify() + var a = M.B { 1,2} + return a +end +terra bar() + return (rify()):foo(3) +end + +test = require("test") +test.eq(bar(),4) diff --git a/samples/Terra/methodsugar.t b/samples/Terra/methodsugar.t new file mode 100644 index 00000000..473f9521 --- /dev/null +++ b/samples/Terra/methodsugar.t @@ -0,0 +1,15 @@ +--the zero line +M = {} +struct M.B {a : int, b : int} + +terra M.B:foo(a : int) + return self.a + a +end + +terra bar() + var b = M.B { 1,2 } + return b:foo(3) +end + +test = require("test") +test.eq(bar(),4) diff --git a/samples/Terra/missingfields.t b/samples/Terra/missingfields.t new file mode 100644 index 00000000..acd09f2d --- /dev/null +++ b/samples/Terra/missingfields.t @@ -0,0 +1,14 @@ +test = terralib.includecstring([[ + typedef struct {} emptyanon; + typedef struct foo foobar; + void test(struct foo * f); + struct foo { int x; }; + union ufoo; + typedef union { int a; int b; } anonunion; +]]) +terra main() + var s : test.foo + s.x = 1 +end + +main() \ No newline at end of file diff --git a/samples/Terra/mixed.t b/samples/Terra/mixed.t new file mode 100644 index 00000000..267bf814 --- /dev/null +++ b/samples/Terra/mixed.t @@ -0,0 +1,13 @@ +a = terra() return 4 end +local d = {} +do + struct A{} + and local terra a() return 5 end + and terra d.d(a : B) end + and terra B:a() end + and struct B {} + and terra c() end + and struct C {} + assert(a() == 5) +end +assert(a() == 4) \ No newline at end of file diff --git a/samples/Terra/multiconstructor.t b/samples/Terra/multiconstructor.t new file mode 100644 index 00000000..b9bdd6d1 --- /dev/null +++ b/samples/Terra/multiconstructor.t @@ -0,0 +1,17 @@ + +terra foo() : {double, double} + return 1.0,3.0 +end + +struct A {c : int, a : int, b : double } + +terra bar() + var r = foo() + var a = A {1,unpackstruct(r)} + var b = A {1,2,(foo())._0} + var c = A {c = 1,a = 2,b = foo()._0} + return a.c + a.a + a.b + b.c + c.c +end + +local test = require("test") +test.eq(7,bar()) \ No newline at end of file diff --git a/samples/Terra/multimacro.t b/samples/Terra/multimacro.t new file mode 100644 index 00000000..b0d72374 --- /dev/null +++ b/samples/Terra/multimacro.t @@ -0,0 +1,17 @@ + +local bar = terralib.internalmacro(function(ctx,tree,typ,x) + return {terralib.newtree(typ.tree, { kind = terralib.kinds.literal, type = double, value = 4.0 }), x } +end) + +local x,y,z = 1,2,3 + +terra foo() : int + var a,b = bar(int,x + y + z) + var c = bar(int,0)._0 + 1 + --bar2(int) = bar2(int) + 5 + --bar3(up(&a),up(&a)) + return a + b + c +end + +local test = require("test") +test.eq(15,foo()) diff --git a/samples/Terra/multiterra.t b/samples/Terra/multiterra.t new file mode 100644 index 00000000..28a97b42 --- /dev/null +++ b/samples/Terra/multiterra.t @@ -0,0 +1,79 @@ +local ffi = require("ffi") + +C = terralib.includecstring [[ +#ifndef _WIN32 +#include +#endif +#include +typedef struct lua_State lua_State; +lua_State * luaL_newstate(); +int terra_init(lua_State * L); +int luaL_openlibs(lua_State * L); +int luaL_loadstring(lua_State * L, const char *); +int lua_close(lua_State * L); +void lua_call(lua_State * L, int,int); +int lua_pushnumber(lua_State * L, double); +int terra_loadstring(lua_State *L, const char *s); +double luaL_checknumber(lua_State * L,int); +]] + +if ffi.os == "Windows" then + -- fake it on windows + C.pthread_t = int + C.pthread_create = terra(t : &int, stuff : &opaque,fn : &opaque -> &opaque, data : &opaque) + return fn(data) + end + C.pthread_join = terra(t : int, stuff : &opaque) + end +end + + +N = 4 + +local acc = global(int[N]) + +terra forkedFn(args : &opaque) : &opaque + var threadid = @[&int](args) + C.printf("threadid %d\n",threadid) + + var L = C.luaL_newstate(); + if L == nil then + C.printf("can't initialize luajit\n") + end + + C.luaL_openlibs(L) + C.terra_init(L) + + C.terra_loadstring(L, [[ a = ...; C = terralib.includec("stdio.h"); terra foo () C.printf("new terra %d\n",a) return a end; return foo() ]]) + C.lua_pushnumber(L,threadid) + C.lua_call(L,1,1) + acc[threadid] = C.luaL_checknumber(L,-1) + C.lua_close(L) + + return nil +end + +terra foo() + var thread : C.pthread_t[N] + for i = 0,N do + acc[i] = -1 + end + + var args : int[N] + for i = 0,N do + args[i] = i + C.pthread_create(&thread[i],nil,forkedFn,&args[i]) + end + + for i = 0,N do + C.pthread_join(thread[i],nil) + end + var sum = 0 + for i = 0,N do + sum = sum + acc[i] + end + return sum +end + +print(foo()) +assert(foo() == N * (N - 1) / 2) \ No newline at end of file diff --git a/samples/Terra/names.t b/samples/Terra/names.t new file mode 100644 index 00000000..3bfd6187 --- /dev/null +++ b/samples/Terra/names.t @@ -0,0 +1,16 @@ +struct A { +} + +local B = (function() + local struct A {} + return A +end)() +C = terralib.types.newstruct("A$1") +D = terralib.types.newstruct("A$1") + +local names = {} +for i,t in ipairs {A,B,C,D} do + local n = tostring(t) + assert(not names[n]) + names[n] = true +end \ No newline at end of file diff --git a/samples/Terra/nestedcalls.t b/samples/Terra/nestedcalls.t new file mode 100644 index 00000000..d9410798 --- /dev/null +++ b/samples/Terra/nestedcalls.t @@ -0,0 +1,11 @@ + +terra foo3(a : &int) : &int + return a +end + +terra bar2(a : int) + return @(foo3(&a)) +end + +local test = require("test") +test.eq(bar2(42),42) \ No newline at end of file diff --git a/samples/Terra/nestextract.t b/samples/Terra/nestextract.t new file mode 100644 index 00000000..13a15db3 --- /dev/null +++ b/samples/Terra/nestextract.t @@ -0,0 +1,40 @@ + + +struct A { + a : int +} + +count = global(int,0) + +terra twoAs(a : int) + return A {a}, A { a } +end + +function A.metamethods.__cast(fromt,tot,exp) + if tot == A and fromt == int then + return `twoAs(exp)._0 + end + error("what") +end + +terra twoInts() + count = count + 1 + return count,2 +end + +terra takesAnA(a : A) + return a.a +end + +dotwice = macro(function(exp) + return {exp,exp} +end) + +terra doit() + return dotwice(takesAnA((twoInts()._0))) +end + + +doit:printpretty() +local test = require("test") +test.meq({1,2},doit()) diff --git a/samples/Terra/nestnoerror.t b/samples/Terra/nestnoerror.t new file mode 100644 index 00000000..c0a41500 --- /dev/null +++ b/samples/Terra/nestnoerror.t @@ -0,0 +1,17 @@ + + +terra haserror() + return (1):foo() +end + +local m = macro(function() + local success = pcall(function() haserror:compile() end) + assert(not success) + return 1 +end) + +terra noerror() + return m() +end + +assert(1 == noerror()) \ No newline at end of file diff --git a/samples/Terra/new.t b/samples/Terra/new.t new file mode 100644 index 00000000..0029c19d --- /dev/null +++ b/samples/Terra/new.t @@ -0,0 +1,14 @@ +local c = terralib.includec("stdlib.h") + +new = macro(function(typquote) + local typ = typquote:astype() + return `[&typ](c.malloc(sizeof(typ))) +end) + +local typ = int +terra doit() + var a : &int = new(int) + return a +end + +doit() \ No newline at end of file diff --git a/samples/Terra/nillocal.t b/samples/Terra/nillocal.t new file mode 100644 index 00000000..93c415c0 --- /dev/null +++ b/samples/Terra/nillocal.t @@ -0,0 +1,6 @@ + +a = 4 +local a = nil + +terra foo() return [assert(not a)] end +assert(foo() == true) \ No newline at end of file diff --git a/samples/Terra/niltype.t b/samples/Terra/niltype.t new file mode 100644 index 00000000..b8b0000d --- /dev/null +++ b/samples/Terra/niltype.t @@ -0,0 +1,9 @@ + +terra foo() + var a : &int = nil + var b = nil + return a == b +end + +local test = require("test") +test.eq(foo(),true) \ No newline at end of file diff --git a/samples/Terra/nojit.t b/samples/Terra/nojit.t new file mode 100644 index 00000000..46b66331 --- /dev/null +++ b/samples/Terra/nojit.t @@ -0,0 +1,20 @@ + + +--var a = 4 + 5 +a = global(4 + 5) +terra foo() + return a +end + + +foo:gettype() +--we currently don't track what initializers need to be run after a nojit compile +--hopefully we can just remove the need to have nojit entirely +--otherwise we need to seperate the calling of variable initializers from the compilation process +--so that they can be called when jit is invoked +a:gettype() + +local test = require("test") +test.eq(foo(),9) + + diff --git a/samples/Terra/nolengthop.t b/samples/Terra/nolengthop.t new file mode 100644 index 00000000..ceac4581 --- /dev/null +++ b/samples/Terra/nolengthop.t @@ -0,0 +1,2 @@ +local a,b = terralib.loadstring([[terra g() return #a]]) +assert(b:find("operator not supported")) \ No newline at end of file diff --git a/samples/Terra/nonprototypec.t b/samples/Terra/nonprototypec.t new file mode 100644 index 00000000..36256bb6 --- /dev/null +++ b/samples/Terra/nonprototypec.t @@ -0,0 +1,12 @@ +C = terralib.includecstring [[ + int foobar() { + return 3; + } +]] + + +terra doit() + return C.foobar() +end + +print(doit()) \ No newline at end of file diff --git a/samples/Terra/nontemporal.t b/samples/Terra/nontemporal.t new file mode 100644 index 00000000..2e634783 --- /dev/null +++ b/samples/Terra/nontemporal.t @@ -0,0 +1,7 @@ + + +terra foobar(a : &vector(float,4),b : vector(int,4)) + terralib.attrstore(a,b,{ nontemporal = true }) +end + +foobar:disas() diff --git a/samples/Terra/numliteral.t b/samples/Terra/numliteral.t new file mode 100644 index 00000000..9d5183ec --- /dev/null +++ b/samples/Terra/numliteral.t @@ -0,0 +1,14 @@ + +terra thetest() + var a,b,c,d,e = 3,3.0,3.f,3LL, 3ULL + return a,b,c,d,e +end + +local exp = { "int32", "double", "float", "int64", "uint64" } + +local test = require("test") +thetest:compile() +local typ = thetest.definitions[1]:gettype() +for i,e in ipairs(typ.returntype:getentries()) do + test.eq(tostring(e.type),exp[i]) +end \ No newline at end of file diff --git a/samples/Terra/objc.t b/samples/Terra/objc.t new file mode 100644 index 00000000..134f5a48 --- /dev/null +++ b/samples/Terra/objc.t @@ -0,0 +1,25 @@ +local f = assert(io.popen("uname", 'r')) +local s = assert(f:read('*a')) +f:close() + +if s~="Darwin\n" then + print("Warning, not running test b/c this isn't a mac") +else + +local OC = require("lib/objc") + +terra main() + OC.NSAutoreleasePool:new() + var str = OC.NSString:stringWithUTF8String("the number of hacks is overwhelming...") + var err = OC.NSError:errorWithDomain_code_userInfo(str,12,nil) + var alert = OC.NSAlert:alertWithError(err) + + var imgname = OC.NSString:stringWithUTF8String("foo.png") + var img = OC.NSImage:alloc():initByReferencingFile(imgname) + alert:setIcon(img) + alert:runModal() +end + +terralib.saveobj("objc",{main = main}, { "-framework", "Foundation", "-framework", "Cocoa" }) + +end \ No newline at end of file diff --git a/samples/Terra/objc2.t b/samples/Terra/objc2.t new file mode 100644 index 00000000..d92e1bde --- /dev/null +++ b/samples/Terra/objc2.t @@ -0,0 +1,42 @@ +local f = assert(io.popen("uname", 'r')) +local s = assert(f:read('*a')) +f:close() + +if s~="Darwin\n" then + print("Warning, not running test b/c this isn't a mac") +else + + +local OC = require("lib/objc") +local IO = terralib.includec("stdio.h") + +struct Rect { + a : double, + b : double, + c : double, + d : double +} + +terra str(data : &uint8) + return OC.NSString:stringWithUTF8String(data) +end + +terra main() + OC.NSAutoreleasePool:new() + var app = OC.NSApplication:sharedApplication() + var rec = Rect {0,0,200,200} + var window = OC.NSWindow:alloc():initWithContentRect_styleMask_backing_defer(rec,1,2,false) + window:makeKeyAndOrderFront(nil) + --[[var img = OC.NSImage:alloc():initByReferencingFile(str("objc2.jpg")) + var imgView = OC.NSImageView:alloc():initWithFrame(rec) + imgView:setImage(img) + window:setContentView(imgView) + IO.printf("entering run loop\n")--]] + app:run() +end + +terralib.saveobj("objc2", {main = main}, {"-framework","Cocoa"}) + +--os.execute("./objc2") + +end \ No newline at end of file diff --git a/samples/Terra/objtest.t b/samples/Terra/objtest.t new file mode 100644 index 00000000..f429f0fe --- /dev/null +++ b/samples/Terra/objtest.t @@ -0,0 +1,45 @@ +C = terralib.includec("stdio.h") +--the zero line +struct A { a : int } + +terra A:foo1() + self.a = self.a + 1 + return self.a +end + +terra A.methods.foo2(self : &A) + self.a = self.a + 1 + return self.a +end + +terra A.methods.foo3(self : A) + self.a = self.a + 1 + return self.a +end + +terra bar() + var a = A { 0 } + var ptra = &a + + var v0 = a.a + var v1 = a:foo1() + var v2 = a.a + var v3 = a:foo2() + var v4 = a.a + var v5 = a:foo3() + var v6 = a.a + + ptra.a = 0 + var p0 = ptra.a + var p1 = ptra:foo1() + var p2 = ptra.a + var p3 = ptra:foo2() + var p4 = ptra.a + var p5 = ptra:foo3() + var p6 = ptra.a + + return v0,v1,v2,v3,v4,v5,v6,p0,p1,p2,p3,p4,p5,p6 +end + +test = require("test") +test.meq({0,1,1,2,2,3,2,0,1,1,2,2,3,2},bar()) \ No newline at end of file diff --git a/samples/Terra/offsetcalc.t b/samples/Terra/offsetcalc.t new file mode 100644 index 00000000..ec6b0c9b --- /dev/null +++ b/samples/Terra/offsetcalc.t @@ -0,0 +1,33 @@ +local offsetinbytescache = {} +function offsetinbytes(structtype,key) + local typetable = offsetinbytescache[structtype] or {} + local value = typetable[key] + if value then + return value + end + offsetinbytescache[structtype] = typetable + + local terra offsetcalc() : int + var a : &structtype = [&structtype](0) + return [&int8](&a.[key]) - [&int8](a) + end + + local result = offsetcalc() + + typetable[key] = result + return result +end + + +struct A { a : int8, c : int8, b : int } + + +terra foo() + return 4LL +end + +local test = require("test") + +test.eq(offsetinbytes(A,"b"),4) + + diff --git a/samples/Terra/opaquealloc.t b/samples/Terra/opaquealloc.t new file mode 100644 index 00000000..91f07604 --- /dev/null +++ b/samples/Terra/opaquealloc.t @@ -0,0 +1,19 @@ + + + +local r,e = pcall(function() + local struct A { + a : opaque + } + local v = terralib.new(A) +end) + +assert(not r and e:match("Errors reported during")) + + +local r,e = pcall(function() + local v = terralib.new(opaque) +end) + + +assert(not r and e:match("attempting to use an opaque type")) \ No newline at end of file diff --git a/samples/Terra/or.t b/samples/Terra/or.t new file mode 100644 index 00000000..98a95761 --- /dev/null +++ b/samples/Terra/or.t @@ -0,0 +1,9 @@ +local test = require("test") + +terra foo(a : double, b : double, c : double) : bool + return a < b or b < c +end + +test.eq(foo(1,2,1),true) +test.eq(foo(2,1,2),true) +test.eq(foo(3,2,1),false) \ No newline at end of file diff --git a/samples/Terra/ordercomplete.t b/samples/Terra/ordercomplete.t new file mode 100644 index 00000000..eb451822 --- /dev/null +++ b/samples/Terra/ordercomplete.t @@ -0,0 +1,25 @@ + + +struct A { + a : int +} + +struct B { + a : A +} + +function A.metamethods.__staticinitialize() + print("STATIC INIT A") + local terra what(b : B) + end + what:gettype(true) +end + +function B.metamethods.__staticinitialize() + print("STATIC INIT B") +end + +terra foo(b : B) +end + +foo:compile() \ No newline at end of file diff --git a/samples/Terra/output.t b/samples/Terra/output.t new file mode 100644 index 00000000..bb2b17b8 --- /dev/null +++ b/samples/Terra/output.t @@ -0,0 +1,16 @@ +C = terralib.includec("stdio.h") +terra main() + C.printf("hello world\n") +end +local m = { main = main } +terralib.saveobj("output.o",m) +local a = terralib.saveobj(nil,"object",m) +terralib.saveobj("output2.bc",m) +local b = terralib.saveobj(nil,"bitcode",m) +terralib.saveobj("output.ll",m) +local c = terralib.saveobj(nil,"llvmir",m) +terralib.saveobj("output",m) +terralib.saveobj("output2","executable",m) + +assert(a:match("hello world")) +assert(c:match("hello world")) \ No newline at end of file diff --git a/samples/Terra/overload.t b/samples/Terra/overload.t new file mode 100644 index 00000000..57483413 --- /dev/null +++ b/samples/Terra/overload.t @@ -0,0 +1,17 @@ + +terra foo(a : int) + return 1 +end + +terra foo(a : &int8) + return 2 +end + +print(#foo.definitions) + +terra doit() + return foo(1) + foo("what") +end + +local test = require("test") +test.eq(doit(),3) \ No newline at end of file diff --git a/samples/Terra/overload2.t b/samples/Terra/overload2.t new file mode 100644 index 00000000..417d5aab --- /dev/null +++ b/samples/Terra/overload2.t @@ -0,0 +1,15 @@ + +terra foo(a : int) + return 1 +end + +terra foo(a : double) + return 2 +end + +terra doit() + return foo(2.5) +end + +local test = require("test") +test.eq(doit(),2) \ No newline at end of file diff --git a/samples/Terra/overload3.t b/samples/Terra/overload3.t new file mode 100644 index 00000000..93a516c6 --- /dev/null +++ b/samples/Terra/overload3.t @@ -0,0 +1,15 @@ + +terra foo(a : {int} ) + return 1 +end + +terra foo(a : {int,int} ) + return 2 +end + +terra doit() + return foo({1,2}) + foo({1,2}) +end + +local test = require("test") +test.eq(doit(),4) \ No newline at end of file diff --git a/samples/Terra/overloadcall.t b/samples/Terra/overloadcall.t new file mode 100644 index 00000000..343deb84 --- /dev/null +++ b/samples/Terra/overloadcall.t @@ -0,0 +1,12 @@ + + +terra foo(a : int) + return a +end + +terra foo(a : int, b : int) + return a + b +end + +local test = require("test") +test.eq(foo(1) + foo(3,4), 8) \ No newline at end of file diff --git a/samples/Terra/overloadmethod.t b/samples/Terra/overloadmethod.t new file mode 100644 index 00000000..226842b8 --- /dev/null +++ b/samples/Terra/overloadmethod.t @@ -0,0 +1,24 @@ + +struct A { a : int } +terra A:foo(a : int) + return self.a + a +end + +terra A:foo(a : &int8) + return self.a +end + + +terra doit() + var a = A { 3 } + return a:foo(1) + a:foo("what") +end +terra doit2() + var a = A { 3 } + var pa = &a + return pa:foo(1) + pa:foo("what") +end + +local test = require("test") +test.eq(doit(),7) +test.eq(doit2(),7) \ No newline at end of file diff --git a/samples/Terra/overloadmethod2.t b/samples/Terra/overloadmethod2.t new file mode 100644 index 00000000..f3c411ec --- /dev/null +++ b/samples/Terra/overloadmethod2.t @@ -0,0 +1,17 @@ + +struct A { a : int } +A.methods.foo = terra(self : A, a : int) + return self.a + a +end + +terra A:foo() + return self.a +end + + +terra doit() + var a = A { 3 } + return a:foo() + a:foo(1) +end +local test = require("test") +test.eq(doit(),7) \ No newline at end of file diff --git a/samples/Terra/overloadmethod3.t b/samples/Terra/overloadmethod3.t new file mode 100644 index 00000000..87290ade --- /dev/null +++ b/samples/Terra/overloadmethod3.t @@ -0,0 +1,18 @@ + +struct A { a : int } +A.methods.foo = terra(self : A) + return 2 +end + +terra A:foo() + return 1 +end + + +terra doit() + var a = A { 3 } + var pa = &a + return a:foo() + pa:foo() +end +local test = require("test") +test.eq(doit(),4) \ No newline at end of file diff --git a/samples/Terra/overloadproduct.t b/samples/Terra/overloadproduct.t new file mode 100644 index 00000000..85230e29 --- /dev/null +++ b/samples/Terra/overloadproduct.t @@ -0,0 +1,65 @@ + +local ans = { +{1,1,1,1}; +{2,2,2,2}; +{1,2,1,2}; +{3,3,3,3}; +{1,1,3,3}; +{0,2,3,0}; +{1,2,3,0}; +{4,4,4,4}; +{1,0,0,4}; +{2,2,4,4}; +{1,2,0,4}; +{3,4,3,4}; +{1,0,3,4}; +{0,2,3,4}; +{1,2,3,4}; +} +function create(foo,a,b,c,d) + if a then + terra foo(a : int, b : int) + return 1 + end + end + if b then + terra foo(a : int, b : double) + return 2 + end + end + if c then + terra foo(a : double, b : int) + return 3 + end + end + if d then + terra foo(a : double, b : double) + return 4 + end + end +end + +local function bitset(i,b) + return bit.band(bit.rshift(i,b),1) == 1 +end + +for i = 1,15 do + local terra foo + local a,b,c,d = bitset(i,0),bitset(i,1),bitset(i,2),bitset(i,3) + create(foo, a,b,c,d) + local function trycall(arg1,arg2) + local r = 0 + pcall(function() + local terra testit() + var a : arg1, b : arg2 = 0,0 + return foo(a,b) + end + r = testit() + end) + return r + end + local r = {trycall(int,int),trycall(int,double),trycall(double,int),trycall(double,double)} + for j,rr in ipairs(r) do + assert(rr == ans[i][j]) + end +end \ No newline at end of file diff --git a/samples/Terra/overloadrecv.t b/samples/Terra/overloadrecv.t new file mode 100644 index 00000000..a07a8bbf --- /dev/null +++ b/samples/Terra/overloadrecv.t @@ -0,0 +1,20 @@ + + +struct A { + a : int +} + +terra A:foo(a : int, b : uint8) + return 1 +end +terra A:foo(a : double, b : uint8) + return 2 +end + +terra useit() + var a = A { 3 } + var pa = &a + return a:foo(1,1) + a:foo(1.1,1) + pa:foo(1,1) + pa:foo(1.1,1) +end + +assert(6 == useit()) \ No newline at end of file diff --git a/samples/Terra/painfulrecstruct.t b/samples/Terra/painfulrecstruct.t new file mode 100644 index 00000000..1b2026b6 --- /dev/null +++ b/samples/Terra/painfulrecstruct.t @@ -0,0 +1,22 @@ + + +struct A { + a : A -> int; + b : int +} + +terra foo(a : A) + return a.b +end + +terra callit(a : A) + return a.a(a) +end + +terra bar() + var a = A { foo, 3 } + return callit(a) +end + +local test = require("test") +test.eq(bar(),3) \ No newline at end of file diff --git a/samples/Terra/paren.t b/samples/Terra/paren.t new file mode 100644 index 00000000..bcd40960 --- /dev/null +++ b/samples/Terra/paren.t @@ -0,0 +1,5 @@ +terra foo(a : int) + return a + (a) +end + +foo(3) \ No newline at end of file diff --git a/samples/Terra/parsecrash.t b/samples/Terra/parsecrash.t new file mode 100644 index 00000000..1ce8c06d --- /dev/null +++ b/samples/Terra/parsecrash.t @@ -0,0 +1,7 @@ +local result,err = terralib.loadstring [[ +terra result() + [startXNeeded] = a + strip*L.stripWidth + [endXNeeded] = 1 +end +]] +assert(result ~= nil) \ No newline at end of file diff --git a/samples/Terra/parsefail.t b/samples/Terra/parsefail.t new file mode 100644 index 00000000..491ce0f2 --- /dev/null +++ b/samples/Terra/parsefail.t @@ -0,0 +1,35 @@ + +local self = 1 +local Rt = 1 +local i = 1 +local j = 1 +terra bar() +var a = Rt.MatrixDouble{[&double](self.ptr) , i,j} +end + +local r,e = terralib.loadstring[[ + + terra foo() + var a = { [&double](4) = 3 } + end +]] + +assert(r == nil and e:match("unexpected symbol near '='")) + +terra foo() + var a = { [""] = 3 } +end + +local s = symbol() + +local function getsym() + return s +end +terra foo2() + var [getsym()] = 3 + var a = { [getsym()] = 4, _1 = [getsym()] } + return a.[getsym()] + a._1 +end + +assert(7 == foo2()) + diff --git a/samples/Terra/parsefail2.t b/samples/Terra/parsefail2.t new file mode 100644 index 00000000..74119074 --- /dev/null +++ b/samples/Terra/parsefail2.t @@ -0,0 +1,5 @@ +terra foo() + var a : int[4] @&a[0] = 3 + return a[0] +end +assert(3 == foo()) \ No newline at end of file diff --git a/samples/Terra/pattern.t b/samples/Terra/pattern.t new file mode 100644 index 00000000..dfb23037 --- /dev/null +++ b/samples/Terra/pattern.t @@ -0,0 +1,43 @@ + +terra foobar() + return 1,2 +end + +terra what() + var _,a,b = 1,foobar() + a,b = foobar() + return a + b +end +terra what2() + var a = foobar() + var b,c = unpackstruct(a) + return b+c +end + +assert(what() == 3) +assert(what2() == 3) + + +struct A { + a : int + b : int +} + +terra what3() : A + var a = A {1,2} + return unpacktuple(a) +end +assert(what3().b == 2) + +terra what4() + var a = A {1,2} + var c,d = unpackstruct(a) + return c+d +end +assert(what4() == 3) + +local terra ra() return A {1,2} end +local a,b = unpackstruct(foobar()) +assert(a == 1 and b == 2) +assert(unpacktuple(ra()).a == 1) +assert(unpackstruct(ra()) == 1) \ No newline at end of file diff --git a/samples/Terra/point.t b/samples/Terra/point.t new file mode 100644 index 00000000..088e3702 --- /dev/null +++ b/samples/Terra/point.t @@ -0,0 +1,13 @@ + +terra bar(a : &int) + @a = @a + 1 +end +terra foo(a : int) : int + var b : int + b = a + bar(&b) + return b +end + +local test = require("test") +test.eq(foo(4),5) \ No newline at end of file diff --git a/samples/Terra/pointerarith.t b/samples/Terra/pointerarith.t new file mode 100644 index 00000000..d59c9b59 --- /dev/null +++ b/samples/Terra/pointerarith.t @@ -0,0 +1,31 @@ +terra foo(a : &int) : ptrdiff + var b : &int b = a + 10 + return b-a +end + +terra bar() + var b:int = 10; + return foo(&b) +end + +terra foo2(a : &int) : &int + a = a + 6 + a = 3 + a + a = a + 10 + return a +end + +terra foo3(a : &int) : &int + a = a - 11 + a = 2 + a + a = a - 10 + return a +end + +terra bar2(a : int) + return @(foo3(foo2(&a))) +end + +local test = require("test") +test.eq(bar(),10) +test.eq(bar2(42),42) diff --git a/samples/Terra/pointerlike.t b/samples/Terra/pointerlike.t new file mode 100644 index 00000000..2d3d8542 --- /dev/null +++ b/samples/Terra/pointerlike.t @@ -0,0 +1,7 @@ + +terra foo() + var a : int[4] + var b : &int = a + return a + 1, a - a, b - a, a - b, 1 + a +end +foo() \ No newline at end of file diff --git a/samples/Terra/pow.t b/samples/Terra/pow.t new file mode 100644 index 00000000..701c8247 --- /dev/null +++ b/samples/Terra/pow.t @@ -0,0 +1,28 @@ +local N = 4 -- N is a Lua variable +terra powN(a : double) + var r = 1 + for i = 0, N do + r = r * a + end + return r +end + + + + +local math = {} +for N = 1,10 do + math["pow"..tostring(N)] = terra(a : double) + var r = 1 + for i = 0, N do + r = r * a + end + return r + end +end + +local test = require("test") +test.eq(powN(3),81) +test.eq(math.pow1(2),2) +test.eq(math.pow2(2),4) +test.eq(math.pow3(2),8) \ No newline at end of file diff --git a/samples/Terra/ppltalk.t b/samples/Terra/ppltalk.t new file mode 100644 index 00000000..d9e29868 --- /dev/null +++ b/samples/Terra/ppltalk.t @@ -0,0 +1,18 @@ + +--this is a comment. +--top level is Lua code: +function min(a,b) + if a < b then return a + else return b end +end + +print(min(3,4)) --3 + +terra mint(a : int, b : int) : int +if a < b then return a +else return b end +end + +print(mint(3,4)) --3 + +mint:disas() \ No newline at end of file diff --git a/samples/Terra/ppnil.t b/samples/Terra/ppnil.t new file mode 100644 index 00000000..93e6e561 --- /dev/null +++ b/samples/Terra/ppnil.t @@ -0,0 +1,4 @@ +terra foo() + return nil +end +foo:printpretty() \ No newline at end of file diff --git a/samples/Terra/pratttest1.t b/samples/Terra/pratttest1.t new file mode 100755 index 00000000..95b9f739 --- /dev/null +++ b/samples/Terra/pratttest1.t @@ -0,0 +1,4 @@ +import "lib/pratttest" +local a = goexp 1 + 3 * 4 ^ 5 ^ 6/(2 - 4 + -a) + -b(c) + +terralib.tree.printraw(a) diff --git a/samples/Terra/prec.t b/samples/Terra/prec.t new file mode 100644 index 00000000..b6afe988 --- /dev/null +++ b/samples/Terra/prec.t @@ -0,0 +1,4 @@ +local a = &double -> double +assert(a:ispointer()) +assert(a.type:isfunction()) +assert(a.type.parameters[1]:ispointer()) \ No newline at end of file diff --git a/samples/Terra/prec2.t b/samples/Terra/prec2.t new file mode 100644 index 00000000..99a8974b --- /dev/null +++ b/samples/Terra/prec2.t @@ -0,0 +1,2 @@ +terra foo(a : float, b : float, c : float, d : float) return a * (b - c) end +foo:printpretty() \ No newline at end of file diff --git a/samples/Terra/pretty.t b/samples/Terra/pretty.t new file mode 100644 index 00000000..3953c97a --- /dev/null +++ b/samples/Terra/pretty.t @@ -0,0 +1,69 @@ + + +terra bar(a : int) + return 4,5 +end +terra takebar(a : double, b : int) + return 1 +end +terra baz() +end +function whatwhat() +end +struct A { data : int } +terra A:moo() end +Aptr = terralib.new(A) +terra foo() + var aa : A + + baz() + whatwhat() + do + end + ::what:: + goto what + while 4 < 3 do + break + terralib.attrload(&aa,{}) + var d = terralib.attrstore(&aa,aa,{}) + return 3,4,4,bar(aa.data) + end + var a = 0.0 + if a < 3 then + a = -(a + 1) + end + + if a < 3 then + a = -a + 1 + elseif a > 4 then + a = a - 1 + end + repeat + a = a + 1 + until a > 55 + var b,c = 4,5 + a,b = 5,c + var d = array(1,2,3) + b = (&a)[1] + var e : int = a + var g = terralib.select(true,0,1) + var ee = sizeof(int) + var more = { a = 5, c = 4, _2 = 3} + baz() + var bbb = bar(1) + var bb = takebar(bbb._0,bbb._1) + var vv = vector(3,4) + var vvv = Aptr + var vvvv = Aptr:moo() + return 3,4,ee,bar(1) +end + +foo:compile() +foo:printpretty(false) --before compilation +foo:printpretty() --after compilation + +local a = `1 + 2 +local b = quote var c = a + a in a + 1 end + +a:printpretty() +b:printpretty() \ No newline at end of file diff --git a/samples/Terra/printd.t b/samples/Terra/printd.t new file mode 100644 index 00000000..d9a9f85d --- /dev/null +++ b/samples/Terra/printd.t @@ -0,0 +1,5 @@ +a = .4 + +terra foo() return a end + +foo:printpretty(false) \ No newline at end of file diff --git a/samples/Terra/printfarray.t b/samples/Terra/printfarray.t new file mode 100644 index 00000000..98c21bef --- /dev/null +++ b/samples/Terra/printfarray.t @@ -0,0 +1,12 @@ + + +C = terralib.includec("stdio.h") + +terra char(a : &int8) : int8 + return a[0] +end +terra foobar() + var a = arrayof(int8,char("a"),0) + C.printf("%s\n",a) +end +foobar() \ No newline at end of file diff --git a/samples/Terra/printfloat.t b/samples/Terra/printfloat.t new file mode 100644 index 00000000..37927f54 --- /dev/null +++ b/samples/Terra/printfloat.t @@ -0,0 +1,7 @@ +local IO = terralib.includec("stdio.h") + +terra foobar() + IO.printf("%f\n",3.3f) +end + +foobar() \ No newline at end of file diff --git a/samples/Terra/proxystruct.t b/samples/Terra/proxystruct.t new file mode 100644 index 00000000..bde53457 --- /dev/null +++ b/samples/Terra/proxystruct.t @@ -0,0 +1,19 @@ +local c = terralib.includecstring [[ + #include + #include +]] + + +struct Node { + next : &Node; + v : int; +} + +terra foo() + var cur : &Node = [&Node](c.malloc(sizeof(Node))) + cur.v = 3 + return cur.v +end + +local test = require("test") +test.eq(foo(),3) diff --git a/samples/Terra/pt.t b/samples/Terra/pt.t new file mode 100644 index 00000000..6212d290 --- /dev/null +++ b/samples/Terra/pt.t @@ -0,0 +1,18 @@ + + +local a = global(double) + +terra getptr() + return &a +end + +local b = getptr() +local c = terralib.pointertolightuserdata(b) +print(b,c,a) + +terra foo(a : &&int) + +end + +foo(c) +print("DONE") \ No newline at end of file diff --git a/samples/Terra/pthreads.t b/samples/Terra/pthreads.t new file mode 100644 index 00000000..b903dcdb --- /dev/null +++ b/samples/Terra/pthreads.t @@ -0,0 +1,47 @@ +local ffi = require("ffi") +if ffi.os == "Windows" then + return +end + +C = terralib.includecstring [[ +#include +#include +]] + +acc = global(int[4]) + +terra forkedFn(args : &opaque) : &opaque + var threadid = @[&int](args) + C.printf("threadid %d\n",threadid) + acc[threadid] = threadid + return nil +end + +terra foo() + var thread0 : C.pthread_t + var thread1 : C.pthread_t + var thread2 : C.pthread_t + var thread3 : C.pthread_t + + acc[0]=-42 + acc[1]=-42 + acc[2]=-42 + acc[3]=-42 + + var args = arrayof(int,0,1,2,3) + + C.pthread_create(&thread0,nil,forkedFn,&args[0]) + C.pthread_create(&thread1,nil,forkedFn,&args[1]) + C.pthread_create(&thread2,nil,forkedFn,&args[2]) + C.pthread_create(&thread3,nil,forkedFn,&args[3]) + + C.pthread_join(thread0,nil) + C.pthread_join(thread1,nil) + C.pthread_join(thread2,nil) + C.pthread_join(thread3,nil) + + return acc[0]+acc[1]+acc[2]+acc[3] +end + +local test = require("test") +test.eq(foo(),0+1+2+3) \ No newline at end of file diff --git a/samples/Terra/quote.t b/samples/Terra/quote.t new file mode 100644 index 00000000..6110380e --- /dev/null +++ b/samples/Terra/quote.t @@ -0,0 +1,10 @@ + +local c = `10 +local a = `4 + c +terra doit() + var c = 3 + return a + a +end + +local test = require("test") +test.eq(doit(),28) \ No newline at end of file diff --git a/samples/Terra/quote10.t b/samples/Terra/quote10.t new file mode 100644 index 00000000..f32e9e22 --- /dev/null +++ b/samples/Terra/quote10.t @@ -0,0 +1,13 @@ + +terra foo() + return 1,2 +end + +local q = `foo() + +terra bar() + return q +end + +local test = require("test") +test.meq({1,2},bar()) \ No newline at end of file diff --git a/samples/Terra/quote2.t b/samples/Terra/quote2.t new file mode 100644 index 00000000..378510a3 --- /dev/null +++ b/samples/Terra/quote2.t @@ -0,0 +1,9 @@ + +local c = `10 +local a = `4 + c +terra doit() + return a +end + +local test = require("test") +test.eq(doit(),14) \ No newline at end of file diff --git a/samples/Terra/quote3.t b/samples/Terra/quote3.t new file mode 100644 index 00000000..9f24b51f --- /dev/null +++ b/samples/Terra/quote3.t @@ -0,0 +1,13 @@ +c = global(0) +terra count() + c = c + 1 + return c +end +foo = macro(function(a,b) return `a + a + a + b + count() end) +terra doit() + var a = -100 + return foo(count(),4) +end + +local test = require("test") +test.eq(doit(),14) \ No newline at end of file diff --git a/samples/Terra/quote4.t b/samples/Terra/quote4.t new file mode 100644 index 00000000..29f5150f --- /dev/null +++ b/samples/Terra/quote4.t @@ -0,0 +1,9 @@ + +local str2 = `{a = 4} +local str = `str2 +terra doit() + return str.a +end + +local test = require("test") +test.eq(doit(),4) \ No newline at end of file diff --git a/samples/Terra/quote5.t b/samples/Terra/quote5.t new file mode 100644 index 00000000..23539bc4 --- /dev/null +++ b/samples/Terra/quote5.t @@ -0,0 +1,20 @@ + +c = global(0) +terra foo(a : int) + c = c + a +end + +local stmts = quote + foo(5) + foo(6) + foo(7) +end + +local stmts2 = {stmts,stmts} +terra doit() + stmts + stmts2 + return c +end +local test = require("test") +test.eq(doit(),54) \ No newline at end of file diff --git a/samples/Terra/quote6.t b/samples/Terra/quote6.t new file mode 100644 index 00000000..59a2b83f --- /dev/null +++ b/samples/Terra/quote6.t @@ -0,0 +1,12 @@ +function mymacro() + return {`4,`5} +end +mymacro = macro(mymacro) + +local exps = {`2,`3, `mymacro()} + +terra doit() + return exps +end +local test = require("test") +test.meq({2,3,4,5},doit()) \ No newline at end of file diff --git a/samples/Terra/quote7.t b/samples/Terra/quote7.t new file mode 100644 index 00000000..cbd4cb3b --- /dev/null +++ b/samples/Terra/quote7.t @@ -0,0 +1,23 @@ +a = {} + +local c = terralib.includec("stdio.h") + + +a.c = quote + c.printf("hello\n") +end + +a.b = quote + var d = 4 + a.c + return d +end + + +terra foo() + a.c + a.b +end + +local test = require("test") +test.eq(foo(),4) \ No newline at end of file diff --git a/samples/Terra/quote8.t b/samples/Terra/quote8.t new file mode 100644 index 00000000..afec0869 --- /dev/null +++ b/samples/Terra/quote8.t @@ -0,0 +1,19 @@ +a = {} + +local c = terralib.includec("stdio.h") + + +a.c = {`1,`2,`3} + +a.b = quote + return a.c,a.c +end + + +terra foo() + a.b +end + + +local test = require("test") +test.meq({1,2,3,1,2,3},foo()) diff --git a/samples/Terra/quote9.t b/samples/Terra/quote9.t new file mode 100644 index 00000000..834a4afd --- /dev/null +++ b/samples/Terra/quote9.t @@ -0,0 +1,19 @@ +a = {} + +local c = terralib.includec("stdio.h") + + +a.c = {`1,`2,`3} + +a.b = quote + return a.c._0,(a.c)._0 +end + + +terra foo() + a.b +end + + +local test = require("test") +test.meq({1,1}, foo()) \ No newline at end of file diff --git a/samples/Terra/quoteblock.t b/samples/Terra/quoteblock.t new file mode 100644 index 00000000..3833dbbf --- /dev/null +++ b/samples/Terra/quoteblock.t @@ -0,0 +1,12 @@ + +local a = symbol() +local myquote = quote + var [a] = 3 +end + +terra bar() + [myquote]; + return [a] +end + +print(bar()) \ No newline at end of file diff --git a/samples/Terra/quoteenv.t b/samples/Terra/quoteenv.t new file mode 100644 index 00000000..607631a1 --- /dev/null +++ b/samples/Terra/quoteenv.t @@ -0,0 +1,25 @@ +--quotes, unlike functions bind symbols eagerly +--function have :compile() to bind their symbols, but no similar thing exists +--for quotes, making it hard to control +--furthermore there aren't many use cases for late-binding quotes, +--but a bunch exist for needing early-bound quotes, like below: + + +function times5(x) + local c = `0 + for i = 1,5 do + c = `c + x + end + return c +end +times5 = macro(times5) + +terra foo() + var a = 3 + return times5(a) +end + +foo:printpretty() +foo:disas() +local test = require("test") +test.eq(foo(),15) diff --git a/samples/Terra/quoteselect.t b/samples/Terra/quoteselect.t new file mode 100644 index 00000000..46864e34 --- /dev/null +++ b/samples/Terra/quoteselect.t @@ -0,0 +1,12 @@ + +local a = {} + +local c = terralib.includec("stdio.h") + +b = `c.printf("hello\n") + +terra foo() + return b +end + +foo() \ No newline at end of file diff --git a/samples/Terra/rd.t b/samples/Terra/rd.t new file mode 100644 index 00000000..c80487fc --- /dev/null +++ b/samples/Terra/rd.t @@ -0,0 +1 @@ +C = terralib.includec("stdarg.h") \ No newline at end of file diff --git a/samples/Terra/receivercasts.t b/samples/Terra/receivercasts.t new file mode 100644 index 00000000..384c7bce --- /dev/null +++ b/samples/Terra/receivercasts.t @@ -0,0 +1,22 @@ + +struct A { a : int } + +terra A.methods.foo(a : int) + return a + 1 +end + +function A.metamethods.__cast(from,to,exp) + if from == A and to == int then + return `exp.a + end + error("what") +end + + + +terra testit() + var a = A { 5 } + return a:foo() +end + +assert(6 == testit()) \ No newline at end of file diff --git a/samples/Terra/recfn.t b/samples/Terra/recfn.t new file mode 100644 index 00000000..3a0d757f --- /dev/null +++ b/samples/Terra/recfn.t @@ -0,0 +1,10 @@ +local terra foo() : {} + bar() +end and +local terra bar() : {} + foo() +end + +bar = nil + +foo:printpretty() \ No newline at end of file diff --git a/samples/Terra/recoverfromerror.t b/samples/Terra/recoverfromerror.t new file mode 100644 index 00000000..92ef01a5 --- /dev/null +++ b/samples/Terra/recoverfromerror.t @@ -0,0 +1,15 @@ + + +terra what() + return " " / 1 +end + +dostuff = macro(function() + pcall(what.compile,what) + return 4 +end) + +terra foobar() + return dostuff() +end +assert(4 == foobar()) \ No newline at end of file diff --git a/samples/Terra/recstruct.t b/samples/Terra/recstruct.t new file mode 100644 index 00000000..dff75679 --- /dev/null +++ b/samples/Terra/recstruct.t @@ -0,0 +1,33 @@ +local c = terralib.includecstring [[ + #include + #include +]] + + +struct Node { + next : &Node; + v : int; +} + +local N = 10 +terra foo() + var cur : &Node = nil + for i = 0, N do + var n = [&Node](c.malloc(sizeof(Node))) + n.v = i + n.next = cur + cur = n + end + var sum = 0 + while cur ~= nil do + c.printf("%d\n",cur.v) + sum = sum + cur.v + var old = cur + cur = cur.next + c.free(old) + end + return sum +end + +local test = require("test") +test.eq(foo(),N * (N - 1) / 2) \ No newline at end of file diff --git a/samples/Terra/recstruct2.t b/samples/Terra/recstruct2.t new file mode 100644 index 00000000..0c92d4d8 --- /dev/null +++ b/samples/Terra/recstruct2.t @@ -0,0 +1,17 @@ + + +local struct A { b : &B } + and struct B { a : &A } + +struct C { a : &A, b : &B, c : &C } + +local struct D {} + +terra foo() + var a : A, b : B, c : C + a.b = &b + b.a = &a + c.c = &c +end + +foo() \ No newline at end of file diff --git a/samples/Terra/rename.t b/samples/Terra/rename.t new file mode 100644 index 00000000..323c09d7 --- /dev/null +++ b/samples/Terra/rename.t @@ -0,0 +1,8 @@ +terralib.includecstring [[ + int foo() { return 3; } +]] + +terra what() return 4 end + + +terralib.saveobj("foo.o",{foo = what}) \ No newline at end of file diff --git a/samples/Terra/renaming.t b/samples/Terra/renaming.t new file mode 100644 index 00000000..c0674d6f --- /dev/null +++ b/samples/Terra/renaming.t @@ -0,0 +1,21 @@ + +C = terralib.includec("stdio.h") +terra main() + C.printf("what\n") +end +main:setinlined(false) + +terra realmain() + main() +end + +terra foo() +end +and terra foo(a : int) +end + +foo:compile() + +terralib.saveobj("renamed",{ main = realmain }) + +terralib.dumpmodule() \ No newline at end of file diff --git a/samples/Terra/requiretwice.t b/samples/Terra/requiretwice.t new file mode 100644 index 00000000..4999edc6 --- /dev/null +++ b/samples/Terra/requiretwice.t @@ -0,0 +1,3 @@ +A = require("lib.golike") +B = require("lib.golike") +assert(A == B) \ No newline at end of file diff --git a/samples/Terra/rvaluerecv.t b/samples/Terra/rvaluerecv.t new file mode 100644 index 00000000..5ccad573 --- /dev/null +++ b/samples/Terra/rvaluerecv.t @@ -0,0 +1,30 @@ +struct A { a : int } + +terra A:foo() + self.a = self.a + 1 + return self.a +end + +terra A.methods.foo2(self : A) + self.a = self.a + 1 + return self.a +end + + +mya = global(A) +mya:set({0}) + +terra geta() + return mya +end + +terra bar() + var v0 = mya.a + var v1 = geta():foo() + var v2 = mya.a + var v3 = geta():foo2() + var v4 = mya.a + return v0,v1,v2,v3,v4 +end +test = require("test") +test.meq({0,1,0,1,0},bar()) \ No newline at end of file diff --git a/samples/Terra/scope.t b/samples/Terra/scope.t new file mode 100644 index 00000000..866d7e26 --- /dev/null +++ b/samples/Terra/scope.t @@ -0,0 +1,13 @@ +terra scope() + var a = 4 + var b = 0 + b = b + a + do + var a = 5 + b = b + a + end + b = b + a + return b +end +local test = require('test') +test.eq(scope(),13) \ No newline at end of file diff --git a/samples/Terra/selectoverload.t b/samples/Terra/selectoverload.t new file mode 100644 index 00000000..b4b33d96 --- /dev/null +++ b/samples/Terra/selectoverload.t @@ -0,0 +1,25 @@ +struct Vec { data : float[3] } + + +local get = {} +get.x = terra(self : &Vec) + return self.data[0] +end +get.y = macro(function(self) + return `self.data[1] +end) + +Vec.metamethods.__entrymissing = macro(function(name,self) + return `[get[name]](&self) +end) + +terra bar() + var a = Vec { array(1.f,2.f,3.f) } + a.y = a.y + 1 + var pa = &a + return a.x + a.y + pa.x +end + +local test = require("test") +test.eq(bar(),5) + diff --git a/samples/Terra/setname.t b/samples/Terra/setname.t new file mode 100644 index 00000000..06c15855 --- /dev/null +++ b/samples/Terra/setname.t @@ -0,0 +1,7 @@ + +two = terra() + return 1 + 1 +end +two:setname("two") + +two:disas() \ No newline at end of file diff --git a/samples/Terra/setter.t b/samples/Terra/setter.t new file mode 100644 index 00000000..abd55acc --- /dev/null +++ b/samples/Terra/setter.t @@ -0,0 +1,39 @@ + +struct A { +a : int; +b : int; +} + +terra A.metamethods.__update(self : &A, a : int, b : int) + self.a = a + self.b = b +end +terra A.metamethods.__apply(self : &A, a : int) + return a + self.b +end + +struct B { + a : int +} + + +B.metamethods.__update = macro(function(me,arg,arg2,rhs) + return quote me.a = arg + arg2 + rhs end +end) + +B.metamethods.__setentry = macro(function(field,self,rhs) + field = field:sub(2) + return quote self.[field] = self.[field] + rhs end +end) + +terra foo() + var a : A + a(4) = 5 + var b : B + b(3,4) = 5 + b._a = 1 + return a.a + a.b + a(3) + b.a +end + +--foo:printpretty() +assert(foo() == 9+8+3+4+5+1) diff --git a/samples/Terra/sgemm-old.t b/samples/Terra/sgemm-old.t new file mode 100644 index 00000000..36d58007 --- /dev/null +++ b/samples/Terra/sgemm-old.t @@ -0,0 +1,205 @@ + +local IO = terralib.includec("stdio.h") +local stdlib = terralib.includec("stdlib.h") + + + +local NB = 64 +local V = 8 + +terra vecload(data : &float, idx : int) + var addr = &data[idx] + return @[&vector(float,V)](addr) +end + +haddavx = terralib.intrinsic("llvm.x86.avx.hadd.ps.256", { vector(float,8), vector(float,8) } -> vector(float,8)) +terra hadd(v : vector(float,8)) + var v1 = haddavx(v,v) + var v2 = haddavx(v1,v1) + return v2[0] + v2[4] +end + + +local AR = 2 +local BR = 2 +local KR = 2 +local NK = 64 + +local BLOCKB = true +local BLOCKA = true + +local function isinteger(x) return math.floor(x) == x end + +assert(isinteger(NK / (KR * V))) +assert(isinteger(NB / AR )) +assert(isinteger(NB / BR )) + + + +blockregisters = macro(function(C,A,B,K,lda,ldc,mm,nn,kk,m,n) + local function mkmatrix(nm,I,J) + local r = {} + for i = 0,I-1 do + r[i] = {} + for j = 0,J-1 do + r[i][j] = symbol(nm..tostring(i)..tostring(j)) + end + end + return r + end + local as,bs,cs = mkmatrix("a",AR,KR),mkmatrix("b",BR,KR),mkmatrix("c",AR,BR) + local stmts = terralib.newlist() + for i = 0, AR-1 do + for j = 0, BR-1 do + stmts:insert(quote var [cs[i][j]] : vector(float,V) = 0.f end) + end + end + + local k = symbol("k") + local kloopbody = terralib.newlist() + + local alreadyloaded = {} + local function get(vs,i,j,loadfn) + if not alreadyloaded[vs[i][j]] then + alreadyloaded[vs[i][j]] = true + kloopbody:insert(loadfn(vs[i][j])) + end + return vs[i][j] + end + + local function getA(i,j) + return get(as,i,j,function(sym) + local result + if BLOCKA then + result = quote + var [sym] = vecload(A, mm * K + NB*(kk + m - mm + i) + k - kk + j *V) + end + else + result = quote + var [sym] = vecload(A, (m + i) * lda + k + j * V) + end + end + return result + end) + end + + local function getB(i,j) + return get(bs,i,j,function(sym) + local result + if BLOCKB then + result = quote + var [sym] = vecload(B, nn * K + NB*(kk + n - nn + i) + k - kk + j * V) + end + else + result = quote + var [sym] = vecload(B, (n + i) * K + k + j * V) + end + end + return result + end) + end + + for l = 0, KR-1 do + for i = 0, AR-1 do + for j = 0, BR-1 do + local aa = getA(i,l) + local bb = getB(j,l) + kloopbody:insert(quote + [cs[i][j]] = [cs[i][j]] + aa * bb + end) + end + end + end + + stmts:insert(quote + for [k] = kk, kk + NK, V*KR do + kloopbody + end + end) + + for i = 0, AR-1 do + for j = 0, BR-1 do + local function getsum(b,e) + if b + 1 == e then + return `[cs[i][j]][b] + else + local mid = (e + b)/2 + assert(math.floor(mid) == mid) + local lhs = getsum(b,mid) + local rhs = getsum(mid,e) + return `lhs + rhs + end + end + local sum + if V == 8 and false then + sum = `hadd([cs[i][j]]) + else + sum = getsum(0,V) + end + stmts:insert(quote + var r = sum + if kk == 0 then + C[(m + i)*ldc + (n + j)] = r + else + C[(m + i)*ldc + (n + j)] = C[(m + i)*ldc + (n + j)] + r + end + end) + end + end + + return stmts +end) + +terra my_sgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : float, A : &float, lda : int, B : &float, ldb : int, + beta : float, C : &float, ldc : int) + + var TB = [&float](stdlib.malloc(K * N * sizeof(float))) + var TA = A + for kk = 0,K,NK do + for nn = 0,N,NB do + for k = kk,kk+NK do + for n = nn,nn+NB do + if BLOCKB then + TB[nn * K + NB*(kk + n - nn) + k - kk] = B[k*ldb + n] + else + TB[n*K + k] = B[k*ldb + n] + end + end + end + end + end + + if BLOCKA then + TA = [&float](stdlib.malloc(M * K * sizeof(float))) + for mm = 0,M,NB do + for kk = 0,K,NK do + for m = mm,mm+NB do + for k = kk,kk+NK do + TA[mm * K + NB*(kk + m - mm) + k - kk] = A[m*lda + k] + end + end + end + end + end + + for mm = 0,M,NB do + for nn = 0, N,NB do + for kk = 0, K, NK do + for m = mm,mm+NB,AR do + for n = nn,nn+NB,BR do + blockregisters(C,TA,TB,K,lda,ldc,mm,nn,kk,m,n) + end + end + end + end + end + stdlib.free(TB) + if BLOCKA then + stdlib.free(TA) + end +end + +my_sgemm:compile() +my_sgemm:printpretty() + +terralib.saveobj("my_sgemm.o", {my_sgemm = my_sgemm}) \ No newline at end of file diff --git a/samples/Terra/sgemm.t b/samples/Terra/sgemm.t new file mode 100644 index 00000000..c5121daa --- /dev/null +++ b/samples/Terra/sgemm.t @@ -0,0 +1,162 @@ + +local IO = terralib.includec("stdio.h") +local stdlib = terralib.includec("stdlib.h") + + + +local NB = 72 +local V = 8 + +terra vecload(data : &float, idx : int) + var addr = &data[idx] + return @[&vector(float,V)](addr) +end + +haddavx = terralib.intrinsic("llvm.x86.avx.hadd.ps.256", { vector(float,8), vector(float,8) } -> vector(float,8)) +terra hadd(v : vector(float,8)) + var v1 = haddavx(v,v) + var v2 = haddavx(v1,v1) + return v2[0] + v2[4] +end + + +local AR = 2 +local BR = 4 +local KR = 1 +local NK = 72 + + +local function isinteger(x) return math.floor(x) == x end + +assert(isinteger(NK / (KR * V))) +assert(isinteger(NB / AR )) +assert(isinteger(NB / BR )) + + + +blockregisters = macro(function(C,A,B,K,lda,ldc,m,n,kk) + local function mkmatrix(nm,I,J) + local r = {} + for i = 0,I-1 do + r[i] = {} + for j = 0,J-1 do + r[i][j] = symbol(nm..tostring(i)..tostring(j)) + end + end + return r + end + local as,bs,cs = mkmatrix("a",AR,KR),mkmatrix("b",BR,KR),mkmatrix("c",AR,BR) + local stmts = terralib.newlist() + for i = 0, AR-1 do + for j = 0, BR-1 do + stmts:insert(quote var [cs[i][j]] : vector(float,V) = 0.f end) + end + end + + local k = symbol("k") + local kloopbody = terralib.newlist() + + local alreadyloaded = {} + local function get(vs,i,j,loadfn) + if not alreadyloaded[vs[i][j]] then + alreadyloaded[vs[i][j]] = true + kloopbody:insert(loadfn(vs[i][j])) + end + return vs[i][j] + end + + local function getA(i,j) + return get(as,i,j,function(sym) + return quote + var [sym] = vecload(A, (m + i) * lda + k + j * V) + end + end) + end + + local function getB(i,j) + return get(bs,i,j,function(sym) + return quote + var [sym] = vecload(B, (n + i) * K + k + j * V) + end + end) + end + + for l = 0, KR-1 do + for i = 0, AR-1 do + for j = 0, BR-1 do + local aa = getA(i,l) + local bb = getB(j,l) + kloopbody:insert(quote + [cs[i][j]] = [cs[i][j]] + aa * bb + end) + end + end + end + + stmts:insert(quote + for [k] = kk, kk + NK, V*KR do + kloopbody + end + end) + + for i = 0, AR-1 do + for j = 0, BR-1 do + local function getsum(b,e) + if b + 1 == e then + return `[cs[i][j]][b] + else + local mid = (e + b)/2 + assert(math.floor(mid) == mid) + local lhs = getsum(b,mid) + local rhs = getsum(mid,e) + return `lhs + rhs + end + end + local sum + if V == 8 and terralib.llvmversion ~= 31 then + sum = `hadd([cs[i][j]]) + else + sum = getsum(0,V) + end + stmts:insert(quote + var r = sum + if kk == 0 then + C[(m + i)*ldc + (n + j)] = r + else + C[(m + i)*ldc + (n + j)] = C[(m + i)*ldc + (n + j)] + r + end + end) + end + end + + return stmts +end) + +terra my_sgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : float, A : &float, lda : int, B : &float, ldb : int, + beta : float, C : &float, ldc : int) + + var TB = [&float](stdlib.malloc(K * N * sizeof(float))) + for k = 0,K do + for n = 0,N do + TB[n*K + k] = B[k*ldb + n] + end + end + + for mm = 0,M,NB do + for nn = 0, N,NB do + for kk = 0, K, NK do + for m = mm,mm+NB,AR do + for n = nn,nn+NB,BR do + blockregisters(C,A,TB,K,lda,ldc,m,n,kk) + end + end + end + end + end + stdlib.free(TB) +end + +my_sgemm:compile() +my_sgemm:printpretty() + +terralib.saveobj("my_sgemm.o", {my_sgemm = my_sgemm}) diff --git a/samples/Terra/sgemm3.t b/samples/Terra/sgemm3.t new file mode 100644 index 00000000..4c625cb6 --- /dev/null +++ b/samples/Terra/sgemm3.t @@ -0,0 +1,124 @@ + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + +llvmprefetch = terralib.intrinsic("llvm.prefetch",{&opaque,int,int,int} -> {}) + + + +function genkernel(NB, RM, RN, V,alpha) + + local A,B,C,mm,nn,ld = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn"),symbol("ld") + local lda,ldb,ldc = ld,ld,ld + local a,b,c,caddr = symmat("a",RM), symmat("b",RN), symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(float,V) + local VP = &VT + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + m*ldc + n*V + var [c[m][n]] = alpha * @VP([caddr[m][n]]) + end) + storec:insert(quote + @VP([caddr[m][n]]) = [c[m][n]] + end) + end + end + + local calcc = terralib.newlist() + + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = @VP(&B[n*V]) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[m*lda]) + end) + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end + end + + + return terra([A] : &float, [B] : &float, [C] : &float, [ld] : int64) + for [mm] = 0, NB, RM do + for [nn] = 0, NB,RN*V do + [loadc]; + for [k] = 0, NB do + llvmprefetch(B + 4*ldb,0,3,1); + [calcc]; + B = B + ldb + A = A + 1 + end + [storec]; + A = A - NB + C = C + RN*V + B = B - ldb*NB + RN*V + end + C = C + RM * ldb - NB + B = B - NB + A = A + lda*RM + end + end +end + +local NB = 48 +local NB2 = 5 * NB + +local V = 1 + +l1dgemm0 = genkernel(NB,1,1,V,0) +l1dgemm1 = genkernel(NB,1,1,V,1) + +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end + +local stdlib = terralib.includec("stdlib.h") +local IO = terralib.includec("stdio.h") + +terra my_sgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : float, A : &float, lda : int, B : &float, ldb : int, + beta : float, C : &float, ldc : int) + + + for mm = 0,M,NB2 do + for nn = 0,N,NB2 do + for kk = 0,K, NB2 do + for m = mm,min(mm+NB2,M),NB do + for n = nn,min(nn+NB2,N),NB do + for k = kk,min(kk+NB2,K),NB do + --IO.printf("%d %d starting at %d\n",m,k,m*lda + NB*k) + if k == 0 then + l1dgemm0(A + (m*lda + k), + B + (k*ldb + n), + C + (m*ldc + n),lda) + else + l1dgemm1(A + (m*lda + k), + B + (k*ldb + n), + C + (m*ldc + n),lda) + end + end + end + end + end + end + end +end + +terralib.saveobj("my_sgemm.o", { my_sgemm = my_sgemm }) \ No newline at end of file diff --git a/samples/Terra/sgemmkernel.t b/samples/Terra/sgemmkernel.t new file mode 100644 index 00000000..46c21454 --- /dev/null +++ b/samples/Terra/sgemmkernel.t @@ -0,0 +1,164 @@ + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + +function genkernel(NB, RM, RN, V,alpha) + + local terra vecload(data : &float, idx : int) + var addr = &data[idx] + return @[&vector(float,V)](addr) + end + local terra vecstore(data : &float, idx : int, v : vector(float,V)) + var addr = &data[idx] + @[&vector(float,V)](addr) = v + end + + local A,B,C,mm,nn = symbol("A"),symbol("B"),symbol("C"),symbol("mn"),symbol("nn") + local lda,ldb,ldc = NB,NB,NB + local a,b,c,caddr = symmat("a",RM), symmat("b",RN), symmat("c",RM,RN), symmat("caddr",RM,RN) + local k = symbol("k") + + local loadc,storec = terralib.newlist(),terralib.newlist() + local VT = vector(float,V) + local VP = &VT + for m = 0, RM-1 do + for n = 0, RN-1 do + loadc:insert(quote + var [caddr[m][n]] = C + (mm+m)*ldc + nn + n*V + var [c[m][n]] = alpha * @VP([caddr[m][n]]) + end) + storec:insert(quote + @VP([caddr[m][n]]) = [c[m][n]] + end) + end + end + + local calcc = terralib.newlist() + + for n = 0, RN-1 do + calcc:insert(quote + var [b[n]] = @VP(&B[k*ldb + nn + n*V]) + end) + end + for m = 0, RM-1 do + calcc:insert(quote + var [a[m]] = VT(A[(mm+m)*lda + k]) + end) + end + for m = 0, RM-1 do + for n = 0, RN-1 do + calcc:insert(quote + [c[m][n]] = [c[m][n]] + [a[m]] * [b[n]] + end) + end + end + + + return terra([A] : &float, [B] : &float, [C] : &float) + for [mm] = 0, NB, RM do + for [nn] = 0, NB,RN*V do + [loadc]; + for [k] = 0, NB do + [calcc]; + end + [storec]; + end + end + end +end + +local NB = 32 +local NB2 = 8 * NB + +local V = 8 + +l1sgemm0 = genkernel(NB,2,4,V,0) +l1sgemm1 = genkernel(NB,2,4,V,1) + +terra min(a : int, b : int) + return terralib.select(a < b, a, b) +end + +local stdlib = terralib.includec("stdlib.h") +local IO = terralib.includec("stdio.h") + +local VT = vector(float,V) + +terra my_sgemm(gettime : {} -> double, M : int, N : int, K : int, alpha : double, A : &float, lda : int, B : &float, ldb : int, + beta : float, C : &float, ldc : int) + + var AA = [&float](stdlib.malloc(sizeof(float)*M*K)) + var BB = [&float](stdlib.malloc(sizeof(float)*K*N)) + var CC = [&float](stdlib.malloc(sizeof(float)*M*N)) + + var i = 0 + for mm = 0,M,NB do + for kk = 0,K,NB do + for m = mm,mm+NB do + for k = kk,kk+NB,V do + @[&VT](&AA[i]) = @[&VT](&A[m*lda + k]) + i = i + V + end + end + end + end + i = 0 + for kk = 0,K,NB do + for nn = 0,N,NB do + for k = kk,kk+NB do + for n = nn,nn+NB,V do + @[&VT](&BB[i]) = @[&VT](&B[k*ldb + n]) + i = i + V + end + end + end + end + + for mm = 0,M,NB2 do + for nn = 0,N,NB2 do + for kk = 0,K, NB2 do + for m = mm,min(mm+NB2,M),NB do + for n = nn,min(nn+NB2,N),NB do + for k = kk,min(kk+NB2,K),NB do + --IO.printf("%d %d starting at %d\n",m,k,m*lda + NB*k) + if k == 0 then + l1sgemm0(AA + (m*lda + NB*k), + BB + (k*ldb + NB*n), + CC + (m*ldc + NB*n)) + else + l1sgemm1(AA + (m*lda + NB*k), + BB + (k*ldb + NB*n), + CC + (m*ldc + NB*n)) + end + end + end + end + end + end + end + + i = 0 + for mm = 0,M,NB do + for nn = 0,N,NB do + for m = mm,mm+NB do + for n = nn,nn+NB,V do + @[&VT](&C[m*ldc + n]) = @[&VT](&CC[i]) + i = i + V + end + end + end + end + + stdlib.free(AA) + stdlib.free(BB) + stdlib.free(CC) +end + +terralib.saveobj("my_sgemmkernel.o", { my_sgemm = my_sgemm }) \ No newline at end of file diff --git a/samples/Terra/shallowfreeze.t b/samples/Terra/shallowfreeze.t new file mode 100644 index 00000000..7d5b3d9e --- /dev/null +++ b/samples/Terra/shallowfreeze.t @@ -0,0 +1,24 @@ + + +struct B { + d : int +} +struct A { + b : &B; + c : int; + d : int; + e : int; +} + + +terra foo() : A + return A {nil} +end + +foo:disas() + +print(foo()) + + + +terralib.dumpmodule() \ No newline at end of file diff --git a/samples/Terra/sharedlib.t b/samples/Terra/sharedlib.t new file mode 100644 index 00000000..64a7afe9 --- /dev/null +++ b/samples/Terra/sharedlib.t @@ -0,0 +1,22 @@ + + +terra foo(a : int, b : int) + return a + b +end + +local ffi = require 'ffi' + +local name = (ffi.os == "Windows" and "foo.dll" or "foo.so") + +local args = {} + +if ffi.os == "Windows" then + args = {"/IMPLIB:foo.lib","/EXPORT:foo" } +end + +terralib.saveobj(name,{ foo = foo }, args) + +local foo2 = terralib.externfunction("foo", {int,int} -> int ) +terralib.linklibrary("./"..name) + +assert(4 == foo2(1,3)) diff --git a/samples/Terra/shift.t b/samples/Terra/shift.t new file mode 100644 index 00000000..67794ebd --- /dev/null +++ b/samples/Terra/shift.t @@ -0,0 +1,7 @@ +local test = require("test") + +terra foo(a : int) + return 1 << 2, a >> 1, -4 >> 1, uint32(-a) >> 1 +end + +test.meq({4,2,-2,2147483646},foo(4)) diff --git a/samples/Terra/signext.t b/samples/Terra/signext.t new file mode 100644 index 00000000..a9a17096 --- /dev/null +++ b/samples/Terra/signext.t @@ -0,0 +1,12 @@ + + +terra bar() + var a : uint8 = 255 + var b = foo(a) + return a == b +end and +terra foo(a : uint8) + return a +end + +print(bar()) \ No newline at end of file diff --git a/samples/Terra/simple.t b/samples/Terra/simple.t new file mode 100644 index 00000000..8d21c5eb --- /dev/null +++ b/samples/Terra/simple.t @@ -0,0 +1,6 @@ + +terra simple(a : int) + return a + a +end +local test = require("test") +test.eq(simple(2),4) \ No newline at end of file diff --git a/samples/Terra/simpleadd.t b/samples/Terra/simpleadd.t new file mode 100644 index 00000000..dd8e42cd --- /dev/null +++ b/samples/Terra/simpleadd.t @@ -0,0 +1,15 @@ +struct Complex { real : float, imag : float } + +terra Complex.metamethods.__add(a : Complex, b : Complex) + return Complex { a.real + b.real, a.imag + b.imag } +end + + +terra foo() + var a = Complex { 1, 2 } + var b = Complex { 3, 4 } + var c = a + b + return c.real,c.imag +end + +print(foo()) \ No newline at end of file diff --git a/samples/Terra/simpleapply.t b/samples/Terra/simpleapply.t new file mode 100644 index 00000000..e6a67034 --- /dev/null +++ b/samples/Terra/simpleapply.t @@ -0,0 +1,19 @@ +struct Vec { data : int[4] } +Vec.metamethods.__apply = terra(self : &Vec, i : int) + return self.data[i] +end + +struct Vec2 { data : int[4] } +Vec2.metamethods.__apply = macro(function(self,b) + return `self.data[b] +end) + +terra bar() + var a = Vec { array(1,2,3,4) } + var b = Vec2 { array(1,2,3,4) } + b(2) = b(2) + 1 + return b(2) + a(2) +end + +local test = require("test") +test.eq(bar(),7) \ No newline at end of file diff --git a/samples/Terra/simpleglobal.t b/samples/Terra/simpleglobal.t new file mode 100644 index 00000000..ff8964ce --- /dev/null +++ b/samples/Terra/simpleglobal.t @@ -0,0 +1,19 @@ + +local a = terralib.global(int) +local b = terralib.global(5) +local c = terralib.global(int,3) +terra foo() + a = 4 +end + + +terra bar() + return c + a + b +end + +foo() +assert(bar() == 12) + +b:set(4) + +assert(bar() == 11) \ No newline at end of file diff --git a/samples/Terra/simplerec.t b/samples/Terra/simplerec.t new file mode 100644 index 00000000..5d0b98ff --- /dev/null +++ b/samples/Terra/simplerec.t @@ -0,0 +1,25 @@ + + + +local terra bar() + return 1 +end +and struct A { + a : int +} +and local struct B { + a : int +} +and terra B:foo() + return self.a +end +and terra foo() + var b , a = B{4}, A{5} + return 1 + b:foo() +end +and local terra mydecl +and struct mystructdecl + +print(bar()) + +print(foo()) diff --git a/samples/Terra/simplestruct.t b/samples/Terra/simplestruct.t new file mode 100644 index 00000000..fc0b5ac8 --- /dev/null +++ b/samples/Terra/simplestruct.t @@ -0,0 +1,10 @@ + +struct A { + a : int +} + +terra foo(a : &A) + return a +end + +print(foo(terralib.new(A,{3}))) diff --git a/samples/Terra/simplevec.t b/samples/Terra/simplevec.t new file mode 100644 index 00000000..b75f3d1a --- /dev/null +++ b/samples/Terra/simplevec.t @@ -0,0 +1,12 @@ + +terra sum(input : &float, N : int) + var acc : vector(float,4) = vector(0.f,0.f,0.f,0.f) + for i = 0,N,4 do + --cast the floats to float4s and load + var entry = @[&vector(float,4)](input + i) + acc = acc + entry + end + return acc[0] + acc[1] + acc[2] + acc[3] +end + +sum:disas() diff --git a/samples/Terra/sintable.t b/samples/Terra/sintable.t new file mode 100644 index 00000000..c1606563 --- /dev/null +++ b/samples/Terra/sintable.t @@ -0,0 +1,17 @@ +local N = 32 +local tbl = terralib.new(float[N]) +for i = 1,N do + tbl[i-1] = math.sin( 2 * math.pi * (i-1)/N) +end + +local ctable = terralib.constant(tbl) + +terra sintable(a : float) : float + var idx = int(a / (2 * math.pi) * N) + return ctable[idx] +end + +sintable:disas() + +print(sintable(0)) +print(sintable(math.pi/4)) \ No newline at end of file diff --git a/samples/Terra/special.t b/samples/Terra/special.t new file mode 100644 index 00000000..218aa3a2 --- /dev/null +++ b/samples/Terra/special.t @@ -0,0 +1,61 @@ + +local test = require("test") + +function mkspecial(N) + local terra pow(a : double) + var i,r = 0,1.0 + while i < N do + r = r * a + i = i + 1 + end + return r + end + return pow +end + + +local pow2 = mkspecial(2) +local pow3 = mkspecial(3) + +test.eq(pow2(2),4) +test.eq(pow3(2),8) + + +function mkor(T) + local terra fn(a : T, b : T) : T + return a or b + end + return fn +end + +local lor = mkor(bool) +local aor = mkor(int) + +test.eq(lor(1,2),true) +test.eq(aor(1,2),3) + + +--[[ +function my_lua_fun() + + val my_list = new_list() + defer delete(my_list) + + + + return { ["a"] = 1, ["b"] = 2 } +end +--wess weimer PhD on controlling actions +a:my_method(b) + +a.my_method(a,b) + +var a : T +a:my_method(b) +T.my_method(a,b) + +Pair = typedef(struct { a = int, b = int}) + +var a : Pair = { 1, 3 } +var a : Pair = my_lua_fun() +]] \ No newline at end of file diff --git a/samples/Terra/speed.t b/samples/Terra/speed.t new file mode 100644 index 00000000..ceca0264 --- /dev/null +++ b/samples/Terra/speed.t @@ -0,0 +1,30 @@ +local c = terralib.includecstring [[ + #include + #include +]] + +terra doit(N : int64) + var cur,last = 1ULL,1ULL + for i = 0ULL, (N-2ULL) do + cur,last = cur+last,cur + end + return cur +end +terra main(argc : int, argv : &&int8) + var N = 4ULL + if argc == 2 then + N = c.atoi(argv[1]) + end + var result = doit(N) + c.printf("%lld\n",result) +end + +terra what() + return c.atoi("54") +end + +local test = require("test") +print(what()) +print(test.time( function() doit:compile() end)) +print(test.time( function() doit(100000000) end)) +print(test.time( function() terralib.saveobj("speed",{main = main}) end)) diff --git a/samples/Terra/splitprimary.t b/samples/Terra/splitprimary.t new file mode 100644 index 00000000..aed90417 --- /dev/null +++ b/samples/Terra/splitprimary.t @@ -0,0 +1,19 @@ +r = 1 +local function foo(a) + r = a + return r +end +local b = foo(1) +(foo)(3) +assert(b + r == 4) + +terra testescapes() + var a = [4] + [ quote + a = a + 1 + end + ] + return [ `&a ][0] +end + +assert(testescapes() == 5) \ No newline at end of file diff --git a/samples/Terra/ssimple.t b/samples/Terra/ssimple.t new file mode 100644 index 00000000..a50f90a8 --- /dev/null +++ b/samples/Terra/ssimple.t @@ -0,0 +1,477 @@ +function failit(fn) + local success,msg = pcall(fn) + if success then + error("failed to fail.",2) + elseif not string.match(msg,"Errors reported during") then + error("failed wrong: "..msg,2) + end +end + +terra foo() + return 1 +end + +a = 4 +terra foo2() + return a +end +b = 4.5 +terra foo3() + return b +end + +terra foo4() + var a = 3 + return a +end + + + +sa = symbol("myname") + +terra foo5() + var [sa] = 5 + return sa +end + +whatwhat = `3 + b + +terra foo6() + return whatwhat +end + +local whatwhat2 = {4,5} + +terra foo7() + return 3,4,5,whatwhat,whatwhat2 +end + +terra foo8() + return whatwhat2,4 +end + +local test = require("test") +assert(foo() == 1) +assert(foo2() == 4) +assert(foo3() == 4.5) +assert(foo4() == 3) +assert(foo5() == 5) +assert(foo6() == 7.5) +test.meq({3,4,5,7.5,4,5},foo7()) +test.meq({4,5,4},foo8()) + +local a = { b = {c = 4}} +terra foo9() + return a.["b"].c +end +print(foo9()) + +struct A { a : int } + +A.methods.what = terra() return 4 end + + +terra foo10() + return A.what() +end + +assert(foo10() == 4) + + +terra foo11() + var c = 3 + return [4 + 5 + a.b.c] + [c] +end + +assert(foo11() == 16) + +failit(function() + local terra missingvar() + return missing + end +end) +local b = coroutine.create(failit) +failit(function() + return `sa.foobar,`b.e +end) +failit(function() + return `a.d +end) +failit(function() + return `a.[1+"nope"] +end) +failit(function() + return `a.[1] +end) + +over = symbol() + +anint = symbol(int) + +terra foo13() + var [anint] + anint = 3 + return anint +end +assert(foo13() == 3) + +terra foo12() + goto [over] + while true do + end + ::[over]:: +end + +foo12() + + +terra foo14() : {int,double} + return 3.5,4.5 +end + +test.meq({3,4.5},foo14()) + +terra foo15() : int + return 3.5 +end +assert(foo15() == 3) + +failit(function() + local terra foo16() : 3 + end +end) + +terra foo16() + var r = 0 + for i = 0,10 do + r = r + 2 + end + return r +end + +assert(foo16() == 20) + +terra foo17() + var i = 0 + repeat + i = i + 1 + var b = i + 1 + until b == 10 + repeat + until true + return i +end +assert(foo17() == 9) + +local terra foo18() + var a : int, b : double = 2.5,2.5 + return a,b +end + +test.meq({2,2.5},foo18()) + +failit(function() + local function doit() + return 1 + {} + end + return `[doit()] +end) + +failit(function() +local terra foo19() + var a : 3, b : double = 2.5,2.5 + return a,b +end +foo19() +end) + +failit(function() +local terra foo20() + var a : int, a : double = 2.5,2.5 + return a,a +end +foo20() +end) + +foo21s = symbol() + +local terra foo21() + var [foo21s],b = 3,4 + return b + foo21s +end +assert(foo21() == 7) + +failit(function() +local terra foo20() + var ["a"] : int, ["b"] = 4,5 + return a +end +end) + +failit(function() + return quote var a end +end) + +astring = "astring" + +local terra foo22() + return astring[0] +end + +assert(foo22() == 97) + +local aquote = `7 +local atree = aquote.tree + +terra foo23() + return atree +end + +assert(foo23() == 7) + +terra foo24(a : int) + return 3,4 + a +end + +terra foo25() + var c,a,b = 4,foo24(3) + return a + b + c +end + + + +assert(foo25() == 14) + +local obfuscate = { foo24 } +terra foo26() + var c,a,b = 4,obfuscate(3) + return a + b + c +end + +local obfuscate2 = { `foo24(3) } +terra foo27() + obfuscate2 + foo24(3) +end +foo27() + +assert(foo26() == 14) + +failit(function() +local terra foo26a(a : int) + if a == 0 then + return a + else + return foo26a(a - 1) + 1 + end +end +foo26a(3) +end) + +local terra foo26b(a : int) : int + if a == 0 then + return a + else + var foo26bptr = foo26b + return foo26b(a - 1) + foo26bptr(0) + 1 + end +end + +assert(foo26b(3) == 3) + + +failit(function() + (terra() return (3)(4) end)() +end) + +failit(function() + local terra notdefined + local terra callnotdefined() + return notdefined(1) + end + callnotdefined() +end) + + + local terra norets() + end + local terra callnotdefined() + return (norets()) + end + callnotdefined() + +local terra returns2() return 1,2 end + +terra foo29(a : int) + if a > 1 then + return (returns2()._0) + else + return 5 + end +end + +assert(foo29(3) == 1 and foo29(0) == 5) + + +terra foo30() + print(4) +end + +foo30() + + +local terra notdefined +local terra definedtwice(a : int) return a end +terra definedtwice(a : int, b : int) return a + b end + +failit(function() +local terra foo31() + return (notdefined), (definedtwice) +end +foo31() +end) + +terra foo35() + return definedtwice(3) + definedtwice(3,4) +end + + +local terra foo36() + var r = returns2() + return definedtwice(unpackstruct(r)) +end +assert(3 == foo36()) + +assert(foo35() == 10) + +struct B { + a : int; + b : double; +} + +terra B:add() + return self.a + self.b +end + +terra B:inc(v : int) + self.a = self.a + v +end + +B.metamethods.__apply = terra(self : &B, v :int) + return v + v +end + +B.metamethods.__entrymissing = macro(function() return 8 end) + +struct C { + a : int; +} + +terra B.metamethods.__add(self : &B, a : int, b : int) + return self.a + a +end +terra B.metamethods.__add(self : &B, a : int, b : int, c : int) + return self.a + a +end + + +function myvoid() + print("CALLED") +end +terra testcallvoid() + myvoid() +end +print("BEFORE") +testcallvoid() +print("AFTER") + +terra C.metamethods.__add(b : &B, c : &C) + return b.a + c.a +end + +terra foo40() + var b = B { 5,6 } + var c = C { 3 } + var ptrb = &b + b:inc(3) + ptrb:inc(3) + return b:add() + ptrb:add() + b(3) + ptrb(3) + b.foo + ptrb.foo + (b + c) +end + +assert(foo40() == 8 + 8 + 6 + 6 + 6 + 11 + 6 + 11 + 3 + 11) +B.metamethods.__add = macro(function(b,c) return `b.a + c.a + 1 end) +terra foo41() + var b = B { 5,6 } + var c = C { 3 } + var ptrb = &b + b:inc(3) + ptrb:inc(3) + return b:add() + ptrb:add() + b(3) + ptrb(3) + b.foo + ptrb.foo + (b + c) +end + +assert(foo41() == 8 + 8 + 6 + 6 + 6 + 11 + 6 + 11 + 3 + 11 + 1) + +terra foo32() + var a = B { b = 3, a = 4 } + var ptra = &a + a.b = a.b + 1 + ptra.a = ptra.a + (@ptra).a + 1 + return a.a, a.b +end + +test.meq({9,4},foo32()) + +terra foo33() + return (vector(3,4,5) + vectorof(double,3,4,5))[0] + sizeof(double) +end + +local notinscope = symbol() + +failit(function() + local terra foo34() + return notinscope + end + foo34() +end) + +local gbl = global(int) + +terra foo37() + gbl = 4 +end +foo37() + +assert(gbl:get() == 4) +assert(foo33() == 14) + +local C = terralib.includec("stdio.h") + +terra foo38() + C.printf("hello, world %f\n", 3.5f) +end +foo38() +local twice = macro(function(exp,call) + return quote + exp = exp + 1 + call._0 + exp = exp + 1 + call._0 + end +end) +terra foo39() + var a = 4 + twice(a,returns2()) + return a +end + +assert(foo39() == 8) +--checkexp truncate and allowluaobjects behavior +--global variable resolution +--select/pointerselect/__get +--expression macro 1 return +--expression macro truncated +--expression macro expanded +--expression macro that returns a multi-ret function +--expression macro that returns a multi-ret function that was an argument +--function call, function call with multi-return, +--function call to overloaded function/undef function +--overloaded operator with mixed macro/function stuff +--reciever cast stuff \ No newline at end of file diff --git a/samples/Terra/staticmethod.t b/samples/Terra/staticmethod.t new file mode 100644 index 00000000..0ce91a4c --- /dev/null +++ b/samples/Terra/staticmethod.t @@ -0,0 +1,9 @@ +struct A { +} +A.metamethods.__getmethod = function(self,idx) + return tonumber(string.sub(idx,2,-1)) +end +terra foo() + return A.f33 + A.b34 +end +assert(67 == foo()) \ No newline at end of file diff --git a/samples/Terra/stattest.t b/samples/Terra/stattest.t new file mode 100644 index 00000000..71380d2d --- /dev/null +++ b/samples/Terra/stattest.t @@ -0,0 +1,13 @@ +local ffi = require("ffi") +if ffi.os == "Windows" then + return +end + +C,T = terralib.includec("sys/stat.h") +terra dostat() + var s : T.stat + C.stat("stattest.t",&s) + return s.st_size +end + +assert(dostat() == 210) diff --git a/samples/Terra/stencil.t b/samples/Terra/stencil.t new file mode 100644 index 00000000..1ce4d732 --- /dev/null +++ b/samples/Terra/stencil.t @@ -0,0 +1,134 @@ + +local C = terralib.includecstring [[ +#include +#include +#ifndef _WIN32 +#include +static double CurrentTimeInSeconds() { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec + tv.tv_usec / 1000000.0; +} +#else +#include +static double CurrentTimeInSeconds() { + return time(NULL); +} +#endif + +int CalcTime(int * times, double * start) { + if(*times == 0) { + *start = CurrentTimeInSeconds(); + } else { + double elapsed = CurrentTimeInSeconds() - *start; + if(elapsed > 0.1f && *times >= 3) { + *start = elapsed / *times; + return 0; + } + } + (*times)++; + return 1; +} +]] + + +function symmat(name,I,...) + if not I then return symbol(name) end + local r = {} + for i = 0,I-1 do + r[i] = symmat(name..tostring(i),...) + end + return r +end + + + +local IO = terralib.includec("stdio.h") + + + +NI,NJ = 1,4 +V = 8 +terra uload(d : &float) + return terralib.attrload([&vector(float,V)](d), { align = 4 }) +end +terra ustore(d : &float, v : vector(float,V)) + terralib.attrstore([&vector(float,V)](d), v, { align = 4 }) +end + +terra runit(N : int, input : &float, output : &float) + input = input + N*NI + V*NJ + output = output + N*NI + V*NJ + for i = NI, N-NI,NI do + for j = V*NJ, N-V*NJ,V*NJ do + [(function() + local C = symmat("C",NI,NJ) + local stmts = terralib.newlist() + for iii = 0,NI-1 do for jjj = 0,NJ-1 do + stmts:insert(quote + var [C[iii][jjj]] : vector(float,V) = 0 + end) + end end + + for ii = -1,1 do + for jj = -1,1 do + for iii = 0,NI-1 do for jjj = 0,NJ-1 do + if math.abs(ii) + math.abs(jj) ~= 2 then + stmts:insert(quote + var d = uload(input + N * (ii + iii) + jj + V*jjj); + [C[iii][jjj]] = [C[iii][jjj]] + d + end) + end + end end + end + end + for iii = 0,NI-1 do for jjj = 0,NJ-1 do + stmts:insert(quote + ustore(output + N * (iii) + V*jjj, [C[iii][jjj]]) + end) + end end + return stmts + end)()] + input = input + V*NJ + output = output + V*NJ + end + input = input + 2*V*NJ + output = output + 2*V*NJ + end +end + +terra doit() + var N = 2048 + var img = [&float](C.malloc(N*N*sizeof(float))) + var img2 = [&float](C.malloc(N*N*sizeof(float))) + + for i = 0, N do + for j = 0, N do + img[i*N+j] = 1 + end + end + + var times = 0 + var mytime : double + while C.CalcTime(×,&mytime) ~= 0 do + runit(N,img,img2) + end + + C.printf("times = %d\n",times) + for i = NI, N-NI do + for j = V*NJ, N-V*NJ do + if img2[i*N+j] ~= 5 then + C.printf("wrong! %d %d %f\n",i,j,img2[i*N+j]) + goto out + end + end + end + ::out:: + var togiga = 1.0/(1024*1024*1024) + var pixels = (N-NI)*(N-V*NJ) + C.printf("%f %f %f\n", mytime, pixels*4*2 *togiga / mytime, 5*pixels * togiga / mytime) + +end +doit() +runit:disas() +doit() diff --git a/samples/Terra/strerror.t b/samples/Terra/strerror.t new file mode 100644 index 00000000..6eb0b83f --- /dev/null +++ b/samples/Terra/strerror.t @@ -0,0 +1,9 @@ +string=terralib.includec("string.h") +buf=terralib.new(int8[1024]) +ffi = require "ffi" +if ffi.os == "Windows" then + string.strerror_s(buf,1024,1) +else + string.strerror_r(1,buf,1024) +end +print(ffi.string(buf)) diff --git a/samples/Terra/string.t b/samples/Terra/string.t new file mode 100644 index 00000000..d44388eb --- /dev/null +++ b/samples/Terra/string.t @@ -0,0 +1,15 @@ + +local c = terralib.includec("stdio.h") + +terra foo() + var a = "whatwhat\n" + return c.puts(a) +end + +local test = require("test") +local ffi = require("ffi") +if ffi.os == "Windows" then + test.eq(foo(),0) +else + test.eq(foo(),10) +end \ No newline at end of file diff --git a/samples/Terra/struct.t b/samples/Terra/struct.t new file mode 100644 index 00000000..5c3f96cd --- /dev/null +++ b/samples/Terra/struct.t @@ -0,0 +1,50 @@ +--the zero line +struct A { b : B } and +struct B {a : int, b : int} + +terra bar(a : B) + a.a = a.a + 1 + return a,3 +end + +terra foo() + var a : B + a.a = 4; + --(bar(a)).a = 3, TODO: why is the offset == 0 for this value? + var c,d = bar(a) + return c.a + a.a + d +end + + +terra baz(a : &B) + a.a = 1 + a.b = 2 + return a.a +end + + +terra foo2() + var a : B + var d = baz(&a) + return a.a + a.b + d +end +local test = require("test") + +test.eq(foo(),12) +test.eq(foo2(),4) + + +local C = tuple(int, int) + +local D = tuple(int, int) + +terra anon() + var c : C + c._0 = 3 + c._1 = 4 + var d : D = c + --var b : B = d + return d._0 + d._1 +end + +test.eq(anon(),7) \ No newline at end of file diff --git a/samples/Terra/structarg.t b/samples/Terra/structarg.t new file mode 100644 index 00000000..c3d1f4ed --- /dev/null +++ b/samples/Terra/structarg.t @@ -0,0 +1,9 @@ +terra foobar() + var a = { a=3,b=4} + var b = {a=5,b=6.0} + b = a + return a.a +end + +local test = require("test") +test.eq(foobar(),3) \ No newline at end of file diff --git a/samples/Terra/structcast.t b/samples/Terra/structcast.t new file mode 100644 index 00000000..9b84a258 --- /dev/null +++ b/samples/Terra/structcast.t @@ -0,0 +1,38 @@ +struct A { a : int } +struct B {a : int, b : A} + +local D = tuple(double, A) + +terra anon() + var b : B + b.a = 4 + b.b.a = 3 + + var d : D + d._0 = 1.0 + d._1.a = 2 + + b = B(d) + + return b.a + b.b.a +end + +terra anon2() + var b = B { b = A{ 2.0 }, a = 4 } + var b2 = B { a = 4, b = A{ 2.0 } } + var b3 = B{ 4, A{2.0} } + return b.a + b.b.a + b2.a + b2.b.a + b3.a + b3.b.a +end + +terra anon3() + return {5,A{6}} +end + +terra anon4() + var b = B(anon3()) + return b.a + b.b.a +end +test = require("test") +test.eq(anon(),3) +test.eq(anon2(),18) +test.eq(anon4(),11) \ No newline at end of file diff --git a/samples/Terra/structconstructor.t b/samples/Terra/structconstructor.t new file mode 100644 index 00000000..8a9bd5c9 --- /dev/null +++ b/samples/Terra/structconstructor.t @@ -0,0 +1,9 @@ +struct A { a : int, b : int} + +terra foobar() + var a = A { 3, 4} + return a.b +end + +local test = require("test") +test.eq(foobar(),4) \ No newline at end of file diff --git a/samples/Terra/structrvalue.t b/samples/Terra/structrvalue.t new file mode 100644 index 00000000..31c0f6eb --- /dev/null +++ b/samples/Terra/structrvalue.t @@ -0,0 +1,16 @@ +--the zero line +struct A { b : B } and +struct B {a : int, b : int} + + +terra foo() + var b = B {1,2} + return b +end + +terra bar() + return foo().b +end + +local test = require("test") +test.eq(bar(),2) \ No newline at end of file diff --git a/samples/Terra/structsyntax.t b/samples/Terra/structsyntax.t new file mode 100644 index 00000000..43d1b06a --- /dev/null +++ b/samples/Terra/structsyntax.t @@ -0,0 +1,14 @@ +struct A { + a : int + b : float + union { + c : int + d : float + } +} + +terra foo() + return A { a = 4, b = 5, c = 3} +end + +assert(foo().c == 3) \ No newline at end of file diff --git a/samples/Terra/sugar.t b/samples/Terra/sugar.t new file mode 100644 index 00000000..6870fde5 --- /dev/null +++ b/samples/Terra/sugar.t @@ -0,0 +1,15 @@ + +local foo = &int + + +local a = int -> {bool,int} + +print(a.name) + +terra foo2() + +end + + + +local test = require("test") diff --git a/samples/Terra/sumlanguage1.t b/samples/Terra/sumlanguage1.t new file mode 100755 index 00000000..84889622 --- /dev/null +++ b/samples/Terra/sumlanguage1.t @@ -0,0 +1,2 @@ +import "lib/sumlanguage" +assert((sum 1.0f,2LL,2ULL,3.0,1U,1 done)==10) \ No newline at end of file diff --git a/samples/Terra/sumlanguage2.t b/samples/Terra/sumlanguage2.t new file mode 100755 index 00000000..7c84b980 --- /dev/null +++ b/samples/Terra/sumlanguage2.t @@ -0,0 +1,4 @@ +import "lib/sumlanguage2" +a = 2 +local b = 3 +assert(sum 1,a,b done == 6) \ No newline at end of file diff --git a/samples/Terra/symbolmangling.t b/samples/Terra/symbolmangling.t new file mode 100644 index 00000000..70c5ad79 --- /dev/null +++ b/samples/Terra/symbolmangling.t @@ -0,0 +1,4 @@ +C = terralib.includecstring [[ + #include +]] +C.fopen("broken.t","r") diff --git a/samples/Terra/symbolvar.t b/samples/Terra/symbolvar.t new file mode 100644 index 00000000..4d4b0cbd --- /dev/null +++ b/samples/Terra/symbolvar.t @@ -0,0 +1,11 @@ + + +local a = symbol() + +terra foo() + var [a] = 3 + return [a] +end + +local test = require("test") +test.eq(3,foo()) \ No newline at end of file diff --git a/samples/Terra/symbolvar2.t b/samples/Terra/symbolvar2.t new file mode 100644 index 00000000..1195e539 --- /dev/null +++ b/samples/Terra/symbolvar2.t @@ -0,0 +1,16 @@ + + +local a = symbol() + +local q = quote + [a] = [a] + 1 +end + +terra foo() + var [a] = 2 + q + return [a] +end + +local test = require("test") +test.eq(3,foo()) \ No newline at end of file diff --git a/samples/Terra/symbolvar3.t b/samples/Terra/symbolvar3.t new file mode 100644 index 00000000..5eed2aaa --- /dev/null +++ b/samples/Terra/symbolvar3.t @@ -0,0 +1,16 @@ + + +local a = symbol() + +local q = quote + var [a] = 2 + [a] = [a] + 1 +end + +terra foo() + q + return [a] +end + +local test = require("test") +test.eq(3,foo()) \ No newline at end of file diff --git a/samples/Terra/symbolvar4.t b/samples/Terra/symbolvar4.t new file mode 100644 index 00000000..1db5c832 --- /dev/null +++ b/samples/Terra/symbolvar4.t @@ -0,0 +1,16 @@ +local a = symbol() +local b = symbol(int) + +local c = symbol(int) +local d = symbol() + + +terra foo() + var [a],[b] = 1.25,1.25 + var [c],[d] = 3.25,3.25 + return [a] + [b] + [c] + [d] +end + +local test = require("test") + +test.eq(foo(),8.50) \ No newline at end of file diff --git a/samples/Terra/symbolvar5.t b/samples/Terra/symbolvar5.t new file mode 100644 index 00000000..6028bb13 --- /dev/null +++ b/samples/Terra/symbolvar5.t @@ -0,0 +1,16 @@ +local a = symbol() +local b = { symbol(int), symbol(int) } + +local c = symbol(int) +local d = { symbol(), symbol() } + + +terra foo() + var [a],[b] = 1.25,1.25,1.25 + var [c],[d] = 3.25,3.25,3.25 + return [a] + [b[1]] + [b[2]] + [c] + [d[1]] + [d[2]] +end + +local test = require("test") + +test.eq(foo(),12.75) \ No newline at end of file diff --git a/samples/Terra/symbolvar6.t b/samples/Terra/symbolvar6.t new file mode 100644 index 00000000..efd07137 --- /dev/null +++ b/samples/Terra/symbolvar6.t @@ -0,0 +1,16 @@ +local a = symbol() +local b = {} + +local c = symbol(int) +local d = {} + + +terra foo() + var [a],[b] = 1.25 + var [c],[d] = 3.25 + return [a] + [c] +end + +local test = require("test") + +test.eq(foo(),4.25) \ No newline at end of file diff --git a/samples/Terra/symbolvar7.t b/samples/Terra/symbolvar7.t new file mode 100644 index 00000000..5c0bc621 --- /dev/null +++ b/samples/Terra/symbolvar7.t @@ -0,0 +1,15 @@ +local a = symbol() +local b = { symbol(int), symbol(int) } + +local c = symbol(int) +local d = symbol(double) + +terra foo() + var [a] : double,[b] = 1.25,1.25,1.25 + var [c] : double,[d] : int = 3.25,3.25 + return [a] + [b[1]] + [b[2]] + [c] + [d] +end + +local test = require("test") + +test.eq(foo(),9.50) \ No newline at end of file diff --git a/samples/Terra/symparam.t b/samples/Terra/symparam.t new file mode 100644 index 00000000..9aea4d3a --- /dev/null +++ b/samples/Terra/symparam.t @@ -0,0 +1,9 @@ + +local a = symbol() +terra foo([a] : int, b : int) + return [a] + b +end + +local test = require("test") + +test.eq(foo(1,2),3) \ No newline at end of file diff --git a/samples/Terra/symparam2.t b/samples/Terra/symparam2.t new file mode 100644 index 00000000..f6c7e393 --- /dev/null +++ b/samples/Terra/symparam2.t @@ -0,0 +1,9 @@ + +local a = symbol(int) +terra foo([a], [a], b : int) + return [a] + b +end + +local test = require("test") + +test.eq(foo(1,2,4),6) \ No newline at end of file diff --git a/samples/Terra/symparam3.t b/samples/Terra/symparam3.t new file mode 100644 index 00000000..35b3e40b --- /dev/null +++ b/samples/Terra/symparam3.t @@ -0,0 +1,15 @@ + +local a = symbol(int) +local c = {} +terra foo([a], b : int, [c]) + return [a] + b +end +local d = {symbol(int),symbol(int)} +terra foo2([a], b : int, [d]) + return [a] + b + [d[1]] + [d[2]] +end + +local test = require("test") + +test.eq(foo2(1,2,3,4),10) +test.eq(foo(1,2),3) \ No newline at end of file diff --git a/samples/Terra/template.t b/samples/Terra/template.t new file mode 100644 index 00000000..b305aeb9 --- /dev/null +++ b/samples/Terra/template.t @@ -0,0 +1,6 @@ + +terra foo() + +end + +local test = require("test") diff --git a/samples/Terra/terracast.t b/samples/Terra/terracast.t new file mode 100644 index 00000000..05df48f7 --- /dev/null +++ b/samples/Terra/terracast.t @@ -0,0 +1,15 @@ + + + +function addone(a) + return a + 1 +end + + +local a1 = terralib.cast(int -> int, addone) + +terra dofn(a : int -> int) + return a(3) +end +local test = require("test") +test.eq(dofn(a1),4) \ No newline at end of file diff --git a/samples/Terra/terralua.t b/samples/Terra/terralua.t new file mode 100644 index 00000000..8084f68c --- /dev/null +++ b/samples/Terra/terralua.t @@ -0,0 +1,32 @@ +function dog(...) + print("Hi. This is Dog.",...) +end + +terra foo() + dog() +end + +terra passanarg() + dog(3,4,5,nil) +end + +function takesastruct(a) + print(a.a,a.b) + a.a = a.a + 1 +end +struct A { a : int , b : double } +terra passastruct() + var a = A {1,3.4} + --takesastruct(a) luajit doesn't like having structs passed to its callbacks by value? + var b = a.a + takesastruct(&a) + var c = a.a + return b + c +end + +foo() +passanarg() + +local test = require("test") + +test.eq(passastruct(),3) diff --git a/samples/Terra/terraluamethod.t b/samples/Terra/terraluamethod.t new file mode 100644 index 00000000..09de7b2d --- /dev/null +++ b/samples/Terra/terraluamethod.t @@ -0,0 +1,16 @@ +struct A { a : int } + +A.methods.up = function(self,b) + self.a = self.a + 1 + (b or 0) +end + +terra foo() + var a = A { 1 } + a:up() + var b = &a + b:up(4) + return a.a +end + +local test = require("test") +test.eq(foo(),7) \ No newline at end of file diff --git a/samples/Terra/terranew.t b/samples/Terra/terranew.t new file mode 100644 index 00000000..67336e38 --- /dev/null +++ b/samples/Terra/terranew.t @@ -0,0 +1,19 @@ + + +A = terralib.new(int[4],{1,2,3,4}) + +terra foo(a : &int) + var sum = 0 + for i = 0,4 do + sum = sum + a[i] + end + return sum,a +end + +local test = require("test") + +local a,b = terralib.unpackstruct(foo(A)) + +test.eq(a,10) +test.eq(terralib.typeof(A),int[4]) +test.eq(terralib.typeof(b),&int) \ No newline at end of file diff --git a/samples/Terra/testdebug.t b/samples/Terra/testdebug.t new file mode 100644 index 00000000..b155e795 --- /dev/null +++ b/samples/Terra/testdebug.t @@ -0,0 +1,57 @@ +if not terralib.traceback then return end +--this test require debug on, if it is not on, relaunch with it on +if 0 == terralib.isdebug then + assert(0 == os.execute(terralib.terrahome.."/bin/terra -g testdebug.t")) + return +end +C = terralib.includec("stdio.h") + +terra foo(a : int, b : int) + var c = a + b + return c * 2 +end + +local ptr = terralib.cast(rawstring,foo:getdefinitions()[1]:getpointer()) + +terra findptr(a : &opaque) + var si : terralib.SymbolInfo + var li : terralib.LineInfo + terralib.lookupsymbol(a,&si) + C.printf("p = %p, addr = %p, sz = %d, nm = %.*s\n",a,si.addr,[int](si.size), si.namelength,si.name) + terralib.lookupline(si.addr,a, &li) + C.printf("line = %.*s:%d\n",li.namelength,li.name,[int](li.linenum)) + return li.linenum +end +assert(11 == findptr(ptr+6)) +assert(10 == findptr(ptr+4)) +local ra = terralib.intrinsic("llvm.returnaddress", int32 -> &opaque ) +local fa = terralib.intrinsic("llvm.frameaddress", int32 -> &opaque ) +terra testbt3() + var frames : (&opaque)[128] + terralib.traceback(nil) + var N = terralib.backtrace(frames,128,ra(0),fa(1)) + for i = 0,N do + C.printf("%p ",frames[i]) + var nm : rawstring + var nmL : uint64 + var si : terralib.SymbolInfo + if terralib.lookupsymbol(frames[i],&si) then + C.printf("frame %.*s\n", si.namelength, si.name) + else + C.printf("\n") + end + end +end +testbt3:setinlined(false) +terra fn2() + testbt3() + C.printf("fn2\n") + return 1 +end +terra what() + fn2() + C.printf("what\n") +end +fn2:setinlined(false) +what() +terralib.disas(terralib.traceback,5,5) \ No newline at end of file diff --git a/samples/Terra/testimport.t b/samples/Terra/testimport.t new file mode 100644 index 00000000..e2ef63db --- /dev/null +++ b/samples/Terra/testimport.t @@ -0,0 +1,6 @@ + +local r,e = terralib.loadstring [[ +import "lib.fakeimport" +]] + +assert(e:match("stack traceback")) \ No newline at end of file diff --git a/samples/Terra/testlang1.t b/samples/Terra/testlang1.t new file mode 100755 index 00000000..89496035 --- /dev/null +++ b/samples/Terra/testlang1.t @@ -0,0 +1,21 @@ +import "lib/testlang" + +print("hello") +local a = image "astring" +image 4 +image and,or,& +image image +image akeyword +image foobar +print(a) +image bar 3 +print(bar,bar1) + +local image what 4 3 +print(what) +local a = 4 +local b = 5 +c = 6 +print(foolist { a, b, c}) +--test eos token +image diff --git a/samples/Terra/testlang2.t b/samples/Terra/testlang2.t new file mode 100755 index 00000000..b4d1229f --- /dev/null +++ b/samples/Terra/testlang2.t @@ -0,0 +1,4 @@ +import "lib/testlang" + +local a = image "astring" +print(a) diff --git a/samples/Terra/testlog.t b/samples/Terra/testlog.t new file mode 100644 index 00000000..2e015773 --- /dev/null +++ b/samples/Terra/testlog.t @@ -0,0 +1,8 @@ +terra foo(a : bool, b : bool, c : bool) return a and b or c end + +foo:disas() +terra callfoo() + var r = foo(true,false,true) + return r +end +assert(callfoo()) \ No newline at end of file diff --git a/samples/Terra/testrequire.t b/samples/Terra/testrequire.t new file mode 100644 index 00000000..e72084de --- /dev/null +++ b/samples/Terra/testrequire.t @@ -0,0 +1,14 @@ +local f = assert(io.popen("uname", 'r')) +local s = assert(f:read('*a')) +f:close() + +if s~="Darwin\n" then + print("Warning, not running test b/c this isn't a mac") +else + +local A = require("lib.objc") +local B = require("lib.objc") + +assert(A == B) + +end \ No newline at end of file diff --git a/samples/Terra/teststd.t b/samples/Terra/teststd.t new file mode 100644 index 00000000..8df604a9 --- /dev/null +++ b/samples/Terra/teststd.t @@ -0,0 +1,45 @@ +local S = require "std" + +c = global(int,0) + +struct A(S.Object) { + a : int; + b : int[5]; +} +struct C { + a : int +} +struct B(S.Object) { + a : A; + b : A[2]; + c : C[3]; + c2 : C; + c3 : C[2]; +} + +terra A:__destruct() + S.printf("A dtor!\n") + c = c + 1 +end +terra B:__destruct() + c = c + 10 +end +terra foo() + var a = A.salloc() +end + +foo() +assert(c:get() == 1) + +terra bar() + var b = B.salloc() +end +bar() +assert(c:get() == 14) + +terra baz() + var b = B.alloc() + b:delete() +end +baz() +assert(c:get() == 27) \ No newline at end of file diff --git a/samples/Terra/testvector.t b/samples/Terra/testvector.t new file mode 100644 index 00000000..7ba46c2b --- /dev/null +++ b/samples/Terra/testvector.t @@ -0,0 +1,57 @@ + +local S = require "std" + +local floatv = S.Vector(int) +terra foo() + var a = floatv.salloc():init() + for i = 0,100 do + a:insert(i) + end + for i = 0,50 do + a:insert(0,i) + end + for i = 0,50 do + a:insert(1,2,i) + end + for i = 0ULL,a:size() do + a(i) = a(i) + 1 + end + for i = 0ULL,50 do + @a:insert() = 5 + end + var s = 0 + for i = 0ULL,a:size() do + s = s + a(i) + end + var z = a:size() + S.assert(a:remove() == 5) + S.assert(a:size() == z - 1) + S.assert(a:remove(0) == 50) + S.assert(a:remove(0) == 50) + S.assert(a:remove(0) == 50) + S.assert(a(1) == 49) + return s +end +assert(foo() == 8875 + 5 *50) + +local g = global(int,0) + +struct A (S.Object) { + a : int +} + +terra A:__destruct() + g = g + self.a +end + +assert(S.Vector(int) == S.Vector(int)) + +terra foo2() + var a = [S.Vector(A)].salloc():init() + for i = 0,50 do + a:insert().a = i + end +end + +foo2() +assert(g:get() == 49*50/2) \ No newline at end of file diff --git a/samples/Terra/torturechain.t b/samples/Terra/torturechain.t new file mode 100644 index 00000000..91af6a06 --- /dev/null +++ b/samples/Terra/torturechain.t @@ -0,0 +1,67 @@ + +terra foo() + return 8.0,9 +end + +local torturechain = quote + var a = 3.0 +in + [quote + var b = 4 + in + [quote + var c = 5.0 + in + a,b,c, [quote + var d = 6 + in + [quote + var e = 7.0 + in + d,e,foo() + end] + end] + end] + end] +end + +terra bindit() + var a,b,c,r1 = torturechain + var d,e,r2 = r1 + var f,g = r2 + return a + b + c + d + e + f + g +end + +local sum = 3+4+5+6+7+8+9 +assert(sum == bindit()) + +terra foo(z : int, a : int, b : int, c : int, d : int, e : int, f : int, g : int) + return z + a + b + c + d + e + f + g +end + + +terra overloadit(a : int, b : int, c : int, d : int, e : int, f : int) + return 0 +end + +terra overloadit(a : double, b : int, c : int, d : int, e : int, f : int) + return 1 +end + + +terra callit() + var a,b,c,r1 = torturechain + var d,e,r2 = r1 + var f,g = r2 + return foo(0,a,b,c,d,e,f,g) +end + +terra callol() + var a,b,c,r1 = torturechain + var d,e,r2 = r1 + var f,g = r2 + return overloadit(a,b,c,e,f,g) +end + +assert(callit() == sum) +assert(callol() == 1) diff --git a/samples/Terra/toterraexpression.t b/samples/Terra/toterraexpression.t new file mode 100644 index 00000000..ba31fb7a --- /dev/null +++ b/samples/Terra/toterraexpression.t @@ -0,0 +1,8 @@ +local x = setmetatable({a = 4}, { __toterraexpression = function(self) return self.a end}) + + +local terra foo() + return x +end + +assert(foo() == 4) \ No newline at end of file diff --git a/samples/Terra/twolang.t b/samples/Terra/twolang.t new file mode 100644 index 00000000..fddc25e6 --- /dev/null +++ b/samples/Terra/twolang.t @@ -0,0 +1,23 @@ + +do +import "lib/foolang" +a = foo bar +import "lib/barlang" +b = bar foo +assert( foo bar + bar foo == 3) +assert(a + b == 3) +end + +local function failparse(str,match) + local r,msg = terralib.loadstring(str) + assert(not r) + local match = msg:match(match) + if not match then print(msg) end + assert(match) +end + +failparse("return (foo bar + bar foo)", "near 'bar'") +failparse('import "lib/foolang";return (foo bar + bar foo)', "near foo") +failparse('import "lib/foolang"; import "lib/foolang";',"entrypoint 'foo' already defined") + +do import "lib/foolang" end do import "lib/foolang" end \ No newline at end of file diff --git a/samples/Terra/typeexp.t b/samples/Terra/typeexp.t new file mode 100644 index 00000000..6f29586e --- /dev/null +++ b/samples/Terra/typeexp.t @@ -0,0 +1,9 @@ +atype = int +terra foo(a : atype) + var b : &atype = &a + return @b +end + +local test = require("test") + +test.eq(foo(3),3) \ No newline at end of file diff --git a/samples/Terra/union.t b/samples/Terra/union.t new file mode 100644 index 00000000..eb18b998 --- /dev/null +++ b/samples/Terra/union.t @@ -0,0 +1,26 @@ + +struct A { a : int, union { c : int8[6], b : int }, d : float, union { e : int, f : float }, g : int } +struct B { union { b : int , c : int8[6] }, a : int } +local C = struct { union { union { b : int} , c : int8[6] }, w : int } +terra foo() + var a : A + var b : B + var c : C + a.b = 4 + a.a = 5 + a.c[4] = 3 + a.d = 4 + return a.a + a.b + a.c[4] + a.d +end + +struct Stuff { union { a : int, b : float } } + +terra what() + var a : Stuff, b : Stuff + a.a,b.a = 3,4 + return a.b/b.b --== .75, this is the best bad interview question +end + +local test = require("test") +test.eq(foo(),16) +test.eq(what(),.75) \ No newline at end of file diff --git a/samples/Terra/unm.t b/samples/Terra/unm.t new file mode 100644 index 00000000..665e2060 --- /dev/null +++ b/samples/Terra/unm.t @@ -0,0 +1,20 @@ + + +struct A { + a : int +} + +A.metamethods.__unm = terra(self : &A) + return A { -self.a } +end + +A.metamethods.__sub = terra(self : &A, rhs : &A) + return A { self.a - rhs.a } +end + +terra doit() + var a,b = A { 1 } , A { 2 } + return (-(a - b)).a +end + +assert(doit() == 1) \ No newline at end of file diff --git a/samples/Terra/unpacktuple.t b/samples/Terra/unpacktuple.t new file mode 100644 index 00000000..7701455f --- /dev/null +++ b/samples/Terra/unpacktuple.t @@ -0,0 +1,30 @@ + + +terra foo() + return 1,2 +end + +terra foo2() + return {a = 1, b = 2} +end + +assert(unpacktuple(1) == 1) +assert(unpacktuple(foo()) == 1) + +assert(unpacktuple(foo(),2) == 2) + +assert(unpacktuple(foo(),nil,1) == 1) +assert(unpacktuple(foo2()).a == 1) + +assert(2 == #{unpacktuple(foo())}) + +assert(1 == #{unpacktuple(foo(),2)}) + +terra usefoo() + var a = foo() + var c,d = unpacktuple(a) + var e = unpacktuple(a,2) + return e +end + +assert(usefoo() == 2) \ No newline at end of file diff --git a/samples/Terra/unsafesym.t b/samples/Terra/unsafesym.t new file mode 100644 index 00000000..c8d2f2f2 --- /dev/null +++ b/samples/Terra/unsafesym.t @@ -0,0 +1,14 @@ + +local mymacro = macro(function(a) + a = a:asvalue() + print(a, terralib.issymbol(a), terralib.unsafetypeofsymbol(a)) + return {} +end) + + + +terra foo(a : int) + mymacro(a) +end + +foo:compile() \ No newline at end of file diff --git a/samples/Terra/unstrict.t b/samples/Terra/unstrict.t new file mode 100644 index 00000000..a4292ec0 --- /dev/null +++ b/samples/Terra/unstrict.t @@ -0,0 +1,5 @@ +Strict.__newindex,Strict.__index = nil,nil + +print(b) +terra foo() end +print(c) \ No newline at end of file diff --git a/samples/Terra/usercast.t b/samples/Terra/usercast.t new file mode 100644 index 00000000..f35bec84 --- /dev/null +++ b/samples/Terra/usercast.t @@ -0,0 +1,24 @@ + +struct A { a : int, b : float } + +function A.metamethods.__cast(from,to,exp) + if from == int and to == A then + return `A {exp, 1.f } + elseif from == float and to == A then + return `A { 1, exp } + end + error("invalid") +end + + +terra moo(a : A) + return a.a + a.b +end + +terra bar() + return moo(1) + moo(1.f) +end + +local test = require("test") + +test.eq(bar(), 4) \ No newline at end of file diff --git a/samples/Terra/varargcstring.t b/samples/Terra/varargcstring.t new file mode 100644 index 00000000..a9406390 --- /dev/null +++ b/samples/Terra/varargcstring.t @@ -0,0 +1,2 @@ +C = terralib.includec("stdio.h") +C.printf("hello, %d\n",3) \ No newline at end of file diff --git a/samples/Terra/vars.t b/samples/Terra/vars.t new file mode 100644 index 00000000..a8f38d3b --- /dev/null +++ b/samples/Terra/vars.t @@ -0,0 +1,23 @@ + + +a = {} +a.b, a.c = global(3),global(double,4) +d = global(5) +--print(a.b) + +--a.b:gettype(nil) + +terra bar() + a.b = a.b + 1 + a.c = a.c + 1 + d = d + 1 +end +terra foo() + return a.b,a.c,d +end + + +local test = require("test") + +bar() +test.meq({4,5,6},foo()) \ No newline at end of file diff --git a/samples/Terra/vars2.t b/samples/Terra/vars2.t new file mode 100644 index 00000000..e3556663 --- /dev/null +++ b/samples/Terra/vars2.t @@ -0,0 +1,17 @@ + +a = global(int) --testing variable with no initializer + + +terra foo() + a = 3 +end + +terra bar() + return a +end + + +local test = require("test") + +foo() +test.eq(bar(),3) \ No newline at end of file diff --git a/samples/Terra/vec.t b/samples/Terra/vec.t new file mode 100644 index 00000000..b3d6790c --- /dev/null +++ b/samples/Terra/vec.t @@ -0,0 +1,27 @@ + +terra foo(a : double) + var v0 : vector(double,3) = a + var v1 : vector(int,3) = 4 + var v2 = v0 / v1 + var v3 = (v0 <= v1) or (v1 >= v0) + var ptr = [&double](&v2) + return v3[0] or v3[1] or v3[2] + --return ptr[0] + ptr[1] + ptr[2] +end + + +N = 64 +terra addsomevecs(a : vector(double,N), b : vector(double,N), c : vector(double,N) ) + return (a + b) / c +end + + +terra foo2(a : double, b : double) + var c = addsomevecs(a,b,b) + return c[0] + c[1] +end + +local test = require("test") + +test.eq(foo(3), true) +test.eq(foo2(3,4), 3.5) \ No newline at end of file diff --git a/samples/Terra/vecarith.t b/samples/Terra/vecarith.t new file mode 100644 index 00000000..6a21b9ba --- /dev/null +++ b/samples/Terra/vecarith.t @@ -0,0 +1,138 @@ +--[[ + +- ++ + +* +/ +% + +^ +and +or +~= == < > >= <= +<< >> + +]] + + +N = 2 + +types = {int,int64,uint64,float,double} +names = {"i","l","u","f","d"} +max = 16 + +for i = 1,#types do + local fenv = getfenv() + for j = 0,max do + local x = global(vector(types[i],N)); + (terra() x = j end)() + fenv[names[i]..j] = x + end +end + + +d375 = global(vector(double,N)) +d175 = global(vector(double,N)) +terra init() + d375 = 3.75 + d175 = 1.75 +end +init() + +terra test0() + var a = i1 + i3 + var b = l1 + l2 + var c = u1 + u2 + var d = f1 + f2 + var e = d1 + d2 + return (a + b + c + d + e)[1] +end + +terra test1() + var a = i1 - i3 + var b = l1 - l2 + var c = u1 - u2 + var d = f1 - f2 + var e = d1 - d2 + return (a - b - c - d - e)[1] +end + +terra test2() + var a = i2 * i3 + var b = l3 * l2 + var c = u3 * u2 + var d = f1 * f2 + var e = d3 * d2 + return (a * b * c * d * e)[1] +end + +terra test3() + var a = i2 / i3 + i1 + var b = l3 / l2 + i1 + var c = u3 / u2 + var d = f1 / f2 + var e = d3 / d2 + return (a * b * c * d * e)[1] +end + +terra test4() + var a = i2 % i3 + var b = l3 % l4 + var c = u3 % u2 + var d = f1 % f2 + var e = d375 % d2 + return ((a == i2) and (b == l3) and (d == f1) and (e == d175))[1] +end + +terra test5() + var a = i2 ^ i3 + var b = l3 ^ l4 + var c = u3 ^ u2 + return ((a == 1) and (b == l7) and (c == u1))[1] +end + +terra test6() + var a = i2 and i3 + var b = l3 and l4 + var c = u3 and u2 + return ((a == 2) and (b == l0) and (c == u2))[1] +end + +terra test7() + var a = i2 or i3 + var b = l3 or l4 + var c = u3 or u2 + return ((a == 3) and (b == l7) and (c == u3))[1] +end + + +terra test8() + var a0,a1 = i2 ~= i3, i2 == i3 + var b0,b1 = i2 < i3, i2 >= i3 + var c0,c1 = i2 > i3, i2 <= i3 + + return (a0 and not a1 and b0 and not b1 and not c0 and c1)[1] +end + +terra test9() + var a0, a1 = i8 >> i1, i8 << i1 + var b0, b1 = -i8 >> i1, -i8 << i1 + return (a0 == i4 and a1 == i16 and b0 == -i4 and b1 == -i16)[1] +end + + +local test = require("test") + +test.eq(test0(),16) +test.eq(test1(),2) +test.eq(test2(),2592) +test.eq(test3(),1.5) + +test.eq(test4(),true) + +test.eq(test5(),true) +test.eq(test6(),true) +test.eq(test7(),true) +test.eq(test8(),true) +test.eq(test9(),true) \ No newline at end of file diff --git a/samples/Terra/veclit.t b/samples/Terra/veclit.t new file mode 100644 index 00000000..20e9aee8 --- /dev/null +++ b/samples/Terra/veclit.t @@ -0,0 +1,29 @@ + +terra foo() + var a = 2* vector(1,2,3) + return a[0] + a[1] + a[2] +end + +terra foo2() + var a = vector(1,2.5,3) + return a[1] +end +terra what() + return 3,4.5 +end +expwhat = macro(function(a) return {`a._0, `a._1} end) +terra foo3() + var a = vector(1,2.5,expwhat(what())) + return a[3] +end + +terra foo4() + var a = vectorof(int,1,2.5,3) + return a[1] +end + +local test = require("test") +test.eq(foo(),12) +test.eq(foo2(),2.5) +test.eq(foo3(),4.5) +test.eq(foo4(),2) \ No newline at end of file diff --git a/samples/Terra/vecobj.t b/samples/Terra/vecobj.t new file mode 100644 index 00000000..59eab9fa --- /dev/null +++ b/samples/Terra/vecobj.t @@ -0,0 +1,54 @@ +local Vec = terralib.memoize(function(typ,N) + N = assert(tonumber(N),"expected a number") + local ops = { "__sub","__add","__mul","__div" } + local struct VecType { + data : typ[N] + } + VecType.metamethods.type, VecType.metamethods.N = typ,N + VecType.metamethods.__typename = function(self) return ("%s_%d"):format(tostring(self.metamethods.type),self.metamethods.N) end + for i, op in ipairs(ops) do + local i = symbol("i") + local function template(ae,be) + return quote + var c : VecType + for [i] = 0,N do + c.data[i] = operator(op,ae,be) + end + return c + end + end + local terra doop(a : VecType, b : VecType) [template(`a.data[i],`b.data[i])] end + terra doop(a : typ, b : VecType) [template(`a,`b.data[i])] end + terra doop(a : VecType, b : typ) [template(`a.data[i],`b)] end + VecType.metamethods[op] = doop + end + terra VecType.methods.FromConstant(x : typ) + var c : VecType + for i = 0,N do + c.data[i] = x + end + return c + end + VecType.metamethods.__apply = macro(function(self,idx) return `self.data[idx] end) + VecType.metamethods.__cast = function(from,to,exp) + if from:isarithmetic() and to == VecType then + return `VecType.FromConstant(exp) + end + error(("unknown conversion %s to %s"):format(tostring(from),tostring(to))) + end + return VecType +end) + +terra foo(v : Vec(float,4), w : Vec(float,4)) + var z : Vec(float,4) = 1 + var x = (v*4)+w+1 + for i = 0,4 do + print(x(i)) + end + return x(2) +end + +foo:printpretty(true,false) +foo:disas() + +assert(20 == foo({{1,2,3,4}},{{5,6,7,8}})) \ No newline at end of file diff --git a/samples/Terra/vecptr.t b/samples/Terra/vecptr.t new file mode 100644 index 00000000..c853065f --- /dev/null +++ b/samples/Terra/vecptr.t @@ -0,0 +1,4 @@ +terra foo(a : &float) + var b = [&vector(float,4)](a) +end +foo:compile() \ No newline at end of file diff --git a/samples/Terra/vecsize.t b/samples/Terra/vecsize.t new file mode 100644 index 00000000..08043aee --- /dev/null +++ b/samples/Terra/vecsize.t @@ -0,0 +1,5 @@ + +assert(terralib.sizeof(int)*3 <= terralib.sizeof(vector(int,3))) +assert(terralib.sizeof(vector(int,1)) == 4) +assert(terralib.sizeof(vector(int,4)) == 16) +assert(terralib.sizeof(vector(int,5)) == 32) \ No newline at end of file diff --git a/samples/Terra/vtablerec.t b/samples/Terra/vtablerec.t new file mode 100644 index 00000000..75e9ea56 --- /dev/null +++ b/samples/Terra/vtablerec.t @@ -0,0 +1,30 @@ + +struct V { + entry : {&A} -> {} +} and +struct A { + vtable : &V +} + +terra what(a : &A) : {} +end + +what:compile() + +terra bar() + var v : V + v.entry = what +end + +bar:compile() + +--[[ +{&A}->{} +&A +A +&V +V +&{&A}->{} +{&A}->{} + +]] \ No newline at end of file diff --git a/samples/Terra/zeroargs.t b/samples/Terra/zeroargs.t new file mode 100644 index 00000000..59b4356d --- /dev/null +++ b/samples/Terra/zeroargs.t @@ -0,0 +1,5 @@ +local b = {} +terra foo() + return 1,b +end +foo() \ No newline at end of file diff --git a/samples/Terra/zeroreturn.t b/samples/Terra/zeroreturn.t new file mode 100644 index 00000000..406a3245 --- /dev/null +++ b/samples/Terra/zeroreturn.t @@ -0,0 +1,18 @@ + + +local b = global(0) + +terra foo(a : int) + if a > 3 then + return + end + b = a +end + +terra getb() return b end + +local test = require("test") +foo(4) +test.eq(getb(),0) +foo(2) +test.eq(getb(),2) \ No newline at end of file diff --git a/samples/Terra/zeroreturn2.t b/samples/Terra/zeroreturn2.t new file mode 100644 index 00000000..82f8e6df --- /dev/null +++ b/samples/Terra/zeroreturn2.t @@ -0,0 +1,17 @@ + +a = global(0) + +terra doit() + a = a + 1 +end +terra geta() + return a +end + +terra bar() + return doit() +end + +bar() +local test = require("test") +test.eq(geta(),1) \ No newline at end of file