[<<][meta][>>][..]
Sun Dec 18 10:03:33 EST 2011

C struct

Thinking about this doesn't seem to give any solution.  There's a
missing realization; I'm glossing over something important.

( I guess this is because this is that a structure definition is the
reification as a Term of Haskell type info, for which I have no direct
support. )

Let's just start to build it and see if it pops up.  First stop:
convert a Term Struct to C struct in PrettyC.hs

First thing that pops up: a the reason why Struct needs to be a Term
constructor is that it has a name.  Can it be nameless?  I.e can we
just extract the type from a nested variable declaration?  Making
Struct part of Term feels wrong, because it is not a value, just a
type.



Ok, so what is a structure?  Optionally it has a name, but it's just
an ordered list of variables, each with its own type.

   (Maybe String, [Var])

Where to start?  The AST of some C code of course.

  a2 = parse "struct foo {int a; float b;};"


  CTranslUnit
    [ CDeclExt (
        CDecl
          [ CTypeSpec (
              CSUType (
                CStruct
                  CStructTag (
                  Just "foo" ) (
                  Just
                    [ CDecl
                        [ CTypeSpec ( CIntType () )
                        ]
                        [ ( Just (
                              CDeclr (
                                Just "a" )
                                []
                                Nothing
                                []
                                () )
                          , Nothing
                          , Nothing
                          )
                        ]
                        ()
                    , CDecl
                        [ CTypeSpec ( CFloatType () )
                        ]
                        [ ( Just (
                              CDeclr (
                                Just "b" )
                                []
                                Nothing
                                []
                                () )
                          , Nothing
                          , Nothing
                          )
                        ]
                        ()
                    ] )
                  []
                  () )
                () )
          ]
          []
          () )
    ]
    ()

It's probably best to start at CTypeSpec.  Filling it in top-down,
replacing undefined by actual members I get to this, which reuses
cVarDecl which maps Term Var to AST elements:

  cStructDecl name vars = 
    CTypeSpec 
    (CSUType 
     (CStruct CStructTag 
      (Just $ ident name)
      (Just -- [CDeclaration a]
       (map (\var -> cVarDecl Nothing var) vars))
      [] -- CAttribute a
      ())
     ()) 


The test (with code printing AST as C code) gives:

  tv vn = Var (Type AFloat []) vn
  tvs ns = map (\n -> tv ("mem" ++ show n)) ns
  t8 = cStructDecl "foo" (tvs [1..10])

  *PrettyC> code t8
  struct foo {
      float mem1;
      float mem2;
      float mem3;
      float mem4;
      float mem5;
      float mem6;
      float mem7;
      float mem8;
      float mem9;
      float mem10;
  }
  *PrettyC> 


So, next is integration.  First I'd like to see if it's possible to
use unnamed structs.  That would separate dealing with the type and
dealing with a shortcut name for it.

What I want to do is something like this:

  float foo123(struct {float a, float b} arg1) {
      return arg1.a;
  }

That doesn't work.  So let's split it in two parts:

1. Find a canonical name for each struct.  This is equivalent to using
   tuples (type products) vs. tagged tuples (abstract data types).

2. Somehow insert the tagged tuples into the C code generator.  This
   probably means it will need to become monadic.

Making the generator monadic is probably a pain, though there seems to
be no way around it.


Got this going.

   cTupleDecl vars = cStructDecl canonicalName vars where
     canonicalName = "tuple" ++ (concat $ map shortTag vars)
     shortTag (Var (Type typeName order) _) = "_" ++ bt typeName ++ ot order where
       -- ot order = concat $ map (\_ -> "x") order
       ot order = show $ length order
       bt AFloat = "f"
       bt AInt   = "i"
       bt ABool  = "b"

   t10 = cTupleDecl [
     Var (Type ABool []) "mem0",
     Var (Type AInt [noTypeDims]) "mem1",
     Var (Type AFloat [noTypeDims, noTypeDims]) "mem2"
     ]

   *PrettyC> code t10
   struct tuple_b0_i1_f2 {
       int mem0; int * mem1; float * * mem2;
   }



[Reply][About]
[<<][meta][>>][..]