SCM Repository
View of /branches/vis12/src/compiler/c-util/c-ty-translate.sml
Parent Directory
|
Revision Log
Revision 2813 -
(download)
(annotate)
Sat Nov 8 21:48:17 2014 UTC (6 years, 3 months ago) by jhr
File size: 8869 byte(s)
Sat Nov 8 21:48:17 2014 UTC (6 years, 3 months ago) by jhr
File size: 8869 byte(s)
Fix for bug 019 (allowing richer initializers for input globals).
(* c-ty-translate.sml * * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu) * All rights reserved. * * Functions to support the translation between the external C API and the internal (host-side) * Diderot representation. *) signature TY_TRANSLATE = sig (* translate a type to its internal Diderot representation *) val toType : TreeIL.Ty.ty -> CLang.ty (* translate a TreeIL type to the C types used to represent it in the external API *) val toCType : TreeIL.Ty.ty -> CLang.ty (* return true if the external API typed used to represent a TreeIL type is a C array type *) val isCArrayTy : TreeIL.Ty.ty -> bool (* generate code to copy values from their internal Diderot representation to the external * C representation. *) val copyToC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list (* generate code to copy values from their external C representation to the internal * Diderot representation. *) val copyFromC : {ty : TreeIL.Ty.ty, dst : CLang.exp, src : CLang.exp} -> CLang.stm list (* return a CLang expression that represents the size of the given Diderot type *) val sizeOfType : TreeIL.Ty.ty -> CLang.exp end structure CTyTranslate : TY_TRANSLATE = struct structure Ty = TreeIL.Ty structure CL = CLang structure N = CNames (* translate a type to its internal Diderot representation *) fun toType ty = (case ty of Ty.BoolTy => CL.T_Named "bool" | Ty.StringTy => CL.T_Named "Diderot_string_t" | Ty.IntTy => CL.T_Named "Diderot_int_t" | Ty.TensorTy[] => CL.T_Named "Diderot_real_t" | Ty.TensorTy[n] => CL.T_Named(N.vecTy n) | Ty.TensorTy[n, m] => CL.T_Named(N.matTy(n,m)) | Ty.TensorTy[n, m, l] => CL.T_Named(N.ten3Ty(n,m,l)) | Ty.SeqTy(Ty.IntTy, n) => CL.T_Named(N.ivecTy n) | Ty.SeqTy(ty, n) => CL.T_Array(toType ty, SOME n) (* do we make the following types externally visible? *) | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy) | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info)) | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info))) | _ => raise Fail(concat["CTyTranslate.toType(", Ty.toString ty, ")"]) (* end case *)) (* translate a TreeIL type to the C types used to represent it in the external API *) fun toCType ty = (case ty of Ty.BoolTy => CL.T_Named "bool" | Ty.StringTy => CL.charPtr | Ty.IntTy => !N.gIntTy | Ty.TensorTy[] => !N.gRealTy | Ty.TensorTy[n] => CL.T_Array(!N.gRealTy, SOME n) | Ty.TensorTy[n, m] => CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m) | Ty.TensorTy[n, m, l] => CL.T_Array(CL.T_Array(CL.T_Array(!N.gRealTy, SOME n), SOME m), SOME l) | Ty.SeqTy(Ty.IntTy, n) => CL.T_Array(!N.gIntTy, SOME n) | Ty.SeqTy(ty, n) => CL.T_Array(toCType ty, SOME n) | Ty.DynSeqTy _ => CL.T_Ptr(CL.T_Named N.dynSeqTy) | Ty.AddrTy info => CL.T_Ptr(CL.T_Num(ImageInfo.sampleTy info)) | Ty.ImageTy info => CL.T_Ptr(CL.T_Named(N.imageTy(ImageInfo.dim info))) | _ => raise Fail(concat["CTyTranslate.toCType(", Ty.toString ty, ")"]) (* end case *)) (* return true if the external API typed used to represent a TreeIL type is a C array type *) fun isCArrayTy ty = (case ty of Ty.TensorTy[n] => true | Ty.TensorTy[n, m] => true | Ty.TensorTy[n, m, l] => true | Ty.SeqTy(Ty.IntTy, n) => true | Ty.SeqTy(ty, n) => true | _ => false (* end case *)) fun subscript (e, i) = CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i)) (* we might want to use memcpy in some cases fun copy (ty, dst, src) = let fun assign () = CL.mkAssign(dst, src) fun addrOf (CL.E_UnOp(CL.%*, x)) = x | addrOf x = CL.mkUnOp(CL.%&, x) fun memcpy () = CL.mkCall("memcpy", [addrOf dst, addrOf src, CL.mkSizeof(trType ty)]) in case ty of Ty.BoolTy => assign() | Ty.StringTy => CL.mkCall("strcpy", [addrOf dst, addrOf src]) | Ty.IntTy => assign() | Ty.TensorTy[] => assign() | Ty.TensorTy _ => memcpy() | Ty.SeqTy _ => memcpy() | Ty.DynSeqTy _ => raise Fail "dynamic sequence" | Ty.ImageTy _ => raise Fail "unexpected image copy" | _ => raise Fail(concat["bogus input type ", Ty.toString ty]) (* end case *) end *) (* generate code to copy values from their internal Diderot representation to the external * C representation. *) fun copyToC {ty, dst, src} = (case ty of Ty.BoolTy => [CL.mkAssign(dst, src)] | Ty.StringTy => [CL.mkAssign(dst, src)] | Ty.IntTy => [CL.mkAssign(dst, src)] | Ty.TensorTy[] => [CL.mkAssign(dst, src)] | Ty.TensorTy[n] => let val src' = CL.mkCast(CL.T_Named(N.unionTy n), src) in List.tabulate (n, fn i => CL.mkAssign( subscript (dst, i), subscript (CL.mkSelect(src', "r"), i))) end | Ty.TensorTy[n, m] => List.concat(List.tabulate(n, fn i => List.tabulate (m, fn j => CL.mkAssign( subscript (dst, i*m + j), subscript(CL.mkSelect(subscript (src, i), "r"), j))))) | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n, fn i => List.concat(List.tabulate (m, fn j => List.tabulate (l, fn k => CL.mkAssign( subscript(dst, i*m*l + j*l + k), subscript(CL.mkSelect(subscript(subscript(src, i), j), "r"), j))))))) | Ty.SeqTy(Ty.IntTy, n) => List.tabulate (n, fn i => CL.mkAssign( subscript (dst, i), subscript (CL.mkSelect(src, "i"), i))) (* do we make the following types externally visible? | Ty.SeqTy(ty, n) => | Ty.DynSeqTy _ => | Ty.AddrTy info => | Ty.ImageTy info => *) | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"]) (* end case *)) (* generate code to copy values from their external C representation to the internal * Diderot representation. *) fun copyFromC {ty, dst, src} = (case ty of Ty.BoolTy => [CL.mkAssign(dst, src)] | Ty.StringTy => [CL.mkAssign(dst, src)] | Ty.IntTy => [CL.mkAssign(dst, src)] | Ty.TensorTy[] => [CL.mkAssign(dst, src)] | Ty.TensorTy[n] => [CL.mkAssign( dst, CL.mkApply(N.mkVec n, List.tabulate(n, fn i => subscript(src, i))))] | Ty.TensorTy[n, m] => List.tabulate(n, fn i => CL.mkAssign( CL.mkSelect(subscript(dst, i), "v"), CL.mkApply(N.mkVec n, List.tabulate(n, fn j => subscript(src, i*m + j))))) | Ty.TensorTy[n, m, l] => List.concat(List.tabulate(n, fn i => List.tabulate (m, fn j => CL.mkAssign( CL.mkSelect(subscript(subscript(dst, i), j), "v"), CL.mkApply(N.mkVec n, List.tabulate(n, fn k => subscript(src, i*m*l + j*l + k))))))) | Ty.SeqTy(Ty.IntTy, n) => [ CL.mkAssign( CL.mkSelect(dst, "v"), CL.mkApply(N.mkIVec n, List.tabulate(n, fn i => subscript(src, i)))) ] (* do we make the following types externally visible? | Ty.SeqTy(ty, n) => | Ty.DynSeqTy _ => | Ty.AddrTy info => | Ty.ImageTy info => *) | _ => raise Fail(concat["CTyTranslate.copyToC(", Ty.toString ty, ")"]) (* end case *)) (* return a CLang expression that represents the size of the given Diderot type *) fun sizeOfType ty = let fun tensorSz n = CL.mkBinOp(CL.mkInt(IntInf.fromInt n), CL.#*, CL.E_Var "SIZEOF_DIDEROT_REAL") in case ty of Ty.BoolTy => CL.mkSizeof CL.boolTy | Ty.StringTy => CL.mkSizeof(CL.T_Named "Diderot_string_t") | Ty.IntTy => CL.E_Var "SIZEOF_DIDEROT_INT" | Ty.TensorTy[] => CL.E_Var "SIZEOF_DIDEROT_REAL" | Ty.TensorTy[n] => tensorSz n | Ty.TensorTy[n, m] => tensorSz (n*m) | Ty.TensorTy[n, m, l] => tensorSz (n*m*l) | Ty.SeqTy(Ty.IntTy, n) => CL.mkSizeof(CL.T_Named(N.ivecTy n)) | Ty.SeqTy(ty, n) => CL.mkSizeof(CL.T_Array(toType ty, SOME n)) (* do we make the following types externally visible? *) | Ty.DynSeqTy _ => CL.mkSizeof CL.voidTy | Ty.AddrTy info => CL.mkSizeof CL.voidTy | Ty.ImageTy info => CL.mkSizeof CL.voidTy | _ => raise Fail(concat["CTyTranslate.sizeOfType(", Ty.toString ty, ")"]) (* end case *) end end
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |