From: Duncan C. <dun...@us...> - 2005-07-27 16:29:13
|
Update of /cvsroot/gtk2hs/gtk2hs/tools/c2hs/c In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5357/tools/c2hs/c Modified Files: CAST.hs CLexer.x CParser.y Log Message: Make c2hs understand asm things: CAST.hs: add new asm constructors for a couple data types. But only as place holders, they do not hold the whole asm statement. CLexer.x: add the asm/__asm/__asm__ keyword. CParser.y: add support for asm top level declarations, asm statements (including GNU C extended asm syntax) and asm annotations on function declarations. Also support C string literal concatentation (which is often used in asm statements). Index: CParser.y =================================================================== RCS file: /cvsroot/gtk2hs/gtk2hs/tools/c2hs/c/CParser.y,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- CParser.y 3 Jul 2005 14:58:16 -0000 1.3 +++ CParser.y 27 Jul 2005 16:28:47 -0000 1.4 @@ -162,6 +162,7 @@ '}' { CTokRBrace _ } -- 45 "..." { CTokEllipsis _ } -- 46 alignof { CTokAlignof _ } -- 47 +asm { CTokAsm _ } auto { CTokAuto _ } -- 48 break { CTokBreak _ } -- 49 case { CTokCase _ } -- 50 @@ -222,6 +223,8 @@ translation_unit : {- empty -} { [] } | translation_unit external_declaration { $2 : $1 } + | translation_unit asm '(' expression ')' ';' + {% withAttrs $2 $ \at -> CAsmExt at : $1 } -- parse external C declaration (K&R A10) @@ -253,12 +256,13 @@ | selection_statement { $1 } | iteration_statement { $1 } | jump_statement { $1 } + | asm_statement { $1 } statement_list :: { [CStat] } statement_list - : {- empty -} { [] } - | statement_list statement { $2 : $1 } + : {- empty -} { [] } + | statement_list statement { $2 : $1 } -- parse C labeled statement (K&R A9.1) @@ -334,6 +338,52 @@ | return expression ';' {% withAttrs $1 $ CReturn (Just $2) } +-- parse GNU C __asm__ (...) statement (recording only a place holder result) +-- +asm_statement :: { CStat } +asm_statement + : asm maybe_type_qualifier '(' expression ')' ';' + {% withAttrs $1 CAsm } + | asm maybe_type_qualifier '(' expression ':' asm_operands ')' ';' + {% withAttrs $1 CAsm } + | asm maybe_type_qualifier '(' expression ':' asm_operands + ':' asm_operands ')' ';' + {% withAttrs $1 CAsm } + | asm maybe_type_qualifier '(' expression ':' asm_operands ':' asm_operands + ':' asm_clobbers ')' ';' + {% withAttrs $1 CAsm } + +maybe_type_qualifier :: { () } +maybe_type_qualifier + : {- empty -} { () } + | type_qualifier { () } + + +asm_operands :: { () } +asm_operands + : {- empty -} { () } + | nonnull_asm_operands { () } + + +nonnull_asm_operands :: { () } +nonnull_asm_operands + : asm_operand { () } + | nonnull_asm_operands ',' asm_operand { () } + + +asm_operand :: { () } +asm_operand + : string '(' expression ')' { () } + | '[' ident ']' string '(' expression ')' { () } + | '[' tyident ']' string '(' expression ')' { () } + + +asm_clobbers :: { () } +asm_clobbers + : string { () } + | asm_clobbers ',' string { () } + + -- parse C declaration (K&R A8) -- -- * We allow the GNU C extension keyword before a declaration and GNU C @@ -394,8 +444,14 @@ -- init_declarator :: { (CDeclr, Maybe CInit) } init_declarator - : declarator { ($1, Nothing) } - | declarator '=' initializer { ($1, Just $3) } + : declarator maybe_asm { ($1, Nothing) } + | declarator maybe_asm '=' initializer { ($1, Just $4) } + + +maybe_asm :: { () } +maybe_asm + : {- empty -} { () } + | asm '(' string ')' { () } init_declarator_list :: { [(CDeclr, Maybe CInit)] } @@ -988,7 +1044,23 @@ : cint {% withAttrs $1 $ case $1 of CTokILit _ i -> CIntConst i } | cchar {% withAttrs $1 $ case $1 of CTokCLit _ c -> CCharConst c } | cfloat {% withAttrs $1 $ case $1 of CTokFLit _ f -> CFloatConst f } - | cstr {% withAttrs $1 $ case $1 of CTokSLit _ s -> CStrConst s } + | string {% withAttrs $1 $ CStrConst (unL $1) } + + +-- deal with C string liternal concatination +-- +string :: { Located String } +string + : cstr { case $1 of CTokSLit _ s -> L s (posOf $1) } + | cstr string_ { case $1 of CTokSLit _ s -> + let s' = concat (s : reverse $2) + in L s' (posOf $1) } + + +string_ :: { [String] } +string_ + : cstr { case $1 of CTokSLit _ s -> [s] } + | string_ cstr { case $2 of CTokSLit _ s -> s : $1 } -- parse GNU C __extension__ annotation (junking the result) Index: CLexer.x =================================================================== RCS file: /cvsroot/gtk2hs/gtk2hs/tools/c2hs/c/CLexer.x,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- CLexer.x 3 Jul 2005 14:58:16 -0000 1.3 +++ CLexer.x 27 Jul 2005 16:28:31 -0000 1.4 @@ -282,6 +282,9 @@ | CTokAlignof Position -- `alignof' -- (or `__alignof', -- `__alignof__') + | CTokAsm Position -- `asm' + -- (or `__asm', + -- `__asm__') | CTokAuto Position -- `auto' | CTokBreak Position -- `break' | CTokCase Position -- `case' @@ -389,6 +392,7 @@ posOf (CTokRBrace pos ) = pos posOf (CTokEllipsis pos ) = pos posOf (CTokAlignof pos ) = pos + posOf (CTokAsm pos ) = pos posOf (CTokAuto pos ) = pos posOf (CTokBreak pos ) = pos posOf (CTokCase pos ) = pos @@ -479,6 +483,7 @@ showsPrec _ (CTokRBrace _ ) = showString "}" showsPrec _ (CTokEllipsis _ ) = showString "..." showsPrec _ (CTokAlignof _ ) = showString "alignof" + showsPrec _ (CTokAsm _ ) = showString "asm" showsPrec _ (CTokAuto _ ) = showString "auto" showsPrec _ (CTokBreak _ ) = showString "break" showsPrec _ (CTokCase _ ) = showString "case" @@ -532,6 +537,9 @@ idkwtok ('a':'l':'i':'g':'n':'o':'f':[]) = tok CTokAlignof idkwtok ('_':'_':'a':'l':'i':'g':'n':'o':'f':[]) = tok CTokAlignof idkwtok ('_':'_':'a':'l':'i':'g':'n':'o':'f':'_':'_':[]) = tok CTokAlignof +idkwtok ('a':'s':'m':[]) = tok CTokAsm +idkwtok ('_':'_':'a':'s':'m':[]) = tok CTokAsm +idkwtok ('_':'_':'a':'s':'m':'_':'_':[]) = tok CTokAsm idkwtok ('a':'u':'t':'o':[]) = tok CTokAuto idkwtok ('b':'r':'e':'a':'k':[]) = tok CTokBreak idkwtok ('c':'a':'s':'e':[]) = tok CTokCase Index: CAST.hs =================================================================== RCS file: /cvsroot/gtk2hs/gtk2hs/tools/c2hs/c/CAST.hs,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- CAST.hs 22 Jun 2005 16:01:20 -0000 1.3 +++ CAST.hs 27 Jul 2005 16:28:30 -0000 1.4 @@ -68,14 +68,18 @@ -- data CExtDecl = CDeclExt CDecl | CFDefExt CFunDef + | CAsmExt Attrs -- a chunk of assembly code (which is + -- not itself recorded) instance Pos CExtDecl where posOf (CDeclExt decl) = posOf decl posOf (CFDefExt fdef) = posOf fdef + posOf (CAsmExt at) = posOf at instance Eq CExtDecl where CDeclExt decl1 == CDeclExt decl2 = decl1 == decl2 CFDefExt fdef1 == CFDefExt fdef2 = fdef1 == fdef2 + CAsmExt at1 == CAsmExt at2 = at1 == at2 -- C function definition (K&R A10.1) (EXPORTED) -- @@ -137,6 +141,8 @@ | CBreak Attrs -- break statement | CReturn (Maybe CExpr) Attrs + | CAsm Attrs -- a chunk of assembly code (which is + -- not itself recorded) instance Pos CStat where posOf (CLabel _ _ at) = posOf at @@ -152,6 +158,7 @@ posOf (CCont at) = posOf at posOf (CBreak at) = posOf at posOf (CReturn _ at) = posOf at + posOf (CAsm at) = posOf at instance Eq CStat where (CLabel _ _ at1) == (CLabel _ _ at2) = at1 == at2 @@ -167,6 +174,7 @@ (CCont at1) == (CCont at2) = at1 == at2 (CBreak at1) == (CBreak at2) = at1 == at2 (CReturn _ at1) == (CReturn _ at2) = at1 == at2 + (CAsm at1) == (CAsm at2) = at1 == at2 -- C declaration (K&R A8), structure declaration (K&R A8.3), parameter -- declaration (K&R A8.6.3), and type name (K&R A8.8) (EXPORTED) |