PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/src/cil.ml

http://github.com/kerneis/cil
OCaml | 7133 lines | 4914 code | 843 blank | 1376 comment | 749 complexity | 2516d5954db74d71c239e40812629e61 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. (*
  2. *
  3. * Copyright (c) 2001-2003,
  4. * George C. Necula <necula@cs.berkeley.edu>
  5. * Scott McPeak <smcpeak@cs.berkeley.edu>
  6. * Wes Weimer <weimer@cs.berkeley.edu>
  7. * Ben Liblit <liblit@cs.berkeley.edu>
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are
  12. * met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. The names of the contributors may not be used to endorse or promote
  22. * products derived from this software without specific prior written
  23. * permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  27. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  28. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  29. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. *)
  38. open Escape
  39. open Pretty
  40. open Cilint
  41. (* open Trace (\* sm: 'trace' function *\) *)
  42. module E = Errormsg
  43. module H = Hashtbl
  44. module IH = Inthash
  45. (*
  46. * CIL: An intermediate language for analyzing C progams.
  47. *
  48. * Scott McPeak, George Necula, Wes Weimer
  49. *
  50. *)
  51. (* The module Cilversion is generated automatically by Makefile from
  52. * information in configure.in *)
  53. let cilVersion = Cilversion.cilVersion
  54. let cilVersionMajor = Cilversion.cilVersionMajor
  55. let cilVersionMinor = Cilversion.cilVersionMinor
  56. let cilVersionRevision = Cilversion.cilVersionRev
  57. (* A few globals that control the interpretation of C source *)
  58. let msvcMode = ref false (* Whether the pretty printer should
  59. * print output for the MS VC
  60. * compiler. Default is GCC *)
  61. let c99Mode = ref false (* True to handle ISO C 99 vs 90 changes.
  62. So far only affects integer parsing. *)
  63. (* Set this to true to get old-style handling of gcc's extern inline C extension:
  64. old-style: the extern inline definition is used until the actual definition is
  65. seen (as long as optimization is enabled)
  66. new-style: the extern inline definition is used only if there is no actual
  67. definition (as long as optimization is enabled)
  68. Note that CIL assumes that optimization is always enabled ;-) *)
  69. let oldstyleExternInline = ref false
  70. let makeStaticGlobal = ref true
  71. let useLogicalOperators = ref false
  72. let useComputedGoto = ref false
  73. let useCaseRange = ref false
  74. module M = Machdep
  75. (* Cil.initCil will set this to the current machine description.
  76. Makefile.cil generates the file src/machdep.ml,
  77. which contains the descriptions of gcc and msvc. *)
  78. let envMachine : M.mach option ref = ref None
  79. let lowerConstants: bool ref = ref true
  80. (** Do lower constants (default true) *)
  81. let insertImplicitCasts: bool ref = ref true
  82. (** Do insert implicit casts (default true) *)
  83. let little_endian = ref true
  84. let char_is_unsigned = ref false
  85. let underscore_name = ref false
  86. type lineDirectiveStyle =
  87. | LineComment (** Before every element, print the line
  88. * number in comments. This is ignored by
  89. * processing tools (thus errors are reproted
  90. * in the CIL output), but useful for
  91. * visual inspection *)
  92. | LineCommentSparse (** Like LineComment but only print a line
  93. * directive for a new source line *)
  94. | LinePreprocessorInput (** Use #line directives *)
  95. | LinePreprocessorOutput (** Use # nnn directives (in gcc mode) *)
  96. let lineDirectiveStyle = ref (Some LinePreprocessorInput)
  97. let print_CIL_Input = ref false
  98. let printCilAsIs = ref false
  99. let lineLength = ref 80
  100. let warnTruncate = ref true
  101. (* sm: return the string 's' if we're printing output for gcc, suppres
  102. * it if we're printing for CIL to parse back in. the purpose is to
  103. * hide things from gcc that it complains about, but still be able
  104. * to do lossless transformations when CIL is the consumer *)
  105. let forgcc (s: string) : string =
  106. if (!print_CIL_Input) then "" else s
  107. let debugConstFold = false
  108. (** The Abstract Syntax of CIL *)
  109. (** The top-level representation of a CIL source file. Its main contents is
  110. the list of global declarations and definitions. *)
  111. type file =
  112. { mutable fileName: string; (** The complete file name *)
  113. mutable globals: global list; (** List of globals as they will appear
  114. in the printed file *)
  115. mutable globinit: fundec option;
  116. (** An optional global initializer function. This is a function where
  117. * you can put stuff that must be executed before the program is
  118. * started. This function, is conceptually at the end of the file,
  119. * although it is not part of the globals list. Use {!Cil.getGlobInit}
  120. * to create/get one. *)
  121. mutable globinitcalled: bool;
  122. (** Whether the global initialization function is called in main. This
  123. should always be false if there is no global initializer. When
  124. you create a global initialization CIL will try to insert code in
  125. main to call it. *)
  126. }
  127. and comment = location * string
  128. (** The main type for representing global declarations and definitions. A list
  129. of these form a CIL file. The order of globals in the file is generally
  130. important. *)
  131. and global =
  132. | GType of typeinfo * location
  133. (** A typedef. All uses of type names (through the [TNamed] constructor)
  134. must be preceeded in the file by a definition of the name. The string
  135. is the defined name and always not-empty. *)
  136. | GCompTag of compinfo * location
  137. (** Defines a struct/union tag with some fields. There must be one of
  138. these for each struct/union tag that you use (through the [TComp]
  139. constructor) since this is the only context in which the fields are
  140. printed. Consequently nested structure tag definitions must be
  141. broken into individual definitions with the innermost structure
  142. defined first. *)
  143. | GCompTagDecl of compinfo * location
  144. (** Declares a struct/union tag. Use as a forward declaration. This is
  145. * printed without the fields. *)
  146. | GEnumTag of enuminfo * location
  147. (** Declares an enumeration tag with some fields. There must be one of
  148. these for each enumeration tag that you use (through the [TEnum]
  149. constructor) since this is the only context in which the items are
  150. printed. *)
  151. | GEnumTagDecl of enuminfo * location
  152. (** Declares an enumeration tag. Use as a forward declaration. This is
  153. * printed without the items. *)
  154. | GVarDecl of varinfo * location
  155. (** A variable declaration (not a definition). If the variable has a
  156. function type then this is a prototype. There can be several
  157. declarations and at most one definition for a given variable. If both
  158. forms appear then they must share the same varinfo structure. A
  159. prototype shares the varinfo with the fundec of the definition. Either
  160. has storage Extern or there must be a definition in this file *)
  161. | GVar of varinfo * initinfo * location
  162. (** A variable definition. Can have an initializer. The initializer is
  163. * updateable so that you can change it without requiring to recreate
  164. * the list of globals. There can be at most one definition for a
  165. * variable in an entire program. Cannot have storage Extern or function
  166. * type. *)
  167. | GFun of fundec * location
  168. (** A function definition. *)
  169. | GAsm of string * location (** Global asm statement. These ones
  170. can contain only a template *)
  171. | GPragma of attribute * location (** Pragmas at top level. Use the same
  172. syntax as attributes *)
  173. | GText of string (** Some text (printed verbatim) at
  174. top level. E.g., this way you can
  175. put comments in the output. *)
  176. (** The various types available. Every type is associated with a list of
  177. * attributes, which are always kept in sorted order. Use {!Cil.addAttribute}
  178. * and {!Cil.addAttributes} to construct list of attributes. If you want to
  179. * inspect a type, you should use {!Cil.unrollType} to see through the uses
  180. * of named types. *)
  181. and typ =
  182. TVoid of attributes (** Void type *)
  183. | TInt of ikind * attributes (** An integer type. The kind specifies
  184. the sign and width. *)
  185. | TFloat of fkind * attributes (** A floating-point type. The kind
  186. specifies the precision. *)
  187. | TPtr of typ * attributes
  188. (** Pointer type. *)
  189. | TArray of typ * exp option * attributes
  190. (** Array type. It indicates the base type and the array length. *)
  191. | TFun of typ * (string * typ * attributes) list option * bool * attributes
  192. (** Function type. Indicates the type of the result, the name, type
  193. * and name attributes of the formal arguments ([None] if no
  194. * arguments were specified, as in a function whose definition or
  195. * prototype we have not seen; [Some \[\]] means void). Use
  196. * {!Cil.argsToList} to obtain a list of arguments. The boolean
  197. * indicates if it is a variable-argument function. If this is the
  198. * type of a varinfo for which we have a function declaration then
  199. * the information for the formals must match that in the
  200. * function's sformals. *)
  201. | TNamed of typeinfo * attributes
  202. (** The use of a named type. All uses of the same type name must
  203. * share the typeinfo. Each such type name must be preceeded
  204. * in the file by a [GType] global. This is printed as just the
  205. * type name. The actual referred type is not printed here and is
  206. * carried only to simplify processing. To see through a sequence
  207. * of named type references, use {!Cil.unrollType}. The attributes
  208. * are in addition to those given when the type name was defined. *)
  209. | TComp of compinfo * attributes
  210. (** A reference to a struct or a union type. All references to the
  211. same struct or union must share the same compinfo among them and
  212. with a [GCompTag] global that preceeds all uses (except maybe
  213. those that are pointers to the composite type). The attributes
  214. given are those pertaining to this use of the type and are in
  215. addition to the attributes that were given at the definition of
  216. the type and which are stored in the compinfo. *)
  217. | TEnum of enuminfo * attributes
  218. (** A reference to an enumeration type. All such references must
  219. share the enuminfo among them and with a [GEnumTag] global that
  220. preceeds all uses. The attributes refer to this use of the
  221. enumeration and are in addition to the attributes of the
  222. enumeration itself, which are stored inside the enuminfo *)
  223. | TBuiltin_va_list of attributes
  224. (** This is the same as the gcc's type with the same name *)
  225. (** Various kinds of integers *)
  226. and ikind =
  227. IChar (** [char] *)
  228. | ISChar (** [signed char] *)
  229. | IUChar (** [unsigned char] *)
  230. | IBool (** [_Bool (C99)] *)
  231. | IInt (** [int] *)
  232. | IUInt (** [unsigned int] *)
  233. | IShort (** [short] *)
  234. | IUShort (** [unsigned short] *)
  235. | ILong (** [long] *)
  236. | IULong (** [unsigned long] *)
  237. | ILongLong (** [long long] (or [_int64] on Microsoft Visual C) *)
  238. | IULongLong (** [unsigned long long] (or [unsigned _int64] on Microsoft
  239. Visual C) *)
  240. (** Various kinds of floating-point numbers*)
  241. and fkind =
  242. FFloat (** [float] *)
  243. | FDouble (** [double] *)
  244. | FLongDouble (** [long double] *)
  245. (** An attribute has a name and some optional parameters *)
  246. and attribute = Attr of string * attrparam list
  247. (** Attributes are lists sorted by the attribute name *)
  248. and attributes = attribute list
  249. (** The type of parameters in attributes *)
  250. and attrparam =
  251. | AInt of int (** An integer constant *)
  252. | AStr of string (** A string constant *)
  253. | ACons of string * attrparam list (** Constructed attributes. These
  254. are printed [foo(a1,a2,...,an)].
  255. The list of parameters can be
  256. empty and in that case the
  257. parentheses are not printed. *)
  258. | ASizeOf of typ (** A way to talk about types *)
  259. | ASizeOfE of attrparam
  260. | ASizeOfS of typsig (** Replacement for ASizeOf in type
  261. signatures. Only used for
  262. attributes inside typsigs.*)
  263. | AAlignOf of typ
  264. | AAlignOfE of attrparam
  265. | AAlignOfS of typsig
  266. | AUnOp of unop * attrparam
  267. | ABinOp of binop * attrparam * attrparam
  268. | ADot of attrparam * string (** a.foo **)
  269. | AStar of attrparam (** * a *)
  270. | AAddrOf of attrparam (** & a **)
  271. | AIndex of attrparam * attrparam (** a1[a2] *)
  272. | AQuestion of attrparam * attrparam * attrparam (** a1 ? a2 : a3 **)
  273. (** Information about a composite type (a struct or a union). Use
  274. {!Cil.mkCompInfo}
  275. to create non-recursive or (potentially) recursive versions of this. Make
  276. sure you have a [GCompTag] for each one of these. *)
  277. and compinfo = {
  278. mutable cstruct: bool; (** True if struct, False if union *)
  279. mutable cname: string; (** The name. Always non-empty. Use
  280. * {!Cil.compFullName} to get the
  281. * full name of a comp (along with
  282. * the struct or union) *)
  283. mutable ckey: int; (** A unique integer constructed from
  284. * the name. Use {!Hashtbl.hash} on
  285. * the string returned by
  286. * {!Cil.compFullName}. All compinfo
  287. * for a given key are shared. *)
  288. mutable cfields: fieldinfo list; (** Information about the fields *)
  289. mutable cattr: attributes; (** The attributes that are defined at
  290. the same time as the composite
  291. type *)
  292. mutable cdefined: bool; (** Whether this is a defined
  293. * compinfo. *)
  294. mutable creferenced: bool; (** True if used. Initially set to
  295. * false *)
  296. }
  297. (** Information about a struct/union field *)
  298. and fieldinfo = {
  299. mutable fcomp: compinfo; (** The compinfo of the host. Note
  300. that this must be shared with the
  301. host since there can be only one
  302. compinfo for a given id *)
  303. mutable fname: string; (** The name of the field. Might be
  304. * the value of
  305. * {!Cil.missingFieldName} in which
  306. * case it must be a bitfield and is
  307. * not printed and it does not
  308. * participate in initialization *)
  309. mutable ftype: typ; (** The type *)
  310. mutable fbitfield: int option; (** If a bitfield then ftype should be
  311. an integer type *)
  312. mutable fattr: attributes; (** The attributes for this field
  313. * (not for its type) *)
  314. mutable floc: location; (** The location where this field
  315. * is defined *)
  316. }
  317. (** Information about an enumeration. This is shared by all references to an
  318. enumeration. Make sure you have a [GEnumTag] for each of of these. *)
  319. and enuminfo = {
  320. mutable ename: string; (** The name. Always non-empty *)
  321. mutable eitems: (string * exp * location) list; (** Items with names
  322. and values. This list
  323. should be
  324. non-empty. The item
  325. values must be
  326. compile-time
  327. constants. *)
  328. mutable eattr: attributes; (** Attributes *)
  329. mutable ereferenced: bool; (** True if used. Initially set to false*)
  330. mutable ekind: ikind;
  331. (** The integer kind used to represent this enum. Per ANSI-C, this
  332. * should always be IInt, but gcc allows other integer kinds *)
  333. }
  334. (** Information about a defined type *)
  335. and typeinfo = {
  336. mutable tname: string;
  337. (** The name. Can be empty only in a [GType] when introducing a composite
  338. * or enumeration tag. If empty cannot be refered to from the file *)
  339. mutable ttype: typ;
  340. (** The actual type. *)
  341. mutable treferenced: bool;
  342. (** True if used. Initially set to false*)
  343. }
  344. (** Information about a variable. These structures are shared by all
  345. * references to the variable. So, you can change the name easily, for
  346. * example. Use one of the {!Cil.makeLocalVar}, {!Cil.makeTempVar} or
  347. * {!Cil.makeGlobalVar} to create instances of this data structure. *)
  348. and varinfo = {
  349. mutable vname: string; (** The name of the variable. Cannot
  350. * be empty. *)
  351. mutable vtype: typ; (** The declared type of the
  352. * variable. *)
  353. mutable vattr: attributes; (** A list of attributes associated
  354. * with the variable. *)
  355. mutable vstorage: storage; (** The storage-class *)
  356. (* The other fields are not used in varinfo when they appear in the formal
  357. * argument list in a [TFun] type *)
  358. mutable vglob: bool; (** True if this is a global variable*)
  359. (** Whether this varinfo is for an inline function. *)
  360. mutable vinline: bool;
  361. mutable vdecl: location; (** Location of variable declaration *)
  362. vinit: initinfo;
  363. (** Optional initializer. Only used for static and global variables.
  364. * Initializers for other types of local variables are turned into
  365. * assignments. *)
  366. mutable vid: int; (** A unique integer identifier. *)
  367. mutable vaddrof: bool; (** True if the address of this
  368. variable is taken. CIL will set
  369. * these flags when it parses C, but
  370. * you should make sure to set the
  371. * flag whenever your transformation
  372. * create [AddrOf] expression. *)
  373. mutable vreferenced: bool; (** True if this variable is ever
  374. referenced. This is computed by
  375. [removeUnusedVars]. It is safe to
  376. just initialize this to False *)
  377. mutable vdescr: doc; (** For most temporary variables, a
  378. description of what the var holds.
  379. (e.g. for temporaries used for
  380. function call results, this string
  381. is a representation of the function
  382. call.) *)
  383. mutable vdescrpure: bool; (** Indicates whether the vdescr above
  384. is a pure expression or call.
  385. True for all CIL expressions and
  386. Lvals, but false for e.g. function
  387. calls.
  388. Printing a non-pure vdescr more
  389. than once may yield incorrect
  390. results. *)
  391. }
  392. (** Storage-class information *)
  393. and storage =
  394. NoStorage | (** The default storage. Nothing is
  395. * printed *)
  396. Static |
  397. Register |
  398. Extern
  399. (** Expressions (Side-effect free)*)
  400. and exp =
  401. Const of constant (** Constant *)
  402. | Lval of lval (** Lvalue *)
  403. | SizeOf of typ (** sizeof(<type>). Has [unsigned
  404. * int] type (ISO 6.5.3.4). This is
  405. * not turned into a constant because
  406. * some transformations might want to
  407. * change types *)
  408. | SizeOfE of exp (** sizeof(<expression>) *)
  409. | SizeOfStr of string
  410. (** sizeof(string_literal). We separate this case out because this is the
  411. * only instance in which a string literal should not be treated as
  412. * having type pointer to character. *)
  413. | AlignOf of typ (** Has [unsigned int] type *)
  414. | AlignOfE of exp
  415. | UnOp of unop * exp * typ (** Unary operation. Includes
  416. the type of the result *)
  417. | BinOp of binop * exp * exp * typ
  418. (** Binary operation. Includes the
  419. type of the result. The arithemtic
  420. conversions are made explicit
  421. for the arguments *)
  422. | Question of exp * exp * exp * typ
  423. (** (a ? b : c) operation. Includes
  424. the type of the result *)
  425. | CastE of typ * exp (** Use {!Cil.mkCast} to make casts *)
  426. | AddrOf of lval (** Always use {!Cil.mkAddrOf} to
  427. * construct one of these. Apply to an
  428. * lvalue of type [T] yields an
  429. * expression of type [TPtr(T)] *)
  430. | AddrOfLabel of stmt ref
  431. | StartOf of lval (** There is no C correspondent for this. C has
  432. * implicit coercions from an array to the address
  433. * of the first element. [StartOf] is used in CIL to
  434. * simplify type checking and is just an explicit
  435. * form of the above mentioned implicit conversion.
  436. * It is not printed. Given an lval of type
  437. * [TArray(T)] produces an expression of type
  438. * [TPtr(T)]. *)
  439. (** Literal constants *)
  440. and constant =
  441. | CInt64 of int64 * ikind * string option
  442. (** Integer constant. Give the ikind (see ISO9899 6.1.3.2)
  443. * and the textual representation, if available. Use
  444. * {!Cil.integer} or {!Cil.kinteger} to create these. Watch
  445. * out for integers that cannot be represented on 64 bits.
  446. * OCAML does not give Overflow exceptions. *)
  447. | CStr of string (** String constant (of pointer type) *)
  448. | CWStr of int64 list (** Wide string constant (of type "wchar_t *") *)
  449. | CChr of char (** Character constant. This has type int, so use
  450. * charConstToInt to read the value in case
  451. * sign-extension is needed. *)
  452. | CReal of float * fkind * string option (** Floating point constant. Give
  453. the fkind (see ISO 6.4.4.2) and
  454. also the textual representation,
  455. if available *)
  456. | CEnum of exp * string * enuminfo
  457. (** An enumeration constant with the given value, name, from the given
  458. * enuminfo. This is not used if {!Cil.lowerEnum} is false (default).
  459. * Use {!Cillower.lowerEnumVisitor} to replace these with integer
  460. * constants. *)
  461. (** Unary operators *)
  462. and unop =
  463. Neg (** Unary minus *)
  464. | BNot (** Bitwise complement (~) *)
  465. | LNot (** Logical Not (!) *)
  466. (** Binary operations *)
  467. and binop =
  468. PlusA (** arithmetic + *)
  469. | PlusPI (** pointer + integer *)
  470. | IndexPI (** pointer + integer but only when
  471. * it arises from an expression
  472. * [e\[i\]] when [e] is a pointer and
  473. * not an array. This is semantically
  474. * the same as PlusPI but CCured uses
  475. * this as a hint that the integer is
  476. * probably positive. *)
  477. | MinusA (** arithmetic - *)
  478. | MinusPI (** pointer - integer *)
  479. | MinusPP (** pointer - pointer *)
  480. | Mult (** * *)
  481. | Div (** / *)
  482. | Mod (** % *)
  483. | Shiftlt (** shift left *)
  484. | Shiftrt (** shift right *)
  485. | Lt (** < (arithmetic comparison) *)
  486. | Gt (** > (arithmetic comparison) *)
  487. | Le (** <= (arithmetic comparison) *)
  488. | Ge (** > (arithmetic comparison) *)
  489. | Eq (** == (arithmetic comparison) *)
  490. | Ne (** != (arithmetic comparison) *)
  491. | BAnd (** bitwise and *)
  492. | BXor (** exclusive-or *)
  493. | BOr (** inclusive-or *)
  494. | LAnd (** logical and *)
  495. | LOr (** logical or *)
  496. (** An lvalue denotes the contents of a range of memory addresses. This range
  497. * is denoted as a host object along with an offset within the object. The
  498. * host object can be of two kinds: a local or global variable, or an object
  499. * whose address is in a pointer expression. We distinguish the two cases so
  500. * that we can tell quickly whether we are accessing some component of a
  501. * variable directly or we are accessing a memory location through a pointer.*)
  502. and lval =
  503. lhost * offset
  504. (** The host part of an {!Cil.lval}. *)
  505. and lhost =
  506. | Var of varinfo
  507. (** The host is a variable. *)
  508. | Mem of exp
  509. (** The host is an object of type [T] when the expression has pointer
  510. * [TPtr(T)]. *)
  511. (** The offset part of an {!Cil.lval}. Each offset can be applied to certain
  512. * kinds of lvalues and its effect is that it advances the starting address
  513. * of the lvalue and changes the denoted type, essentially focussing to some
  514. * smaller lvalue that is contained in the original one. *)
  515. and offset =
  516. | NoOffset (** No offset. Can be applied to any lvalue and does
  517. * not change either the starting address or the type.
  518. * This is used when the lval consists of just a host
  519. * or as a terminator in a list of other kinds of
  520. * offsets. *)
  521. | Field of fieldinfo * offset
  522. (** A field offset. Can be applied only to an lvalue
  523. * that denotes a structure or a union that contains
  524. * the mentioned field. This advances the offset to the
  525. * beginning of the mentioned field and changes the
  526. * type to the type of the mentioned field. *)
  527. | Index of exp * offset
  528. (** An array index offset. Can be applied only to an
  529. * lvalue that denotes an array. This advances the
  530. * starting address of the lval to the beginning of the
  531. * mentioned array element and changes the denoted type
  532. * to be the type of the array element *)
  533. (* The following equivalences hold *)
  534. (* Mem(AddrOf(Mem a, aoff)), off = Mem a, aoff + off *)
  535. (* Mem(AddrOf(Var v, aoff)), off = Var v, aoff + off *)
  536. (* AddrOf (Mem a, NoOffset) = a *)
  537. (** Initializers for global variables. You can create an initializer with
  538. * {!Cil.makeZeroInit}. *)
  539. and init =
  540. | SingleInit of exp (** A single initializer *)
  541. | CompoundInit of typ * (offset * init) list
  542. (** Used only for initializers of structures, unions and arrays.
  543. * The offsets are all of the form [Field(f, NoOffset)] or
  544. * [Index(i, NoOffset)] and specify the field or the index being
  545. * initialized. For structures all fields
  546. * must have an initializer (except the unnamed bitfields), in
  547. * the proper order. This is necessary since the offsets are not
  548. * printed. For arrays the list must contain a prefix of the
  549. * initializers; the rest are 0-initialized.
  550. * For unions there must be exactly one initializer. If
  551. * the initializer is not for the first field then a field
  552. * designator is printed, so you better be on GCC since MSVC does
  553. * not understand this. You can scan an initializer list with
  554. * {!Cil.foldLeftCompound}. *)
  555. (** We want to be able to update an initializer in a global variable, so we
  556. * define it as a mutable field *)
  557. and initinfo = {
  558. mutable init : init option;
  559. }
  560. (** Function definitions. *)
  561. and fundec =
  562. { mutable svar: varinfo;
  563. (** Holds the name and type as a variable, so we can refer to it
  564. * easily from the program. All references to this function either
  565. * in a function call or in a prototype must point to the same
  566. * varinfo. *)
  567. mutable sformals: varinfo list;
  568. (** Formals. These must be shared with the formals that appear in the
  569. * type of the function. Use {!Cil.setFormals} or
  570. * {!Cil.setFunctionType} to set these
  571. * formals and ensure that they are reflected in the function type.
  572. * Do not make copies of these because the body refers to them. *)
  573. mutable slocals: varinfo list;
  574. (** Locals. Does not include the sformals. Do not make copies of
  575. * these because the body refers to them. *)
  576. mutable smaxid: int; (** Max local id. Starts at 0. *)
  577. mutable sbody: block; (** The function body. *)
  578. mutable smaxstmtid: int option; (** max id of a (reachable) statement
  579. * in this function, if we have
  580. * computed it. range = 0 ...
  581. * (smaxstmtid-1). This is computed by
  582. * {!Cil.computeCFGInfo}. *)
  583. mutable sallstmts: stmt list; (** After you call {!Cil.computeCFGInfo}
  584. * this field is set to contain all
  585. * statements in the function *)
  586. }
  587. (** A block is a sequence of statements with the control falling through from
  588. one element to the next *)
  589. and block =
  590. { mutable battrs: attributes; (** Attributes for the block *)
  591. mutable bstmts: stmt list; (** The statements comprising the block*)
  592. }
  593. (** Statements.
  594. The statement is the structural unit in the control flow graph. Use mkStmt
  595. to make a statement and then fill in the fields. *)
  596. and stmt = {
  597. mutable labels: label list; (** Whether the statement starts with
  598. some labels, case statements or
  599. default statement *)
  600. mutable skind: stmtkind; (** The kind of statement *)
  601. (* Now some additional control flow information. Initially this is not
  602. * filled in. *)
  603. mutable sid: int; (** A number (>= 0) that is unique
  604. in a function. *)
  605. mutable succs: stmt list; (** The successor statements. They can
  606. always be computed from the skind
  607. and the context in which this
  608. statement appears *)
  609. mutable preds: stmt list; (** The inverse of the succs function*)
  610. }
  611. (** Labels *)
  612. and label =
  613. Label of string * location * bool
  614. (** A real label. If the bool is "true", the label is from the
  615. * input source program. If the bool is "false", the label was
  616. * created by CIL or some other transformation *)
  617. | Case of exp * location (** A case statement *)
  618. | CaseRange of exp * exp * location (** A case statement corresponding to a
  619. range of values *)
  620. | Default of location (** A default statement *)
  621. (* The various kinds of statements *)
  622. and stmtkind =
  623. | Instr of instr list (** A group of instructions that do not
  624. contain control flow. Control
  625. implicitly falls through. *)
  626. | Return of exp option * location (** The return statement. This is a
  627. leaf in the CFG. *)
  628. | Goto of stmt ref * location (** A goto statement. Appears from
  629. actual goto's in the code. *)
  630. | ComputedGoto of exp * location
  631. | Break of location (** A break to the end of the nearest
  632. enclosing Loop or Switch *)
  633. | Continue of location (** A continue to the start of the
  634. nearest enclosing [Loop] *)
  635. | If of exp * block * block * location (** A conditional.
  636. Two successors, the "then" and
  637. the "else" branches. Both
  638. branches fall-through to the
  639. successor of the If statement *)
  640. | Switch of exp * block * (stmt list) * location
  641. (** A switch statement. The block
  642. contains within all of the cases.
  643. We also have direct pointers to the
  644. statements that implement the
  645. cases. Which cases they implement
  646. you can get from the labels of the
  647. statement *)
  648. | Loop of block * location * (stmt option) * (stmt option)
  649. (** A [while(1)] loop. The
  650. * termination test is implemented
  651. * in the body of a loop using a
  652. * [Break] statement. If
  653. * prepareCFG has been called, the
  654. * first stmt option will point to
  655. * the stmt containing the
  656. * continue label for this loop
  657. * and the second will point to
  658. * the stmt containing the break
  659. * label for this loop. *)
  660. | Block of block (** Just a block of statements. Use it
  661. as a way to keep some attributes
  662. local *)
  663. (** On MSVC we support structured exception handling. This is what you
  664. * might expect. Control can get into the finally block either from the
  665. * end of the body block, or if an exception is thrown. The location
  666. * corresponds to the try keyword. *)
  667. | TryFinally of block * block * location
  668. (** On MSVC we support structured exception handling. The try/except
  669. * statement is a bit tricky:
  670. __try { blk }
  671. __except (e) {
  672. handler
  673. }
  674. The argument to __except must be an expression. However, we keep a
  675. list of instructions AND an expression in case you need to make
  676. function calls. We'll print those as a comma expression. The control
  677. can get to the __except expression only if an exception is thrown.
  678. After that, depending on the value of the expression the control
  679. goes to the handler, propagates the exception, or retries the
  680. exception !!! The location corresponds to the try keyword.
  681. *)
  682. | TryExcept of block * (instr list * exp) * block * location
  683. (** Instructions. They may cause effects directly but may not have control
  684. flow.*)
  685. and instr =
  686. Set of lval * exp * location (** An assignment. A cast is present
  687. if the exp has different type
  688. from lval *)
  689. | Call of lval option * exp * exp list * location
  690. (** optional: result is an lval. A cast might be
  691. necessary if the declared result type of the
  692. function is not the same as that of the
  693. destination. If the function is declared then
  694. casts are inserted for those arguments that
  695. correspond to declared formals. (The actual
  696. number of arguments might be smaller or larger
  697. than the declared number of arguments. C allows
  698. this.) If the type of the result variable is not
  699. the same as the declared type of the function
  700. result then an implicit cast exists. *)
  701. (* See the GCC specification for the meaning of ASM.
  702. * If the source is MS VC then only the templates
  703. * are used *)
  704. (* sm: I've added a notes.txt file which contains more
  705. * information on interpreting Asm instructions *)
  706. | Asm of attributes * (* Really only const and volatile can appear
  707. * here *)
  708. string list * (* templates (CR-separated) *)
  709. (string option * string * lval) list *
  710. (* outputs must be lvals with
  711. * optional names and constraints.
  712. * I would like these
  713. * to be actually variables, but I
  714. * run into some trouble with ASMs
  715. * in the Linux sources *)
  716. (string option * string * exp) list *
  717. (* inputs with optional names and constraints *)
  718. string list * (* register clobbers *)
  719. location
  720. (** An inline assembly instruction. The arguments are (1) a list of
  721. attributes (only const and volatile can appear here and only for
  722. GCC), (2) templates (CR-separated), (3) a list of
  723. outputs, each of which is an lvalue with a constraint, (4) a list
  724. of input expressions along with constraints, (5) clobbered
  725. registers, and (5) location information *)
  726. (** Describes a location in a source file *)
  727. and location = {
  728. line: int; (** The line number. -1 means "do not know" *)
  729. file: string; (** The name of the source file*)
  730. byte: int; (** The byte position in the source file *)
  731. }
  732. (* Type signatures. Two types are identical iff they have identical
  733. * signatures *)
  734. and typsig =
  735. TSArray of typsig * int64 option * attribute list
  736. | TSPtr of typsig * attribute list
  737. | TSComp of bool * string * attribute list
  738. | TSFun of typsig * typsig list option * bool * attribute list
  739. | TSEnum of string * attribute list
  740. | TSBase of typ
  741. let locUnknown = { line = -1;
  742. file = "";
  743. byte = -1;}
  744. (* A reference to the current location *)
  745. let currentLoc : location ref = ref locUnknown
  746. (* A reference to the current global being visited *)
  747. let currentGlobal: global ref = ref (GText "dummy")
  748. let compareLoc (a: location) (b: location) : int =
  749. let namecmp = compare a.file b.file in
  750. if namecmp != 0
  751. then namecmp
  752. else
  753. let linecmp = a.line - b.line in
  754. if linecmp != 0
  755. then linecmp
  756. else a.byte - b.byte
  757. let argsToList : (string * typ * attributes) list option
  758. -> (string * typ * attributes) list
  759. = function
  760. None -> []
  761. | Some al -> al
  762. (* A hack to allow forward reference of d_exp *)
  763. let pd_exp : (unit -> exp -> doc) ref =
  764. ref (fun _ -> E.s (E.bug "pd_exp not initialized"))
  765. let pd_type : (unit -> typ -> doc) ref =
  766. ref (fun _ -> E.s (E.bug "pd_type not initialized"))
  767. let pd_attr : (unit -> attribute -> doc) ref =
  768. ref (fun _ -> E.s (E.bug "pd_attr not initialized"))
  769. (** Different visiting actions. 'a will be instantiated with [exp], [instr],
  770. etc. *)
  771. type 'a visitAction =
  772. SkipChildren (** Do not visit the children. Return
  773. the node as it is. *)
  774. | DoChildren (** Continue with the children of this
  775. node. Rebuild the node on return
  776. if any of the children changes
  777. (use == test) *)
  778. | ChangeTo of 'a (** Replace the expression with the
  779. given one *)
  780. | ChangeDoChildrenPost of 'a * ('a -> 'a) (** First consider that the entire
  781. exp is replaced by the first
  782. parameter. Then continue with
  783. the children. On return rebuild
  784. the node if any of the children
  785. has changed and then apply the
  786. function on the node *)
  787. (* sm/gn: cil visitor interface for traversing Cil trees. *)
  788. (* Use visitCilStmt and/or visitCilFile to use this. *)
  789. (* Some of the nodes are changed in place if the children are changed. Use
  790. * one of Change... actions if you want to copy the node *)
  791. (** A visitor interface for traversing CIL trees. Create instantiations of
  792. * this type by specializing the class {!Cil.nopCilVisitor}. *)
  793. class type cilVisitor = object
  794. method vvdec: varinfo -> varinfo visitAction
  795. (** Invoked for each variable declaration. The subtrees to be traversed
  796. * are those corresponding to the type and attributes of the variable.
  797. * Note that variable declarations are all the [GVar], [GVarDecl], [GFun],
  798. * all the [varinfo] in formals of function types, and the formals and
  799. * locals for function definitions. This means that the list of formals
  800. * in a function definition will be traversed twice, once as part of the
  801. * function type and second as part of the formals in a function
  802. * definition. *)
  803. method vvrbl: varinfo -> varinfo visitAction
  804. (** Invoked on each variable use. Here only the [SkipChildren] and
  805. * [ChangeTo] actions make sense since there are no subtrees. Note that
  806. * the type and attributes of the variable are not traversed for a
  807. * variable use *)
  808. method vexpr: exp -> exp visitAction
  809. (** Invoked on each expression occurence. The subtrees are the
  810. * subexpressions, the types (for a [Cast] or [SizeOf] expression) or the
  811. * variable use. *)
  812. method vlval: lval -> lval visitAction
  813. (** Invoked on each lvalue occurence *)
  814. method voffs: offset -> offset visitAction
  815. (** Invoked on each offset occurrence that is *not* as part
  816. * of an initializer list specification, i.e. in an lval or
  817. * recursively inside an offset. *)
  818. method vinitoffs: offset -> offset visitAction
  819. (** Invoked on each offset appearing in the list of a
  820. * CompoundInit initializer. *)
  821. method vinst: instr -> instr list visitAction
  822. (** Invoked on each instruction occurrence. The [ChangeTo] action can
  823. * replace this instruction with a list of instructions *)
  824. method vstmt: stmt -> stmt visitAction
  825. (** Control-flow statement. *)
  826. method vblock: block -> block visitAction (** Block. Replaced in
  827. place. *)
  828. method vfunc: fundec -> fundec visitAction (** Function definition.
  829. Replaced in place. *)
  830. method vglob: global -> global list visitAction (** Global (vars, types,
  831. etc.) *)
  832. method vinit: varinfo -> offset -> init -> init visitAction
  833. (** Initializers for globals,
  834. * pass the global where this
  835. * occurs, and the offset *)
  836. method vtype: typ -> typ visitAction (** Use of some type. Note
  837. * that for structure/union
  838. * and enumeration types the
  839. * definition of the
  840. * composite type is not
  841. * visited. Use [vglob] to
  842. * visit it. *)
  843. method vattr: attribute -> attribute list visitAction
  844. (** Attribute. Each attribute can be replaced by a list *)
  845. method vattrparam: attrparam -> attrparam visitAction
  846. (** Attribute parameters. *)
  847. (** Add here instructions while visiting to queue them to
  848. * preceede the current statement or instruction being processed *)
  849. method queueInstr: instr list -> unit
  850. (** Gets the queue of instructions and resets the queue *)
  851. method unqueueInstr: unit -> instr list
  852. end
  853. (* the default visitor does nothing at each node, but does *)
  854. (* not stop; hence they return true *)
  855. class nopCilVisitor : cilVisitor = object
  856. method vvrbl (v:varinfo) = DoChildren (* variable *)
  857. method vvdec (v:varinfo) = DoChildren (* variable
  858. * declaration *)
  859. method vexpr (e:exp) = DoChildren (* expression *)
  860. method vlval (l:lval) = DoChildren (* lval (base is 1st
  861. * field) *)
  862. method voffs (o:offset) = DoChildren (* lval or recursive offset *)
  863. method vinitoffs (o:offset) = DoChildren (* initializer offset *)
  864. method vinst (i:instr) = DoChildren (* imperative instruction *)
  865. method vstmt (s:stmt) = DoChildren (* constrol-flow statement *)
  866. method vblock (b: block) = DoChildren
  867. method vfunc (f:fundec) = DoChildren (* function definition *)
  868. method vglob (g:global) = DoChildren (* global (vars, types, etc.) *)
  869. method vinit (forg: varinfo) (off: offset) (i:init) = DoChildren (* global initializers *)
  870. method vtype (t:typ) = DoChildren (* use of some type *)
  871. method vattr (a: attribute) = DoChildren
  872. method vattrparam (a: attrparam) = DoChildren
  873. val mutable instrQueue = []
  874. method queueInstr (il: instr list) =
  875. List.iter (fun i -> instrQueue <- i :: instrQueue) il
  876. method unqueueInstr () =
  877. let res = List.rev instrQueue in
  878. instrQueue <- [];
  879. res
  880. end
  881. let assertEmptyQueue vis =
  882. if vis#unqueueInstr () <> [] then
  883. (* Either a visitor inserted an instruction somewhere that it shouldn't
  884. have (i.e. at the top level rather than inside of a statement), or
  885. there's a bug in the visitor engine. *)
  886. E.s (E.bug "Visitor's instruction queue is not empty.\n You should only use queueInstr inside a function body!");
  887. ()
  888. let lu = locUnknown
  889. (* sm: utility *)
  890. let startsWith (prefix: string) (s: string) : bool =
  891. (
  892. let prefixLen = (String.length prefix) in
  893. (String.length s) >= prefixLen &&
  894. (String.sub s 0 prefixLen) = prefix
  895. )
  896. let endsWith (suffix: string) (s: string) : bool =
  897. let suffixLen = String.length suffix in
  898. let sLen = String.length s in
  899. sLen >= suffixLen &&
  900. (String.sub s (sLen - suffixLen) suffixLen) = suffix
  901. let stripUnderscores (s: string) : string =
  902. if (startsWith "__" s) && (endsWith "__" s) then
  903. String.sub s 2 ((String.length s) - 4)
  904. else
  905. s
  906. let get_instrLoc (inst : instr) =
  907. match inst with
  908. Set(_, _, loc) -> loc
  909. | Call(_, _, _, loc) -> loc
  910. | Asm(_, _, _, _, _, loc) -> loc
  911. let get_globalLoc (g : global) =
  912. match g with
  913. | GFun(_,l) -> (l)
  914. | GType(_,l) -> (l)
  915. | GEnumTag(_,l) -> (l)
  916. | GEnumTagDecl(_,l) -> (l)
  917. | GCompTag(_,l) -> (l)
  918. | GCompTagDecl(_,l) -> (l)
  919. | GVarDecl(_,l) -> (l)
  920. | GVar(_,_,l) -> (l)
  921. | GAsm(_,l) -> (l)
  922. | GPragma(_,l) -> (l)
  923. | GText(_) -> locUnknown
  924. let rec get_stmtLoc (statement : stmtkind) =
  925. match statement with
  926. Instr([]) -> lu
  927. | Instr(hd::tl) -> get_instrLoc(hd)
  928. | Return(_, loc) -> loc
  929. | Goto(_, loc) -> loc
  930. | ComputedGoto(_, loc) -> loc
  931. | Break(loc) -> loc
  932. | Continue(loc) -> loc
  933. | If(_, _, _, loc) -> loc
  934. | Switch (_, _, _, loc) -> loc
  935. | Loop (_, loc, _, _) -> loc
  936. | Block b -> if b.bstmts == [] then lu
  937. else get_stmtLoc ((List.hd b.bstmts).skind)
  938. | TryFinally (_, _, l) -> l
  939. | TryExcept (_, _, _, l) -> l
  940. (* The next variable identifier to use. Counts up *)
  941. let nextGlobalVID = ref 1
  942. (* The next compindo identifier to use. Counts up. *)
  943. let nextCompinfoKey = ref 1
  944. (* Some error reporting functions *)
  945. let d_loc (_: unit) (loc: location) : doc =
  946. text loc.file ++ chr ':' ++ num loc.line
  947. let d_thisloc (_: unit) : doc = d_loc () !currentLoc
  948. let error (fmt : ('a,unit,doc) format) : 'a =
  949. let f d =
  950. E.hadErrors := true;
  951. ignore (eprintf "%t: Error: %a@!"
  952. d_thisloc insert d);
  953. nil
  954. in
  955. Pretty.gprintf f fmt
  956. let unimp (fmt : ('a,unit,doc) format) : 'a =
  957. let f d =
  958. E.hadErrors := true;
  959. ignore (eprintf "%t: Unimplemented: %a@!"
  960. d_thisloc insert d);
  961. nil
  962. in
  963. Pretty.gprintf f fmt
  964. let bug (fmt : ('a,unit,doc) format) : 'a =
  965. let f d =
  966. E.hadErrors := true;
  967. ignore (eprintf "%t: Bug: %a@!"
  968. d_thisloc insert d);
  969. E.showContext ();
  970. nil
  971. in
  972. Pretty.gprintf f fmt
  973. let errorLoc (loc: location) (fmt : ('a,unit,doc) format) : 'a =
  974. let f d =
  975. E.hadErrors := true;
  976. ignore (eprintf "%a: Error: %a@!"
  977. d_loc loc insert d);
  978. E.showContext ();
  979. nil
  980. in
  981. Pretty.gprintf f fmt
  982. let warn (fmt : ('a,unit,doc) format) : 'a =
  983. let f d =
  984. ignore (eprintf "%t: Warning: %a@!"
  985. d_thisloc insert d);
  986. nil
  987. in
  988. Pretty.gprintf f fmt
  989. let warnOpt (fmt : ('a,unit,doc) format) : 'a =
  990. let f d =
  991. if !E.warnFlag then
  992. ignore (eprintf "%t: Warning: %a@!"
  993. d_thisloc insert d);
  994. nil
  995. in
  996. Pretty.gprintf f fmt
  997. let warnContext (fmt : ('a,unit,doc) format) : 'a =
  998. let f d =
  999. ignore (eprintf "%t: Warning: %a@!"
  1000. d_thisloc insert d);
  1001. E.showContext ();
  1002. nil
  1003. in
  1004. Pretty.gprintf f fmt
  1005. let warnContextOpt (fmt : ('a,unit,doc) format) : 'a =
  1006. let f d =
  1007. if !E.warnFlag then
  1008. ignore (eprintf "%t: Warning: %a@!"
  1009. d_thisloc insert d);
  1010. E.showContext ();
  1011. nil
  1012. in
  1013. Pretty.gprintf f fmt
  1014. let warnLoc (loc: location) (fmt : ('a,unit,doc) format) : 'a =
  1015. let f d =
  1016. ignore (eprintf "%a: Warning: %a@!"
  1017. d_loc loc insert d);
  1018. E.showContext ();
  1019. nil
  1020. in
  1021. Pretty.gprintf f fmt
  1022. let zero = Const(CInt64(Int64.zero, IInt, None))
  1023. (** Given the character c in a (CChr c), sign-extend it to 32 bits.
  1024. (This is the official way of interpreting character constants, according to
  1025. ISO C 6.4.4.4.10, which says that character constants are chars cast to ints)
  1026. Returns CInt64(sign-extened c, IInt, None) *)
  1027. let charConstToInt (c: char) : constant =
  1028. let c' = Char.code c in
  1029. let value =
  1030. if c' < 128
  1031. then Int64.of_int c'
  1032. else Int64.of_int (c' - 256)
  1033. in
  1034. CInt64(value, IInt, None)
  1035. (** Convert a 64-bit int to an OCaml int, or raise an exception if that
  1036. can't be done. *)
  1037. let i64_to_int (i: int64) : int =
  1038. let i': int = Int64.to_int i in (* i.e. i' = i mod 2^31 *)
  1039. if i = Int64.of_int i' then i'
  1040. else E.s (E.unimp "%a: Int constant too large: %Ld\n" d_loc !currentLoc i)
  1041. let cilint_to_int (i: cilint) : int =
  1042. try int_of_cilint i
  1043. with _ -> E.s (E.unimp "%a: Int constant too large: %s\n"
  1044. d_loc !currentLoc (string_of_cilint i))
  1045. let voidType = TVoid([])
  1046. let intType = TInt(IInt,[])
  1047. let uintType = TInt(IUInt,[])
  1048. let longType = TInt(ILong,[])
  1049. let ulongType = TInt(IULong,[])
  1050. let charType = TInt(IChar, [])
  1051. let boolType = TInt(IBool, [])
  1052. let charPtrType = TPtr(charType,[])
  1053. let charConstPtrType = TPtr(TInt(IChar, [Attr("const", [])]),[])
  1054. let stringLiteralType = ref charPtrType
  1055. let voidPtrType = TPtr(voidType, [])
  1056. let intPtrType = TPtr(intType, [])
  1057. let uintPtrType = TPtr(uintType, [])
  1058. let boolPtrType = TPtr(boolType, [])
  1059. let doubleType = TFloat(FDouble, [])
  1060. (* An integer type that fits pointers. Initialized by initCIL *)
  1061. let upointType = ref voidType
  1062. (* An integer type that fits a pointer difference. Initialized by initCIL *)
  1063. let ptrdiffType = ref voidType
  1064. (* An integer type that fits wchar_t. Initialized by initCIL *)
  1065. let wcharKind = ref IChar
  1066. let wcharType = ref voidType
  1067. (* An integer type that is the type of sizeof. Initialized by initCIL *)
  1068. let typeOfSizeOf = ref voidType
  1069. let kindOfSizeOf = ref IUInt
  1070. let initCIL_called = ref false
  1071. (** Returns true if and only if the given integer type is signed. *)
  1072. let isSigned = function
  1073. | IBool
  1074. | IUChar
  1075. | IUShort
  1076. | IUInt
  1077. | IULong
  1078. | IULongLong ->
  1079. false
  1080. | ISChar
  1081. | IShort
  1082. | IInt
  1083. | ILong
  1084. | ILongLong ->
  1085. true
  1086. | IChar ->
  1087. not !M.theMachine.M.char_is_unsigned
  1088. let mkStmt (sk: stmtkind) : stmt =
  1089. { skind = sk;
  1090. labels = [];
  1091. sid = -1; succs = []; preds = [] }
  1092. let mkBlock (slst: stmt list) : block =
  1093. { battrs = []; bstmts = slst; }
  1094. let mkEmptyStmt () = mkStmt (Instr [])
  1095. let mkStmtOneInstr (i: instr) = mkStmt (Instr [i])
  1096. let dummyInstr = (Asm([], ["dummy statement!!"], [], [], [], lu))
  1097. let dummyStmt = mkStmt (Instr [dummyInstr])
  1098. let compactStmts (b: stmt list) : stmt list =
  1099. (* Try to compress statements. Scan the list of statements and remember
  1100. * the last instrunction statement encountered, along with a Clist of
  1101. * instructions in it. *)
  1102. let rec compress (lastinstrstmt: stmt) (* Might be dummStmt *)
  1103. (lastinstrs: instr Clist.clist)
  1104. (body: stmt list) =
  1105. let finishLast (tail: stmt list) : stmt list =
  1106. if lastinstrstmt == dummyStmt then tail
  1107. else begin
  1108. lastinstrstmt.skind <- Instr (Clist.toList lastinstrs);
  1109. lastinstrstmt :: tail
  1110. end
  1111. in
  1112. match body with
  1113. [] -> finishLast []
  1114. | ({skind=Instr il} as s) :: rest ->
  1115. let ils = Clist.fromList il in
  1116. if lastinstrstmt != dummyStmt && s.labels == [] then
  1117. compress lastinstrstmt (Clist.append lastinstrs ils) rest
  1118. else
  1119. finishLast (compress s ils rest)
  1120. | {skind=Block b;labels = []} :: rest when b.battrs = [] ->
  1121. compress lastinstrstmt lastinstrs (b.bstmts@rest)
  1122. | s :: rest ->
  1123. let res = s :: compress dummyStmt Clist.empty rest in
  1124. finishLast res
  1125. in
  1126. compress dummyStmt Clist.empty b
  1127. (** Construct sorted lists of attributes ***)
  1128. let rec addAttribute (Attr(an, _) as a: attribute) (al: attributes) =
  1129. let rec insertSorted = function
  1130. [] -> [a]
  1131. | ((Attr(an0, _) as a0) :: rest) as l ->
  1132. if an < an0 then a :: l
  1133. else if Util.equals a a0 then l (* Do not add if already in there *)
  1134. else a0 :: insertSorted rest (* Make sure we see all attributes with
  1135. * this name *)
  1136. in
  1137. insertSorted al
  1138. (** The second attribute list is sorted *)
  1139. and addAttributes al0 (al: attributes) : attributes =
  1140. if al0 == [] then al else
  1141. List.fold_left (fun acc a -> addAttribute a acc) al al0
  1142. and dropAttribute (an: string) (al: attributes) =
  1143. List.filter (fun (Attr(an', _)) -> an <> an') al
  1144. and dropAttributes (anl: string list) (al: attributes) =
  1145. List.fold_left (fun acc an -> dropAttribute an acc) al anl
  1146. and filterAttributes (s: string) (al: attribute list) : attribute list =
  1147. List.filter (fun (Attr(an, _)) -> an = s) al
  1148. (* sm: *)
  1149. let hasAttribute s al =
  1150. (filterAttributes s al <> [])
  1151. type attributeClass =
  1152. AttrName of bool
  1153. (* Attribute of a name. If argument is true and we are on MSVC then
  1154. * the attribute is printed using __declspec as part of the storage
  1155. * specifier *)
  1156. | AttrFunType of bool
  1157. (* Attribute of a function type. If argument is true and we are on
  1158. * MSVC then the attribute is printed just before the function name *)
  1159. | AttrType (* Attribute of a type *)
  1160. (* This table contains the mapping of predefined attributes to classes.
  1161. * Extend this table with more attributes as you need. This table is used to
  1162. * determine how to associate attributes with names or type during cabs2cil
  1163. * conversion *)
  1164. let attributeHash: (string, attributeClass) H.t =
  1165. let table = H.create 13 in
  1166. List.iter (fun a -> H.add table a (AttrName false))
  1167. [ "section"; "constructor"; "destructor"; "unused"; "used"; "weak";
  1168. "no_instrument_function"; "alias"; "no_check_memory_usage";
  1169. "exception"; "model"; (* "restrict"; *)
  1170. "aconst"; "__asm__" (* Gcc uses this to specifiy the name to be used in
  1171. * assembly for a global *)];
  1172. (* Now come the MSVC declspec attributes *)
  1173. List.iter (fun a -> H.add table a (AttrName true))
  1174. [ "thread"; "naked"; "dllimport"; "dllexport";
  1175. "selectany"; "allocate"; "nothrow"; "novtable"; "property"; "noreturn";
  1176. "uuid"; "align" ];
  1177. List.iter (fun a -> H.add table a (AttrFunType false))
  1178. [ "format"; "regparm"; "longcall";
  1179. "noinline"; "always_inline"; "gnu_inline"; "leaf";
  1180. "artificial"; "warn_unused_result"; "nonnull";
  1181. ];
  1182. List.iter (fun a -> H.add table a (AttrFunType true))
  1183. [ "stdcall";"cdecl"; "fastcall" ];
  1184. List.iter (fun a -> H.add table a AttrType)
  1185. [ "const"; "volatile"; "restrict"; "mode" ];
  1186. table
  1187. (* Partition the attributes into classes *)
  1188. let partitionAttributes
  1189. ~(default:attributeClass)
  1190. (attrs: attribute list) :
  1191. attribute list * attribute list * attribute list =
  1192. let rec loop (n,f,t) = function
  1193. [] -> n, f, t
  1194. | (Attr(an, _) as a) :: rest ->
  1195. match (try H.find attributeHash an with Not_found -> default) with
  1196. AttrName _ -> loop (addAttribute a n, f, t) rest
  1197. | AttrFunType _ ->
  1198. loop (n, addAttribute a f, t) rest
  1199. | AttrType -> loop (n, f, addAttribute a t) rest
  1200. in
  1201. loop ([], [], []) attrs
  1202. (* Get the full name of a comp *)
  1203. let compFullName comp =
  1204. (if comp.cstruct then "struct " else "union ") ^ comp.cname
  1205. let missingFieldName = "___missing_field_name"
  1206. (** Creates a a (potentially recursive) composite type. Make sure you add a
  1207. * GTag for it to the file! **)
  1208. let mkCompInfo
  1209. (isstruct: bool)
  1210. (n: string)
  1211. (* fspec is a function that when given a forward
  1212. * representation of the structure type constructs the type of
  1213. * the fields. The function can ignore this argument if not
  1214. * constructing a recursive type. *)
  1215. (mkfspec: compinfo -> (string * typ * int option * attribute list *
  1216. location) list)
  1217. (a: attribute list) : compinfo =
  1218. (* make a new name for anonymous structs *)
  1219. if n = "" then
  1220. E.s (E.bug "mkCompInfo: missing structure name\n");
  1221. (* Make a new self cell and a forward reference *)
  1222. let comp =
  1223. { cstruct = isstruct; cname = ""; ckey = 0; cfields = [];
  1224. cattr = a; creferenced = false;
  1225. (* Make this compinfo undefined by default *)
  1226. cdefined = false; }
  1227. in
  1228. comp.cname <- n;
  1229. comp.ckey <- !nextCompinfoKey;
  1230. incr nextCompinfoKey;
  1231. let flds =
  1232. Util.list_map (fun (fn, ft, fb, fa, fl) ->
  1233. { fcomp = comp;
  1234. ftype = ft;
  1235. fname = fn;
  1236. fbitfield = fb;
  1237. fattr = fa;
  1238. floc = fl}) (mkfspec comp) in
  1239. comp.cfields <- flds;
  1240. if flds <> [] then comp.cdefined <- true;
  1241. comp
  1242. (** Make a copy of a compinfo, changing the name and the key *)
  1243. let copyCompInfo (ci: compinfo) (n: string) : compinfo =
  1244. let ci' = {ci with cname = n;
  1245. ckey = !nextCompinfoKey; } in
  1246. incr nextCompinfoKey;
  1247. (* Copy the fields and set the new pointers to parents *)
  1248. ci'.cfields <- Util.list_map (fun f -> {f with fcomp = ci'}) ci'.cfields;
  1249. ci'
  1250. (**** Utility functions ******)
  1251. let rec typeAttrs = function
  1252. TVoid a -> a
  1253. | TInt (_, a) -> a
  1254. | TFloat (_, a) -> a
  1255. | TNamed (t, a) -> addAttributes a (typeAttrs t.ttype)
  1256. | TPtr (_, a) -> a
  1257. | TArray (_, _, a) -> a
  1258. | TComp (comp, a) -> addAttributes comp.cattr a
  1259. | TEnum (enum, a) -> addAttributes enum.eattr a
  1260. | TFun (_, _, _, a) -> a
  1261. | TBuiltin_va_list a -> a
  1262. let setTypeAttrs t a =
  1263. match t with
  1264. TVoid _ -> TVoid a
  1265. | TInt (i, _) -> TInt (i, a)
  1266. | TFloat (f, _) -> TFloat (f, a)
  1267. | TNamed (t, _) -> TNamed(t, a)
  1268. | TPtr (t', _) -> TPtr(t', a)
  1269. | TArray (t', l, _) -> TArray(t', l, a)
  1270. | TComp (comp, _) -> TComp (comp, a)
  1271. | TEnum (enum, _) -> TEnum (enum, a)
  1272. | TFun (r, args, v, _) -> TFun(r,args,v,a)
  1273. | TBuiltin_va_list _ -> TBuiltin_va_list a
  1274. let typeAddAttributes a0 t =
  1275. begin
  1276. match a0 with
  1277. | [] ->
  1278. (* no attributes, keep same type *)
  1279. t
  1280. | _ ->
  1281. (* anything else: add a0 to existing attributes *)
  1282. let add (a: attributes) = addAttributes a0 a in
  1283. match t with
  1284. TVoid a -> TVoid (add a)
  1285. | TInt (ik, a) -> TInt (ik, add a)
  1286. | TFloat (fk, a) -> TFloat (fk, add a)
  1287. | TEnum (enum, a) -> TEnum (enum, add a)
  1288. | TPtr (t, a) -> TPtr (t, add a)
  1289. | TArray (t, l, a) -> TArray (t, l, add a)
  1290. | TFun (t, args, isva, a) -> TFun(t, args, isva, add a)
  1291. | TComp (comp, a) -> TComp (comp, add a)
  1292. | TNamed (t, a) -> TNamed (t, add a)
  1293. | TBuiltin_va_list a -> TBuiltin_va_list (add a)
  1294. end
  1295. let typeRemoveAttributes (anl: string list) t =
  1296. let drop (al: attributes) = dropAttributes anl al in
  1297. match t with
  1298. TVoid a -> TVoid (drop a)
  1299. | TInt (ik, a) -> TInt (ik, drop a)
  1300. | TFloat (fk, a) -> TFloat (fk, drop a)
  1301. | TEnum (enum, a) -> TEnum (enum, drop a)
  1302. | TPtr (t, a) -> TPtr (t, drop a)
  1303. | TArray (t, l, a) -> TArray (t, l, drop a)
  1304. | TFun (t, args, isva, a) -> TFun(t, args, isva, drop a)
  1305. | TComp (comp, a) -> TComp (comp, drop a)
  1306. | TNamed (t, a) -> TNamed (t, drop a)
  1307. | TBuiltin_va_list a -> TBuiltin_va_list (drop a)
  1308. let unrollType (t: typ) : typ =
  1309. let rec withAttrs (al: attributes) (t: typ) : typ =
  1310. match t with
  1311. TNamed (r, a') -> withAttrs (addAttributes al a') r.ttype
  1312. | x -> typeAddAttributes al x
  1313. in
  1314. withAttrs [] t
  1315. let rec unrollTypeDeep (t: typ) : typ =
  1316. let rec withAttrs (al: attributes) (t: typ) : typ =
  1317. match t with
  1318. TNamed (r, a') -> withAttrs (addAttributes al a') r.ttype
  1319. | TPtr(t, a') -> TPtr(unrollTypeDeep t, addAttributes al a')
  1320. | TArray(t, l, a') -> TArray(unrollTypeDeep t, l, addAttributes al a')
  1321. | TFun(rt, args, isva, a') ->
  1322. TFun (unrollTypeDeep rt,
  1323. (match args with
  1324. None -> None
  1325. | Some argl ->
  1326. Some (Util.list_map (fun (an,at,aa) ->
  1327. (an, unrollTypeDeep at, aa)) argl)),
  1328. isva,
  1329. addAttributes al a')
  1330. | x -> typeAddAttributes al x
  1331. in
  1332. withAttrs [] t
  1333. let isVoidType t =
  1334. match unrollType t with
  1335. TVoid _ -> true
  1336. | _ -> false
  1337. let isVoidPtrType t =
  1338. match unrollType t with
  1339. TPtr(tau,_) when isVoidType tau -> true
  1340. | _ -> false
  1341. let var vi : lval = (Var vi, NoOffset)
  1342. (* let assign vi e = Instrs(Set (var vi, e), lu) *)
  1343. let mkString s = Const(CStr s)
  1344. let mkWhile ~(guard:exp) ~(body: stmt list) : stmt list =
  1345. (* Do it like this so that the pretty printer recognizes it *)
  1346. [ mkStmt (Loop (mkBlock (mkStmt (If(guard,
  1347. mkBlock [ mkEmptyStmt () ],
  1348. mkBlock [ mkStmt (Break lu)], lu)) ::
  1349. body), lu, None, None)) ]
  1350. let mkFor ~(start: stmt list) ~(guard: exp) ~(next: stmt list)
  1351. ~(body: stmt list) : stmt list =
  1352. (start @
  1353. (mkWhile guard (body @ next)))
  1354. let mkForIncr ~(iter : varinfo) ~(first: exp) ~stopat:(past: exp) ~(incr: exp)
  1355. ~(body: stmt list) : stmt list =
  1356. (* See what kind of operator we need *)
  1357. let compop, nextop =
  1358. match unrollType iter.vtype with
  1359. TPtr _ -> Lt, PlusPI
  1360. | _ -> Lt, PlusA
  1361. in
  1362. mkFor
  1363. [ mkStmt (Instr [(Set (var iter, first, lu))]) ]
  1364. (BinOp(compop, Lval(var iter), past, intType))
  1365. [ mkStmt (Instr [(Set (var iter,
  1366. (BinOp(nextop, Lval(var iter), incr, iter.vtype)),
  1367. lu))])]
  1368. body
  1369. let rec stripCasts (e: exp) =
  1370. match e with CastE(_, e') -> stripCasts e' | _ -> e
  1371. (* the name of the C function we call to get ccgr ASTs
  1372. external parse : string -> file = "cil_main"
  1373. *)
  1374. (*
  1375. Pretty Printing
  1376. *)
  1377. let d_ikind () = function
  1378. IChar -> text "char"
  1379. | ISChar -> text "signed char"
  1380. | IUChar -> text "unsigned char"
  1381. | IBool -> text "_Bool"
  1382. | IInt -> text "int"
  1383. | IUInt -> text "unsigned int"
  1384. | IShort -> text "short"
  1385. | IUShort -> text "unsigned short"
  1386. | ILong -> text "long"
  1387. | IULong -> text "unsigned long"
  1388. | ILongLong ->
  1389. if !msvcMode then text "__int64" else text "long long"
  1390. | IULongLong ->
  1391. if !msvcMode then text "unsigned __int64"
  1392. else text "unsigned long long"
  1393. let d_fkind () = function
  1394. FFloat -> text "float"
  1395. | FDouble -> text "double"
  1396. | FLongDouble -> text "long double"
  1397. let d_storage () = function
  1398. NoStorage -> nil
  1399. | Static -> text "static "
  1400. | Extern -> text "extern "
  1401. | Register -> text "register "
  1402. (* sm: need this value below *)
  1403. let mostNeg32BitInt : int64 = (Int64.of_string "-0x80000000")
  1404. let mostNeg64BitInt : int64 = (Int64.of_string "-0x8000000000000000")
  1405. let bytesSizeOfInt (ik: ikind): int =
  1406. match ik with
  1407. | IChar | ISChar | IUChar -> 1
  1408. | IBool -> !M.theMachine.M.sizeof_bool
  1409. | IInt | IUInt -> !M.theMachine.M.sizeof_int
  1410. | IShort | IUShort -> !M.theMachine.M.sizeof_short
  1411. | ILong | IULong -> !M.theMachine.M.sizeof_long
  1412. | ILongLong | IULongLong -> !M.theMachine.M.sizeof_longlong
  1413. (* constant *)
  1414. let d_const () c =
  1415. match c with
  1416. CInt64(_, _, Some s) -> text s (* Always print the text if there is one *)
  1417. | CInt64(i, ik, None) ->
  1418. (** We must make sure to capture the type of the constant. For some
  1419. * constants this is done with a suffix, for others with a cast prefix.*)
  1420. let suffix : string =
  1421. match ik with
  1422. IUInt -> "U"
  1423. | ILong -> "L"
  1424. | IULong -> "UL"
  1425. | ILongLong -> if !msvcMode then "L" else "LL"
  1426. | IULongLong -> if !msvcMode then "UL" else "ULL"
  1427. | _ -> ""
  1428. in
  1429. let prefix : string =
  1430. if suffix <> "" then ""
  1431. else if ik = IInt then ""
  1432. else "(" ^ (sprint !lineLength (d_ikind () ik)) ^ ")"
  1433. in
  1434. (* Watch out here for negative integers that we should be printing as
  1435. * large positive ones *)
  1436. if i < Int64.zero && (not (isSigned ik)) then
  1437. if bytesSizeOfInt ik <> 8 then
  1438. (* I am convinced that we shall never store smaller than 64-bits
  1439. * integers in negative form. -- Gabriel *)
  1440. E.s (E.bug "unexpected negative unsigned integer (please report this bug)")
  1441. else
  1442. text (prefix ^ "0x" ^ Int64.format "%x" i ^ suffix)
  1443. else (
  1444. if (i = mostNeg32BitInt) then
  1445. (* sm: quirk here: if you print -2147483648 then this is two tokens *)
  1446. (* in C, and the second one is too large to represent in a signed *)
  1447. (* int.. so we do what's done in limits.h, and print (-2147483467-1); *)
  1448. (* in gcc this avoids a warning, but it might avoid a real problem *)
  1449. (* on another compiler or a 64-bit architecture *)
  1450. text (prefix ^ "(-0x7FFFFFFF-1)")
  1451. else if (i = mostNeg64BitInt) then
  1452. (* The same is true of the largest 64-bit negative. *)
  1453. text (prefix ^ "(-0x7FFFFFFFFFFFFFFF-1)")
  1454. else
  1455. text (prefix ^ (Int64.to_string i ^ suffix))
  1456. )
  1457. | CStr(s) -> text ("\"" ^ escape_string s ^ "\"")
  1458. | CWStr(s) ->
  1459. (* text ("L\"" ^ escape_string s ^ "\"") *)
  1460. (List.fold_left (fun acc elt ->
  1461. acc ++
  1462. if (elt >= Int64.zero &&
  1463. elt <= (Int64.of_int 255)) then
  1464. text (escape_char (Char.chr (Int64.to_int elt)))
  1465. else
  1466. ( text (Printf.sprintf "\\x%LX\"" elt) ++ break ++
  1467. (text "\""))
  1468. ) (text "L\"") s ) ++ text "\""
  1469. (* we cannot print L"\xabcd" "feedme" as L"\xabcdfeedme" --
  1470. * the former has 7 wide characters and the later has 3. *)
  1471. | CChr(c) -> text ("'" ^ escape_char c ^ "'")
  1472. | CReal(_, _, Some s) -> text s
  1473. | CReal(f, fsize, None) ->
  1474. text (string_of_float f) ++
  1475. (match fsize with
  1476. FFloat -> chr 'f'
  1477. | FDouble -> nil
  1478. | FLongDouble -> chr 'L')
  1479. | CEnum(_, s, ei) -> text s
  1480. (* Parentheses/precedence level. An expression "a op b" is printed
  1481. * parenthesized if its parentheses level is >= that that of its context.
  1482. * Identifiers have the lowest level and weakly binding operators (e.g. |)
  1483. * have the largest level. The correctness criterion is that a smaller level
  1484. * MUST correspond to a stronger precedence! *)
  1485. let derefStarLevel = 20
  1486. let indexLevel = 20
  1487. let arrowLevel = 20
  1488. let addrOfLevel = 30
  1489. let additiveLevel = 60
  1490. let comparativeLevel = 70
  1491. let bitwiseLevel = 75
  1492. let questionLevel = 100
  1493. let getParenthLevel (e: exp) =
  1494. match e with
  1495. | Question _ -> questionLevel
  1496. | BinOp((LAnd | LOr), _,_,_) -> 80
  1497. (* Bit operations. *)
  1498. | BinOp((BOr|BXor|BAnd),_,_,_) -> bitwiseLevel (* 75 *)
  1499. (* Comparisons *)
  1500. | BinOp((Eq|Ne|Gt|Lt|Ge|Le),_,_,_) ->
  1501. comparativeLevel (* 70 *)
  1502. (* Additive. Shifts can have higher
  1503. * level than + or - but I want
  1504. * parentheses around them *)
  1505. | BinOp((MinusA|MinusPP|MinusPI|PlusA|
  1506. PlusPI|IndexPI|Shiftlt|Shiftrt),_,_,_)
  1507. -> additiveLevel (* 60 *)
  1508. (* Multiplicative *)
  1509. | BinOp((Div|Mod|Mult),_,_,_) -> 40
  1510. (* Unary *)
  1511. | CastE(_,_) -> 30
  1512. | AddrOf(_) -> 30
  1513. | AddrOfLabel(_) -> 30
  1514. | StartOf(_) -> 30
  1515. | UnOp((Neg|BNot|LNot),_,_) -> 30
  1516. (* Lvals *)
  1517. | Lval(Mem _ , _) -> derefStarLevel (* 20 *)
  1518. | Lval(Var _, (Field _|Index _)) -> indexLevel (* 20 *)
  1519. | SizeOf _ | SizeOfE _ | SizeOfStr _ -> 20
  1520. | AlignOf _ | AlignOfE _ -> 20
  1521. | Lval(Var _, NoOffset) -> 0 (* Plain variables *)
  1522. | Const _ -> 0 (* Constants *)
  1523. let getParenthLevelAttrParam (a: attrparam) =
  1524. (* Create an expression of the same shape, and use {!getParenthLevel} *)
  1525. match a with
  1526. AInt _ | AStr _ | ACons _ -> 0
  1527. | ASizeOf _ | ASizeOfE _ | ASizeOfS _ -> 20
  1528. | AAlignOf _ | AAlignOfE _ | AAlignOfS _ -> 20
  1529. | AUnOp (uo, _) -> getParenthLevel (UnOp(uo, zero, intType))
  1530. | ABinOp (bo, _, _) -> getParenthLevel (BinOp(bo, zero, zero, intType))
  1531. | AAddrOf _ -> 30
  1532. | ADot _ | AIndex _ | AStar _ -> 20
  1533. | AQuestion _ -> questionLevel
  1534. (* Separate out the storage-modifier name attributes *)
  1535. let separateStorageModifiers (al: attribute list) =
  1536. let isstoragemod (Attr(an, _): attribute) : bool =
  1537. try
  1538. match H.find attributeHash an with
  1539. AttrName issm -> issm
  1540. | _ -> false
  1541. with Not_found -> false
  1542. in
  1543. let stom, rest = List.partition isstoragemod al in
  1544. if not !msvcMode then
  1545. stom, rest
  1546. else
  1547. (* Put back the declspec. Put it without the leading __ since these will
  1548. * be added later *)
  1549. let stom' =
  1550. Util.list_map (fun (Attr(an, args)) ->
  1551. Attr("declspec", [ACons(an, args)])) stom in
  1552. stom', rest
  1553. let isIntegralType t =
  1554. match unrollType t with
  1555. (TInt _ | TEnum _) -> true
  1556. | _ -> false
  1557. let isArithmeticType t =
  1558. match unrollType t with
  1559. (TInt _ | TEnum _ | TFloat _) -> true
  1560. | _ -> false
  1561. let isPointerType t =
  1562. match unrollType t with
  1563. TPtr _ -> true
  1564. | _ -> false
  1565. let isScalarType t =
  1566. isArithmeticType t || isPointerType t
  1567. let isFunctionType t =
  1568. match unrollType t with
  1569. TFun _ -> true
  1570. | _ -> false
  1571. (**** Compute the type of an expression ****)
  1572. let rec typeOf (e: exp) : typ =
  1573. match e with
  1574. | Const(CInt64 (_, ik, _)) -> TInt(ik, [])
  1575. (* Character constants have type int. ISO/IEC 9899:1999 (E),
  1576. * section 6.4.4.4 [Character constants], paragraph 10, if you
  1577. * don't believe me. *)
  1578. | Const(CChr _) -> intType
  1579. (* The type of a string is a pointer to characters ! The only case when
  1580. * you would want it to be an array is as an argument to sizeof, but we
  1581. * have SizeOfStr for that *)
  1582. | Const(CStr s) -> !stringLiteralType
  1583. | Const(CWStr s) -> TPtr(!wcharType,[])
  1584. | Const(CReal (_, fk, _)) -> TFloat(fk, [])
  1585. | Const(CEnum(tag, _, ei)) -> typeOf tag
  1586. | Lval(lv) -> typeOfLval lv
  1587. | SizeOf _ | SizeOfE _ | SizeOfStr _ -> !typeOfSizeOf
  1588. | AlignOf _ | AlignOfE _ -> !typeOfSizeOf
  1589. | UnOp (_, _, t)
  1590. | BinOp (_, _, _, t)
  1591. | Question (_, _, _, t)
  1592. | CastE (t, _) -> t
  1593. | AddrOf (lv) -> TPtr(typeOfLval lv, [])
  1594. | AddrOfLabel (lv) -> voidPtrType
  1595. | StartOf (lv) -> begin
  1596. match unrollType (typeOfLval lv) with
  1597. TArray (t,_, a) -> TPtr(t, a)
  1598. | _ -> E.s (E.bug "typeOf: StartOf on a non-array")
  1599. end
  1600. and typeOfInit (i: init) : typ =
  1601. match i with
  1602. SingleInit e -> typeOf e
  1603. | CompoundInit (t, _) -> t
  1604. and typeOfLval = function
  1605. Var vi, off -> typeOffset vi.vtype off
  1606. | Mem addr, off -> begin
  1607. match unrollType (typeOf addr) with
  1608. TPtr (t, _) -> typeOffset t off
  1609. | _ -> E.s (bug "typeOfLval: Mem on a non-pointer (%a)" !pd_exp addr)
  1610. end
  1611. and typeOffset basetyp =
  1612. let blendAttributes baseAttrs =
  1613. let (_, _, contageous) =
  1614. partitionAttributes ~default:(AttrName false) baseAttrs in
  1615. typeAddAttributes contageous
  1616. in
  1617. function
  1618. NoOffset -> basetyp
  1619. | Index (_, o) -> begin
  1620. match unrollType basetyp with
  1621. TArray (t, _, baseAttrs) ->
  1622. let elementType = typeOffset t o in
  1623. blendAttributes baseAttrs elementType
  1624. | t -> E.s (E.bug "typeOffset: Index on a non-array")
  1625. end
  1626. | Field (fi, o) ->
  1627. match unrollType basetyp with
  1628. TComp (_, baseAttrs) ->
  1629. let fieldType = typeOffset fi.ftype o in
  1630. blendAttributes baseAttrs fieldType
  1631. | _ -> E.s (bug "typeOffset: Field on a non-compound")
  1632. (**
  1633. **
  1634. ** MACHINE DEPENDENT PART
  1635. **
  1636. **)
  1637. exception SizeOfError of string * typ
  1638. let unsignedVersionOf (ik:ikind): ikind =
  1639. match ik with
  1640. | ISChar | IChar -> IUChar
  1641. | IShort -> IUShort
  1642. | IInt -> IUInt
  1643. | ILong -> IULong
  1644. | ILongLong -> IULongLong
  1645. | _ -> ik
  1646. let signedVersionOf (ik:ikind): ikind =
  1647. match ik with
  1648. | IUChar | IChar -> ISChar
  1649. | IUShort -> IShort
  1650. | IUInt -> IInt
  1651. | IULong -> ILong
  1652. | IULongLong -> ILongLong
  1653. | _ -> ik
  1654. (* Return the integer conversion rank of an integer kind *)
  1655. let intRank (ik:ikind) : int =
  1656. match ik with
  1657. | IBool -> 0
  1658. | IChar | ISChar | IUChar -> 1
  1659. | IShort | IUShort -> 2
  1660. | IInt | IUInt -> 3
  1661. | ILong | IULong -> 4
  1662. | ILongLong | IULongLong -> 5
  1663. (* Return the common integer kind of the two integer arguments, as
  1664. defined in ISO C 6.3.1.8 ("Usual arithmetic conversions") *)
  1665. let commonIntKind (ik1:ikind) (ik2:ikind) : ikind =
  1666. let r1 = intRank ik1 in
  1667. let r2 = intRank ik2 in
  1668. if (isSigned ik1) = (isSigned ik2) then begin
  1669. (* Both signed or both unsigned. *)
  1670. if r1 > r2 then ik1 else ik2
  1671. end
  1672. else begin
  1673. let signedKind, unsignedKind, signedRank, unsignedRank =
  1674. if isSigned ik1 then ik1, ik2, r1, r2 else ik2, ik1, r2, r1
  1675. in
  1676. (* The rules for signed + unsigned get hairy.
  1677. (unsigned short + long) is converted to signed long,
  1678. but (unsigned int + long) is converted to unsigned long.*)
  1679. if unsignedRank >= signedRank then unsignedKind
  1680. else if (bytesSizeOfInt signedKind) > (bytesSizeOfInt unsignedKind) then
  1681. signedKind
  1682. else
  1683. unsignedVersionOf signedKind
  1684. end
  1685. let intKindForSize (s:int) (unsigned:bool) : ikind =
  1686. if unsigned then
  1687. (* Test the most common sizes first *)
  1688. if s = 1 then IUChar
  1689. else if s = !M.theMachine.M.sizeof_int then IUInt
  1690. else if s = !M.theMachine.M.sizeof_long then IULong
  1691. else if s = !M.theMachine.M.sizeof_short then IUShort
  1692. else if s = !M.theMachine.M.sizeof_longlong then IULongLong
  1693. else raise Not_found
  1694. else
  1695. (* Test the most common sizes first *)
  1696. if s = 1 then ISChar
  1697. else if s = !M.theMachine.M.sizeof_int then IInt
  1698. else if s = !M.theMachine.M.sizeof_long then ILong
  1699. else if s = !M.theMachine.M.sizeof_short then IShort
  1700. else if s = !M.theMachine.M.sizeof_longlong then ILongLong
  1701. else raise Not_found
  1702. let floatKindForSize (s:int) =
  1703. if s = !M.theMachine.M.sizeof_double then FDouble
  1704. else if s = !M.theMachine.M.sizeof_float then FFloat
  1705. else if s = !M.theMachine.M.sizeof_longdouble then FLongDouble
  1706. else raise Not_found
  1707. (* Represents an integer as for a given kind. Returns a flag saying
  1708. whether any "interesting" bits were lost during truncation. By
  1709. "interesting", we mean that the lost bits were not all-0 or all-1. *)
  1710. let truncateCilint (k: ikind) (i: cilint) : cilint * truncation =
  1711. (* Truncations to _Bool are special: they behave like "!= 0"
  1712. ISO C99 6.3.1.2 *)
  1713. if k = IBool then
  1714. if is_zero_cilint i then
  1715. zero_cilint, NoTruncation
  1716. else
  1717. one_cilint, NoTruncation
  1718. else
  1719. let nrBits = 8 * (bytesSizeOfInt k) in
  1720. if isSigned k then
  1721. truncate_signed_cilint i nrBits
  1722. else
  1723. truncate_unsigned_cilint i nrBits
  1724. let mkCilint (ik:ikind) (i:int64) : cilint =
  1725. fst (truncateCilint ik (cilint_of_int64 i))
  1726. (* Construct an integer constant with possible truncation *)
  1727. let kintegerCilint (k: ikind) (i: cilint) : exp =
  1728. let i', truncated = truncateCilint k i in
  1729. if truncated = BitTruncation && !warnTruncate then
  1730. ignore (warnOpt "Truncating integer %s to %s"
  1731. (string_of_cilint i) (string_of_cilint i'));
  1732. Const (CInt64(int64_of_cilint i', k, None))
  1733. (* Construct an integer constant with possible truncation *)
  1734. let kinteger64 (k: ikind) (i: int64) : exp =
  1735. kintegerCilint k (cilint_of_int64 i)
  1736. (* Construct an integer of a given kind. *)
  1737. let kinteger (k: ikind) (i: int) =
  1738. kintegerCilint k (cilint_of_int i)
  1739. (** Construct an integer of kind IInt. On targets where C's 'int' is 16-bits,
  1740. the integer may get truncated. *)
  1741. let integer (i: int) = kinteger IInt i
  1742. let one = integer 1
  1743. let mone = integer (-1)
  1744. (* True if the integer fits within the kind's range *)
  1745. let fitsInInt (k: ikind) (i: cilint) : bool =
  1746. let _, truncated = truncateCilint k i in
  1747. truncated = NoTruncation
  1748. (* Return the smallest kind that will hold the integer's value. The
  1749. kind will be unsigned if the 2nd argument is true, signed
  1750. otherwise. Note that if the value doesn't fit in any of the
  1751. available types, you will get ILongLong (2nd argument false) or
  1752. IULongLong (2nd argument true). *)
  1753. let intKindForValue (i: cilint) (unsigned: bool) =
  1754. if unsigned then
  1755. if fitsInInt IUChar i then IUChar
  1756. else if fitsInInt IUShort i then IUShort
  1757. else if fitsInInt IUInt i then IUInt
  1758. else if fitsInInt IULong i then IULong
  1759. else IULongLong
  1760. else
  1761. if fitsInInt ISChar i then ISChar
  1762. else if fitsInInt IShort i then IShort
  1763. else if fitsInInt IInt i then IInt
  1764. else if fitsInInt ILong i then ILong
  1765. else ILongLong
  1766. (** If the given expression is an integer constant or a CastE'd
  1767. integer constant, return that constant's value as an ikint, int64 pair.
  1768. Otherwise return None. *)
  1769. let rec getInteger (e:exp) : cilint option =
  1770. match e with
  1771. | Const(CInt64 (n, ik, _)) -> Some (mkCilint ik n)
  1772. | Const(CChr c) -> getInteger (Const (charConstToInt c))
  1773. | Const(CEnum(v, _, _)) -> getInteger v
  1774. | CastE(t, e) -> begin
  1775. (* Handle any truncation due to cast. We optimistically ignore
  1776. loss-of-precision due to floating-point casts. *)
  1777. let mkInt ik n = Some (fst (truncateCilint ik n)) in
  1778. match unrollType t, getInteger e with
  1779. | TInt (ik, _), Some n -> mkInt ik n
  1780. | TPtr _, Some n -> begin
  1781. match !upointType with
  1782. TInt (ik, _) -> mkInt ik n
  1783. | _ -> raise (Failure "pointer size unknown")
  1784. end
  1785. | TEnum (ei, _), Some n -> mkInt ei.ekind n
  1786. | TFloat _, v -> v
  1787. | _, _ -> None
  1788. end
  1789. | _ -> None
  1790. let isZero (e: exp) : bool =
  1791. match getInteger e with
  1792. | Some n -> is_zero_cilint n
  1793. | _ -> false
  1794. type offsetAcc =
  1795. { oaFirstFree: int; (* The first free bit *)
  1796. oaLastFieldStart: int; (* Where the previous field started *)
  1797. oaLastFieldWidth: int; (* The width of the previous field. Might not
  1798. * be same as FirstFree - FieldStart because
  1799. * of internal padding *)
  1800. oaPrevBitPack: (int * ikind * int) option; (* If the previous fields
  1801. * were packed bitfields,
  1802. * the bit where packing
  1803. * has started, the ikind
  1804. * of the bitfield and the
  1805. * width of the ikind *)
  1806. }
  1807. (* Hack to prevent infinite recursion in alignments *)
  1808. let ignoreAlignmentAttrs = ref false
  1809. (* Get the minimum aligment in bytes for a given type *)
  1810. let rec alignOf_int t =
  1811. let alignOfType () =
  1812. match t with
  1813. | TInt((IChar|ISChar|IUChar), _) -> 1
  1814. | TInt(IBool, _) -> !M.theMachine.M.alignof_bool
  1815. | TInt((IShort|IUShort), _) -> !M.theMachine.M.alignof_short
  1816. | TInt((IInt|IUInt), _) -> !M.theMachine.M.alignof_int
  1817. | TInt((ILong|IULong), _) -> !M.theMachine.M.alignof_long
  1818. | TInt((ILongLong|IULongLong), _) -> !M.theMachine.M.alignof_longlong
  1819. | TEnum(ei, _) -> alignOf_int (TInt(ei.ekind, []))
  1820. | TFloat(FFloat, _) -> !M.theMachine.M.alignof_float
  1821. | TFloat(FDouble, _) -> !M.theMachine.M.alignof_double
  1822. | TFloat(FLongDouble, _) -> !M.theMachine.M.alignof_longdouble
  1823. | TNamed (t, _) -> alignOf_int t.ttype
  1824. | TArray (t, _, _) -> alignOf_int t
  1825. | TPtr _ | TBuiltin_va_list _ -> !M.theMachine.M.alignof_ptr
  1826. (* For composite types get the maximum alignment of any field inside *)
  1827. | TComp (c, _) ->
  1828. (* On GCC the zero-width fields do not contribute to the alignment.
  1829. * On MSVC only those zero-width that _do_ appear after other
  1830. * bitfields contribute to the alignment. So we drop those that
  1831. * do not occur after othe bitfields *)
  1832. let rec dropZeros (afterbitfield: bool) = function
  1833. | f :: rest when f.fbitfield = Some 0 && not afterbitfield ->
  1834. dropZeros afterbitfield rest
  1835. | f :: rest -> f :: dropZeros (f.fbitfield <> None) rest
  1836. | [] -> []
  1837. in
  1838. let fields = dropZeros false c.cfields in
  1839. List.fold_left
  1840. (fun sofar f ->
  1841. (* Bitfields with zero width do not contribute to the alignment in
  1842. * GCC *)
  1843. if not !msvcMode && f.fbitfield = Some 0 then sofar else
  1844. max sofar (alignOfField f)) 1 fields
  1845. (* These are some error cases *)
  1846. | TFun _ when not !msvcMode -> !M.theMachine.M.alignof_fun
  1847. | TFun _ as t -> raise (SizeOfError ("function", t))
  1848. | TVoid _ as t -> raise (SizeOfError ("void", t))
  1849. in
  1850. match filterAttributes "aligned" (typeAttrs t) with
  1851. [] ->
  1852. (* no __aligned__ attribute, so get the default alignment *)
  1853. alignOfType ()
  1854. | _ when !ignoreAlignmentAttrs ->
  1855. ignore (warn "ignoring recursive align attributes on %a"
  1856. (!pd_type) t);
  1857. alignOfType ()
  1858. | (Attr(_, [a]) as at)::rest -> begin
  1859. if rest <> [] then
  1860. ignore (warn "ignoring duplicate align attributes on %a"
  1861. (!pd_type) t);
  1862. match intOfAttrparam a with
  1863. Some n -> n
  1864. | None ->
  1865. ignore (warn "alignment attribute \"%a\" not understood on %a"
  1866. (!pd_attr) at (!pd_type) t);
  1867. alignOfType ()
  1868. end
  1869. | Attr(_, [])::rest ->
  1870. (* aligned with no arg means a power of two at least as large as
  1871. any alignment on the system.*)
  1872. if rest <> [] then
  1873. ignore(warn "ignoring duplicate align attributes on %a"
  1874. (!pd_type) t);
  1875. !M.theMachine.M.alignof_aligned
  1876. | at::_ ->
  1877. ignore (warn "alignment attribute \"%a\" not understood on %a"
  1878. (!pd_attr) at (!pd_type) t);
  1879. alignOfType ()
  1880. (* alignment of a possibly-packed struct field. *)
  1881. and alignOfField (fi: fieldinfo) =
  1882. let fieldIsPacked = hasAttribute "packed" fi.fattr
  1883. || hasAttribute "packed" fi.fcomp.cattr in
  1884. if fieldIsPacked then 1
  1885. else alignOf_int fi.ftype
  1886. and intOfAttrparam (a:attrparam) : int option =
  1887. let rec doit a : int =
  1888. match a with
  1889. AInt(n) -> n
  1890. | ABinOp(Shiftlt, a1, a2) -> (doit a1) lsl (doit a2)
  1891. | ABinOp(Div, a1, a2) -> (doit a1) / (doit a2)
  1892. | ASizeOf(t) ->
  1893. let bs = bitsSizeOf t in
  1894. bs / 8
  1895. | AAlignOf(t) ->
  1896. alignOf_int t
  1897. | _ -> raise (SizeOfError ("", voidType))
  1898. in
  1899. (* Use ignoreAlignmentAttrs here to prevent stack overflow if a buggy
  1900. program does something like
  1901. struct s {...} __attribute__((aligned(sizeof(struct s))))
  1902. This is too conservative, but it's often enough.
  1903. *)
  1904. assert (not !ignoreAlignmentAttrs);
  1905. ignoreAlignmentAttrs := true;
  1906. try
  1907. let n = doit a in
  1908. ignoreAlignmentAttrs := false;
  1909. Some n
  1910. with SizeOfError _ -> (* Can't compile *)
  1911. ignoreAlignmentAttrs := false;
  1912. None
  1913. (* GCC version *)
  1914. (* Does not use the sofar.oaPrevBitPack *)
  1915. and offsetOfFieldAcc_GCC
  1916. (fi: fieldinfo)
  1917. (sofar: offsetAcc) : offsetAcc =
  1918. (* field type *)
  1919. let ftype = unrollType fi.ftype in
  1920. let ftypeAlign = 8 * alignOfField fi in
  1921. let ftypeBits = bitsSizeOf ftype in
  1922. match ftype, fi.fbitfield with
  1923. (* A width of 0 means that we must end the current packing. It seems that
  1924. * GCC pads only up to the alignment boundary for the type of this field.
  1925. * *)
  1926. | _, Some 0 ->
  1927. let firstFree = addTrailing sofar.oaFirstFree ftypeAlign in
  1928. { oaFirstFree = firstFree;
  1929. oaLastFieldStart = firstFree;
  1930. oaLastFieldWidth = 0;
  1931. oaPrevBitPack = None }
  1932. (* A bitfield cannot span more alignment boundaries of its type than the
  1933. * type itself *)
  1934. | _, Some wdthis
  1935. when (sofar.oaFirstFree + wdthis + ftypeAlign - 1) / ftypeAlign
  1936. - sofar.oaFirstFree / ftypeAlign > ftypeBits / ftypeAlign ->
  1937. let start = addTrailing sofar.oaFirstFree ftypeAlign in
  1938. { oaFirstFree = start + wdthis;
  1939. oaLastFieldStart = start;
  1940. oaLastFieldWidth = wdthis;
  1941. oaPrevBitPack = None }
  1942. (* Try a simple method. Just put the field down *)
  1943. | _, Some wdthis ->
  1944. { oaFirstFree = sofar.oaFirstFree + wdthis;
  1945. oaLastFieldStart = sofar.oaFirstFree;
  1946. oaLastFieldWidth = wdthis;
  1947. oaPrevBitPack = None
  1948. }
  1949. (* Non-bitfield *)
  1950. | _, None ->
  1951. (* Align this field *)
  1952. let newStart = addTrailing sofar.oaFirstFree ftypeAlign in
  1953. { oaFirstFree = newStart + ftypeBits;
  1954. oaLastFieldStart = newStart;
  1955. oaLastFieldWidth = ftypeBits;
  1956. oaPrevBitPack = None;
  1957. }
  1958. (* MSVC version *)
  1959. and offsetOfFieldAcc_MSVC (fi: fieldinfo)
  1960. (sofar: offsetAcc) : offsetAcc =
  1961. (* field type *)
  1962. let ftype = unrollType fi.ftype in
  1963. let ftypeAlign = 8 * alignOf_int ftype in
  1964. let ftypeBits = bitsSizeOf ftype in
  1965. (*
  1966. ignore (E.log "offsetOfFieldAcc_MSVC(%s of %s:%a%a,firstFree=%d, pack=%a)\n"
  1967. fi.fname fi.fcomp.cname
  1968. d_type ftype
  1969. insert
  1970. (match fi.fbitfield with
  1971. None -> nil
  1972. | Some wdthis -> dprintf ":%d" wdthis)
  1973. sofar.oaFirstFree
  1974. insert
  1975. (match sofar.oaPrevBitPack with
  1976. None -> text "None"
  1977. | Some (prevpack, _, wdpack) -> dprintf "Some(prev=%d,wd=%d)"
  1978. prevpack wdpack));
  1979. *)
  1980. match ftype, fi.fbitfield, sofar.oaPrevBitPack with
  1981. (* Ignore zero-width bitfields that come after non-bitfields *)
  1982. | TInt (ikthis, _), Some 0, None ->
  1983. let firstFree = sofar.oaFirstFree in
  1984. { oaFirstFree = firstFree;
  1985. oaLastFieldStart = firstFree;
  1986. oaLastFieldWidth = 0;
  1987. oaPrevBitPack = None }
  1988. (* If we are in a bitpack and we see a bitfield for a type with the
  1989. * different width than the pack, then we finish the pack and retry *)
  1990. | _, Some _, Some (packstart, _, wdpack) when wdpack != ftypeBits ->
  1991. let firstFree =
  1992. if sofar.oaFirstFree = packstart then packstart else
  1993. packstart + wdpack
  1994. in
  1995. offsetOfFieldAcc_MSVC fi
  1996. { oaFirstFree = addTrailing firstFree ftypeAlign;
  1997. oaLastFieldStart = sofar.oaLastFieldStart;
  1998. oaLastFieldWidth = sofar.oaLastFieldWidth;
  1999. oaPrevBitPack = None }
  2000. (* A width of 0 means that we must end the current packing. *)
  2001. | TInt (ikthis, _), Some 0, Some (packstart, _, wdpack) ->
  2002. let firstFree =
  2003. if sofar.oaFirstFree = packstart then packstart else
  2004. packstart + wdpack
  2005. in
  2006. let firstFree = addTrailing firstFree ftypeAlign in
  2007. { oaFirstFree = firstFree;
  2008. oaLastFieldStart = firstFree;
  2009. oaLastFieldWidth = 0;
  2010. oaPrevBitPack = Some (firstFree, ikthis, ftypeBits) }
  2011. (* Check for a bitfield that fits in the current pack after some other
  2012. * bitfields *)
  2013. | TInt(ikthis, _), Some wdthis, Some (packstart, ikprev, wdpack)
  2014. when packstart + wdpack >= sofar.oaFirstFree + wdthis ->
  2015. { oaFirstFree = sofar.oaFirstFree + wdthis;
  2016. oaLastFieldStart = sofar.oaFirstFree;
  2017. oaLastFieldWidth = wdthis;
  2018. oaPrevBitPack = sofar.oaPrevBitPack
  2019. }
  2020. | _, _, Some (packstart, _, wdpack) -> (* Finish up the bitfield pack and
  2021. * restart. *)
  2022. let firstFree =
  2023. if sofar.oaFirstFree = packstart then packstart else
  2024. packstart + wdpack
  2025. in
  2026. offsetOfFieldAcc_MSVC fi
  2027. { oaFirstFree = addTrailing firstFree ftypeAlign;
  2028. oaLastFieldStart = sofar.oaLastFieldStart;
  2029. oaLastFieldWidth = sofar.oaLastFieldWidth;
  2030. oaPrevBitPack = None }
  2031. (* No active bitfield pack. But we are seeing a bitfield. *)
  2032. | TInt(ikthis, _), Some wdthis, None ->
  2033. let firstFree = addTrailing sofar.oaFirstFree ftypeAlign in
  2034. { oaFirstFree = firstFree + wdthis;
  2035. oaLastFieldStart = firstFree;
  2036. oaLastFieldWidth = wdthis;
  2037. oaPrevBitPack = Some (firstFree, ikthis, ftypeBits); }
  2038. (* No active bitfield pack. Non-bitfield *)
  2039. | _, None, None ->
  2040. (* Align this field *)
  2041. let firstFree = addTrailing sofar.oaFirstFree ftypeAlign in
  2042. { oaFirstFree = firstFree + ftypeBits;
  2043. oaLastFieldStart = firstFree;
  2044. oaLastFieldWidth = ftypeBits;
  2045. oaPrevBitPack = None;
  2046. }
  2047. | _, Some _, None -> E.s (E.bug "offsetAcc")
  2048. and offsetOfFieldAcc ~(fi: fieldinfo)
  2049. ~(sofar: offsetAcc) : offsetAcc =
  2050. if !msvcMode then offsetOfFieldAcc_MSVC fi sofar
  2051. else offsetOfFieldAcc_GCC fi sofar
  2052. (* The size of a type, in bits. If a struct or array, then trailing padding is
  2053. * added *)
  2054. and bitsSizeOf t =
  2055. if not !initCIL_called then
  2056. E.s (E.error "You did not call Cil.initCIL before using the CIL library");
  2057. match t with
  2058. | TInt (ik,_) -> 8 * (bytesSizeOfInt ik)
  2059. | TFloat(FDouble, _) -> 8 * !M.theMachine.M.sizeof_double
  2060. | TFloat(FLongDouble, _) -> 8 * !M.theMachine.M.sizeof_longdouble
  2061. | TFloat _ -> 8 * !M.theMachine.M.sizeof_float
  2062. | TEnum (ei, _) -> bitsSizeOf (TInt(ei.ekind, []))
  2063. | TPtr _ -> 8 * !M.theMachine.M.sizeof_ptr
  2064. | TBuiltin_va_list _ -> 8 * !M.theMachine.M.sizeof_ptr
  2065. | TNamed (t, _) -> bitsSizeOf t.ttype
  2066. | TComp (comp, _) when comp.cfields == [] -> begin
  2067. (* Empty structs are allowed in msvc mode *)
  2068. if not comp.cdefined && not !msvcMode then
  2069. raise (SizeOfError ("abstract type", t)) (*abstract type*)
  2070. else
  2071. 0
  2072. end
  2073. | TComp (comp, _) when comp.cstruct -> (* Struct *)
  2074. (* Go and get the last offset *)
  2075. let startAcc =
  2076. { oaFirstFree = 0;
  2077. oaLastFieldStart = 0;
  2078. oaLastFieldWidth = 0;
  2079. oaPrevBitPack = None;
  2080. } in
  2081. let lastoff =
  2082. List.fold_left (fun acc fi -> offsetOfFieldAcc ~fi ~sofar:acc)
  2083. startAcc comp.cfields
  2084. in
  2085. if !msvcMode && lastoff.oaFirstFree = 0 && comp.cfields <> [] then
  2086. (* On MSVC if we have just a zero-width bitfields then the length
  2087. * is 32 and is not padded *)
  2088. 32
  2089. else begin
  2090. (* Drop e.g. the align attribute from t. For this purpose,
  2091. consider only the attributes on comp itself.*)
  2092. let structAlign = 8 * alignOf_int
  2093. (TComp (comp, [])) in
  2094. addTrailing lastoff.oaFirstFree structAlign
  2095. end
  2096. | TComp (comp, _) -> (* when not comp.cstruct *)
  2097. (* Get the maximum of all fields *)
  2098. let startAcc =
  2099. { oaFirstFree = 0;
  2100. oaLastFieldStart = 0;
  2101. oaLastFieldWidth = 0;
  2102. oaPrevBitPack = None;
  2103. } in
  2104. let max =
  2105. List.fold_left (fun acc fi ->
  2106. let lastoff = offsetOfFieldAcc ~fi ~sofar:startAcc in
  2107. if lastoff.oaFirstFree > acc then
  2108. lastoff.oaFirstFree else acc) 0 comp.cfields in
  2109. (* Add trailing by simulating adding an extra field *)
  2110. addTrailing max (8 * alignOf_int t)
  2111. | TArray(bt, Some len, _) -> begin
  2112. match constFold true len with
  2113. Const(CInt64(l,lk,_)) ->
  2114. let sz = mul_cilint (mkCilint lk l) (cilint_of_int (bitsSizeOf bt)) in
  2115. (* Check for overflow.
  2116. There are other places in these cil.ml that overflow can occur,
  2117. but this multiplication is the most likely to be a problem. *)
  2118. if not (is_int_cilint sz) then
  2119. raise (SizeOfError ("Array is so long that its size can't be "
  2120. ^"represented with an OCaml int.", t))
  2121. else
  2122. addTrailing (int_of_cilint sz) (8 * alignOf_int t)
  2123. | _ -> raise (SizeOfError ("array non-constant length", t))
  2124. end
  2125. | TVoid _ -> 8 * !M.theMachine.M.sizeof_void
  2126. | TFun _ when not !msvcMode -> (* On GCC the size of a function is defined *)
  2127. 8 * !M.theMachine.M.sizeof_fun
  2128. | TArray (_, None, _) -> (* it seems that on GCC the size of such an
  2129. * array is 0 *)
  2130. 0
  2131. | TFun _ -> raise (SizeOfError ("function", t))
  2132. and addTrailing nrbits roundto =
  2133. (nrbits + roundto - 1) land (lnot (roundto - 1))
  2134. and sizeOf t =
  2135. try
  2136. integer ((bitsSizeOf t) lsr 3)
  2137. with SizeOfError _ -> SizeOf(t)
  2138. and bitsOffset (baset: typ) (off: offset) : int * int =
  2139. let rec loopOff (baset: typ) (width: int) (start: int) = function
  2140. NoOffset -> start, width
  2141. | Index(e, off) -> begin
  2142. let ei =
  2143. match getInteger e with
  2144. Some i -> cilint_to_int i
  2145. | None -> raise (SizeOfError ("index not constant", baset))
  2146. in
  2147. let bt =
  2148. match unrollType baset with
  2149. TArray(bt, _, _) -> bt
  2150. | _ -> E.s (E.bug "bitsOffset: Index on a non-array")
  2151. in
  2152. let bitsbt = bitsSizeOf bt in
  2153. loopOff bt bitsbt (start + ei * bitsbt) off
  2154. end
  2155. | Field(f, off) when not f.fcomp.cstruct ->
  2156. (* All union fields start at offset 0 *)
  2157. loopOff f.ftype (bitsSizeOf f.ftype) start off
  2158. | Field(f, off) ->
  2159. (* Construct a list of fields preceeding and including this one *)
  2160. let prevflds =
  2161. let rec loop = function
  2162. [] -> E.s (E.bug "bitsOffset: Cannot find field %s in %s\n"
  2163. f.fname f.fcomp.cname)
  2164. | fi' :: _ when fi' == f -> [fi']
  2165. | fi' :: rest -> fi' :: loop rest
  2166. in
  2167. loop f.fcomp.cfields
  2168. in
  2169. let lastoff =
  2170. List.fold_left (fun acc fi' -> offsetOfFieldAcc ~fi:fi' ~sofar:acc)
  2171. { oaFirstFree = 0; (* Start at 0 because each struct is done
  2172. * separately *)
  2173. oaLastFieldStart = 0;
  2174. oaLastFieldWidth = 0;
  2175. oaPrevBitPack = None } prevflds
  2176. in
  2177. (* ignore (E.log "Field %s of %s: start=%d, lastFieldStart=%d\n"
  2178. f.fname f.fcomp.cname start lastoff.oaLastFieldStart); *)
  2179. loopOff f.ftype lastoff.oaLastFieldWidth
  2180. (start + lastoff.oaLastFieldStart) off
  2181. in
  2182. loopOff baset (bitsSizeOf baset) 0 off
  2183. (** Do constant folding on an expression. If the first argument is true then
  2184. will also compute compiler-dependent expressions such as sizeof.
  2185. See also {!Cil.constFoldVisitor}, which will run constFold on all
  2186. expressions in a given AST node.*)
  2187. and constFold (machdep: bool) (e: exp) : exp =
  2188. match e with
  2189. BinOp(bop, e1, e2, tres) -> constFoldBinOp machdep bop e1 e2 tres
  2190. | UnOp(unop, e1, tres) -> begin
  2191. try
  2192. let tk =
  2193. match unrollType tres with
  2194. TInt(ik, _) -> ik
  2195. | TEnum (ei, _) -> ei.ekind
  2196. | _ -> raise Not_found (* probably a float *)
  2197. in
  2198. match constFold machdep e1 with
  2199. Const(CInt64(i,ik,_)) -> begin
  2200. let ic = mkCilint ik i in
  2201. match unop with
  2202. Neg -> kintegerCilint tk (neg_cilint ic)
  2203. | BNot -> kintegerCilint tk (lognot_cilint ic)
  2204. | LNot -> if is_zero_cilint ic then one else zero
  2205. end
  2206. | e1c -> UnOp(unop, e1c, tres)
  2207. with Not_found -> e
  2208. end
  2209. (* Characters are integers *)
  2210. | Const(CChr c) -> Const(charConstToInt c)
  2211. | Const(CEnum (v, _, _)) -> constFold machdep v
  2212. | SizeOf t when machdep -> begin
  2213. try
  2214. let bs = bitsSizeOf t in
  2215. kinteger !kindOfSizeOf (bs / 8)
  2216. with SizeOfError _ -> e
  2217. end
  2218. | SizeOfE e when machdep -> constFold machdep (SizeOf (typeOf e))
  2219. | SizeOfStr s when machdep -> kinteger !kindOfSizeOf (1 + String.length s)
  2220. | AlignOf t when machdep -> kinteger !kindOfSizeOf (alignOf_int t)
  2221. | AlignOfE e when machdep -> begin
  2222. (* The alignment of an expression is not always the alignment of its
  2223. * type. I know that for strings this is not true *)
  2224. match e with
  2225. Const (CStr _) when not !msvcMode ->
  2226. kinteger !kindOfSizeOf !M.theMachine.M.alignof_str
  2227. (* For an array, it is the alignment of the array ! *)
  2228. | _ -> constFold machdep (AlignOf (typeOf e))
  2229. end
  2230. | CastE(it,
  2231. AddrOf (Mem (CastE(TPtr(bt, _), z)), off))
  2232. when machdep && isZero z -> begin
  2233. try
  2234. let start, width = bitsOffset bt off in
  2235. if start mod 8 <> 0 then
  2236. E.s (error "Using offset of bitfield");
  2237. constFold machdep (CastE(it, (kinteger !kindOfSizeOf (start / 8))))
  2238. with SizeOfError _ -> e
  2239. end
  2240. | CastE (t, e) -> begin
  2241. match constFold machdep e, unrollType t with
  2242. (* Might truncate silently *)
  2243. | Const(CInt64(i,k,_)), TInt(nk,a)
  2244. (* It's okay to drop a cast to const.
  2245. If the cast has any other attributes, leave the cast alone. *)
  2246. when (dropAttributes ["const"] a) = [] ->
  2247. let i', _ = truncateCilint nk (mkCilint k i) in
  2248. Const(CInt64(int64_of_cilint i', nk, None))
  2249. | e', _ -> CastE (t, e')
  2250. end
  2251. | Lval lv -> Lval (constFoldLval machdep lv)
  2252. | AddrOf lv -> AddrOf (constFoldLval machdep lv)
  2253. | StartOf lv -> StartOf (constFoldLval machdep lv)
  2254. | _ -> e
  2255. and constFoldLval machdep (host,offset) =
  2256. let newhost =
  2257. match host with
  2258. | Mem e -> Mem (constFold machdep e)
  2259. | Var _ -> host
  2260. in
  2261. let rec constFoldOffset machdep = function
  2262. | NoOffset -> NoOffset
  2263. | Field (fi,offset) -> Field (fi, constFoldOffset machdep offset)
  2264. | Index (exp,offset) -> Index (constFold machdep exp,
  2265. constFoldOffset machdep offset)
  2266. in
  2267. (newhost, constFoldOffset machdep offset)
  2268. and constFoldBinOp (machdep: bool) bop e1 e2 tres =
  2269. let e1' = constFold machdep e1 in
  2270. let e2' = constFold machdep e2 in
  2271. if isIntegralType tres then begin
  2272. let newe =
  2273. let tk =
  2274. match unrollType tres with
  2275. TInt(ik, _) -> ik
  2276. | TEnum (ei, _) -> ei.ekind
  2277. | _ -> E.s (bug "constFoldBinOp")
  2278. in
  2279. let collapse0 () = kinteger tk 0 in
  2280. let collapse e = e (*mkCast e tres*) in
  2281. let shiftInBounds i2 =
  2282. (* We only try to fold shifts if the second arg is positive and
  2283. less than the size of the type of the first argument.
  2284. Otherwise, the semantics are processor-dependent, so let the
  2285. compiler sort it out. *)
  2286. if machdep then
  2287. try
  2288. compare_cilint i2 zero_cilint >= 0 &&
  2289. compare_cilint i2 (cilint_of_int (bitsSizeOf (typeOf e1'))) < 0
  2290. with SizeOfError _ -> false
  2291. else false
  2292. in
  2293. (* Assume that the necessary promotions have been done *)
  2294. match bop, getInteger e1', getInteger e2' with
  2295. | PlusA, Some i1, Some i2 -> kintegerCilint tk (add_cilint i1 i2)
  2296. | PlusA, Some z, _ when is_zero_cilint z -> collapse e2'
  2297. | PlusA, _, Some z when is_zero_cilint z -> collapse e1'
  2298. | MinusA, Some i1, Some i2 -> kintegerCilint tk (sub_cilint i1 i2)
  2299. | MinusA, _, Some z when is_zero_cilint z -> collapse e1'
  2300. | Mult, Some i1, Some i2 -> kintegerCilint tk (mul_cilint i1 i2)
  2301. | Mult, Some z, _ when is_zero_cilint z -> collapse0 ()
  2302. | Mult, _, Some z when is_zero_cilint z -> collapse0 ()
  2303. | Mult, Some o, _ when compare_cilint o one_cilint = 0 -> collapse e2'
  2304. | Mult, _, Some o when compare_cilint o one_cilint = 0 -> collapse e1'
  2305. | Div, Some i1, Some i2 -> begin
  2306. try kintegerCilint tk (div0_cilint i1 i2)
  2307. with Division_by_zero -> BinOp(bop, e1', e2', tres)
  2308. end
  2309. | Div, _, Some o when compare_cilint o one_cilint = 0 -> collapse e1'
  2310. | Mod, Some i1, Some i2 -> begin
  2311. try kintegerCilint tk (rem_cilint i1 i2)
  2312. with Division_by_zero -> BinOp(bop, e1', e2', tres)
  2313. end
  2314. | Mod, _, Some o when compare_cilint o one_cilint = 0 -> collapse0 ()
  2315. | BAnd, Some i1, Some i2 -> kintegerCilint tk (logand_cilint i1 i2)
  2316. | BAnd, Some z, _ when is_zero_cilint z -> collapse0 ()
  2317. | BAnd, _, Some z when is_zero_cilint z -> collapse0 ()
  2318. | BOr, Some i1, Some i2 -> kintegerCilint tk (logor_cilint i1 i2)
  2319. | BOr, Some z, _ when is_zero_cilint z -> collapse e2'
  2320. | BOr, _, Some z when is_zero_cilint z -> collapse e1'
  2321. | BXor, Some i1, Some i2 -> kintegerCilint tk (logxor_cilint i1 i2)
  2322. | BXor, Some z, _ when is_zero_cilint z -> collapse e2'
  2323. | BXor, _, Some z when is_zero_cilint z -> collapse e1'
  2324. | Shiftlt, Some i1, Some i2 when shiftInBounds i2 ->
  2325. kintegerCilint tk (shift_left_cilint i1 (int_of_cilint i2))
  2326. | Shiftlt, Some z, _ when is_zero_cilint z -> collapse0 ()
  2327. | Shiftlt, _, Some z when is_zero_cilint z -> collapse e1'
  2328. | Shiftrt, Some i1, Some i2 when shiftInBounds i2 ->
  2329. kintegerCilint tk (shift_right_cilint i1 (int_of_cilint i2))
  2330. | Shiftrt, Some z, _ when is_zero_cilint z -> collapse0 ()
  2331. | Shiftrt, _, Some z when is_zero_cilint z -> collapse e1'
  2332. | Eq, Some i1, Some i2 -> if compare_cilint i1 i2 = 0 then one else zero
  2333. | Ne, Some i1, Some i2 -> if compare_cilint i1 i2 <> 0 then one else zero
  2334. | Le, Some i1, Some i2 -> if compare_cilint i1 i2 <= 0 then one else zero
  2335. | Ge, Some i1, Some i2 -> if compare_cilint i1 i2 >= 0 then one else zero
  2336. | Lt, Some i1, Some i2 -> if compare_cilint i1 i2 < 0 then one else zero
  2337. | Gt, Some i1, Some i2 -> if compare_cilint i1 i2 > 0 then one else zero
  2338. | LAnd, Some i1, _ -> if is_zero_cilint i1 then collapse0 () else collapse e2'
  2339. | LAnd, _, Some i2 -> if is_zero_cilint i2 then collapse0 () else collapse e1'
  2340. | LOr, Some i1, _ -> if is_zero_cilint i1 then collapse e2' else one
  2341. | LOr, _, Some i2 -> if is_zero_cilint i2 then collapse e1' else one
  2342. | _ -> BinOp(bop, e1', e2', tres)
  2343. in
  2344. if debugConstFold then
  2345. ignore (E.log "Folded %a to %a\n"
  2346. (!pd_exp) (BinOp(bop, e1', e2', tres)) (!pd_exp) newe);
  2347. newe
  2348. end else
  2349. BinOp(bop, e1', e2', tres)
  2350. let parseInt (str: string) : exp =
  2351. let hasSuffix str =
  2352. let l = String.length str in
  2353. fun s ->
  2354. let ls = String.length s in
  2355. l >= ls && s = String.uppercase (String.sub str (l - ls) ls)
  2356. in
  2357. let l = String.length str in
  2358. (* See if it is octal or hex *)
  2359. let octalhex = (l >= 1 && String.get str 0 = '0') in
  2360. (* The length of the suffix and a list of possible kinds. See ISO
  2361. * 6.4.4.1 *)
  2362. let hasSuffix = hasSuffix str in
  2363. let suffixlen, kinds =
  2364. if hasSuffix "ULL" || hasSuffix "LLU" then
  2365. 3, [IULongLong]
  2366. else if hasSuffix "LL" then
  2367. 2, if octalhex then [ILongLong; IULongLong] else [ILongLong]
  2368. else if hasSuffix "UL" || hasSuffix "LU" then
  2369. 2, [IULong; IULongLong]
  2370. else if hasSuffix "L" then
  2371. 1, if octalhex then [ILong; IULong; ILongLong; IULongLong]
  2372. else [ILong; ILongLong]
  2373. else if hasSuffix "U" then
  2374. 1, [IUInt; IULong; IULongLong]
  2375. else if (!msvcMode && hasSuffix "UI64") then
  2376. 4, [IULongLong]
  2377. else if (!msvcMode && hasSuffix "I64") then
  2378. 3, [ILongLong]
  2379. else
  2380. 0, if octalhex then [IInt; IUInt; ILong; IULong; ILongLong; IULongLong]
  2381. else if not !c99Mode then [ IInt; ILong; IULong; ILongLong; IULongLong]
  2382. else [IInt; ILong; ILongLong]
  2383. in
  2384. (* Convert to integer. To prevent overflow we do the arithmetic on
  2385. * cilints. We work only with positive integers since the lexer
  2386. * takes care of the sign *)
  2387. let rec toInt (base: cilint) (acc: cilint) (idx: int) : cilint =
  2388. let doAcc (what: int) =
  2389. let acc' = add_cilint (mul_cilint base acc) (cilint_of_int what) in
  2390. toInt base acc' (idx + 1)
  2391. in
  2392. if idx >= l - suffixlen then begin
  2393. acc
  2394. end else
  2395. let ch = String.get str idx in
  2396. if ch >= '0' && ch <= '9' then
  2397. doAcc (Char.code ch - Char.code '0')
  2398. else if ch >= 'a' && ch <= 'f' then
  2399. doAcc (10 + Char.code ch - Char.code 'a')
  2400. else if ch >= 'A' && ch <= 'F' then
  2401. doAcc (10 + Char.code ch - Char.code 'A')
  2402. else
  2403. E.s (bug "Invalid integer constant: %s (char %c at idx=%d)"
  2404. str ch idx)
  2405. in
  2406. let i =
  2407. if octalhex then
  2408. if l >= 2 &&
  2409. (let c = String.get str 1 in c = 'x' || c = 'X') then
  2410. toInt (cilint_of_int 16) zero_cilint 2
  2411. else
  2412. toInt (cilint_of_int 8) zero_cilint 1
  2413. else
  2414. toInt (cilint_of_int 10) zero_cilint 0
  2415. in
  2416. (* Construct an integer of the first kinds that fits. i must be
  2417. * POSITIVE *)
  2418. let res =
  2419. let rec loop = function
  2420. k::rest ->
  2421. if fitsInInt k i then kintegerCilint k i
  2422. else loop rest
  2423. | [] -> E.s (E.unimp "Cannot represent the integer %s\n"
  2424. (string_of_cilint i))
  2425. in
  2426. loop kinds
  2427. in
  2428. res
  2429. (* with e -> begin *)
  2430. (* ignore (E.log "int_of_string %s (%s)\n" str *)
  2431. (* (Printexc.to_string e)); *)
  2432. (* zero *)
  2433. (* end *)
  2434. let d_unop () u =
  2435. match u with
  2436. Neg -> text "-"
  2437. | BNot -> text "~"
  2438. | LNot -> text "!"
  2439. let d_binop () b =
  2440. match b with
  2441. PlusA | PlusPI | IndexPI -> text "+"
  2442. | MinusA | MinusPP | MinusPI -> text "-"
  2443. | Mult -> text "*"
  2444. | Div -> text "/"
  2445. | Mod -> text "%"
  2446. | Shiftlt -> text "<<"
  2447. | Shiftrt -> text ">>"
  2448. | Lt -> text "<"
  2449. | Gt -> text ">"
  2450. | Le -> text "<="
  2451. | Ge -> text ">="
  2452. | Eq -> text "=="
  2453. | Ne -> text "!="
  2454. | BAnd -> text "&"
  2455. | BXor -> text "^"
  2456. | BOr -> text "|"
  2457. | LAnd -> text "&&"
  2458. | LOr -> text "||"
  2459. let invalidStmt = mkStmt (Instr [])
  2460. (** Construct a hash with the builtins *)
  2461. let builtinFunctions : (string, typ * typ list * bool) H.t =
  2462. H.create 49
  2463. (* Initialize the builtin functions after the machine has been initialized. *)
  2464. let initGccBuiltins () : unit =
  2465. if not !initCIL_called then
  2466. E.s (bug "Call initCIL before initGccBuiltins");
  2467. if H.length builtinFunctions <> 0 then
  2468. E.s (bug "builtins already initialized.");
  2469. let h = builtinFunctions in
  2470. (* See if we have builtin_va_list *)
  2471. let hasbva = !M.theMachine.M.__builtin_va_list in
  2472. let ulongLongType = TInt(IULongLong, []) in
  2473. let floatType = TFloat(FFloat, []) in
  2474. let longDoubleType = TFloat (FLongDouble, []) in
  2475. let voidConstPtrType = TPtr(TVoid [Attr ("const", [])], []) in
  2476. let sizeType = !typeOfSizeOf in
  2477. let v4sfType = TFloat (FFloat,[Attr("__vector_size__", [AInt 16])]) in
  2478. H.add h "__builtin___fprintf_chk" (intType, [ voidPtrType; intType; charConstPtrType ], true) (* first argument is really FILE*, not void*, but we don't want to build in the definition for FILE *);
  2479. H.add h "__builtin___memcpy_chk" (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  2480. H.add h "__builtin___memmove_chk" (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  2481. H.add h "__builtin___mempcpy_chk" (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  2482. H.add h "__builtin___memset_chk" (voidPtrType, [ voidPtrType; intType; sizeType; sizeType ], false);
  2483. H.add h "__builtin___printf_chk" (intType, [ intType; charConstPtrType ], true);
  2484. H.add h "__builtin___snprintf_chk" (intType, [ charPtrType; sizeType; intType; sizeType; charConstPtrType ], true);
  2485. H.add h "__builtin___sprintf_chk" (intType, [ charPtrType; intType; sizeType; charConstPtrType ], true);
  2486. H.add h "__builtin___stpcpy_chk" (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  2487. H.add h "__builtin___strcat_chk" (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  2488. H.add h "__builtin___strcpy_chk" (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  2489. H.add h "__builtin___strncat_chk" (charPtrType, [ charPtrType; charConstPtrType; sizeType; sizeType ], false);
  2490. H.add h "__builtin___strncpy_chk" (charPtrType, [ charPtrType; charConstPtrType; sizeType; sizeType ], false);
  2491. H.add h "__builtin___vfprintf_chk" (intType, [ voidPtrType; intType; charConstPtrType; TBuiltin_va_list [] ], false) (* first argument is really FILE*, not void*, but we don't want to build in the definition for FILE *);
  2492. H.add h "__builtin___vprintf_chk" (intType, [ intType; charConstPtrType; TBuiltin_va_list [] ], false);
  2493. H.add h "__builtin___vsnprintf_chk" (intType, [ charPtrType; sizeType; intType; sizeType; charConstPtrType; TBuiltin_va_list [] ], false);
  2494. H.add h "__builtin___vsprintf_chk" (intType, [ charPtrType; intType; sizeType; charConstPtrType; TBuiltin_va_list [] ], false);
  2495. H.add h "__builtin_acos" (doubleType, [ doubleType ], false);
  2496. H.add h "__builtin_acosf" (floatType, [ floatType ], false);
  2497. H.add h "__builtin_acosl" (longDoubleType, [ longDoubleType ], false);
  2498. H.add h "__builtin_alloca" (voidPtrType, [ sizeType ], false);
  2499. H.add h "__builtin_asin" (doubleType, [ doubleType ], false);
  2500. H.add h "__builtin_asinf" (floatType, [ floatType ], false);
  2501. H.add h "__builtin_asinl" (longDoubleType, [ longDoubleType ], false);
  2502. H.add h "__builtin_atan" (doubleType, [ doubleType ], false);
  2503. H.add h "__builtin_atanf" (floatType, [ floatType ], false);
  2504. H.add h "__builtin_atanl" (longDoubleType, [ longDoubleType ], false);
  2505. H.add h "__builtin_atan2" (doubleType, [ doubleType; doubleType ], false);
  2506. H.add h "__builtin_atan2f" (floatType, [ floatType; floatType ], false);
  2507. H.add h "__builtin_atan2l" (longDoubleType, [ longDoubleType;
  2508. longDoubleType ], false);
  2509. let addSwap sizeInBits =
  2510. try
  2511. assert (sizeInBits mod 8 = 0);
  2512. let sizeInBytes = sizeInBits / 8 in
  2513. let sizedIntType = TInt (intKindForSize sizeInBytes false, []) in
  2514. let name = Printf.sprintf "__builtin_bswap%d" sizeInBits in
  2515. H.add h name (sizedIntType, [ sizedIntType ], false)
  2516. with Not_found ->
  2517. ()
  2518. in
  2519. addSwap 16;
  2520. addSwap 32;
  2521. addSwap 64;
  2522. H.add h "__builtin_ceil" (doubleType, [ doubleType ], false);
  2523. H.add h "__builtin_ceilf" (floatType, [ floatType ], false);
  2524. H.add h "__builtin_ceill" (longDoubleType, [ longDoubleType ], false);
  2525. H.add h "__builtin_cos" (doubleType, [ doubleType ], false);
  2526. H.add h "__builtin_cosf" (floatType, [ floatType ], false);
  2527. H.add h "__builtin_cosl" (longDoubleType, [ longDoubleType ], false);
  2528. H.add h "__builtin_cosh" (doubleType, [ doubleType ], false);
  2529. H.add h "__builtin_coshf" (floatType, [ floatType ], false);
  2530. H.add h "__builtin_coshl" (longDoubleType, [ longDoubleType ], false);
  2531. H.add h "__builtin_clz" (intType, [ uintType ], false);
  2532. H.add h "__builtin_clzl" (intType, [ ulongType ], false);
  2533. H.add h "__builtin_clzll" (intType, [ ulongLongType ], false);
  2534. H.add h "__builtin_constant_p" (intType, [ intType ], false);
  2535. H.add h "__builtin_ctz" (intType, [ uintType ], false);
  2536. H.add h "__builtin_ctzl" (intType, [ ulongType ], false);
  2537. H.add h "__builtin_ctzll" (intType, [ ulongLongType ], false);
  2538. H.add h "__builtin_exp" (doubleType, [ doubleType ], false);
  2539. H.add h "__builtin_expf" (floatType, [ floatType ], false);
  2540. H.add h "__builtin_expl" (longDoubleType, [ longDoubleType ], false);
  2541. H.add h "__builtin_expect" (longType, [ longType; longType ], false);
  2542. H.add h "__builtin_trap" (voidType, [], false);
  2543. H.add h "__builtin_unreachable" (voidType, [], false);
  2544. H.add h "__builtin_fabs" (doubleType, [ doubleType ], false);
  2545. H.add h "__builtin_fabsf" (floatType, [ floatType ], false);
  2546. H.add h "__builtin_fabsl" (longDoubleType, [ longDoubleType ], false);
  2547. H.add h "__builtin_ffs" (intType, [ uintType ], false);
  2548. H.add h "__builtin_ffsl" (intType, [ ulongType ], false);
  2549. H.add h "__builtin_ffsll" (intType, [ ulongLongType ], false);
  2550. H.add h "__builtin_frame_address" (voidPtrType, [ uintType ], false);
  2551. H.add h "__builtin_floor" (doubleType, [ doubleType ], false);
  2552. H.add h "__builtin_floorf" (floatType, [ floatType ], false);
  2553. H.add h "__builtin_floorl" (longDoubleType, [ longDoubleType ], false);
  2554. H.add h "__builtin_huge_val" (doubleType, [], false);
  2555. H.add h "__builtin_huge_valf" (floatType, [], false);
  2556. H.add h "__builtin_huge_vall" (longDoubleType, [], false);
  2557. H.add h "__builtin_inf" (doubleType, [], false);
  2558. H.add h "__builtin_inff" (floatType, [], false);
  2559. H.add h "__builtin_infl" (longDoubleType, [], false);
  2560. H.add h "__builtin_memcpy" (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType ], false);
  2561. H.add h "__builtin_mempcpy" (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType ], false);
  2562. H.add h "__builtin_memset" (voidPtrType,
  2563. [ voidPtrType; intType; intType ], false);
  2564. H.add h "__builtin_bcopy" (voidType, [ voidConstPtrType; voidPtrType; sizeType ], false);
  2565. H.add h "__builtin_bzero" (voidType,
  2566. [ voidPtrType; sizeType ], false);
  2567. H.add h "__builtin_fmod" (doubleType, [ doubleType ], false);
  2568. H.add h "__builtin_fmodf" (floatType, [ floatType ], false);
  2569. H.add h "__builtin_fmodl" (longDoubleType, [ longDoubleType ], false);
  2570. H.add h "__builtin_frexp" (doubleType, [ doubleType; intPtrType ], false);
  2571. H.add h "__builtin_frexpf" (floatType, [ floatType; intPtrType ], false);
  2572. H.add h "__builtin_frexpl" (longDoubleType, [ longDoubleType;
  2573. intPtrType ], false);
  2574. H.add h "__builtin_ldexp" (doubleType, [ doubleType; intType ], false);
  2575. H.add h "__builtin_ldexpf" (floatType, [ floatType; intType ], false);
  2576. H.add h "__builtin_ldexpl" (longDoubleType, [ longDoubleType;
  2577. intType ], false);
  2578. H.add h "__builtin_log" (doubleType, [ doubleType ], false);
  2579. H.add h "__builtin_logf" (floatType, [ floatType ], false);
  2580. H.add h "__builtin_logl" (longDoubleType, [ longDoubleType ], false);
  2581. H.add h "__builtin_log10" (doubleType, [ doubleType ], false);
  2582. H.add h "__builtin_log10f" (floatType, [ floatType ], false);
  2583. H.add h "__builtin_log10l" (longDoubleType, [ longDoubleType ], false);
  2584. H.add h "__builtin_modff" (floatType, [ floatType;
  2585. TPtr(floatType,[]) ], false);
  2586. H.add h "__builtin_modfl" (longDoubleType, [ longDoubleType;
  2587. TPtr(longDoubleType, []) ],
  2588. false);
  2589. H.add h "__builtin_nan" (doubleType, [ charConstPtrType ], false);
  2590. H.add h "__builtin_nanf" (floatType, [ charConstPtrType ], false);
  2591. H.add h "__builtin_nanl" (longDoubleType, [ charConstPtrType ], false);
  2592. H.add h "__builtin_nans" (doubleType, [ charConstPtrType ], false);
  2593. H.add h "__builtin_nansf" (floatType, [ charConstPtrType ], false);
  2594. H.add h "__builtin_nansl" (longDoubleType, [ charConstPtrType ], false);
  2595. H.add h "__builtin_next_arg" ((if hasbva then TBuiltin_va_list [] else voidPtrType), [], false) (* When we parse builtin_next_arg we drop the argument *);
  2596. H.add h "__builtin_object_size" (sizeType, [ voidPtrType; intType ], false);
  2597. H.add h "__builtin_parity" (intType, [ uintType ], false);
  2598. H.add h "__builtin_parityl" (intType, [ ulongType ], false);
  2599. H.add h "__builtin_parityll" (intType, [ ulongLongType ], false);
  2600. H.add h "__builtin_popcount" (intType, [ uintType ], false);
  2601. H.add h "__builtin_popcountl" (intType, [ ulongType ], false);
  2602. H.add h "__builtin_popcountll" (intType, [ ulongLongType ], false);
  2603. H.add h "__builtin_powi" (doubleType, [ doubleType; intType ], false);
  2604. H.add h "__builtin_powif" (floatType, [ floatType; intType ], false);
  2605. H.add h "__builtin_powil" (longDoubleType, [ longDoubleType; intType ], false);
  2606. H.add h "__builtin_prefetch" (voidType, [ voidConstPtrType ], true);
  2607. H.add h "__builtin_return" (voidType, [ voidConstPtrType ], false);
  2608. H.add h "__builtin_return_address" (voidPtrType, [ uintType ], false);
  2609. H.add h "__builtin_extract_return_addr" (voidPtrType, [ voidPtrType ], false);
  2610. H.add h "__builtin_frob_return_address" (voidPtrType, [ voidPtrType ], false);
  2611. H.add h "__builtin_sin" (doubleType, [ doubleType ], false);
  2612. H.add h "__builtin_sinf" (floatType, [ floatType ], false);
  2613. H.add h "__builtin_sinl" (longDoubleType, [ longDoubleType ], false);
  2614. H.add h "__builtin_sinh" (doubleType, [ doubleType ], false);
  2615. H.add h "__builtin_sinhf" (floatType, [ floatType ], false);
  2616. H.add h "__builtin_sinhl" (longDoubleType, [ longDoubleType ], false);
  2617. H.add h "__builtin_sqrt" (doubleType, [ doubleType ], false);
  2618. H.add h "__builtin_sqrtf" (floatType, [ floatType ], false);
  2619. H.add h "__builtin_sqrtl" (longDoubleType, [ longDoubleType ], false);
  2620. H.add h "__builtin_stpcpy" (charPtrType, [ charPtrType; charConstPtrType ], false);
  2621. H.add h "__builtin_strchr" (charPtrType, [ charPtrType; intType ], false);
  2622. H.add h "__builtin_strcmp" (intType, [ charConstPtrType; charConstPtrType ], false);
  2623. H.add h "__builtin_strcpy" (charPtrType, [ charPtrType; charConstPtrType ], false);
  2624. H.add h "__builtin_strlen" (sizeType, [ charConstPtrType ], false);
  2625. H.add h "__builtin_strcspn" (sizeType, [ charConstPtrType; charConstPtrType ], false);
  2626. H.add h "__builtin_strncat" (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  2627. H.add h "__builtin_strncmp" (intType, [ charConstPtrType; charConstPtrType; sizeType ], false);
  2628. H.add h "__builtin_strncpy" (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  2629. H.add h "__builtin_strspn" (sizeType, [ charConstPtrType; charConstPtrType ], false);
  2630. H.add h "__builtin_strpbrk" (charPtrType, [ charConstPtrType; charConstPtrType ], false);
  2631. (* When we parse builtin_types_compatible_p, we change its interface *)
  2632. H.add h "__builtin_types_compatible_p"
  2633. (intType, [ !typeOfSizeOf;(* Sizeof the type *)
  2634. !typeOfSizeOf (* Sizeof the type *) ],
  2635. false);
  2636. H.add h "__builtin_tan" (doubleType, [ doubleType ], false);
  2637. H.add h "__builtin_tanf" (floatType, [ floatType ], false);
  2638. H.add h "__builtin_tanl" (longDoubleType, [ longDoubleType ], false);
  2639. H.add h "__builtin_tanh" (doubleType, [ doubleType ], false);
  2640. H.add h "__builtin_tanhf" (floatType, [ floatType ], false);
  2641. H.add h "__builtin_tanhl" (longDoubleType, [ longDoubleType ], false);
  2642. (* MMX Builtins *)
  2643. H.add h "__builtin_ia32_addps" (v4sfType, [v4sfType; v4sfType], false);
  2644. H.add h "__builtin_ia32_subps" (v4sfType, [v4sfType; v4sfType], false);
  2645. H.add h "__builtin_ia32_mulps" (v4sfType, [v4sfType; v4sfType], false);
  2646. H.add h "__builtin_ia32_unpckhps" (v4sfType, [v4sfType; v4sfType], false);
  2647. H.add h "__builtin_ia32_unpcklps" (v4sfType, [v4sfType; v4sfType], false);
  2648. H.add h "__builtin_ia32_maxps" (v4sfType, [v4sfType; v4sfType], false);
  2649. (* Atomic Builtins
  2650. These builtins have an overloaded return type, hence the "magic" void type
  2651. with __overloaded__ attribute, used to infer return type from parameters in
  2652. cabs2cil.ml.
  2653. For the same reason, we do not specify the type of the parameters. *)
  2654. H.add h "__sync_fetch_and_add" (TVoid[Attr("overloaded",[])], [ ], true);
  2655. H.add h "__sync_fetch_and_sub" (TVoid[Attr("overloaded",[])], [ ], true);
  2656. H.add h "__sync_fetch_and_or" (TVoid[Attr("overloaded",[])], [ ], true);
  2657. H.add h "__sync_fetch_and_and" (TVoid[Attr("overloaded",[])], [ ], true);
  2658. H.add h "__sync_fetch_and_xor" (TVoid[Attr("overloaded",[])], [ ], true);
  2659. H.add h "__sync_fetch_and_nand" (TVoid[Attr("overloaded",[])], [ ], true);
  2660. H.add h "__sync_add_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2661. H.add h "__sync_sub_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2662. H.add h "__sync_or_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2663. H.add h "__sync_and_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2664. H.add h "__sync_xor_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2665. H.add h "__sync_nand_and_fetch" (TVoid[Attr("overloaded",[])], [ ], true);
  2666. H.add h "__sync_bool_compare_and_swap" (TInt (IBool, []), [ ], true);
  2667. H.add h "__sync_val_compare_and_swap" (TVoid[Attr("overloaded",[])], [ ],
  2668. true);
  2669. H.add h "__sync_synchronize" (voidType, [ ], true);
  2670. H.add h "__sync_lock_test_and_set" (TVoid[Attr("overloaded",[])], [ ], true);
  2671. H.add h "__sync_lock_release" (voidType, [ ], true);
  2672. (* __atomic builtins for various bit widths
  2673. Most __atomic functions are offered for various bit widths, using
  2674. a different suffix for each concrete bit width: "_1", "_2", and
  2675. so on up to "_16". Each of these functions also exists in a form
  2676. with no bit width specified, and occasionally with a bit width
  2677. suffix of "_n".
  2678. Note that these __atomic functions are not really va_arg, but we
  2679. set the va_arg flag nonetheless because it prevents CIL from
  2680. trying to check the type of parameters against the prototype.
  2681. *)
  2682. let addAtomicForWidths baseName ?n ~none ~num () =
  2683. (* ?n gives the return type to be used with the "_n" suffix, if any *)
  2684. (* ~none gives the return type to be used with no suffix *)
  2685. (* ~num gives the return type to be used with the "_1" through "_16" suffixes *)
  2686. let addWithSuffix suffix returnType =
  2687. let identifier = "__atomic_" ^ baseName ^ suffix in
  2688. H.add h identifier (returnType, [], true)
  2689. in
  2690. List.iter begin
  2691. fun bitWidth ->
  2692. let suffix = "_" ^ (string_of_int bitWidth) in
  2693. addWithSuffix suffix num
  2694. end [1; 2; 4; 8; 16];
  2695. addWithSuffix "" none;
  2696. match n with
  2697. | None -> ()
  2698. | Some typ -> addWithSuffix "_n" typ
  2699. in
  2700. let anyType = TVoid [Attr("overloaded", [])] in
  2701. (* binary operations combined with a fetch of either the old or new value *)
  2702. List.iter begin
  2703. fun operation ->
  2704. addAtomicForWidths ("fetch_" ^ operation) ~none:anyType ~num:anyType ();
  2705. addAtomicForWidths (operation ^ "_fetch") ~none:anyType ~num:anyType ()
  2706. end ["add"; "and"; "nand"; "or"; "sub"; "xor"];
  2707. (* other atomic operations provided at various bit widths *)
  2708. addAtomicForWidths "compare_exchange" ~none:boolType ~n:boolType ~num:boolType ();
  2709. addAtomicForWidths "exchange" ~none:voidType ~n:anyType ~num:anyType ();
  2710. addAtomicForWidths "load" ~none:voidType ~n:anyType ~num:anyType ();
  2711. addAtomicForWidths "store" ~none:voidType ~n:voidType ~num:voidType ();
  2712. (* Some atomic builtins actually have a decent, C-compatible type *)
  2713. H.add h "__atomic_test_and_set" (boolType, [voidPtrType; intType], false);
  2714. H.add h "__atomic_clear" (voidType, [boolPtrType; intType], false);
  2715. H.add h "__atomic_thread_fence" (voidType, [intType], false);
  2716. H.add h "__atomic_signal_fence" (voidType, [intType], false);
  2717. H.add h "__atomic_always_lock_free" (boolType, [sizeType; voidPtrType], false);
  2718. H.add h "__atomic_is_lock_free" (boolType, [sizeType; voidPtrType], false);
  2719. H.add h "__atomic_feraiseexcept" (voidType, [intType], false);
  2720. if hasbva then begin
  2721. H.add h "__builtin_va_end" (voidType, [ TBuiltin_va_list [] ], false);
  2722. H.add h "__builtin_varargs_start"
  2723. (voidType, [ TBuiltin_va_list [] ], false);
  2724. (* When we parse builtin_{va,stdarg}_start, we drop the second argument *)
  2725. H.add h "__builtin_va_start" (voidType, [ TBuiltin_va_list [] ], false);
  2726. H.add h "__builtin_stdarg_start" (voidType, [ TBuiltin_va_list []; ],
  2727. false);
  2728. (* When we parse builtin_va_arg we change its interface *)
  2729. H.add h "__builtin_va_arg" (voidType, [ TBuiltin_va_list [];
  2730. !typeOfSizeOf;(* Sizeof the type *)
  2731. voidPtrType; (* Ptr to res *) ],
  2732. false);
  2733. H.add h "__builtin_va_copy" (voidType, [ TBuiltin_va_list [];
  2734. TBuiltin_va_list [] ],
  2735. false);
  2736. end;
  2737. H.add h "__builtin_apply_args" (voidPtrType, [ ], false);
  2738. let fnPtr = TPtr(TFun (voidType, None, false, []), []) in
  2739. H.add h "__builtin_apply" (voidPtrType, [fnPtr; voidPtrType; sizeType], false);
  2740. H.add h "__builtin_va_arg_pack" (intType, [ ], false);
  2741. H.add h "__builtin_va_arg_pack_len" (intType, [ ], false);
  2742. ()
  2743. (** Construct a hash with the builtins *)
  2744. let initMsvcBuiltins () : unit =
  2745. if not !initCIL_called then
  2746. E.s (bug "Call initCIL before initGccBuiltins");
  2747. if H.length builtinFunctions <> 0 then
  2748. E.s (bug "builtins already initialized.");
  2749. let h = builtinFunctions in
  2750. (** Take a number of wide string literals *)
  2751. H.add h "__annotation" (voidType, [ ], true);
  2752. ()
  2753. (** This is used as the location of the prototypes of builtin functions. *)
  2754. let builtinLoc: location = { line = 1;
  2755. file = "<compiler builtins>";
  2756. byte = 0;}
  2757. let pTypeSig : (typ -> typsig) ref =
  2758. ref (fun _ -> E.s (E.bug "pTypeSig not initialized"))
  2759. (** A printer interface for CIL trees. Create instantiations of
  2760. * this type by specializing the class {!Cil.defaultCilPrinter}. *)
  2761. class type cilPrinter = object
  2762. method setCurrentFormals : varinfo list -> unit
  2763. method setPrintInstrTerminator : string -> unit
  2764. method getPrintInstrTerminator : unit -> string
  2765. method pVDecl: unit -> varinfo -> doc
  2766. (** Invoked for each variable declaration. Note that variable
  2767. * declarations are all the [GVar], [GVarDecl], [GFun], all the [varinfo]
  2768. * in formals of function types, and the formals and locals for function
  2769. * definitions. *)
  2770. method pVar: varinfo -> doc
  2771. (** Invoked on each variable use. *)
  2772. method pLval: unit -> lval -> doc
  2773. (** Invoked on each lvalue occurence *)
  2774. method pOffset: doc -> offset -> doc
  2775. (** Invoked on each offset occurence. The second argument is the base. *)
  2776. method pInstr: unit -> instr -> doc
  2777. (** Invoked on each instruction occurrence. *)
  2778. method pStmt: unit -> stmt -> doc
  2779. (** Control-flow statement. This is used by
  2780. * {!Cil.printGlobal} and by {!Cil.dumpGlobal}. *)
  2781. method dStmt: out_channel -> int -> stmt -> unit
  2782. (** Dump a control-flow statement to a file with a given indentation. This is used by
  2783. * {!Cil.dumpGlobal}. *)
  2784. method dBlock: out_channel -> int -> block -> unit
  2785. (** Dump a control-flow block to a file with a given indentation. This is
  2786. * used by {!Cil.dumpGlobal}. *)
  2787. method pBlock: unit -> block -> Pretty.doc
  2788. (** Print a block. *)
  2789. method pGlobal: unit -> global -> doc
  2790. (** Global (vars, types, etc.). This can be slow and is used only by
  2791. * {!Cil.printGlobal} but by {!Cil.dumpGlobal} for everything else except
  2792. * [GVar] and [GFun]. *)
  2793. method dGlobal: out_channel -> global -> unit
  2794. (** Dump a global to a file. This is used by {!Cil.dumpGlobal}. *)
  2795. method pFieldDecl: unit -> fieldinfo -> doc
  2796. (** A field declaration *)
  2797. method pType: doc option -> unit -> typ -> doc
  2798. (* Use of some type in some declaration. The first argument is used to print
  2799. * the declared element, or is None if we are just printing a type with no
  2800. * name being declared. Note that for structure/union and enumeration types
  2801. * the definition of the composite type is not visited. Use [vglob] to
  2802. * visit it. *)
  2803. method pAttr: attribute -> doc * bool
  2804. (** Attribute. Also return an indication whether this attribute must be
  2805. * printed inside the __attribute__ list or not. *)
  2806. method pAttrParam: unit -> attrparam -> doc
  2807. (** Attribute paramter *)
  2808. method pAttrs: unit -> attributes -> doc
  2809. (** Attribute lists *)
  2810. method pLabel: unit -> label -> doc
  2811. (** Label *)
  2812. method pLineDirective: ?forcefile:bool -> location -> Pretty.doc
  2813. (** Print a line-number. This is assumed to come always on an empty line.
  2814. * If the forcefile argument is present and is true then the file name
  2815. * will be printed always. Otherwise the file name is printed only if it
  2816. * is different from the last time time this function is called. The last
  2817. * file name is stored in a private field inside the cilPrinter object. *)
  2818. method pStmtKind : stmt -> unit -> stmtkind -> Pretty.doc
  2819. (** Print a statement kind. The code to be printed is given in the
  2820. * {!Cil.stmtkind} argument. The initial {!Cil.stmt} argument
  2821. * records the statement which follows the one being printed;
  2822. * {!Cil.defaultCilPrinterClass} uses this information to prettify
  2823. * statement printing in certain special cases. *)
  2824. method pExp: unit -> exp -> doc
  2825. (** Print expressions *)
  2826. method pInit: unit -> init -> doc
  2827. (** Print initializers. This can be slow and is used by
  2828. * {!Cil.printGlobal} but not by {!Cil.dumpGlobal}. *)
  2829. method dInit: out_channel -> int -> init -> unit
  2830. (** Dump a global to a file with a given indentation. This is used by
  2831. * {!Cil.dumpGlobal}. *)
  2832. end
  2833. class defaultCilPrinterClass : cilPrinter = object (self)
  2834. val mutable currentFormals : varinfo list = []
  2835. method private getLastNamedArgument (s:string) : exp =
  2836. match List.rev currentFormals with
  2837. f :: _ -> Lval (var f)
  2838. | [] ->
  2839. E.s (bug "Cannot find the last named argument when printing call to %s\n" s)
  2840. method private setCurrentFormals (fms : varinfo list) =
  2841. currentFormals <- fms
  2842. (*** VARIABLES ***)
  2843. (* variable use *)
  2844. method pVar (v:varinfo) = text v.vname
  2845. (* variable declaration *)
  2846. method pVDecl () (v:varinfo) =
  2847. let stom, rest = separateStorageModifiers v.vattr in
  2848. (* First the storage modifiers *)
  2849. text (if v.vinline then "__inline " else "")
  2850. ++ d_storage () v.vstorage
  2851. ++ (self#pAttrs () stom)
  2852. ++ (self#pType (Some (text v.vname)) () v.vtype)
  2853. ++ text " "
  2854. ++ self#pAttrs () rest
  2855. (*** L-VALUES ***)
  2856. method pLval () (lv:lval) = (* lval (base is 1st field) *)
  2857. match lv with
  2858. Var vi, o -> self#pOffset (self#pVar vi) o
  2859. | Mem e, Field(fi, o) ->
  2860. self#pOffset
  2861. ((self#pExpPrec arrowLevel () e) ++ text ("->" ^ fi.fname)) o
  2862. | Mem e, NoOffset ->
  2863. text "*" ++ self#pExpPrec derefStarLevel () e
  2864. | Mem e, o ->
  2865. self#pOffset
  2866. (text "(*" ++ self#pExpPrec derefStarLevel () e ++ text ")") o
  2867. (** Offsets **)
  2868. method pOffset (base: doc) = function
  2869. | NoOffset -> base
  2870. | Field (fi, o) ->
  2871. self#pOffset (base ++ text "." ++ text fi.fname) o
  2872. | Index (e, o) ->
  2873. self#pOffset (base ++ text "[" ++ self#pExp () e ++ text "]") o
  2874. method private pLvalPrec (contextprec: int) () lv =
  2875. if getParenthLevel (Lval(lv)) >= contextprec then
  2876. text "(" ++ self#pLval () lv ++ text ")"
  2877. else
  2878. self#pLval () lv
  2879. (*** EXPRESSIONS ***)
  2880. method pExp () (e: exp) : doc =
  2881. let level = getParenthLevel e in
  2882. match e with
  2883. Const(c) -> d_const () c
  2884. | Lval(l) -> self#pLval () l
  2885. | UnOp(u,e1,_) ->
  2886. (d_unop () u) ++ chr ' ' ++ (self#pExpPrec level () e1)
  2887. | BinOp(b,e1,e2,_) ->
  2888. align
  2889. ++ (self#pExpPrec level () e1)
  2890. ++ chr ' '
  2891. ++ (d_binop () b)
  2892. ++ chr ' '
  2893. ++ (self#pExpPrec level () e2)
  2894. ++ unalign
  2895. | Question(e1,e2,e3,_) ->
  2896. (self#pExpPrec level () e1)
  2897. ++ text " ? "
  2898. ++ (self#pExpPrec level () e2)
  2899. ++ text " : "
  2900. ++ (self#pExpPrec level () e3)
  2901. | CastE(t,e) ->
  2902. text "("
  2903. ++ self#pType None () t
  2904. ++ text ")"
  2905. ++ self#pExpPrec level () e
  2906. | SizeOf (t) ->
  2907. text "sizeof(" ++ self#pType None () t ++ chr ')'
  2908. | SizeOfE (Lval (Var fv, NoOffset)) when fv.vname = "__builtin_va_arg_pack" && (not !printCilAsIs) ->
  2909. text "__builtin_va_arg_pack()"
  2910. | SizeOfE (e) ->
  2911. text "sizeof(" ++ self#pExp () e ++ chr ')'
  2912. | SizeOfStr s ->
  2913. text "sizeof(" ++ d_const () (CStr s) ++ chr ')'
  2914. | AlignOf (t) ->
  2915. text "__alignof__(" ++ self#pType None () t ++ chr ')'
  2916. | AlignOfE (e) ->
  2917. text "__alignof__(" ++ self#pExp () e ++ chr ')'
  2918. | AddrOf(lv) ->
  2919. text "& " ++ (self#pLvalPrec addrOfLevel () lv)
  2920. | AddrOfLabel(sref) -> begin
  2921. (* Grab one of the labels *)
  2922. let rec pickLabel = function
  2923. [] -> None
  2924. | Label (l, _, _) :: _ -> Some l
  2925. | _ :: rest -> pickLabel rest
  2926. in
  2927. match pickLabel !sref.labels with
  2928. Some lbl -> text ("&& " ^ lbl)
  2929. | None ->
  2930. ignore (error "Cannot find label for target of address of label");
  2931. text "&& __invalid_label"
  2932. end
  2933. | StartOf(lv) -> self#pLval () lv
  2934. (* Print an expression, given the precedence of the context in which it
  2935. * appears. *)
  2936. method private pExpPrec (contextprec: int) () (e: exp) =
  2937. let thisLevel = getParenthLevel e in
  2938. let needParens =
  2939. if thisLevel >= contextprec then
  2940. true
  2941. else if contextprec == bitwiseLevel then
  2942. (* quiet down some GCC warnings *)
  2943. thisLevel == additiveLevel || thisLevel == comparativeLevel
  2944. else
  2945. false
  2946. in
  2947. if needParens then
  2948. chr '(' ++ self#pExp () e ++ chr ')'
  2949. else
  2950. self#pExp () e
  2951. method pInit () = function
  2952. SingleInit e -> self#pExp () e
  2953. | CompoundInit (t, initl) ->
  2954. (* We do not print the type of the Compound *)
  2955. (*
  2956. let dinit e = d_init () e in
  2957. dprintf "{@[%a@]}"
  2958. (docList ~sep:(chr ',' ++ break) dinit) initl
  2959. *)
  2960. let printDesignator =
  2961. if not !msvcMode then begin
  2962. (* Print only for union when we do not initialize the first field *)
  2963. match unrollType t, initl with
  2964. TComp(ci, _), [(Field(f, NoOffset), _)] ->
  2965. if not (ci.cstruct) && ci.cfields != [] &&
  2966. (List.hd ci.cfields) != f then
  2967. true
  2968. else
  2969. false
  2970. | _ -> false
  2971. end else
  2972. false
  2973. in
  2974. let d_oneInit = function
  2975. Field(f, NoOffset), i ->
  2976. (if printDesignator then
  2977. text ("." ^ f.fname ^ " = ")
  2978. else nil) ++ self#pInit () i
  2979. | Index(e, NoOffset), i ->
  2980. (if printDesignator then
  2981. text "[" ++ self#pExp () e ++ text "] = " else nil) ++
  2982. self#pInit () i
  2983. | _ -> E.s (unimp "Trying to print malformed initializer")
  2984. in
  2985. chr '{' ++ (align
  2986. ++ ((docList ~sep:(chr ',' ++ break) d_oneInit) () initl)
  2987. ++ unalign)
  2988. ++ chr '}'
  2989. (*
  2990. | ArrayInit (_, _, il) ->
  2991. chr '{' ++ (align
  2992. ++ ((docList (chr ',' ++ break) (self#pInit ())) () il)
  2993. ++ unalign)
  2994. ++ chr '}'
  2995. *)
  2996. (* dump initializers to a file. *)
  2997. method dInit (out: out_channel) (ind: int) (i: init) =
  2998. (* Dump an array *)
  2999. let dumpArray (bt: typ) (il: 'a list) (getelem: 'a -> init) =
  3000. let onALine = (* How many elements on a line *)
  3001. match unrollType bt with TComp _ | TArray _ -> 1 | _ -> 4
  3002. in
  3003. let rec outputElements (isfirst: bool) (room_on_line: int) = function
  3004. [] -> output_string out "}"
  3005. | (i: 'a) :: rest ->
  3006. if not isfirst then output_string out ", ";
  3007. let new_room_on_line =
  3008. if room_on_line == 0 then begin
  3009. output_string out "\n"; output_string out (String.make ind ' ');
  3010. onALine - 1
  3011. end else
  3012. room_on_line - 1
  3013. in
  3014. self#dInit out (ind + 2) (getelem i);
  3015. outputElements false new_room_on_line rest
  3016. in
  3017. output_string out "{ ";
  3018. outputElements true onALine il
  3019. in
  3020. match i with
  3021. SingleInit e ->
  3022. fprint out !lineLength (indent ind (self#pExp () e))
  3023. | CompoundInit (t, initl) -> begin
  3024. match unrollType t with
  3025. TArray(bt, _, _) ->
  3026. dumpArray bt initl (fun (_, i) -> i)
  3027. | _ ->
  3028. (* Now a structure or a union *)
  3029. fprint out !lineLength (indent ind (self#pInit () i))
  3030. end
  3031. (*
  3032. | ArrayInit (bt, len, initl) -> begin
  3033. (* If the base type does not contain structs then use the pInit
  3034. match unrollType bt with
  3035. TComp _ | TArray _ ->
  3036. dumpArray bt initl (fun x -> x)
  3037. | _ -> *)
  3038. fprint out !lineLength (indent ind (self#pInit () i))
  3039. end
  3040. *)
  3041. (** What terminator to print after an instruction. sometimes we want to
  3042. * print sequences of instructions separated by comma *)
  3043. val mutable printInstrTerminator = ";"
  3044. method private setPrintInstrTerminator (term : string) =
  3045. printInstrTerminator <- term
  3046. method private getPrintInstrTerminator () = printInstrTerminator
  3047. (*** INSTRUCTIONS ****)
  3048. method pInstr () (i:instr) = (* imperative instruction *)
  3049. match i with
  3050. | Set(lv,e,l) -> begin
  3051. (* Be nice to some special cases *)
  3052. match e with
  3053. BinOp((PlusA|PlusPI|IndexPI),Lval(lv'),Const(CInt64(one,_,_)),_)
  3054. when Util.equals lv lv' && one = Int64.one && not !printCilAsIs ->
  3055. self#pLineDirective l
  3056. ++ self#pLvalPrec indexLevel () lv
  3057. ++ text (" ++" ^ printInstrTerminator)
  3058. | BinOp((MinusA|MinusPI),Lval(lv'),
  3059. Const(CInt64(one,_,_)), _)
  3060. when Util.equals lv lv' && one = Int64.one && not !printCilAsIs ->
  3061. self#pLineDirective l
  3062. ++ self#pLvalPrec indexLevel () lv
  3063. ++ text (" --" ^ printInstrTerminator)
  3064. | BinOp((PlusA|PlusPI|IndexPI),Lval(lv'),Const(CInt64(mone,_,_)),_)
  3065. when Util.equals lv lv' && mone = Int64.minus_one
  3066. && not !printCilAsIs ->
  3067. self#pLineDirective l
  3068. ++ self#pLvalPrec indexLevel () lv
  3069. ++ text (" --" ^ printInstrTerminator)
  3070. | BinOp((PlusA|PlusPI|IndexPI|MinusA|MinusPP|MinusPI|BAnd|BOr|BXor|
  3071. Mult|Div|Mod|Shiftlt|Shiftrt) as bop,
  3072. Lval(lv'),e,_) when Util.equals lv lv'
  3073. && not !printCilAsIs ->
  3074. self#pLineDirective l
  3075. ++ self#pLval () lv
  3076. ++ text " " ++ d_binop () bop
  3077. ++ text "= "
  3078. ++ self#pExp () e
  3079. ++ text printInstrTerminator
  3080. | _ ->
  3081. self#pLineDirective l
  3082. ++ self#pLval () lv
  3083. ++ text " = "
  3084. ++ self#pExp () e
  3085. ++ text printInstrTerminator
  3086. end
  3087. (* In cabs2cil we have turned the call to builtin_va_arg into a
  3088. * three-argument call: the last argument is the address of the
  3089. * destination *)
  3090. | Call(None, Lval(Var vi, NoOffset), [dest; SizeOf t; adest], l)
  3091. when vi.vname = "__builtin_va_arg" && not !printCilAsIs ->
  3092. let destlv = match stripCasts adest with
  3093. AddrOf destlv -> destlv
  3094. (* If this fails, it's likely that an extension interfered
  3095. with the AddrOf *)
  3096. | _ -> E.s (E.bug
  3097. "%a: Encountered unexpected call to %s with dest %a\n"
  3098. d_loc l vi.vname self#pExp adest)
  3099. in
  3100. self#pLineDirective l
  3101. ++ self#pLval () destlv ++ text " = "
  3102. (* Now the function name *)
  3103. ++ text "__builtin_va_arg"
  3104. ++ text "(" ++ (align
  3105. (* Now the arguments *)
  3106. ++ self#pExp () dest
  3107. ++ chr ',' ++ break
  3108. ++ self#pType None () t
  3109. ++ unalign)
  3110. ++ text (")" ^ printInstrTerminator)
  3111. (* In cabs2cil we have dropped the last argument in the call to
  3112. * __builtin_va_start and __builtin_stdarg_start. *)
  3113. | Call(None, Lval(Var vi, NoOffset), [marker], l)
  3114. when ((vi.vname = "__builtin_stdarg_start" ||
  3115. vi.vname = "__builtin_va_start") && not !printCilAsIs) ->
  3116. if currentFormals <> [] then begin
  3117. let last = self#getLastNamedArgument vi.vname in
  3118. self#pInstr () (Call(None,Lval(Var vi,NoOffset),[marker; last],l))
  3119. end
  3120. else begin
  3121. (* We can't print this call because someone called pInstr outside
  3122. of a pFunDecl, so we don't know what the formals of the current
  3123. function are. Just put in a placeholder for now; this isn't
  3124. valid C. *)
  3125. self#pLineDirective l
  3126. ++ dprintf
  3127. "%s(%a, /* last named argument of the function calling %s */)"
  3128. vi.vname self#pExp marker vi.vname
  3129. ++ text printInstrTerminator
  3130. end
  3131. (* In cabs2cil we have dropped the last argument in the call to
  3132. * __builtin_next_arg. *)
  3133. | Call(res, Lval(Var vi, NoOffset), [ ], l)
  3134. when vi.vname = "__builtin_next_arg" && not !printCilAsIs -> begin
  3135. let last = self#getLastNamedArgument vi.vname in
  3136. self#pInstr () (Call(res,Lval(Var vi,NoOffset),[last],l))
  3137. end
  3138. (* In cparser we have turned the call to
  3139. * __builtin_types_compatible_p(t1, t2) into
  3140. * __builtin_types_compatible_p(sizeof t1, sizeof t2), so that we can
  3141. * represent the types as expressions.
  3142. * Remove the sizeofs when printing. *)
  3143. | Call(dest, Lval(Var vi, NoOffset), [SizeOf t1; SizeOf t2], l)
  3144. when vi.vname = "__builtin_types_compatible_p" && not !printCilAsIs ->
  3145. self#pLineDirective l
  3146. (* Print the destination *)
  3147. ++ (match dest with
  3148. None -> nil
  3149. | Some lv -> self#pLval () lv ++ text " = ")
  3150. (* Now the call itself *)
  3151. ++ dprintf "%s(%a, %a)" vi.vname
  3152. (self#pType None) t1 (self#pType None) t2
  3153. ++ text printInstrTerminator
  3154. | Call(_, Lval(Var vi, NoOffset), _, l)
  3155. when vi.vname = "__builtin_types_compatible_p" && not !printCilAsIs ->
  3156. E.s (bug "__builtin_types_compatible_p: cabs2cil should have added sizeof to the arguments.")
  3157. | Call(dest,e,args,l) ->
  3158. self#pLineDirective l
  3159. ++ (match dest with
  3160. None -> nil
  3161. | Some lv ->
  3162. self#pLval () lv ++ text " = " ++
  3163. (* Maybe we need to print a cast *)
  3164. (let destt = typeOfLval lv in
  3165. match unrollType (typeOf e) with
  3166. TFun (rt, _, _, _)
  3167. when not (Util.equals (!pTypeSig rt)
  3168. (!pTypeSig destt)) ->
  3169. text "(" ++ self#pType None () destt ++ text ")"
  3170. | _ -> nil))
  3171. (* Now the function name *)
  3172. ++ (let ed = self#pExp () e in
  3173. match e with
  3174. Lval(Var _, _) -> ed
  3175. | _ -> text "(" ++ ed ++ text ")")
  3176. ++ text "(" ++
  3177. (align
  3178. (* Now the arguments *)
  3179. ++ (docList ~sep:(chr ',' ++ break)
  3180. (self#pExp ()) () args)
  3181. ++ unalign)
  3182. ++ text (")" ^ printInstrTerminator)
  3183. | Asm(attrs, tmpls, outs, ins, clobs, l) ->
  3184. if !msvcMode then
  3185. self#pLineDirective l
  3186. ++ text "__asm {"
  3187. ++ (align
  3188. ++ (docList ~sep:line text () tmpls)
  3189. ++ unalign)
  3190. ++ text ("}" ^ printInstrTerminator)
  3191. else
  3192. self#pLineDirective l
  3193. ++ text ("__asm__ ")
  3194. ++ self#pAttrs () attrs
  3195. ++ text " ("
  3196. ++ (align
  3197. ++ (docList ~sep:line
  3198. (fun x -> text ("\"" ^ escape_string x ^ "\""))
  3199. () tmpls)
  3200. ++
  3201. (if outs = [] && ins = [] && clobs = [] then
  3202. chr ':'
  3203. else
  3204. (text ": "
  3205. ++ (docList ~sep:(chr ',' ++ break)
  3206. (fun (idopt, c, lv) ->
  3207. text(match idopt with
  3208. None -> ""
  3209. | Some id -> "[" ^ id ^ "] "
  3210. ) ++
  3211. text ("\"" ^ escape_string c ^ "\" (")
  3212. ++ self#pLval () lv
  3213. ++ text ")") () outs)))
  3214. ++
  3215. (if ins = [] && clobs = [] then
  3216. nil
  3217. else
  3218. (text ": "
  3219. ++ (docList ~sep:(chr ',' ++ break)
  3220. (fun (idopt, c, e) ->
  3221. text(match idopt with
  3222. None -> ""
  3223. | Some id -> "[" ^ id ^ "] "
  3224. ) ++
  3225. text ("\"" ^ escape_string c ^ "\" (")
  3226. ++ self#pExp () e
  3227. ++ text ")") () ins)))
  3228. ++
  3229. (if clobs = [] then nil
  3230. else
  3231. (text ": "
  3232. ++ (docList ~sep:(chr ',' ++ break)
  3233. (fun c -> text ("\"" ^ escape_string c ^ "\""))
  3234. ()
  3235. clobs)))
  3236. ++ unalign)
  3237. ++ text (")" ^ printInstrTerminator)
  3238. (**** STATEMENTS ****)
  3239. method pStmt () (s:stmt) = (* control-flow statement *)
  3240. self#pStmtNext invalidStmt () s
  3241. method dStmt (out: out_channel) (ind: int) (s:stmt) : unit =
  3242. fprint out !lineLength (indent ind (self#pStmt () s))
  3243. method dBlock (out: out_channel) (ind: int) (b:block) : unit =
  3244. fprint out !lineLength (indent ind (align ++ self#pBlock () b))
  3245. method private pStmtNext (next: stmt) () (s: stmt) =
  3246. (* print the labels *)
  3247. ((docList ~sep:line (fun l -> self#pLabel () l)) () s.labels)
  3248. (* print the statement itself. If the labels are non-empty and the
  3249. * statement is empty, print a semicolon *)
  3250. ++
  3251. (if s.skind = Instr [] && s.labels <> [] then
  3252. text ";"
  3253. else
  3254. (if s.labels <> [] then line else nil)
  3255. ++ self#pStmtKind next () s.skind)
  3256. method private pLabel () = function
  3257. Label (s, _, true) -> text (s ^ ": ")
  3258. | Label (s, _, false) -> text (s ^ ": /* CIL Label */ ")
  3259. | Case (e, _) -> text "case " ++ self#pExp () e ++ text ": "
  3260. | CaseRange (e1, e2, _) -> text "case " ++ self#pExp () e1 ++ text " ... "
  3261. ++ self#pExp () e2 ++ text ": "
  3262. | Default _ -> text "default: "
  3263. (* The pBlock will put the unalign itself *)
  3264. method pBlock () (blk: block) =
  3265. let rec dofirst () = function
  3266. [] -> nil
  3267. | [x] -> self#pStmtNext invalidStmt () x
  3268. | x :: rest -> dorest nil x rest
  3269. and dorest acc prev = function
  3270. [] -> acc ++ (self#pStmtNext invalidStmt () prev)
  3271. | x :: rest ->
  3272. dorest (acc ++ (self#pStmtNext x () prev) ++ line)
  3273. x rest
  3274. in
  3275. (* Let the host of the block decide on the alignment. The d_block will
  3276. * pop the alignment as well *)
  3277. text "{"
  3278. ++
  3279. (if blk.battrs <> [] then
  3280. self#pAttrsGen true blk.battrs
  3281. else nil)
  3282. ++ line
  3283. ++ (dofirst () blk.bstmts)
  3284. ++ unalign ++ line ++ text "}"
  3285. (* Store here the name of the last file printed in a line number. This is
  3286. * private to the object *)
  3287. val mutable lastFileName = ""
  3288. val mutable lastLineNumber = -1
  3289. (* Make sure that you only call self#pLineDirective on an empty line *)
  3290. method pLineDirective ?(forcefile=false) l =
  3291. currentLoc := l;
  3292. match !lineDirectiveStyle with
  3293. | None -> nil
  3294. | Some _ when l.line <= 0 -> nil
  3295. (* Do not print lineComment if the same line as above *)
  3296. | Some LineCommentSparse when l.line = lastLineNumber -> nil
  3297. | Some style ->
  3298. let directive =
  3299. match style with
  3300. | LineComment | LineCommentSparse -> text "//#line "
  3301. | LinePreprocessorOutput when not !msvcMode -> chr '#'
  3302. | LinePreprocessorOutput | LinePreprocessorInput -> text "#line"
  3303. in
  3304. lastLineNumber <- l.line;
  3305. let filename =
  3306. if forcefile || l.file <> lastFileName then
  3307. begin
  3308. lastFileName <- l.file;
  3309. text " \"" ++ text l.file ++ text "\""
  3310. end
  3311. else
  3312. nil
  3313. in
  3314. leftflush ++ directive ++ chr ' ' ++ num l.line ++ filename ++ line
  3315. method private pIfConditionThen loc condition thenBlock =
  3316. self#pLineDirective loc
  3317. ++ text "if"
  3318. ++ (align
  3319. ++ text " ("
  3320. ++ self#pExp () condition
  3321. ++ text ") "
  3322. ++ self#pBlock () thenBlock)
  3323. method private pStmtKind (next: stmt) () = function
  3324. Return(None, l) ->
  3325. self#pLineDirective l
  3326. ++ text "return;"
  3327. | Return(Some e, l) ->
  3328. self#pLineDirective l
  3329. ++ text "return ("
  3330. ++ self#pExp () e
  3331. ++ text ");"
  3332. | Goto (sref, l) -> begin
  3333. (* Grab one of the labels *)
  3334. let rec pickLabel = function
  3335. [] -> None
  3336. | Label (l, _, _) :: _ -> Some l
  3337. | _ :: rest -> pickLabel rest
  3338. in
  3339. match pickLabel !sref.labels with
  3340. Some lbl -> self#pLineDirective l ++ text ("goto " ^ lbl ^ ";")
  3341. | None ->
  3342. ignore (error "Cannot find label for target of goto");
  3343. text "goto __invalid_label;"
  3344. end
  3345. | ComputedGoto(e, l) ->
  3346. self#pLineDirective l
  3347. ++ text "goto *("
  3348. ++ self#pExp () e
  3349. ++ text ");"
  3350. | Break l ->
  3351. self#pLineDirective l
  3352. ++ text "break;"
  3353. | Continue l ->
  3354. self#pLineDirective l
  3355. ++ text "continue;"
  3356. | Instr il ->
  3357. align
  3358. ++ (docList ~sep:line (fun i -> self#pInstr () i) () il)
  3359. ++ unalign
  3360. | If(be,t,{bstmts=[];battrs=[]},l) when not !printCilAsIs ->
  3361. self#pIfConditionThen l be t
  3362. | If(be,t,{bstmts=[{skind=Goto(gref,_);labels=[]}];
  3363. battrs=[]},l)
  3364. when !gref == next && not !printCilAsIs ->
  3365. self#pIfConditionThen l be t
  3366. | If(be,{bstmts=[];battrs=[]},e,l) when not !printCilAsIs ->
  3367. self#pIfConditionThen l (UnOp(LNot,be,intType)) e
  3368. | If(be,{bstmts=[{skind=Goto(gref,_);labels=[]}];
  3369. battrs=[]},e,l)
  3370. when !gref == next && not !printCilAsIs ->
  3371. self#pIfConditionThen l (UnOp(LNot,be,intType)) e
  3372. | If(be,t,e,l) ->
  3373. self#pIfConditionThen l be t
  3374. ++ (match e with
  3375. { bstmts=[{skind=If _} as elsif]; battrs=[] } ->
  3376. text " else"
  3377. ++ line (* Don't indent else-ifs *)
  3378. ++ self#pStmtNext next () elsif
  3379. | _ ->
  3380. text " " (* sm: indent next code 2 spaces (was 4) *)
  3381. ++ align
  3382. ++ text "else "
  3383. ++ self#pBlock () e)
  3384. | Switch(e,b,_,l) ->
  3385. self#pLineDirective l
  3386. ++ (align
  3387. ++ text "switch ("
  3388. ++ self#pExp () e
  3389. ++ text ") "
  3390. ++ self#pBlock () b)
  3391. | Loop(b, l, _, _) -> begin
  3392. (* Maybe the first thing is a conditional. Turn it into a WHILE *)
  3393. try
  3394. let term, bodystmts =
  3395. let rec skipEmpty = function
  3396. [] -> []
  3397. | {skind=Instr [];labels=[]} :: rest -> skipEmpty rest
  3398. | x -> x
  3399. in
  3400. (* Bill McCloskey: Do not remove the If if it has labels *)
  3401. match skipEmpty b.bstmts with
  3402. {skind=If(e,tb,fb,_); labels=[]} :: rest
  3403. when not !printCilAsIs -> begin
  3404. match skipEmpty tb.bstmts, skipEmpty fb.bstmts with
  3405. [], {skind=Break _; labels=[]} :: _ -> e, rest
  3406. | {skind=Break _; labels=[]} :: _, []
  3407. -> UnOp(LNot, e, intType), rest
  3408. | _ -> raise Not_found
  3409. end
  3410. | _ -> raise Not_found
  3411. in
  3412. self#pLineDirective l
  3413. ++ text "wh"
  3414. ++ (align
  3415. ++ text "ile ("
  3416. ++ self#pExp () term
  3417. ++ text ") "
  3418. ++ self#pBlock () {bstmts=bodystmts; battrs=b.battrs})
  3419. with Not_found ->
  3420. self#pLineDirective l
  3421. ++ text "wh"
  3422. ++ (align
  3423. ++ text "ile (1) "
  3424. ++ self#pBlock () b)
  3425. end
  3426. | Block b -> align ++ self#pBlock () b
  3427. | TryFinally (b, h, l) ->
  3428. self#pLineDirective l
  3429. ++ text "__try "
  3430. ++ align
  3431. ++ self#pBlock () b
  3432. ++ text " __fin" ++ align ++ text "ally "
  3433. ++ self#pBlock () h
  3434. | TryExcept (b, (il, e), h, l) ->
  3435. self#pLineDirective l
  3436. ++ text "__try "
  3437. ++ align
  3438. ++ self#pBlock () b
  3439. ++ text " __e" ++ align ++ text "xcept(" ++ line
  3440. ++ align
  3441. (* Print the instructions but with a comma at the end, instead of
  3442. * semicolon *)
  3443. ++ (printInstrTerminator <- ",";
  3444. let res =
  3445. (docList ~sep:line (self#pInstr ())
  3446. () il)
  3447. in
  3448. printInstrTerminator <- ";";
  3449. res)
  3450. ++ self#pExp () e
  3451. ++ text ") " ++ unalign
  3452. ++ self#pBlock () h
  3453. (*** GLOBALS ***)
  3454. method pGlobal () (g:global) : doc = (* global (vars, types, etc.) *)
  3455. match g with
  3456. | GFun (fundec, l) ->
  3457. (* If the function has attributes then print a prototype because
  3458. * GCC cannot accept function attributes in a definition *)
  3459. let oldattr = fundec.svar.vattr in
  3460. (* Always pring the file name before function declarations *)
  3461. let proto =
  3462. if oldattr <> [] then
  3463. (self#pLineDirective l) ++ (self#pVDecl () fundec.svar)
  3464. ++ chr ';' ++ line
  3465. else nil in
  3466. (* Temporarily remove the function attributes *)
  3467. fundec.svar.vattr <- [];
  3468. let body = (self#pLineDirective ~forcefile:true l)
  3469. ++ (self#pFunDecl () fundec) in
  3470. fundec.svar.vattr <- oldattr;
  3471. proto ++ body ++ line
  3472. | GType (typ, l) ->
  3473. self#pLineDirective ~forcefile:true l ++
  3474. text "typedef "
  3475. ++ (self#pType (Some (text typ.tname)) () typ.ttype)
  3476. ++ text ";\n"
  3477. | GEnumTag (enum, l) ->
  3478. self#pLineDirective l ++
  3479. text "enum" ++ align ++ text (" " ^ enum.ename) ++
  3480. text " {" ++ line
  3481. ++ (docList ~sep:(chr ',' ++ line)
  3482. (fun (n,i, loc) ->
  3483. text (n ^ " = ")
  3484. ++ self#pExp () i)
  3485. () enum.eitems)
  3486. ++ unalign ++ line ++ text "} "
  3487. ++ self#pAttrs () enum.eattr ++ text";\n"
  3488. | GEnumTagDecl (enum, l) -> (* This is a declaration of a tag *)
  3489. self#pLineDirective l ++
  3490. text "enum " ++ text enum.ename ++ chr ' '
  3491. ++ self#pAttrs () enum.eattr ++ text ";\n"
  3492. | GCompTag (comp, l) -> (* This is a definition of a tag *)
  3493. let n = comp.cname in
  3494. let su, su1, su2 =
  3495. if comp.cstruct then "struct", "str", "uct"
  3496. else "union", "uni", "on"
  3497. in
  3498. let sto_mod, rest_attr = separateStorageModifiers comp.cattr in
  3499. self#pLineDirective ~forcefile:true l ++
  3500. text su1 ++ (align ++ text su2 ++ chr ' ' ++ (self#pAttrs () sto_mod)
  3501. ++ text n
  3502. ++ text " {" ++ line
  3503. ++ ((docList ~sep:line (self#pFieldDecl ())) ()
  3504. comp.cfields)
  3505. ++ unalign)
  3506. ++ line ++ text "}" ++
  3507. (self#pAttrs () rest_attr) ++ text ";\n"
  3508. | GCompTagDecl (comp, l) -> (* This is a declaration of a tag *)
  3509. let su = if comp.cstruct then "struct " else "union " in
  3510. let sto_mod, rest_attr = separateStorageModifiers comp.cattr in
  3511. self#pLineDirective l
  3512. ++ text su ++ self#pAttrs () sto_mod
  3513. ++ text comp.cname ++ chr ' '
  3514. ++ self#pAttrs () rest_attr ++ text ";\n"
  3515. | GVar (vi, io, l) ->
  3516. self#pLineDirective ~forcefile:true l ++
  3517. self#pVDecl () vi
  3518. ++ chr ' '
  3519. ++ (match io.init with
  3520. None -> nil
  3521. | Some i -> text " = " ++
  3522. (let islong =
  3523. match i with
  3524. CompoundInit (_, il) when List.length il >= 8 -> true
  3525. | _ -> false
  3526. in
  3527. if islong then
  3528. line ++ self#pLineDirective l ++ text " "
  3529. else nil) ++
  3530. (self#pInit () i))
  3531. ++ text ";\n"
  3532. (* print global variable 'extern' declarations, and function prototypes *)
  3533. | GVarDecl (vi, l) ->
  3534. if not !printCilAsIs && H.mem builtinFunctions vi.vname then begin
  3535. (* Compiler builtins need no prototypes. Just print them in
  3536. comments. *)
  3537. text "/* compiler builtin: \n " ++
  3538. (self#pVDecl () vi)
  3539. ++ text "; */\n"
  3540. end else
  3541. self#pLineDirective l ++
  3542. (self#pVDecl () vi)
  3543. ++ text ";\n"
  3544. | GAsm (s, l) ->
  3545. self#pLineDirective l ++
  3546. text ("__asm__(\"" ^ escape_string s ^ "\");\n")
  3547. | GPragma (Attr(an, args), l) ->
  3548. (* sm: suppress printing pragmas that gcc does not understand *)
  3549. (* assume anything starting with "ccured" is ours *)
  3550. (* also don't print the 'combiner' pragma *)
  3551. (* nor 'cilnoremove' *)
  3552. let suppress =
  3553. not !print_CIL_Input &&
  3554. not !msvcMode &&
  3555. ((startsWith "box" an) ||
  3556. (startsWith "ccured" an) ||
  3557. (an = "merger") ||
  3558. (an = "cilnoremove")) in
  3559. let d =
  3560. match an, args with
  3561. | _, [] ->
  3562. text an
  3563. | "weak", [ACons (symbol, [])] ->
  3564. text "weak " ++ text symbol
  3565. | _ ->
  3566. text (an ^ "(")
  3567. ++ docList ~sep:(chr ',') (self#pAttrParam ()) () args
  3568. ++ text ")"
  3569. in
  3570. self#pLineDirective l
  3571. ++ (if suppress then text "/* " else text "")
  3572. ++ (text "#pragma ")
  3573. ++ d
  3574. ++ (if suppress then text " */\n" else text "\n")
  3575. | GText s ->
  3576. if s <> "//" then
  3577. text s ++ text "\n"
  3578. else
  3579. nil
  3580. method dGlobal (out: out_channel) (g: global) : unit =
  3581. (* For all except functions and variable with initializers, use the
  3582. * pGlobal *)
  3583. match g with
  3584. GFun (fdec, l) ->
  3585. (* If the function has attributes then print a prototype because
  3586. * GCC cannot accept function attributes in a definition *)
  3587. let oldattr = fdec.svar.vattr in
  3588. let proto =
  3589. if oldattr <> [] then
  3590. (self#pLineDirective l) ++ (self#pVDecl () fdec.svar)
  3591. ++ chr ';' ++ line
  3592. else nil in
  3593. fprint out !lineLength
  3594. (proto ++ (self#pLineDirective ~forcefile:true l));
  3595. (* Temporarily remove the function attributes *)
  3596. fdec.svar.vattr <- [];
  3597. fprint out !lineLength (self#pFunDecl () fdec);
  3598. fdec.svar.vattr <- oldattr;
  3599. output_string out "\n"
  3600. | GVar (vi, {init = Some i}, l) -> begin
  3601. fprint out !lineLength
  3602. (self#pLineDirective ~forcefile:true l ++
  3603. self#pVDecl () vi
  3604. ++ text " = "
  3605. ++ (let islong =
  3606. match i with
  3607. CompoundInit (_, il) when List.length il >= 8 -> true
  3608. | _ -> false
  3609. in
  3610. if islong then
  3611. line ++ self#pLineDirective l ++ text " "
  3612. else nil));
  3613. self#dInit out 3 i;
  3614. output_string out ";\n"
  3615. end
  3616. | g -> fprint out !lineLength (self#pGlobal () g)
  3617. method pFieldDecl () fi =
  3618. (self#pType
  3619. (Some (text (if fi.fname = missingFieldName then "" else fi.fname)))
  3620. ()
  3621. fi.ftype)
  3622. ++ text " "
  3623. ++ (match fi.fbitfield with None -> nil
  3624. | Some i -> text ": " ++ num i ++ text " ")
  3625. ++ self#pAttrs () fi.fattr
  3626. ++ text ";"
  3627. method private pFunDecl () f =
  3628. self#pVDecl () f.svar
  3629. ++ line
  3630. ++ text "{ "
  3631. ++ (align
  3632. (* locals. *)
  3633. ++ line
  3634. ++ (docList ~sep:line
  3635. (fun vi -> match vi.vinit.init with
  3636. | None -> self#pVDecl () vi ++ text ";"
  3637. | Some i -> self#pVDecl () vi ++ text " = " ++
  3638. self#pInit () i ++ text ";")
  3639. () f.slocals)
  3640. ++ line ++ line
  3641. (* the body *)
  3642. ++ ((* remember the declaration *) currentFormals <- f.sformals;
  3643. let body = self#pBlock () f.sbody in
  3644. currentFormals <- [];
  3645. body))
  3646. ++ line
  3647. ++ text "}"
  3648. (***** PRINTING DECLARATIONS and TYPES ****)
  3649. method pType (nameOpt: doc option) (* Whether we are declaring a name or
  3650. * we are just printing a type *)
  3651. () (t:typ) = (* use of some type *)
  3652. let name = match nameOpt with None -> nil | Some d -> d in
  3653. let printAttributes (a: attributes) =
  3654. let pa = self#pAttrs () a in
  3655. match nameOpt with
  3656. | None when not !print_CIL_Input && not !msvcMode ->
  3657. (* Cannot print the attributes in this case because gcc does not
  3658. * like them here, except if we are printing for CIL, or for MSVC.
  3659. * In fact, for MSVC we MUST print attributes such as __stdcall *)
  3660. if pa = nil then nil else
  3661. text "/*" ++ pa ++ text "*/"
  3662. | _ -> pa
  3663. in
  3664. match t with
  3665. TVoid a ->
  3666. text "void"
  3667. ++ self#pAttrs () a
  3668. ++ text " "
  3669. ++ name
  3670. | TInt (ikind,a) ->
  3671. d_ikind () ikind
  3672. ++ self#pAttrs () a
  3673. ++ text " "
  3674. ++ name
  3675. | TFloat(fkind, a) ->
  3676. d_fkind () fkind
  3677. ++ self#pAttrs () a
  3678. ++ text " "
  3679. ++ name
  3680. | TComp (comp, a) -> (* A reference to a struct *)
  3681. let su = if comp.cstruct then "struct" else "union" in
  3682. text (su ^ " " ^ comp.cname ^ " ")
  3683. ++ self#pAttrs () a
  3684. ++ name
  3685. | TEnum (enum, a) ->
  3686. text ("enum " ^ enum.ename ^ " ")
  3687. ++ self#pAttrs () a
  3688. ++ name
  3689. | TPtr (bt, a) ->
  3690. (* Parenthesize the ( * attr name) if a pointer to a function or an
  3691. * array. However, on MSVC the __stdcall modifier must appear right
  3692. * before the pointer constructor "(__stdcall *f)". We push them into
  3693. * the parenthesis. *)
  3694. let (paren: doc option), (bt': typ) =
  3695. match bt with
  3696. TFun(rt, args, isva, fa) when !msvcMode ->
  3697. let an, af', at = partitionAttributes ~default:AttrType fa in
  3698. (* We take the af' and we put them into the parentheses *)
  3699. Some (text "(" ++ printAttributes af'),
  3700. TFun(rt, args, isva, addAttributes an at)
  3701. | TFun _ | TArray _ -> Some (text "("), bt
  3702. | _ -> None, bt
  3703. in
  3704. let name' = text "*" ++ printAttributes a ++ name in
  3705. let name'' = (* Put the parenthesis *)
  3706. match paren with
  3707. Some p -> p ++ name' ++ text ")"
  3708. | _ -> name'
  3709. in
  3710. self#pType
  3711. (Some name'')
  3712. ()
  3713. bt'
  3714. | TArray (elemt, lo, a) ->
  3715. (* ignore the const attribute for arrays *)
  3716. let a' = dropAttributes [ "const" ] a in
  3717. let name' =
  3718. if a' == [] then name else
  3719. if nameOpt == None then printAttributes a' else
  3720. text "(" ++ printAttributes a' ++ name ++ text ")"
  3721. in
  3722. self#pType
  3723. (Some (name'
  3724. ++ text "["
  3725. ++ (match lo with None -> nil | Some e -> self#pExp () e)
  3726. ++ text "]"))
  3727. ()
  3728. elemt
  3729. | TFun (restyp, args, isvararg, a) ->
  3730. let name' =
  3731. if a == [] then name else
  3732. if nameOpt == None then printAttributes a else
  3733. text "(" ++ printAttributes a ++ name ++ text ")"
  3734. in
  3735. self#pType
  3736. (Some
  3737. (name'
  3738. ++ text "("
  3739. ++ (align
  3740. ++
  3741. (if args = Some [] && isvararg then
  3742. text "..."
  3743. else
  3744. (if args = None then nil
  3745. else if args = Some [] then text "void"
  3746. else
  3747. let pArg (aname, atype, aattr) =
  3748. let stom, rest = separateStorageModifiers aattr in
  3749. (* First the storage modifiers *)
  3750. (self#pAttrs () stom)
  3751. ++ (self#pType (Some (text aname)) () atype)
  3752. ++ text " "
  3753. ++ self#pAttrs () rest
  3754. in
  3755. (docList ~sep:(chr ',' ++ break) pArg) ()
  3756. (argsToList args))
  3757. ++ (if isvararg then break ++ text ", ..." else nil))
  3758. ++ unalign)
  3759. ++ text ")"))
  3760. ()
  3761. restyp
  3762. | TNamed (t, a) ->
  3763. text t.tname ++ self#pAttrs () a ++ text " " ++ name
  3764. | TBuiltin_va_list a ->
  3765. text "__builtin_va_list"
  3766. ++ self#pAttrs () a
  3767. ++ text " "
  3768. ++ name
  3769. (**** PRINTING ATTRIBUTES *********)
  3770. method pAttrs () (a: attributes) =
  3771. self#pAttrsGen false a
  3772. (* Print one attribute. Return also an indication whether this attribute
  3773. * should be printed inside the __attribute__ list *)
  3774. method pAttr (Attr(an, args): attribute) : doc * bool =
  3775. (* Recognize and take care of some known cases *)
  3776. match an, args with
  3777. "const", [] -> text "const", false
  3778. (* Put the aconst inside the attribute list *)
  3779. | "aconst", [] when not !msvcMode -> text "__const__", true
  3780. | "thread", [] when not !msvcMode -> text "__thread", false
  3781. (*
  3782. | "used", [] when not !msvcMode -> text "__attribute_used__", false
  3783. *)
  3784. | "volatile", [] -> text "volatile", false
  3785. | "restrict", [] -> text "__restrict", false
  3786. | "missingproto", [] -> text "/* missing proto */", false
  3787. | "cdecl", [] when !msvcMode -> text "__cdecl", false
  3788. | "stdcall", [] when !msvcMode -> text "__stdcall", false
  3789. | "fastcall", [] when !msvcMode -> text "__fastcall", false
  3790. | "declspec", args when !msvcMode ->
  3791. text "__declspec("
  3792. ++ docList (self#pAttrParam ()) () args
  3793. ++ text ")", false
  3794. | "w64", [] when !msvcMode -> text "__w64", false
  3795. | "asm", args ->
  3796. text "__asm__("
  3797. ++ docList (self#pAttrParam ()) () args
  3798. ++ text ")", false
  3799. (* we suppress printing mode(__si__) because it triggers an *)
  3800. (* internal compiler error in all current gcc versions *)
  3801. (* sm: I've now encountered a problem with mode(__hi__)... *)
  3802. (* I don't know what's going on, but let's try disabling all "mode"..*)
  3803. | "mode", [ACons(tag,[])] ->
  3804. text "/* mode(" ++ text tag ++ text ") */", false
  3805. (* sm: also suppress "format" because we seem to print it in *)
  3806. (* a way gcc does not like *)
  3807. | "format", _ -> text "/* format attribute */", false
  3808. (* sm: here's another one I don't want to see gcc warnings about.. *)
  3809. | "mayPointToStack", _ when not !print_CIL_Input
  3810. (* [matth: may be inside another comment.]
  3811. -> text "/*mayPointToStack*/", false
  3812. *)
  3813. -> text "", false
  3814. | "arraylen", [a] ->
  3815. (* text "/*[" ++ self#pAttrParam () a ++ text "]*/" *) nil, false
  3816. | _ -> (* This is the dafault case *)
  3817. (* Add underscores to the name *)
  3818. let an' = if !msvcMode then "__" ^ an else "__" ^ an ^ "__" in
  3819. if args = [] then
  3820. text an', true
  3821. else
  3822. text (an' ^ "(")
  3823. ++ (docList (self#pAttrParam ()) () args)
  3824. ++ text ")",
  3825. true
  3826. method private pAttrPrec (contextprec: int) () (a: attrparam) =
  3827. let thisLevel = getParenthLevelAttrParam a in
  3828. let needParens =
  3829. if thisLevel >= contextprec then
  3830. true
  3831. else if contextprec == bitwiseLevel then
  3832. (* quiet down some GCC warnings *)
  3833. thisLevel == additiveLevel || thisLevel == comparativeLevel
  3834. else
  3835. false
  3836. in
  3837. if needParens then
  3838. chr '(' ++ self#pAttrParam () a ++ chr ')'
  3839. else
  3840. self#pAttrParam () a
  3841. method pAttrParam () a =
  3842. let level = getParenthLevelAttrParam a in
  3843. match a with
  3844. | AInt n -> num n
  3845. | AStr s -> text ("\"" ^ escape_string s ^ "\"")
  3846. | ACons(s, []) -> text s
  3847. | ACons(s,al) ->
  3848. text (s ^ "(")
  3849. ++ (docList (self#pAttrParam ()) () al)
  3850. ++ text ")"
  3851. | ASizeOfE a -> text "sizeof(" ++ self#pAttrParam () a ++ text ")"
  3852. | ASizeOf t -> text "sizeof(" ++ self#pType None () t ++ text ")"
  3853. | ASizeOfS ts -> text "sizeof(<typsig>)"
  3854. | AAlignOfE a -> text "__alignof__(" ++ self#pAttrParam () a ++ text ")"
  3855. | AAlignOf t -> text "__alignof__(" ++ self#pType None () t ++ text ")"
  3856. | AAlignOfS ts -> text "__alignof__(<typsig>)"
  3857. | AUnOp(u,a1) ->
  3858. (d_unop () u) ++ chr ' ' ++ (self#pAttrPrec level () a1)
  3859. | ABinOp(b,a1,a2) ->
  3860. align
  3861. ++ text "("
  3862. ++ (self#pAttrPrec level () a1)
  3863. ++ text ") "
  3864. ++ (d_binop () b)
  3865. ++ break
  3866. ++ text " (" ++ (self#pAttrPrec level () a2) ++ text ") "
  3867. ++ unalign
  3868. | ADot (ap, s) -> (self#pAttrParam () ap) ++ text ("." ^ s)
  3869. | AStar a1 ->
  3870. text "(*" ++ (self#pAttrPrec derefStarLevel () a1) ++ text ")"
  3871. | AAddrOf a1 -> text "& " ++ (self#pAttrPrec addrOfLevel () a1)
  3872. | AIndex (a1, a2) -> self#pAttrParam () a1 ++ text "[" ++
  3873. self#pAttrParam () a2 ++ text "]"
  3874. | AQuestion (a1, a2, a3) ->
  3875. self#pAttrParam () a1 ++ text " ? " ++
  3876. self#pAttrParam () a2 ++ text " : " ++
  3877. self#pAttrParam () a3
  3878. (* A general way of printing lists of attributes *)
  3879. method private pAttrsGen (block: bool) (a: attributes) =
  3880. (* Scan all the attributes and separate those that must be printed inside
  3881. * the __attribute__ list *)
  3882. let rec loop (in__attr__: doc list) = function
  3883. [] -> begin
  3884. match in__attr__ with
  3885. [] -> nil
  3886. | _ :: _->
  3887. (* sm: added 'forgcc' calls to not comment things out
  3888. * if CIL is the consumer; this is to address a case
  3889. * Daniel ran into where blockattribute(nobox) was being
  3890. * dropped by the merger
  3891. *)
  3892. (if block then
  3893. text (" " ^ (forgcc "/*") ^ " __blockattribute__(")
  3894. else
  3895. text "__attribute__((")
  3896. ++ (docList ~sep:(chr ',' ++ break)
  3897. (fun a -> a)) () in__attr__
  3898. ++ text ")"
  3899. ++ (if block then text (forgcc "*/") else text ")")
  3900. end
  3901. | x :: rest ->
  3902. let dx, ina = self#pAttr x in
  3903. if ina then
  3904. loop (dx :: in__attr__) rest
  3905. else if dx = nil then
  3906. loop in__attr__ rest
  3907. else
  3908. dx ++ text " " ++ loop in__attr__ rest
  3909. in
  3910. let res = loop [] a in
  3911. if res = nil then
  3912. res
  3913. else
  3914. text " " ++ res ++ text " "
  3915. end (* class defaultCilPrinterClass *)
  3916. let defaultCilPrinter = new defaultCilPrinterClass
  3917. (* Top-level printing functions *)
  3918. let printType (pp: cilPrinter) () (t: typ) : doc =
  3919. pp#pType None () t
  3920. let printExp (pp: cilPrinter) () (e: exp) : doc =
  3921. pp#pExp () e
  3922. let printLval (pp: cilPrinter) () (lv: lval) : doc =
  3923. pp#pLval () lv
  3924. let printGlobal (pp: cilPrinter) () (g: global) : doc =
  3925. pp#pGlobal () g
  3926. let dumpGlobal (pp: cilPrinter) (out: out_channel) (g: global) : unit =
  3927. pp#dGlobal out g
  3928. let printAttr (pp: cilPrinter) () (a: attribute) : doc =
  3929. let ad, _ = pp#pAttr a in ad
  3930. let printAttrs (pp: cilPrinter) () (a: attributes) : doc =
  3931. pp#pAttrs () a
  3932. let printInstr (pp: cilPrinter) () (i: instr) : doc =
  3933. pp#pInstr () i
  3934. let printStmt (pp: cilPrinter) () (s: stmt) : doc =
  3935. pp#pStmt () s
  3936. let printBlock (pp: cilPrinter) () (b: block) : doc =
  3937. (* We must add the alignment ourselves, beucase pBlock will pop it *)
  3938. align ++ pp#pBlock () b
  3939. let dumpStmt (pp: cilPrinter) (out: out_channel) (ind: int) (s: stmt) : unit =
  3940. pp#dStmt out ind s
  3941. let dumpBlock (pp: cilPrinter) (out: out_channel) (ind: int) (b: block) : unit =
  3942. pp#dBlock out ind b
  3943. let printInit (pp: cilPrinter) () (i: init) : doc =
  3944. pp#pInit () i
  3945. let dumpInit (pp: cilPrinter) (out: out_channel) (ind: int) (i: init) : unit =
  3946. pp#dInit out ind i
  3947. (* Now define some short cuts *)
  3948. let d_exp () e = printExp defaultCilPrinter () e
  3949. let _ = pd_exp := d_exp
  3950. let d_lval () lv = printLval defaultCilPrinter () lv
  3951. let d_offset base () off = defaultCilPrinter#pOffset base off
  3952. let d_init () i = printInit defaultCilPrinter () i
  3953. let d_type () t = printType defaultCilPrinter () t
  3954. let _ = pd_type := d_type
  3955. let d_global () g = printGlobal defaultCilPrinter () g
  3956. let d_attrlist () a = printAttrs defaultCilPrinter () a
  3957. let d_attr () a = printAttr defaultCilPrinter () a
  3958. let _ = pd_attr := d_attr
  3959. let d_attrparam () e = defaultCilPrinter#pAttrParam () e
  3960. let d_label () l = defaultCilPrinter#pLabel () l
  3961. let d_stmt () s = printStmt defaultCilPrinter () s
  3962. let d_block () b = printBlock defaultCilPrinter () b
  3963. let d_instr () i = printInstr defaultCilPrinter () i
  3964. let d_shortglobal () = function
  3965. GPragma (Attr(an, _), _) -> dprintf "#pragma %s" an
  3966. | GType (ti, _) -> dprintf "typedef %s" ti.tname
  3967. | GVarDecl (vi, _) -> dprintf "declaration of %s" vi.vname
  3968. | GVar (vi, _, _) -> dprintf "definition of %s" vi.vname
  3969. | GCompTag(ci,_) -> dprintf "definition of %s" (compFullName ci)
  3970. | GCompTagDecl(ci,_) -> dprintf "declaration of %s" (compFullName ci)
  3971. | GEnumTag(ei,_) -> dprintf "definition of enum %s" ei.ename
  3972. | GEnumTagDecl(ei,_) -> dprintf "declaration of enum %s" ei.ename
  3973. | GFun(fd, _) -> dprintf "definition of %s" fd.svar.vname
  3974. | GText _ -> text "GText"
  3975. | GAsm _ -> text "GAsm"
  3976. (* sm: given an ordinary CIL object printer, yield one which
  3977. * behaves the same, except it never prints #line directives
  3978. * (this is useful for debugging printfs) *)
  3979. let dn_obj (func: unit -> 'a -> doc) : (unit -> 'a -> doc) =
  3980. begin
  3981. (* construct the closure to return *)
  3982. let theFunc () (obj:'a) : doc =
  3983. begin
  3984. let prevStyle = !lineDirectiveStyle in
  3985. lineDirectiveStyle := None;
  3986. let ret = (func () obj) in (* call underlying printer *)
  3987. lineDirectiveStyle := prevStyle;
  3988. ret
  3989. end in
  3990. theFunc
  3991. end
  3992. (* now define shortcuts for the non-location-printing versions,
  3993. * with the naming prefix "dn_" *)
  3994. let dn_exp = (dn_obj d_exp)
  3995. let dn_lval = (dn_obj d_lval)
  3996. (* dn_offset is missing because it has a different interface *)
  3997. let dn_init = (dn_obj d_init)
  3998. let dn_type = (dn_obj d_type)
  3999. let dn_global = (dn_obj d_global)
  4000. let dn_attrlist = (dn_obj d_attrlist)
  4001. let dn_attr = (dn_obj d_attr)
  4002. let dn_attrparam = (dn_obj d_attrparam)
  4003. let dn_stmt = (dn_obj d_stmt)
  4004. let dn_instr = (dn_obj d_instr)
  4005. (* Now define a cilPlainPrinter *)
  4006. class plainCilPrinterClass =
  4007. (* We keep track of the composite types that we have done to avoid
  4008. * recursion *)
  4009. let donecomps : (int, unit) H.t = H.create 13 in
  4010. object (self)
  4011. inherit defaultCilPrinterClass as super
  4012. (*** PLAIN TYPES ***)
  4013. method pType (dn: doc option) () (t: typ) =
  4014. match dn with
  4015. None -> self#pOnlyType () t
  4016. | Some d -> d ++ text " : " ++ self#pOnlyType () t
  4017. method private pOnlyType () = function
  4018. TVoid a -> dprintf "TVoid(@[%a@])" self#pAttrs a
  4019. | TInt(ikind, a) -> dprintf "TInt(@[%a,@?%a@])"
  4020. d_ikind ikind self#pAttrs a
  4021. | TFloat(fkind, a) ->
  4022. dprintf "TFloat(@[%a,@?%a@])" d_fkind fkind self#pAttrs a
  4023. | TNamed (t, a) ->
  4024. dprintf "TNamed(@[%s,@?%a,@?%a@])"
  4025. t.tname self#pOnlyType t.ttype self#pAttrs a
  4026. | TPtr(t, a) -> dprintf "TPtr(@[%a,@?%a@])" self#pOnlyType t self#pAttrs a
  4027. | TArray(t,l,a) ->
  4028. let dl = match l with
  4029. None -> text "None" | Some l -> dprintf "Some(@[%a@])" self#pExp l in
  4030. dprintf "TArray(@[%a,@?%a,@?%a@])"
  4031. self#pOnlyType t insert dl self#pAttrs a
  4032. | TEnum(enum,a) -> dprintf "Enum(%s,@[%a@])" enum.ename self#pAttrs a
  4033. | TFun(tr,args,isva,a) ->
  4034. dprintf "TFun(@[%a,@?%a%s,@?%a@])"
  4035. self#pOnlyType tr
  4036. insert
  4037. (if args = None then text "None"
  4038. else (docList ~sep:(chr ',' ++ break)
  4039. (fun (an,at,aa) ->
  4040. dprintf "%s: %a" an self#pOnlyType at))
  4041. ()
  4042. (argsToList args))
  4043. (if isva then "..." else "") self#pAttrs a
  4044. | TComp (comp, a) ->
  4045. if H.mem donecomps comp.ckey then
  4046. dprintf "TCompLoop(%s %s, _, %a)"
  4047. (if comp.cstruct then "struct" else "union") comp.cname
  4048. self#pAttrs comp.cattr
  4049. else begin
  4050. H.add donecomps comp.ckey (); (* Add it before we do the fields *)
  4051. dprintf "TComp(@[%s %s,@?%a,@?%a,@?%a@])"
  4052. (if comp.cstruct then "struct" else "union") comp.cname
  4053. (docList ~sep:(chr ',' ++ break)
  4054. (fun f -> dprintf "%s : %a" f.fname self#pOnlyType f.ftype))
  4055. comp.cfields
  4056. self#pAttrs comp.cattr
  4057. self#pAttrs a
  4058. end
  4059. | TBuiltin_va_list a ->
  4060. dprintf "TBuiltin_va_list(%a)" self#pAttrs a
  4061. (* Some plain pretty-printers. Unlike the above these expose all the
  4062. * details of the internal representation *)
  4063. method pExp () = function
  4064. Const(c) ->
  4065. let d_plainconst () c =
  4066. match c with
  4067. CInt64(i, ik, so) ->
  4068. let fmt = if isSigned ik then "%d" else "%x" in
  4069. dprintf "Int64(%s,%a,%s)"
  4070. (Int64.format fmt i)
  4071. d_ikind ik
  4072. (match so with Some s -> s | _ -> "None")
  4073. | CStr(s) ->
  4074. text ("CStr(\"" ^ escape_string s ^ "\")")
  4075. | CWStr(s) ->
  4076. dprintf "CWStr(%a)" d_const c
  4077. | CChr(c) -> text ("CChr('" ^ escape_char c ^ "')")
  4078. | CReal(f, fk, so) ->
  4079. dprintf "CReal(%f, %a, %s)"
  4080. f
  4081. d_fkind fk
  4082. (match so with Some s -> s | _ -> "None")
  4083. | CEnum(_, s, _) -> text s
  4084. in
  4085. text "Const(" ++ d_plainconst () c ++ text ")"
  4086. | Lval(lv) ->
  4087. text "Lval("
  4088. ++ (align
  4089. ++ self#pLval () lv
  4090. ++ unalign)
  4091. ++ text ")"
  4092. | CastE(t,e) -> dprintf "CastE(@[%a,@?%a@])" self#pOnlyType t self#pExp e
  4093. | UnOp(u,e1,_) ->
  4094. dprintf "UnOp(@[%a,@?%a@])"
  4095. d_unop u self#pExp e1
  4096. | BinOp(b,e1,e2,_) ->
  4097. let d_plainbinop () b =
  4098. match b with
  4099. PlusA -> text "PlusA"
  4100. | PlusPI -> text "PlusPI"
  4101. | IndexPI -> text "IndexPI"
  4102. | MinusA -> text "MinusA"
  4103. | MinusPP -> text "MinusPP"
  4104. | MinusPI -> text "MinusPI"
  4105. | _ -> d_binop () b
  4106. in
  4107. dprintf "%a(@[%a,@?%a@])" d_plainbinop b
  4108. self#pExp e1 self#pExp e2
  4109. | Question(e1,e2,e3,_) ->
  4110. dprintf "Question(@[%a,@?%a,@?%a@])"
  4111. self#pExp e1 self#pExp e2 self#pExp e3
  4112. | SizeOf (t) ->
  4113. text "sizeof(" ++ self#pType None () t ++ chr ')'
  4114. | SizeOfE (e) ->
  4115. text "sizeofE(" ++ self#pExp () e ++ chr ')'
  4116. | SizeOfStr (s) ->
  4117. text "sizeofStr(" ++ d_const () (CStr s) ++ chr ')'
  4118. | AlignOf (t) ->
  4119. text "__alignof__(" ++ self#pType None () t ++ chr ')'
  4120. | AlignOfE (e) ->
  4121. text "__alignof__(" ++ self#pExp () e ++ chr ')'
  4122. | StartOf lv -> dprintf "StartOf(%a)" self#pLval lv
  4123. | AddrOf (lv) -> dprintf "AddrOf(%a)" self#pLval lv
  4124. | AddrOfLabel (sref) -> dprintf "AddrOfLabel(%a)" self#pStmt !sref
  4125. method private d_plainoffset () = function
  4126. NoOffset -> text "NoOffset"
  4127. | Field(fi,o) ->
  4128. dprintf "Field(@[%s:%a,@?%a@])"
  4129. fi.fname self#pOnlyType fi.ftype self#d_plainoffset o
  4130. | Index(e, o) ->
  4131. dprintf "Index(@[%a,@?%a@])" self#pExp e self#d_plainoffset o
  4132. method pInit () = function
  4133. SingleInit e -> dprintf "SI(%a)" d_exp e
  4134. | CompoundInit (t, initl) ->
  4135. let d_plainoneinit (o, i) =
  4136. self#d_plainoffset () o ++ text " = " ++ self#pInit () i
  4137. in
  4138. dprintf "CI(@[%a,@?%a@])" self#pOnlyType t
  4139. (docList ~sep:(chr ',' ++ break) d_plainoneinit) initl
  4140. (*
  4141. | ArrayInit (t, len, initl) ->
  4142. let idx = ref (- 1) in
  4143. let d_plainoneinit i =
  4144. incr idx;
  4145. text "[" ++ num !idx ++ text "] = " ++ self#pInit () i
  4146. in
  4147. dprintf "AI(@[%a,%d,@?%a@])" self#pOnlyType t len
  4148. (docList ~sep:(chr ',' ++ break) d_plainoneinit) initl
  4149. *)
  4150. method pLval () (lv: lval) =
  4151. match lv with
  4152. | Var vi, o -> dprintf "Var(@[%s,@?%a@])" vi.vname self#d_plainoffset o
  4153. | Mem e, o -> dprintf "Mem(@[%a,@?%a@])" self#pExp e self#d_plainoffset o
  4154. end
  4155. let plainCilPrinter = new plainCilPrinterClass
  4156. (* And now some shortcuts *)
  4157. let d_plainexp () e = plainCilPrinter#pExp () e
  4158. let d_plaintype () t = plainCilPrinter#pType None () t
  4159. let d_plaininit () i = plainCilPrinter#pInit () i
  4160. let d_plainlval () l = plainCilPrinter#pLval () l
  4161. class type descriptiveCilPrinter = object
  4162. inherit cilPrinter
  4163. method startTemps: unit -> unit
  4164. method stopTemps: unit -> unit
  4165. method pTemps: unit -> Pretty.doc
  4166. end
  4167. class descriptiveCilPrinterClass (enable: bool) : descriptiveCilPrinter =
  4168. object (self)
  4169. (** Like defaultCilPrinterClass, but instead of temporary variable
  4170. names it prints the description that was provided when the temp was
  4171. created. This is usually better for messages that are printed for end
  4172. users, although you may want the temporary names for debugging.
  4173. The boolean here enables descriptive printing. Usually use true
  4174. here, but you can set enable to false to make this class behave
  4175. like defaultCilPrinterClass. This allows subclasses to turn the
  4176. feature off. *)
  4177. inherit defaultCilPrinterClass as super
  4178. val mutable temps: (varinfo * string * doc) list = []
  4179. val mutable useTemps: bool = false
  4180. method startTemps () : unit =
  4181. temps <- [];
  4182. useTemps <- true
  4183. method stopTemps () : unit =
  4184. temps <- [];
  4185. useTemps <- false
  4186. method pTemps () : doc =
  4187. if temps = [] then
  4188. nil
  4189. else
  4190. text "\nWhere:\n " ++
  4191. docList ~sep:(text "\n ")
  4192. (fun (_, s, d) -> dprintf "%s = %a" s insert d) ()
  4193. (List.rev temps)
  4194. method private pVarDescriptive (vi: varinfo) : doc =
  4195. if vi.vdescr <> nil then begin
  4196. if vi.vdescrpure || not useTemps then
  4197. vi.vdescr
  4198. else begin
  4199. try
  4200. let _, name, _ = List.find (fun (vi', _, _) -> vi == vi') temps in
  4201. text name
  4202. with Not_found ->
  4203. let name = "tmp" ^ string_of_int (List.length temps) in
  4204. temps <- (vi, name, vi.vdescr) :: temps;
  4205. text name
  4206. end
  4207. end else
  4208. super#pVar vi
  4209. (* Only substitute temp vars that appear in expressions.
  4210. (Other occurrences of lvalues are the left-hand sides of assignments,
  4211. but we shouldn't substitute there since "foo(a,b) = foo(a,b)"
  4212. would make no sense to the user.) *)
  4213. method pExp () (e:exp) : doc =
  4214. if enable then
  4215. match e with
  4216. Lval (Var vi, o)
  4217. | StartOf (Var vi, o) ->
  4218. self#pOffset (self#pVarDescriptive vi) o
  4219. | AddrOf (Var vi, o) ->
  4220. (* No parens needed, since offsets have higher precedence than & *)
  4221. text "& " ++ self#pOffset (self#pVarDescriptive vi) o
  4222. | _ -> super#pExp () e
  4223. else
  4224. super#pExp () e
  4225. end
  4226. let descriptiveCilPrinter: descriptiveCilPrinter =
  4227. ((new descriptiveCilPrinterClass true) :> descriptiveCilPrinter)
  4228. let dd_exp = descriptiveCilPrinter#pExp
  4229. let dd_lval = descriptiveCilPrinter#pLval
  4230. (* zra: this allows pretty printers not in cil.ml to
  4231. be exposed to cilmain.ml *)
  4232. let printerForMaincil = ref defaultCilPrinter
  4233. let rec d_typsig () = function
  4234. TSArray (ts, eo, al) ->
  4235. dprintf "TSArray(@[%a,@?%a,@?%a@])"
  4236. d_typsig ts
  4237. insert (text (match eo with None -> "None"
  4238. | Some e -> "Some " ^ Int64.to_string e))
  4239. d_attrlist al
  4240. | TSPtr (ts, al) ->
  4241. dprintf "TSPtr(@[%a,@?%a@])"
  4242. d_typsig ts d_attrlist al
  4243. | TSComp (iss, name, al) ->
  4244. dprintf "TSComp(@[%s %s,@?%a@])"
  4245. (if iss then "struct" else "union") name
  4246. d_attrlist al
  4247. | TSFun (rt, args, isva, al) ->
  4248. dprintf "TSFun(@[%a,@?%a,%B,@?%a@])"
  4249. d_typsig rt
  4250. insert
  4251. (match args with
  4252. | None -> text "None"
  4253. | Some args ->
  4254. docList ~sep:(chr ',' ++ break) (d_typsig ()) () args)
  4255. isva
  4256. d_attrlist al
  4257. | TSEnum (n, al) ->
  4258. dprintf "TSEnum(@[%s,@?%a@])"
  4259. n d_attrlist al
  4260. | TSBase t -> dprintf "TSBase(%a)" d_type t
  4261. let newVID () =
  4262. let t = !nextGlobalVID in
  4263. incr nextGlobalVID;
  4264. t
  4265. (* Make a varinfo. Used mostly as a helper function below *)
  4266. let makeVarinfo global name ?init typ =
  4267. (* Strip const from type for locals *)
  4268. let vi =
  4269. { vname = name;
  4270. vid = newVID ();
  4271. vglob = global;
  4272. vtype = if global then typ else typeRemoveAttributes ["const"] typ;
  4273. vdecl = lu;
  4274. vinit = {init=init};
  4275. vinline = false;
  4276. vattr = [];
  4277. vstorage = NoStorage;
  4278. vaddrof = false;
  4279. vreferenced = false;
  4280. vdescr = nil;
  4281. vdescrpure = true;
  4282. } in
  4283. vi
  4284. let copyVarinfo (vi: varinfo) (newname: string) : varinfo =
  4285. let vi' = {vi with vname = newname; vid = newVID () } in
  4286. vi'
  4287. let makeLocal fdec name typ init = (* a helper function *)
  4288. fdec.smaxid <- 1 + fdec.smaxid;
  4289. let vi = makeVarinfo false name ?init:init typ in
  4290. vi
  4291. (* Make a local variable and add it to a function *)
  4292. let makeLocalVar fdec ?(insert = true) name ?init typ =
  4293. let vi = makeLocal fdec name typ init in
  4294. if insert then fdec.slocals <- fdec.slocals @ [vi];
  4295. vi
  4296. let makeTempVar fdec ?(insert = true) ?(name = "__cil_tmp")
  4297. ?(descr = nil) ?(descrpure = true) typ : varinfo =
  4298. let rec findUniqueName () : string=
  4299. let n = name ^ (string_of_int (1 + fdec.smaxid)) in
  4300. (* Is this check a performance problem? We could bring the old
  4301. unchecked makeTempVar back as a separate function that assumes
  4302. the prefix name does not occur in the original program. *)
  4303. if (List.exists (fun vi -> vi.vname = n) fdec.slocals)
  4304. || (List.exists (fun vi -> vi.vname = n) fdec.sformals) then begin
  4305. fdec.smaxid <- 1 + fdec.smaxid;
  4306. findUniqueName ()
  4307. end else
  4308. n
  4309. in
  4310. let name = findUniqueName () in
  4311. let vi = makeLocalVar fdec ~insert name typ in
  4312. vi.vdescr <- descr;
  4313. vi.vdescrpure <- descrpure;
  4314. vi
  4315. (* Set the formals and re-create the function name based on the information*)
  4316. let setFormals (f: fundec) (forms: varinfo list) =
  4317. f.sformals <- forms; (* Set the formals *)
  4318. match unrollType f.svar.vtype with
  4319. TFun(rt, _, isva, fa) ->
  4320. f.svar.vtype <-
  4321. TFun(rt,
  4322. Some (Util.list_map (fun a -> (a.vname, a.vtype, a.vattr)) forms),
  4323. isva, fa)
  4324. | _ -> E.s (E.bug "Set formals. %s does not have function type\n"
  4325. f.svar.vname)
  4326. (* Set the types of arguments and results as given by the function type
  4327. * passed as the second argument *)
  4328. let setFunctionType (f: fundec) (t: typ) =
  4329. match unrollType t with
  4330. TFun (rt, Some args, va, a) ->
  4331. if List.length f.sformals <> List.length args then
  4332. E.s (E.bug "setFunctionType: number of arguments differs from the number of formals");
  4333. (* Change the function type. *)
  4334. f.svar.vtype <- t;
  4335. (* Change the sformals and we know that indirectly we'll change the
  4336. * function type *)
  4337. List.iter2
  4338. (fun (an,at,aa) f ->
  4339. f.vtype <- at; f.vattr <- aa)
  4340. args f.sformals
  4341. | _ -> E.s (E.bug "setFunctionType: not a function type")
  4342. (* Set the types of arguments and results as given by the function type
  4343. * passed as the second argument *)
  4344. let setFunctionTypeMakeFormals (f: fundec) (t: typ) =
  4345. match unrollType t with
  4346. TFun (rt, Some args, va, a) ->
  4347. if f.sformals <> [] then
  4348. E.s (E.warn "setFunctionTypMakeFormals called on function %s with some formals already"
  4349. f.svar.vname);
  4350. (* Change the function type. *)
  4351. f.svar.vtype <- t;
  4352. f.sformals <- [];
  4353. f.sformals <- Util.list_map (fun (n,t,a) -> makeLocal f n t None) args;
  4354. setFunctionType f t
  4355. | _ -> E.s (E.bug "setFunctionTypeMakeFormals: not a function type: %a"
  4356. d_type t)
  4357. let setMaxId (f: fundec) =
  4358. f.smaxid <- List.length f.sformals + List.length f.slocals
  4359. (* Make a formal variable for a function. Insert it in both the sformals
  4360. * and the type of the function. You can optionally specify where to insert
  4361. * this one. If where = "^" then it is inserted first. If where = "$" then
  4362. * it is inserted last. Otherwise where must be the name of a formal after
  4363. * which to insert this. By default it is inserted at the end. *)
  4364. let makeFormalVar fdec ?(where = "$") name typ : varinfo =
  4365. (* Search for the insertion place *)
  4366. let thenewone = ref fdec.svar in (* Just a placeholder *)
  4367. let makeit () : varinfo =
  4368. let vi = makeLocal fdec name typ None in
  4369. thenewone := vi;
  4370. vi
  4371. in
  4372. let rec loopFormals = function
  4373. [] ->
  4374. if where = "$" then [makeit ()]
  4375. else E.s (E.error "makeFormalVar: cannot find insert-after formal %s"
  4376. where)
  4377. | f :: rest when f.vname = where -> f :: makeit () :: rest
  4378. | f :: rest -> f :: loopFormals rest
  4379. in
  4380. let newformals =
  4381. if where = "^" then makeit () :: fdec.sformals else
  4382. loopFormals fdec.sformals in
  4383. setFormals fdec newformals;
  4384. !thenewone
  4385. (* Make a global variable. Your responsibility to make sure that the name
  4386. * is unique *)
  4387. let makeGlobalVar name typ =
  4388. let vi = makeVarinfo true name typ in
  4389. vi
  4390. (* Make an empty function *)
  4391. let emptyFunction name =
  4392. { svar = makeGlobalVar name (TFun(voidType, Some [], false,[]));
  4393. smaxid = 0;
  4394. slocals = [];
  4395. sformals = [];
  4396. sbody = mkBlock [];
  4397. smaxstmtid = None;
  4398. sallstmts = [];
  4399. }
  4400. (* A dummy function declaration handy for initialization *)
  4401. let dummyFunDec = emptyFunction "@dummy"
  4402. let dummyFile =
  4403. { globals = [];
  4404. fileName = "<dummy>";
  4405. globinit = None;
  4406. globinitcalled = false;}
  4407. (***** Load and store files as unmarshalled Ocaml binary data. ****)
  4408. type savedFile =
  4409. { savedFile: file;
  4410. savedNextVID: int;
  4411. savedNextCompinfoKey: int}
  4412. let saveBinaryFileChannel (cil_file : file) (outchan : out_channel) =
  4413. let save = {savedFile = cil_file;
  4414. savedNextVID = !nextGlobalVID;
  4415. savedNextCompinfoKey = !nextCompinfoKey} in
  4416. Marshal.to_channel outchan save []
  4417. let saveBinaryFile (cil_file : file) (filename : string) =
  4418. let outchan = open_out_bin filename in
  4419. saveBinaryFileChannel cil_file outchan;
  4420. close_out outchan
  4421. (** Read a {!Cil.file} in binary form from the filesystem. The first
  4422. * argument is the name of a file previously created by
  4423. * {!Cil.saveBinaryFile}. Because this also reads some global state,
  4424. * this should be called before any other CIL code is parsed or generated. *)
  4425. let loadBinaryFile (filename : string) : file =
  4426. let inchan = open_in_bin filename in
  4427. let loaded : savedFile = (Marshal.from_channel inchan : savedFile) in
  4428. close_in inchan ;
  4429. (* nextGlobalVID = 11 because CIL initialises many dummy variables *)
  4430. if !nextGlobalVID != 11 || !nextCompinfoKey != 1 then begin
  4431. (* In this case, we should change all of the varinfo and compinfo
  4432. keys in loaded.savedFile to prevent conflicts. But since that hasn't
  4433. been implemented yet, just print a warning. If you do implement this,
  4434. please send it to the CIL maintainers. *)
  4435. ignore (E.warn "You are possibly loading a binary file after another file has been loaded. This isn't currently supported, so varinfo and compinfo id numbers may conflict.")
  4436. end;
  4437. nextGlobalVID := max loaded.savedNextVID !nextGlobalVID;
  4438. nextCompinfoKey := max loaded.savedNextCompinfoKey !nextCompinfoKey;
  4439. loaded.savedFile
  4440. (* Take the name of a file and make a valid symbol name out of it. There are
  4441. * a few characters that are not valid in symbols *)
  4442. let makeValidSymbolName (s: string) =
  4443. let s = String.copy s in (* So that we can update in place *)
  4444. let l = String.length s in
  4445. for i = 0 to l - 1 do
  4446. let c = String.get s i in
  4447. let isinvalid =
  4448. match c with
  4449. '-' | '.' -> true
  4450. | _ -> false
  4451. in
  4452. if isinvalid then
  4453. String.set s i '_';
  4454. done;
  4455. s
  4456. let rec addOffset (toadd: offset) (off: offset) : offset =
  4457. match off with
  4458. NoOffset -> toadd
  4459. | Field(fid', offset) -> Field(fid', addOffset toadd offset)
  4460. | Index(e, offset) -> Index(e, addOffset toadd offset)
  4461. (* Add an offset at the end of an lv *)
  4462. let addOffsetLval toadd (b, off) : lval =
  4463. b, addOffset toadd off
  4464. let rec removeOffset (off: offset) : offset * offset =
  4465. match off with
  4466. NoOffset -> NoOffset, NoOffset
  4467. | Field(f, NoOffset) -> NoOffset, off
  4468. | Index(i, NoOffset) -> NoOffset, off
  4469. | Field(f, restoff) ->
  4470. let off', last = removeOffset restoff in
  4471. Field(f, off'), last
  4472. | Index(i, restoff) ->
  4473. let off', last = removeOffset restoff in
  4474. Index(i, off'), last
  4475. let removeOffsetLval ((b, off): lval) : lval * offset =
  4476. let off', last = removeOffset off in
  4477. (b, off'), last
  4478. (*** Define the visiting engine ****)
  4479. (* visit all the nodes in a Cil expression *)
  4480. let doVisit (vis: cilVisitor)
  4481. (action: 'a visitAction)
  4482. (children: cilVisitor -> 'a -> 'a)
  4483. (node: 'a) : 'a =
  4484. match action with
  4485. SkipChildren -> node
  4486. | ChangeTo node' -> node'
  4487. | DoChildren -> children vis node
  4488. | ChangeDoChildrenPost(node', f) -> f (children vis node')
  4489. (* mapNoCopy is like map but avoid copying the list if the function does not
  4490. * change the elements. *)
  4491. let mapNoCopy (f: 'a -> 'a) l =
  4492. let rec aux acc changed = function
  4493. [] -> if changed then List.rev acc else l
  4494. | i :: resti ->
  4495. let i' = f i in
  4496. aux (i' :: acc) (changed || i != i') resti
  4497. in aux [] false l
  4498. let rec mapNoCopyList (f: 'a -> 'a list) l =
  4499. let rec aux acc changed = function
  4500. [] -> if changed then List.rev acc else l
  4501. | i :: resti ->
  4502. let il' = f i in
  4503. let has_changed =
  4504. match il' with
  4505. [i'] when i' == i -> false
  4506. | _ -> true in
  4507. aux (List.rev_append il' acc) (changed || has_changed) resti
  4508. in aux [] false l
  4509. (* A visitor for lists *)
  4510. let doVisitList (vis: cilVisitor)
  4511. (action: 'a list visitAction)
  4512. (children: cilVisitor -> 'a -> 'a)
  4513. (node: 'a) : 'a list =
  4514. match action with
  4515. SkipChildren -> [node]
  4516. | ChangeTo nodes' -> nodes'
  4517. | DoChildren -> [children vis node]
  4518. | ChangeDoChildrenPost(nodes', f) ->
  4519. f (mapNoCopy (fun n -> children vis n) nodes')
  4520. let debugVisit = false
  4521. let rec visitCilExpr (vis: cilVisitor) (e: exp) : exp =
  4522. doVisit vis (vis#vexpr e) childrenExp e
  4523. and childrenExp (vis: cilVisitor) (e: exp) : exp =
  4524. let vExp e = visitCilExpr vis e in
  4525. let vTyp t = visitCilType vis t in
  4526. let vLval lv = visitCilLval vis lv in
  4527. match e with
  4528. | Const (CEnum(v, s, ei)) ->
  4529. let v' = vExp v in
  4530. if v' != v then Const (CEnum(v', s, ei)) else e
  4531. | Const _ -> e
  4532. | SizeOf t ->
  4533. let t'= vTyp t in
  4534. if t' != t then SizeOf t' else e
  4535. | SizeOfE e1 ->
  4536. let e1' = vExp e1 in
  4537. if e1' != e1 then SizeOfE e1' else e
  4538. | SizeOfStr s -> e
  4539. | AlignOf t ->
  4540. let t' = vTyp t in
  4541. if t' != t then AlignOf t' else e
  4542. | AlignOfE e1 ->
  4543. let e1' = vExp e1 in
  4544. if e1' != e1 then AlignOfE e1' else e
  4545. | Lval lv ->
  4546. let lv' = vLval lv in
  4547. if lv' != lv then Lval lv' else e
  4548. | UnOp (uo, e1, t) ->
  4549. let e1' = vExp e1 in let t' = vTyp t in
  4550. if e1' != e1 || t' != t then UnOp(uo, e1', t') else e
  4551. | BinOp (bo, e1, e2, t) ->
  4552. let e1' = vExp e1 in let e2' = vExp e2 in let t' = vTyp t in
  4553. if e1' != e1 || e2' != e2 || t' != t then BinOp(bo, e1',e2',t') else e
  4554. | Question (e1, e2, e3, t) ->
  4555. let e1' = vExp e1 in let e2' = vExp e2 in let e3' = vExp e3 in let t' = vTyp t in
  4556. if e1' != e1 || e2' != e2 || e3' != e3 || t' != t then Question(e1',e2',e3',t') else e
  4557. | CastE (t, e1) ->
  4558. let t' = vTyp t in let e1' = vExp e1 in
  4559. if t' != t || e1' != e1 then CastE(t', e1') else e
  4560. | AddrOf lv ->
  4561. let lv' = vLval lv in
  4562. if lv' != lv then AddrOf lv' else e
  4563. | AddrOfLabel _ -> e
  4564. | StartOf lv ->
  4565. let lv' = vLval lv in
  4566. if lv' != lv then StartOf lv' else e
  4567. and visitCilInit (vis: cilVisitor) (forglob: varinfo)
  4568. (atoff: offset) (i: init) : init =
  4569. let rec childrenInit (vis: cilVisitor) (i: init) : init =
  4570. let fExp e = visitCilExpr vis e in
  4571. let fTyp t = visitCilType vis t in
  4572. match i with
  4573. | SingleInit e ->
  4574. let e' = fExp e in
  4575. if e' != e then SingleInit e' else i
  4576. | CompoundInit (t, initl) ->
  4577. let t' = fTyp t in
  4578. (* Collect the new initializer list, in reverse. We prefer two
  4579. * traversals to ensure tail-recursion. *)
  4580. let newinitl : (offset * init) list ref = ref [] in
  4581. (* Keep track whether the list has changed *)
  4582. let hasChanged = ref false in
  4583. let doOneInit ((o, i) as oi) =
  4584. let o' = visitCilInitOffset vis o in (* use initializer version *)
  4585. let i' = visitCilInit vis forglob (addOffset o' atoff) i in
  4586. let newio =
  4587. if o' != o || i' != i then
  4588. begin hasChanged := true; (o', i') end else oi
  4589. in
  4590. newinitl := newio :: !newinitl
  4591. in
  4592. List.iter doOneInit initl;
  4593. let initl' = if !hasChanged then List.rev !newinitl else initl in
  4594. if t' != t || initl' != initl then CompoundInit (t', initl') else i
  4595. in
  4596. doVisit vis (vis#vinit forglob atoff i) childrenInit i
  4597. and visitCilLval (vis: cilVisitor) (lv: lval) : lval =
  4598. doVisit vis (vis#vlval lv) childrenLval lv
  4599. and childrenLval (vis: cilVisitor) (lv: lval) : lval =
  4600. (* and visit its subexpressions *)
  4601. let vExp e = visitCilExpr vis e in
  4602. let vOff off = visitCilOffset vis off in
  4603. match lv with
  4604. Var v, off ->
  4605. let v' = doVisit vis (vis#vvrbl v) (fun _ x -> x) v in
  4606. let off' = vOff off in
  4607. if v' != v || off' != off then Var v', off' else lv
  4608. | Mem e, off ->
  4609. let e' = vExp e in
  4610. let off' = vOff off in
  4611. if e' != e || off' != off then Mem e', off' else lv
  4612. and visitCilOffset (vis: cilVisitor) (off: offset) : offset =
  4613. doVisit vis (vis#voffs off) childrenOffset off
  4614. and childrenOffset (vis: cilVisitor) (off: offset) : offset =
  4615. let vOff off = visitCilOffset vis off in
  4616. match off with
  4617. Field (f, o) ->
  4618. let o' = vOff o in
  4619. if o' != o then Field (f, o') else off
  4620. | Index (e, o) ->
  4621. let e' = visitCilExpr vis e in
  4622. let o' = vOff o in
  4623. if e' != e || o' != o then Index (e', o') else off
  4624. | NoOffset -> off
  4625. (* sm: for offsets in initializers, the 'startvisit' will be the
  4626. * vinitoffs method, but we can re-use the childrenOffset from
  4627. * above since recursive offsets are visited by voffs. (this point
  4628. * is moot according to cil.mli which claims the offsets in
  4629. * initializers will never recursively contain offsets)
  4630. *)
  4631. and visitCilInitOffset (vis: cilVisitor) (off: offset) : offset =
  4632. doVisit vis (vis#vinitoffs off) childrenOffset off
  4633. and visitCilInstr (vis: cilVisitor) (i: instr) : instr list =
  4634. let oldloc = !currentLoc in
  4635. currentLoc := (get_instrLoc i);
  4636. assertEmptyQueue vis;
  4637. let res = doVisitList vis (vis#vinst i) childrenInstr i in
  4638. currentLoc := oldloc;
  4639. (* See if we have accumulated some instructions *)
  4640. vis#unqueueInstr () @ res
  4641. and childrenInstr (vis: cilVisitor) (i: instr) : instr =
  4642. let fExp e = visitCilExpr vis e in
  4643. let fLval lv = visitCilLval vis lv in
  4644. match i with
  4645. | Set(lv,e,l) ->
  4646. let lv' = fLval lv in let e' = fExp e in
  4647. if lv' != lv || e' != e then Set(lv',e',l) else i
  4648. | Call(None,f,args,l) ->
  4649. let f' = fExp f in let args' = mapNoCopy fExp args in
  4650. if f' != f || args' != args then Call(None,f',args',l) else i
  4651. | Call(Some lv,fn,args,l) ->
  4652. let lv' = fLval lv in let fn' = fExp fn in
  4653. let args' = mapNoCopy fExp args in
  4654. if lv' != lv || fn' != fn || args' != args
  4655. then Call(Some lv', fn', args', l) else i
  4656. | Asm(sl,isvol,outs,ins,clobs,l) ->
  4657. let outs' = mapNoCopy (fun ((id,s,lv) as pair) ->
  4658. let lv' = fLval lv in
  4659. if lv' != lv then (id,s,lv') else pair) outs in
  4660. let ins' = mapNoCopy (fun ((id,s,e) as pair) ->
  4661. let e' = fExp e in
  4662. if e' != e then (id,s,e') else pair) ins in
  4663. if outs' != outs || ins' != ins then
  4664. Asm(sl,isvol,outs',ins',clobs,l) else i
  4665. (* visit all nodes in a Cil statement tree in preorder *)
  4666. and visitCilStmt (vis: cilVisitor) (s: stmt) : stmt =
  4667. let oldloc = !currentLoc in
  4668. currentLoc := (get_stmtLoc s.skind) ;
  4669. assertEmptyQueue vis;
  4670. let toPrepend : instr list ref = ref [] in (* childrenStmt may add to this *)
  4671. let res = doVisit vis (vis#vstmt s) (childrenStmt toPrepend) s in
  4672. (* Now see if we have saved some instructions *)
  4673. toPrepend := !toPrepend @ vis#unqueueInstr ();
  4674. (match !toPrepend with
  4675. [] -> () (* Return the same statement *)
  4676. | _ ->
  4677. (* Make our statement contain the instructions to prepend *)
  4678. res.skind <- Block { battrs = []; bstmts = [ mkStmt (Instr !toPrepend);
  4679. mkStmt res.skind ] });
  4680. currentLoc := oldloc;
  4681. res
  4682. and childrenStmt (toPrepend: instr list ref) : cilVisitor -> stmt -> stmt =
  4683. (* this is a hack to avoid currying and reduce GC pressure *)
  4684. () ; fun vis s ->
  4685. let fExp e = (visitCilExpr vis e) in
  4686. let fBlock b = visitCilBlock vis b in
  4687. let fInst i = visitCilInstr vis i in
  4688. (* Just change the statement kind *)
  4689. let skind' =
  4690. match s.skind with
  4691. Break _ | Continue _ | Goto _ | Return (None, _) -> s.skind
  4692. | ComputedGoto (e, l) ->
  4693. let e' = fExp e in
  4694. if e' != e then ComputedGoto (e', l) else s.skind
  4695. | Return (Some e, l) ->
  4696. let e' = fExp e in
  4697. if e' != e then Return (Some e', l) else s.skind
  4698. | Loop (b, l, s1, s2) ->
  4699. let b' = fBlock b in
  4700. if b' != b then Loop (b', l, s1, s2) else s.skind
  4701. | If(e, s1, s2, l) ->
  4702. let e' = fExp e in
  4703. (*if e queued any instructions, pop them here and remember them so that
  4704. they are inserted before the If stmt, not in the then block. *)
  4705. toPrepend := vis#unqueueInstr ();
  4706. let s1'= fBlock s1 in let s2'= fBlock s2 in
  4707. (* the stmts in the blocks should have cleaned up after themselves.*)
  4708. assertEmptyQueue vis;
  4709. if e' != e || s1' != s1 || s2' != s2 then
  4710. If(e', s1', s2', l) else s.skind
  4711. | Switch (e, b, stmts, l) ->
  4712. let e' = fExp e in
  4713. toPrepend := vis#unqueueInstr (); (* insert these before the switch *)
  4714. let b' = fBlock b in
  4715. (* the stmts in b should have cleaned up after themselves.*)
  4716. assertEmptyQueue vis;
  4717. (* Don't do stmts, but we better not change those *)
  4718. if e' != e || b' != b then Switch (e', b', stmts, l) else s.skind
  4719. | Instr il ->
  4720. let il' = mapNoCopyList fInst il in
  4721. if il' != il then Instr il' else s.skind
  4722. | Block b ->
  4723. let b' = fBlock b in
  4724. if b' != b then Block b' else s.skind
  4725. | TryFinally (b, h, l) ->
  4726. let b' = fBlock b in
  4727. let h' = fBlock h in
  4728. if b' != b || h' != h then TryFinally(b', h', l) else s.skind
  4729. | TryExcept (b, (il, e), h, l) ->
  4730. let b' = fBlock b in
  4731. assertEmptyQueue vis;
  4732. (* visit the instructions *)
  4733. let il' = mapNoCopyList fInst il in
  4734. (* Visit the expression *)
  4735. let e' = fExp e in
  4736. let il'' =
  4737. let more = vis#unqueueInstr () in
  4738. if more != [] then
  4739. il' @ more
  4740. else
  4741. il'
  4742. in
  4743. let h' = fBlock h in
  4744. (* Now collect the instructions *)
  4745. if b' != b || il'' != il || e' != e || h' != h then
  4746. TryExcept(b', (il'', e'), h', l)
  4747. else s.skind
  4748. in
  4749. if skind' != s.skind then s.skind <- skind';
  4750. (* Visit the labels *)
  4751. let labels' =
  4752. let fLabel = function
  4753. Case (e, l) as lb ->
  4754. let e' = fExp e in
  4755. if e' != e then Case (e', l) else lb
  4756. | CaseRange (e1, e2, l) as lb ->
  4757. let e1' = fExp e1 in
  4758. let e2' = fExp e2 in
  4759. if e1' != e1 || e2' != e2 then CaseRange (e1', e2', l) else lb
  4760. | lb -> lb
  4761. in
  4762. mapNoCopy fLabel s.labels
  4763. in
  4764. if labels' != s.labels then s.labels <- labels';
  4765. s
  4766. and visitCilBlock (vis: cilVisitor) (b: block) : block =
  4767. doVisit vis (vis#vblock b) childrenBlock b
  4768. and childrenBlock (vis: cilVisitor) (b: block) : block =
  4769. let fStmt s = visitCilStmt vis s in
  4770. let stmts' = mapNoCopy fStmt b.bstmts in
  4771. if stmts' != b.bstmts then { battrs = b.battrs; bstmts = stmts'} else b
  4772. and visitCilType (vis : cilVisitor) (t : typ) : typ =
  4773. doVisit vis (vis#vtype t) childrenType t
  4774. and childrenType (vis : cilVisitor) (t : typ) : typ =
  4775. (* look for types referred to inside t's definition *)
  4776. let fTyp t = visitCilType vis t in
  4777. let fAttr a = visitCilAttributes vis a in
  4778. match t with
  4779. TPtr(t1, a) ->
  4780. let t1' = fTyp t1 in
  4781. let a' = fAttr a in
  4782. if t1' != t1 || a' != a then TPtr(t1', a') else t
  4783. | TArray(t1, None, a) ->
  4784. let t1' = fTyp t1 in
  4785. let a' = fAttr a in
  4786. if t1' != t1 || a' != a then TArray(t1', None, a') else t
  4787. | TArray(t1, Some e, a) ->
  4788. let t1' = fTyp t1 in
  4789. let e' = visitCilExpr vis e in
  4790. let a' = fAttr a in
  4791. if t1' != t1 || e' != e || a' != a then TArray(t1', Some e', a') else t
  4792. (* DON'T recurse into the compinfo, this is done in visitCilGlobal.
  4793. User can iterate over cinfo.cfields manually, if desired.*)
  4794. | TComp(cinfo, a) ->
  4795. let a' = fAttr a in
  4796. if a != a' then TComp(cinfo, a') else t
  4797. | TFun(rettype, args, isva, a) ->
  4798. let rettype' = fTyp rettype in
  4799. (* iterate over formals, as variable declarations *)
  4800. let argslist = argsToList args in
  4801. let visitArg ((an,at,aa) as arg) =
  4802. let at' = fTyp at in
  4803. let aa' = fAttr aa in
  4804. if at' != at || aa' != aa then (an,at',aa') else arg
  4805. in
  4806. let argslist' = mapNoCopy visitArg argslist in
  4807. let a' = fAttr a in
  4808. if rettype' != rettype || argslist' != argslist || a' != a then
  4809. let args' = if argslist' == argslist then args else Some argslist' in
  4810. TFun(rettype', args', isva, a') else t
  4811. | TNamed(t1, a) -> (* Do not go into the type. Will do it at the time of
  4812. * GType *)
  4813. let a' = fAttr a in
  4814. if a' != a then TNamed (t1, a') else t
  4815. | _ -> (* other types (TVoid, TInt, TFloat, TEnum, and TBuiltin_va_list)
  4816. don't contain nested types, but they do have attributes. *)
  4817. let a = typeAttrs t in
  4818. let a' = fAttr a in
  4819. if a' != a then setTypeAttrs t a' else t
  4820. (* for declarations, we visit the types inside; but for uses, *)
  4821. (* we just visit the varinfo node *)
  4822. and visitCilVarDecl (vis : cilVisitor) (v : varinfo) : varinfo =
  4823. doVisit vis (vis#vvdec v) childrenVarDecl v
  4824. and childrenVarDecl (vis : cilVisitor) (v : varinfo) : varinfo =
  4825. v.vtype <- visitCilType vis v.vtype;
  4826. v.vattr <- visitCilAttributes vis v.vattr;
  4827. (match v.vinit.init with
  4828. None -> ()
  4829. | Some i -> let i' = visitCilInit vis v NoOffset i in
  4830. if i' != i then v.vinit.init <- Some i');
  4831. v
  4832. and visitCilAttributes (vis: cilVisitor) (al: attribute list) : attribute list=
  4833. let al' =
  4834. mapNoCopyList (fun x -> doVisitList vis (vis#vattr x) childrenAttribute x) al in
  4835. if al' != al then
  4836. (* Must re-sort *)
  4837. addAttributes al' []
  4838. else
  4839. al
  4840. and childrenAttribute (vis: cilVisitor) (a: attribute) : attribute =
  4841. let fAttrP a = visitCilAttrParams vis a in
  4842. match a with
  4843. Attr (n, args) ->
  4844. let args' = mapNoCopy fAttrP args in
  4845. if args' != args then Attr(n, args') else a
  4846. and visitCilAttrParams (vis: cilVisitor) (a: attrparam) : attrparam =
  4847. doVisit vis (vis#vattrparam a) childrenAttrparam a
  4848. and childrenAttrparam (vis: cilVisitor) (aa: attrparam) : attrparam =
  4849. let fTyp t = visitCilType vis t in
  4850. let fAttrP a = visitCilAttrParams vis a in
  4851. match aa with
  4852. AInt _ | AStr _ -> aa
  4853. | ACons(n, args) ->
  4854. let args' = mapNoCopy fAttrP args in
  4855. if args' != args then ACons(n, args') else aa
  4856. | ASizeOf t ->
  4857. let t' = fTyp t in
  4858. if t' != t then ASizeOf t' else aa
  4859. | ASizeOfE e ->
  4860. let e' = fAttrP e in
  4861. if e' != e then ASizeOfE e' else aa
  4862. | AAlignOf t ->
  4863. let t' = fTyp t in
  4864. if t' != t then AAlignOf t' else aa
  4865. | AAlignOfE e ->
  4866. let e' = fAttrP e in
  4867. if e' != e then AAlignOfE e' else aa
  4868. | ASizeOfS _ | AAlignOfS _ ->
  4869. ignore (warn "Visitor inside of a type signature.");
  4870. aa
  4871. | AUnOp (uo, e1) ->
  4872. let e1' = fAttrP e1 in
  4873. if e1' != e1 then AUnOp (uo, e1') else aa
  4874. | ABinOp (bo, e1, e2) ->
  4875. let e1' = fAttrP e1 in
  4876. let e2' = fAttrP e2 in
  4877. if e1' != e1 || e2' != e2 then ABinOp (bo, e1', e2') else aa
  4878. | ADot (ap, s) ->
  4879. let ap' = fAttrP ap in
  4880. if ap' != ap then ADot (ap', s) else aa
  4881. | AStar ap ->
  4882. let ap' = fAttrP ap in
  4883. if ap' != ap then AStar ap' else aa
  4884. | AAddrOf ap ->
  4885. let ap' = fAttrP ap in
  4886. if ap' != ap then AAddrOf ap' else aa
  4887. | AIndex (e1, e2) ->
  4888. let e1' = fAttrP e1 in
  4889. let e2' = fAttrP e2 in
  4890. if e1' != e1 || e2' != e2 then AIndex (e1', e2') else aa
  4891. | AQuestion (e1, e2, e3) ->
  4892. let e1' = fAttrP e1 in
  4893. let e2' = fAttrP e2 in
  4894. let e3' = fAttrP e3 in
  4895. if e1' != e1 || e2' != e2 || e3' != e3
  4896. then AQuestion (e1', e2', e3') else aa
  4897. let rec visitCilFunction (vis : cilVisitor) (f : fundec) : fundec =
  4898. if debugVisit then ignore (E.log "Visiting function %s\n" f.svar.vname);
  4899. assertEmptyQueue vis;
  4900. let f = doVisit vis (vis#vfunc f) childrenFunction f in
  4901. let toPrepend = vis#unqueueInstr () in
  4902. if toPrepend <> [] then
  4903. f.sbody.bstmts <- mkStmt (Instr toPrepend) :: f.sbody.bstmts;
  4904. f
  4905. and childrenFunction (vis : cilVisitor) (f : fundec) : fundec =
  4906. let visitVarDecl vd = visitCilVarDecl vis vd in
  4907. f.svar <- visitCilVarDecl vis f.svar; (* hit the function name *)
  4908. (* visit local declarations *)
  4909. f.slocals <- mapNoCopy visitVarDecl f.slocals;
  4910. (* visit the formals *)
  4911. let newformals = mapNoCopy visitVarDecl f.sformals in
  4912. (* Make sure the type reflects the formals *)
  4913. setFormals f newformals;
  4914. (* Remember any new instructions that were generated while visiting
  4915. variable declarations. *)
  4916. let toPrepend = vis#unqueueInstr () in
  4917. f.sbody <- visitCilBlock vis f.sbody; (* visit the body *)
  4918. if toPrepend <> [] then
  4919. f.sbody.bstmts <- mkStmt (Instr toPrepend) :: f.sbody.bstmts;
  4920. f
  4921. let rec visitCilGlobal (vis: cilVisitor) (g: global) : global list =
  4922. (*(trace "visit" (dprintf "visitCilGlobal\n"));*)
  4923. let oldloc = !currentLoc in
  4924. currentLoc := (get_globalLoc g) ;
  4925. currentGlobal := g;
  4926. let res = doVisitList vis (vis#vglob g) childrenGlobal g in
  4927. currentLoc := oldloc;
  4928. res
  4929. and childrenGlobal (vis: cilVisitor) (g: global) : global =
  4930. match g with
  4931. | GFun (f, l) ->
  4932. let f' = visitCilFunction vis f in
  4933. if f' != f then GFun (f', l) else g
  4934. | GType(t, l) ->
  4935. t.ttype <- visitCilType vis t.ttype;
  4936. g
  4937. | GEnumTagDecl _ | GCompTagDecl _ -> g (* Nothing to visit *)
  4938. | GEnumTag (enum, _) ->
  4939. (* (trace "visit" (dprintf "visiting global enum %s\n" enum.ename)); *)
  4940. (* Do the values and attributes of the enumerated items *)
  4941. let itemVisit (name, exp, loc) = (name, visitCilExpr vis exp, loc) in
  4942. enum.eitems <- mapNoCopy itemVisit enum.eitems;
  4943. enum.eattr <- visitCilAttributes vis enum.eattr;
  4944. g
  4945. | GCompTag (comp, _) ->
  4946. (* (trace "visit" (dprintf "visiting global comp %s\n" comp.cname)); *)
  4947. (* Do the types and attirbutes of the fields *)
  4948. let fieldVisit = fun fi ->
  4949. fi.ftype <- visitCilType vis fi.ftype;
  4950. fi.fattr <- visitCilAttributes vis fi.fattr
  4951. in
  4952. List.iter fieldVisit comp.cfields;
  4953. comp.cattr <- visitCilAttributes vis comp.cattr;
  4954. g
  4955. | GVarDecl(v, l) ->
  4956. let v' = visitCilVarDecl vis v in
  4957. if v' != v then GVarDecl (v', l) else g
  4958. | GVar (v, inito, l) ->
  4959. let v' = visitCilVarDecl vis v in
  4960. if v' != v then GVar (v', inito, l) else g
  4961. | GPragma (a, l) -> begin
  4962. match visitCilAttributes vis [a] with
  4963. [a'] -> if a' != a then GPragma (a', l) else g
  4964. | _ -> E.s (E.unimp "visitCilAttributes returns more than one attribute")
  4965. end
  4966. | _ -> g
  4967. (** A visitor that does constant folding. If "machdep" is true then we do
  4968. * machine dependent simplification (e.g., sizeof) *)
  4969. class constFoldVisitorClass (machdep: bool) : cilVisitor = object
  4970. inherit nopCilVisitor
  4971. method vinst i =
  4972. match i with
  4973. (* Skip two functions to which we add Sizeof to the type arguments.
  4974. See the comments for these above. *)
  4975. Call(_,(Lval (Var vi,NoOffset)),_,_)
  4976. when ((vi.vname = "__builtin_va_arg")
  4977. || (vi.vname = "__builtin_types_compatible_p")) ->
  4978. SkipChildren
  4979. | _ -> DoChildren
  4980. method vexpr (e: exp) =
  4981. (* Do it bottom up *)
  4982. ChangeDoChildrenPost (e, constFold machdep)
  4983. end
  4984. let constFoldVisitor (machdep: bool) = new constFoldVisitorClass machdep
  4985. (* Iterate over all globals, including the global initializer *)
  4986. let iterGlobals (fl: file)
  4987. (doone: global -> unit) : unit =
  4988. let doone' g =
  4989. currentLoc := get_globalLoc g;
  4990. doone g
  4991. in
  4992. List.iter doone' fl.globals;
  4993. (match fl.globinit with
  4994. None -> ()
  4995. | Some g -> doone' (GFun(g, locUnknown)))
  4996. (* Fold over all globals, including the global initializer *)
  4997. let foldGlobals (fl: file)
  4998. (doone: 'a -> global -> 'a)
  4999. (acc: 'a) : 'a =
  5000. let doone' acc g =
  5001. currentLoc := get_globalLoc g;
  5002. doone acc g
  5003. in
  5004. let acc' = List.fold_left doone' acc fl.globals in
  5005. (match fl.globinit with
  5006. None -> acc'
  5007. | Some g -> doone' acc' (GFun(g, locUnknown)))
  5008. (** Find a function or function prototype with the given name in the file.
  5009. * If it does not exist, create a prototype with the given type, and return
  5010. * the new varinfo. This is useful when you need to call a libc function
  5011. * whose prototype may or may not already exist in the file.
  5012. *
  5013. * Because the new prototype is added to the start of the file, you shouldn't
  5014. * refer to any struct or union types in the function type.*)
  5015. let findOrCreateFunc (f:file) (name:string) (t:typ) : varinfo =
  5016. let rec search glist =
  5017. match glist with
  5018. GVarDecl(vi,_) :: rest | GFun ({svar = vi},_) :: rest when vi.vname = name ->
  5019. if not (isFunctionType vi.vtype) then
  5020. E.s (error ("findOrCreateFunc: can't create %s because another "
  5021. ^^"global exists with that name.") name);
  5022. vi
  5023. | _ :: rest -> search rest (* tail recursive *)
  5024. | [] -> (*not found, so create one *)
  5025. let t' = unrollTypeDeep t in
  5026. let new_decl = makeGlobalVar name t' in
  5027. f.globals <- GVarDecl(new_decl, locUnknown) :: f.globals;
  5028. new_decl
  5029. in
  5030. search f.globals
  5031. (* A visitor for the whole file that does not change the globals *)
  5032. let visitCilFileSameGlobals (vis : cilVisitor) (f : file) : unit =
  5033. let fGlob g = visitCilGlobal vis g in
  5034. iterGlobals f (fun g ->
  5035. match fGlob g with
  5036. [g'] when g' == g || Util.equals g' g -> () (* Try to do the pointer check first *)
  5037. | gl ->
  5038. ignore (E.log "You used visitCilFilSameGlobals but the global got changed:\n %a\nchanged to %a\n" d_global g (docList ~sep:line (d_global ())) gl);
  5039. ())
  5040. (* Be careful with visiting the whole file because it might be huge. *)
  5041. let visitCilFile (vis : cilVisitor) (f : file) : unit =
  5042. let fGlob g = visitCilGlobal vis g in
  5043. (* Scan the globals. Make sure this is tail recursive. *)
  5044. let rec loop (acc: global list) = function
  5045. [] -> f.globals <- List.rev acc
  5046. | g :: restg ->
  5047. loop ((List.rev (fGlob g)) @ acc) restg
  5048. in
  5049. loop [] f.globals;
  5050. (* the global initializer *)
  5051. (match f.globinit with
  5052. None -> ()
  5053. | Some g -> f.globinit <- Some (visitCilFunction vis g))
  5054. (** Create or fetch the global initializer. Tries to put a call to the
  5055. * function with the main_name into it *)
  5056. let getGlobInit ?(main_name="main") (fl: file) =
  5057. match fl.globinit with
  5058. Some f -> f
  5059. | None -> begin
  5060. (* Sadly, we cannot use the Filename library because it does not like
  5061. * function names with multiple . in them *)
  5062. let f =
  5063. let len = String.length fl.fileName in
  5064. (* Find the last path separator and record the first . that we see,
  5065. * going backwards *)
  5066. let lastDot = ref len in
  5067. let rec findLastPathSep i =
  5068. if i < 0 then -1 else
  5069. let c = String.get fl.fileName i in
  5070. if c = '/' || c = '\\' then i
  5071. else begin
  5072. if c = '.' && !lastDot = len then
  5073. lastDot := i;
  5074. findLastPathSep (i - 1)
  5075. end
  5076. in
  5077. let lastPathSep = findLastPathSep (len - 1) in
  5078. let basenoext =
  5079. String.sub fl.fileName (lastPathSep + 1) (!lastDot - lastPathSep - 1)
  5080. in
  5081. emptyFunction
  5082. (makeValidSymbolName ("__globinit_" ^ basenoext))
  5083. in
  5084. fl.globinit <- Some f;
  5085. (* Now try to add a call to the global initialized at the beginning of
  5086. * main *)
  5087. let inserted = ref false in
  5088. List.iter
  5089. (function
  5090. GFun(m, lm) when m.svar.vname = main_name ->
  5091. (* Prepend a prototype to the global initializer *)
  5092. fl.globals <- GVarDecl (f.svar, lm) :: fl.globals;
  5093. m.sbody.bstmts <-
  5094. compactStmts (mkStmt (Instr [Call(None,
  5095. Lval(var f.svar),
  5096. [], locUnknown)])
  5097. :: m.sbody.bstmts);
  5098. inserted := true;
  5099. if !E.verboseFlag then
  5100. ignore (E.log "Inserted the globinit\n");
  5101. fl.globinitcalled <- true;
  5102. | _ -> ())
  5103. fl.globals;
  5104. if not !inserted then
  5105. ignore (E.warn "Cannot find %s to add global initializer %s"
  5106. main_name f.svar.vname);
  5107. f
  5108. end
  5109. (* Fold over all globals, including the global initializer *)
  5110. let mapGlobals (fl: file)
  5111. (doone: global -> global) : unit =
  5112. fl.globals <- Util.list_map doone fl.globals;
  5113. (match fl.globinit with
  5114. None -> ()
  5115. | Some g -> begin
  5116. match doone (GFun(g, locUnknown)) with
  5117. GFun(g', _) -> fl.globinit <- Some g'
  5118. | _ -> E.s (E.bug "mapGlobals: globinit is not a function")
  5119. end)
  5120. let dumpFile (pp: cilPrinter) (out : out_channel) (outfile: string) file =
  5121. printDepth := 99999; (* We don't want ... in the output *)
  5122. Pretty.fastMode := true;
  5123. if !E.verboseFlag then
  5124. ignore (E.log "printing file %s\n" outfile);
  5125. let print x = fprint out 78 x in
  5126. print (text ("/* Generated by CIL v. " ^ cilVersion ^ " */\n" ^
  5127. (* sm: I want to easily tell whether the generated output
  5128. * is with print_CIL_Input or not *)
  5129. "/* print_CIL_Input is " ^ (if !print_CIL_Input then "true" else "false") ^ " */\n\n"));
  5130. iterGlobals file (fun g -> dumpGlobal pp out g);
  5131. (* sm: we have to flush the output channel; if we don't then under *)
  5132. (* some circumstances (I haven't figure out exactly when, but it happens *)
  5133. (* more often with big inputs), we get a truncated output file *)
  5134. flush out
  5135. (******************
  5136. ******************
  5137. ******************)
  5138. (* Convert an expression into an attribute, if possible. Otherwise raise
  5139. * NotAnAttrParam *)
  5140. exception NotAnAttrParam of exp
  5141. let rec expToAttrParam (e: exp) : attrparam =
  5142. match e with
  5143. Const(CInt64(i,k,_)) ->
  5144. let i' = mkCilint k i in
  5145. if not (is_int_cilint i') then
  5146. raise (NotAnAttrParam e);
  5147. AInt (int_of_cilint i')
  5148. | Lval (Var v, NoOffset) -> ACons(v.vname, [])
  5149. | SizeOf t -> ASizeOf t
  5150. | SizeOfE e' -> ASizeOfE (expToAttrParam e')
  5151. | UnOp(uo, e', _) -> AUnOp (uo, expToAttrParam e')
  5152. | BinOp(bo, e1',e2', _) -> ABinOp (bo, expToAttrParam e1',
  5153. expToAttrParam e2')
  5154. | _ -> raise (NotAnAttrParam e)
  5155. (******************** OPTIMIZATIONS *****)
  5156. let rec peepHole1 (* Process one instruction and possibly replace it *)
  5157. (doone: instr -> instr list option)
  5158. (* Scan a block and recurse inside nested blocks *)
  5159. (ss: stmt list) : unit =
  5160. let rec doInstrList (il: instr list) : instr list =
  5161. match il with
  5162. [] -> []
  5163. | i :: rest -> begin
  5164. match doone i with
  5165. None -> i :: doInstrList rest
  5166. | Some sl -> doInstrList (sl @ rest)
  5167. end
  5168. in
  5169. List.iter
  5170. (fun s ->
  5171. match s.skind with
  5172. Instr il -> s.skind <- Instr (doInstrList il)
  5173. | If (e, tb, eb, _) ->
  5174. peepHole1 doone tb.bstmts;
  5175. peepHole1 doone eb.bstmts
  5176. | Switch (e, b, _, _) -> peepHole1 doone b.bstmts
  5177. | Loop (b, l, _, _) -> peepHole1 doone b.bstmts
  5178. | Block b -> peepHole1 doone b.bstmts
  5179. | TryFinally (b, h, l) ->
  5180. peepHole1 doone b.bstmts;
  5181. peepHole1 doone h.bstmts
  5182. | TryExcept (b, (il, e), h, l) ->
  5183. peepHole1 doone b.bstmts;
  5184. peepHole1 doone h.bstmts;
  5185. s.skind <- TryExcept(b, (doInstrList il, e), h, l);
  5186. | Return _ | Goto _ | ComputedGoto _ | Break _ | Continue _ -> ())
  5187. ss
  5188. let rec peepHole2 (* Process two instructions and possibly replace them both *)
  5189. (dotwo: instr * instr -> instr list option)
  5190. (ss: stmt list) : unit =
  5191. let rec doInstrList (il: instr list) : instr list =
  5192. match il with
  5193. [] -> []
  5194. | [i] -> [i]
  5195. | (i1 :: ((i2 :: rest) as rest2)) ->
  5196. begin
  5197. match dotwo (i1,i2) with
  5198. None -> i1 :: doInstrList rest2
  5199. | Some sl -> doInstrList (sl @ rest)
  5200. end
  5201. in
  5202. List.iter
  5203. (fun s ->
  5204. match s.skind with
  5205. Instr il -> s.skind <- Instr (doInstrList il)
  5206. | If (e, tb, eb, _) ->
  5207. peepHole2 dotwo tb.bstmts;
  5208. peepHole2 dotwo eb.bstmts
  5209. | Switch (e, b, _, _) -> peepHole2 dotwo b.bstmts
  5210. | Loop (b, l, _, _) -> peepHole2 dotwo b.bstmts
  5211. | Block b -> peepHole2 dotwo b.bstmts
  5212. | TryFinally (b, h, l) -> peepHole2 dotwo b.bstmts;
  5213. peepHole2 dotwo h.bstmts
  5214. | TryExcept (b, (il, e), h, l) ->
  5215. peepHole2 dotwo b.bstmts;
  5216. peepHole2 dotwo h.bstmts;
  5217. s.skind <- TryExcept (b, (doInstrList il, e), h, l)
  5218. | Return _ | Goto _ | ComputedGoto _ | Break _ | Continue _ -> ())
  5219. ss
  5220. (*** Type signatures ***)
  5221. (* Helper class for typeSig: replace any types in attributes with typsigs *)
  5222. class typeSigVisitor(typeSigConverter: typ->typsig) = object
  5223. inherit nopCilVisitor
  5224. method vattrparam ap =
  5225. match ap with
  5226. | ASizeOf t -> ChangeTo (ASizeOfS (typeSigConverter t))
  5227. | AAlignOf t -> ChangeTo (AAlignOfS (typeSigConverter t))
  5228. | _ -> DoChildren
  5229. end
  5230. let typeSigAddAttrs a0 t =
  5231. if a0 == [] then t else
  5232. match t with
  5233. TSBase t -> TSBase (typeAddAttributes a0 t)
  5234. | TSPtr (ts, a) -> TSPtr (ts, addAttributes a0 a)
  5235. | TSArray (ts, l, a) -> TSArray(ts, l, addAttributes a0 a)
  5236. | TSComp (iss, n, a) -> TSComp (iss, n, addAttributes a0 a)
  5237. | TSEnum (n, a) -> TSEnum (n, addAttributes a0 a)
  5238. | TSFun(ts, tsargs, isva, a) -> TSFun(ts, tsargs, isva, addAttributes a0 a)
  5239. (* Compute a type signature.
  5240. Use ~ignoreSign:true to convert all signed integer types to unsigned,
  5241. so that signed and unsigned will compare the same. *)
  5242. let rec typeSigWithAttrs ?(ignoreSign=false) doattr t =
  5243. let typeSig = typeSigWithAttrs ~ignoreSign doattr in
  5244. let attrVisitor = new typeSigVisitor typeSig in
  5245. let doattr al = visitCilAttributes attrVisitor (doattr al) in
  5246. match t with
  5247. | TInt (ik, al) ->
  5248. let ik' =
  5249. if ignoreSign then unsignedVersionOf ik else ik
  5250. in
  5251. TSBase (TInt (ik', doattr al))
  5252. | TFloat (fk, al) -> TSBase (TFloat (fk, doattr al))
  5253. | TVoid al -> TSBase (TVoid (doattr al))
  5254. | TEnum (enum, a) -> TSEnum (enum.ename, doattr a)
  5255. | TPtr (t, a) -> TSPtr (typeSig t, doattr a)
  5256. | TArray (t,l,a) -> (* We do not want fancy expressions in array lengths.
  5257. * So constant fold the lengths *)
  5258. let l' =
  5259. match l with
  5260. Some l -> begin
  5261. match constFold true l with
  5262. Const(CInt64(i, _, _)) -> Some i
  5263. | e -> E.s (E.bug "Invalid length in array type: %a\n"
  5264. (!pd_exp) e)
  5265. end
  5266. | None -> None
  5267. in
  5268. TSArray(typeSig t, l', doattr a)
  5269. | TComp (comp, a) ->
  5270. TSComp (comp.cstruct, comp.cname, doattr (addAttributes comp.cattr a))
  5271. | TFun(rt,args,isva,a) ->
  5272. TSFun(typeSig rt, (Util.list_map_opt (fun (_, atype, _) -> (typeSig atype)) args), isva, doattr a)
  5273. | TNamed(t, a) -> typeSigAddAttrs (doattr a) (typeSig t.ttype)
  5274. | TBuiltin_va_list al -> TSBase (TBuiltin_va_list (doattr al))
  5275. let typeSig t =
  5276. typeSigWithAttrs (fun al -> al) t
  5277. let _ = pTypeSig := typeSig
  5278. (* Remove the attribute from the top-level of the type signature *)
  5279. let setTypeSigAttrs (a: attribute list) = function
  5280. TSBase t -> TSBase (setTypeAttrs t a)
  5281. | TSPtr (ts, _) -> TSPtr (ts, a)
  5282. | TSArray (ts, l, _) -> TSArray(ts, l, a)
  5283. | TSComp (iss, n, _) -> TSComp (iss, n, a)
  5284. | TSEnum (n, _) -> TSEnum (n, a)
  5285. | TSFun (ts, tsargs, isva, _) -> TSFun (ts, tsargs, isva, a)
  5286. let typeSigAttrs = function
  5287. TSBase t -> typeAttrs t
  5288. | TSPtr (ts, a) -> a
  5289. | TSArray (ts, l, a) -> a
  5290. | TSComp (iss, n, a) -> a
  5291. | TSEnum (n, a) -> a
  5292. | TSFun (ts, tsargs, isva, a) -> a
  5293. let dExp: doc -> exp =
  5294. fun d -> Const(CStr(sprint !lineLength d))
  5295. let dInstr: doc -> location -> instr =
  5296. fun d l -> Asm([], [sprint !lineLength d], [], [], [], l)
  5297. let dGlobal: doc -> location -> global =
  5298. fun d l -> GAsm(sprint !lineLength d, l)
  5299. (* Make an AddrOf. Given an lval of type T will give back an expression of
  5300. * type ptr(T) *)
  5301. let mkAddrOf ((b, off) as lval) : exp =
  5302. (* Never take the address of a register variable *)
  5303. (match lval with
  5304. Var vi, off when vi.vstorage = Register -> vi.vstorage <- NoStorage
  5305. | _ -> ());
  5306. match lval with
  5307. Mem e, NoOffset -> e
  5308. (* Don't do this:
  5309. | b, Index(z, NoOffset) when isZero z -> StartOf (b, NoOffset)
  5310. &a[0] is not the same as a, e.g. within typeof and sizeof.
  5311. Code must be able to handle the results without this anyway... *)
  5312. | _ -> AddrOf lval
  5313. let mkAddrOrStartOf (lv: lval) : exp =
  5314. match unrollType (typeOfLval lv) with
  5315. TArray _ -> StartOf lv
  5316. | _ -> mkAddrOf lv
  5317. (* Make a Mem, while optimizing AddrOf. The type of the addr must be
  5318. * TPtr(t) and the type of the resulting lval is t. Note that in CIL the
  5319. * implicit conversion between a function and a pointer to a function does
  5320. * not apply. You must do the conversion yourself using AddrOf *)
  5321. let mkMem ~(addr: exp) ~(off: offset) : lval =
  5322. let res =
  5323. match addr, off with
  5324. AddrOf lv, _ -> addOffsetLval off lv
  5325. | StartOf lv, _ -> (* Must be an array *)
  5326. addOffsetLval (Index(zero, off)) lv
  5327. | _, _ -> Mem addr, off
  5328. in
  5329. (* ignore (E.log "memof : %a:%a\nresult = %a\n"
  5330. d_plainexp addr d_plainoffset off d_plainexp res); *)
  5331. res
  5332. let splitFunctionType (ftype: typ)
  5333. : typ * (string * typ * attributes) list option * bool * attributes =
  5334. match unrollType ftype with
  5335. TFun (rt, args, isva, a) -> rt, args, isva, a
  5336. | _ -> E.s (bug "splitFunctionType invoked on a non function type %a"
  5337. d_type ftype)
  5338. let splitFunctionTypeVI (fvi: varinfo)
  5339. : typ * (string * typ * attributes) list option * bool * attributes =
  5340. match unrollType fvi.vtype with
  5341. TFun (rt, args, isva, a) -> rt, args, isva, a
  5342. | _ -> E.s (bug "Function %s invoked on a non function type" fvi.vname)
  5343. let isArrayType t =
  5344. match unrollType t with
  5345. TArray _ -> true
  5346. | _ -> false
  5347. let rec isConstant = function
  5348. | Const _ -> true
  5349. | UnOp (_, e, _) -> isConstant e
  5350. | BinOp (_, e1, e2, _) -> isConstant e1 && isConstant e2
  5351. | Question (e1, e2, e3, _) -> isConstant e1 && isConstant e2 && isConstant e3
  5352. | Lval (Var vi, NoOffset) ->
  5353. (vi.vglob && isArrayType vi.vtype || isFunctionType vi.vtype)
  5354. | Lval _ -> false
  5355. | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ -> true
  5356. | CastE (_, e) -> isConstant e
  5357. | AddrOf (Var vi, off) | StartOf (Var vi, off)
  5358. -> vi.vglob && isConstantOffset off
  5359. | AddrOf (Mem e, off) | StartOf(Mem e, off)
  5360. -> isConstant e && isConstantOffset off
  5361. | AddrOfLabel _ -> true
  5362. and isConstantOffset = function
  5363. NoOffset -> true
  5364. | Field(fi, off) -> isConstantOffset off
  5365. | Index(e, off) -> isConstant e && isConstantOffset off
  5366. let getCompField (cinfo:compinfo) (fieldName:string) : fieldinfo =
  5367. (List.find (fun fi -> fi.fname = fieldName) cinfo.cfields)
  5368. let rec mkCastT ~(e: exp) ~(oldt: typ) ~(newt: typ) =
  5369. (* Do not remove old casts because they are conversions !!! *)
  5370. if Util.equals (typeSig oldt) (typeSig newt) then begin
  5371. e
  5372. end else begin
  5373. (* Watch out for constants *)
  5374. match newt, e with
  5375. (* Casts to _Bool are special: they behave like "!= 0" ISO C99 6.3.1.2 *)
  5376. TInt(IBool, []), Const(CInt64(i, _, _)) ->
  5377. let v = if i = Int64.zero then Int64.zero else Int64.one in
  5378. Const (CInt64(v, IBool, None))
  5379. | TInt(newik, []), Const(CInt64(i, _, _)) -> kinteger64 newik i
  5380. | _ -> CastE(newt,e)
  5381. end
  5382. let mkCast ~(e: exp) ~(newt: typ) =
  5383. mkCastT e (typeOf e) newt
  5384. type existsAction =
  5385. ExistsTrue (* We have found it *)
  5386. | ExistsFalse (* Stop processing this branch *)
  5387. | ExistsMaybe (* This node is not what we are
  5388. * looking for but maybe its
  5389. * successors are *)
  5390. let existsType (f: typ -> existsAction) (t: typ) : bool =
  5391. let memo : (int, unit) H.t = H.create 17 in (* Memo table *)
  5392. let rec loop t =
  5393. match f t with
  5394. ExistsTrue -> true
  5395. | ExistsFalse -> false
  5396. | ExistsMaybe ->
  5397. (match t with
  5398. TNamed (t', _) -> loop t'.ttype
  5399. | TComp (c, _) -> loopComp c
  5400. | TArray (t', _, _) -> loop t'
  5401. | TPtr (t', _) -> loop t'
  5402. | TFun (rt, args, _, _) ->
  5403. (loop rt || List.exists (fun (_, at, _) -> loop at)
  5404. (argsToList args))
  5405. | _ -> false)
  5406. and loopComp c =
  5407. if H.mem memo c.ckey then
  5408. (* We are looping, the answer must be false *)
  5409. false
  5410. else begin
  5411. H.add memo c.ckey ();
  5412. List.exists (fun f -> loop f.ftype) c.cfields
  5413. end
  5414. in
  5415. loop t
  5416. (* Try to do an increment, with constant folding *)
  5417. let increm (e: exp) (i: int) =
  5418. let et = typeOf e in
  5419. let bop = if isPointerType et then PlusPI else PlusA in
  5420. constFold false (BinOp(bop, e, integer i, et))
  5421. exception LenOfArray
  5422. let lenOfArray (eo: exp option) : int =
  5423. match eo with
  5424. None -> raise LenOfArray
  5425. | Some e -> begin
  5426. match constFold true e with
  5427. | Const(CInt64(ni, _, _)) when ni >= Int64.zero ->
  5428. i64_to_int ni
  5429. | e -> raise LenOfArray
  5430. end
  5431. (*** Make an initializer for zeroe-ing a data type ***)
  5432. let rec makeZeroInit (t: typ) : init =
  5433. match unrollType t with
  5434. TInt (ik, _) -> SingleInit (Const(CInt64(Int64.zero, ik, None)))
  5435. | TFloat(fk, _) -> SingleInit(Const(CReal(0.0, fk, None)))
  5436. | TEnum (e, _) -> SingleInit (kinteger e.ekind 0)
  5437. | TComp (comp, _) as t' when comp.cstruct ->
  5438. let inits =
  5439. List.fold_right
  5440. (fun f acc ->
  5441. if f.fname <> missingFieldName then
  5442. (Field(f, NoOffset), makeZeroInit f.ftype) :: acc
  5443. else
  5444. acc)
  5445. comp.cfields []
  5446. in
  5447. CompoundInit (t', inits)
  5448. | TComp (comp, _) when not comp.cstruct ->
  5449. let fstfield, rest =
  5450. match comp.cfields with
  5451. f :: rest -> f, rest
  5452. | [] -> E.s (unimp "Cannot create init for empty union")
  5453. in
  5454. let fieldToInit =
  5455. if !msvcMode then
  5456. (* ISO C99 [6.7.8.10] says that the first field of the union
  5457. is the one we should initialize. *)
  5458. fstfield
  5459. else begin
  5460. (* gcc initializes the whole union to zero. So choose the largest
  5461. field, and set that to zero. Choose the first field if possible.
  5462. MSVC also initializes the whole union, but use the ISO behavior
  5463. for MSVC because it only allows compound initializers to refer
  5464. to the first union field. *)
  5465. let fieldSize f = try bitsSizeOf f.ftype with SizeOfError _ -> 0 in
  5466. let widestField, widestFieldWidth =
  5467. List.fold_left (fun acc thisField ->
  5468. let widestField, widestFieldWidth = acc in
  5469. let thisSize = fieldSize thisField in
  5470. if thisSize > widestFieldWidth then
  5471. thisField, thisSize
  5472. else
  5473. acc)
  5474. (fstfield, fieldSize fstfield)
  5475. rest
  5476. in
  5477. widestField
  5478. end
  5479. in
  5480. CompoundInit(t, [(Field(fieldToInit, NoOffset),
  5481. makeZeroInit fieldToInit.ftype)])
  5482. | TArray(bt, Some len, _) as t' ->
  5483. let n =
  5484. match constFold true len with
  5485. Const(CInt64(n, _, _)) -> i64_to_int n
  5486. | _ -> E.s (E.unimp "Cannot understand length of array")
  5487. in
  5488. let initbt = makeZeroInit bt in
  5489. let rec loopElems acc i =
  5490. if i < 0 then acc
  5491. else loopElems ((Index(integer i, NoOffset), initbt) :: acc) (i - 1)
  5492. in
  5493. CompoundInit(t', loopElems [] (n - 1))
  5494. | TArray (bt, None, at) as t' ->
  5495. (* Unsized array, allow it and fill it in later
  5496. * (see cabs2cil.ml, collectInitializer) *)
  5497. CompoundInit (t', [])
  5498. | TPtr _ as t ->
  5499. SingleInit(if !insertImplicitCasts then mkCast zero t else zero)
  5500. | x -> E.s (unimp "Cannot initialize type: %a" d_type x)
  5501. (** Fold over the list of initializers in a Compound (not also the nested
  5502. * ones). [doinit] is called on every present initializer, even if it is of
  5503. * compound type. The parameters of [doinit] are: the offset in the compound
  5504. * (this is [Field(f,NoOffset)] or [Index(i,NoOffset)]), the initializer
  5505. * value, expected type of the initializer value, accumulator. In the case of
  5506. * arrays there might be missing zero-initializers at the end of the list.
  5507. * These are scanned only if [implicit] is true. This is much like
  5508. * [List.fold_left] except we also pass the type of the initializer. *)
  5509. let foldLeftCompound
  5510. ~(implicit: bool)
  5511. ~(doinit: offset -> init -> typ -> 'a -> 'a)
  5512. ~(ct: typ)
  5513. ~(initl: (offset * init) list)
  5514. ~(acc: 'a) : 'a =
  5515. match unrollType ct with
  5516. TArray(bt, leno, _) -> begin
  5517. (* Scan the existing initializer *)
  5518. let part =
  5519. List.fold_left (fun acc (o, i) -> doinit o i bt acc) acc initl in
  5520. (* See how many more we have to do *)
  5521. match leno with
  5522. Some lene when implicit -> begin
  5523. match constFold true lene with
  5524. Const(CInt64(i, _, _)) ->
  5525. let len_array = i64_to_int i in
  5526. let len_init = List.length initl in
  5527. if len_array > len_init then
  5528. let zi = makeZeroInit bt in
  5529. let rec loop acc i =
  5530. if i >= len_array then acc
  5531. else
  5532. loop (doinit (Index(integer i, NoOffset)) zi bt acc)
  5533. (i + 1)
  5534. in
  5535. loop part (len_init + 1)
  5536. else
  5537. part
  5538. | _ -> E.s (unimp "foldLeftCompound: array with initializer and non-constant length\n")
  5539. end
  5540. | _ when not implicit -> part
  5541. | _ -> E.s (unimp "foldLeftCompound: TArray with initializer and no length")
  5542. end
  5543. | TComp (comp, _) ->
  5544. let getTypeOffset = function
  5545. Field(f, NoOffset) -> f.ftype
  5546. | _ -> E.s (bug "foldLeftCompound: malformed initializer")
  5547. in
  5548. List.fold_left
  5549. (fun acc (o, i) -> doinit o i (getTypeOffset o) acc) acc initl
  5550. | _ -> E.s (E.unimp "Type of Compound is not array or struct or union")
  5551. let rec isCompleteType t =
  5552. match unrollType t with
  5553. | TArray(t, None, _) -> false
  5554. | TArray(t, Some z, _) when isZero z -> false
  5555. | TComp (comp, _) -> (* Struct or union *)
  5556. List.for_all (fun fi -> isCompleteType fi.ftype) comp.cfields
  5557. | _ -> true
  5558. module A = Alpha
  5559. (** Uniquefy the variable names *)
  5560. let uniqueVarNames (f: file) : unit =
  5561. (* Setup the alpha conversion table for globals *)
  5562. let gAlphaTable: (string,
  5563. location A.alphaTableData ref) H.t = H.create 113 in
  5564. (* Keep also track of the global names that we have used. Map them to the
  5565. * variable ID. We do this only to check that we do not have two globals
  5566. * with the same name. *)
  5567. let globalNames: (string, int) H.t = H.create 113 in
  5568. (* Scan the file and add the global names to the table *)
  5569. iterGlobals f
  5570. (function
  5571. GVarDecl(vi, l)
  5572. | GVar(vi, _, l)
  5573. | GFun({svar = vi}, l) ->
  5574. (* See if we have used this name already for something else *)
  5575. (try
  5576. let oldid = H.find globalNames vi.vname in
  5577. if oldid <> vi.vid then
  5578. ignore (warn "The name %s is used for two distinct globals"
  5579. vi.vname);
  5580. (* Here if we have used this name already. Go ahead *)
  5581. ()
  5582. with Not_found -> begin
  5583. (* Here if this is the first time we define a name *)
  5584. H.add globalNames vi.vname vi.vid;
  5585. (* And register it *)
  5586. A.registerAlphaName gAlphaTable None vi.vname !currentLoc;
  5587. ()
  5588. end)
  5589. | _ -> ());
  5590. (* Now we must scan the function bodies and rename the locals *)
  5591. iterGlobals f
  5592. (function
  5593. GFun(fdec, l) -> begin
  5594. currentLoc := l;
  5595. (* Setup an undo list to be able to revert the changes to the
  5596. * global alpha table *)
  5597. let undolist = ref [] in
  5598. (* Process one local variable *)
  5599. let processLocal (v: varinfo) =
  5600. let newname, oldloc =
  5601. A.newAlphaName gAlphaTable (Some undolist) v.vname
  5602. !currentLoc
  5603. in
  5604. if false && newname <> v.vname then (* Disable this warning *)
  5605. ignore (warn "uniqueVarNames: Changing the name of local %s in %s to %s (due to duplicate at %a)"
  5606. v.vname fdec.svar.vname newname d_loc oldloc);
  5607. v.vname <- newname
  5608. in
  5609. (* Do the formals first *)
  5610. List.iter processLocal fdec.sformals;
  5611. (* Fix the type again *)
  5612. setFormals fdec fdec.sformals;
  5613. (* And now the locals *)
  5614. List.iter processLocal fdec.slocals;
  5615. (* Undo the changes to the global table *)
  5616. A.undoAlphaChanges gAlphaTable !undolist;
  5617. ()
  5618. end
  5619. | _ -> ());
  5620. ()
  5621. (* A visitor that makes a deep copy of a function body *)
  5622. class copyFunctionVisitor (newname: string) = object (self)
  5623. inherit nopCilVisitor
  5624. (* Keep here a maping from locals to their copies *)
  5625. val map : (string, varinfo) H.t = H.create 113
  5626. (* Keep here a maping from statements to their copies *)
  5627. val stmtmap : (int, stmt) H.t = H.create 113
  5628. val sid = ref 0 (* Will have to assign ids to statements *)
  5629. (* Keep here a list of statements to be patched *)
  5630. val patches : stmt list ref = ref []
  5631. val argid = ref 0
  5632. (* This is the main function *)
  5633. method vfunc (f: fundec) : fundec visitAction =
  5634. (* We need a map from the old locals/formals to the new ones *)
  5635. H.clear map;
  5636. argid := 0;
  5637. (* Make a copy of the fundec. *)
  5638. let f' = {f with svar = f.svar} in
  5639. let patchfunction (f' : fundec) =
  5640. (* Change the name. Only this late to allow the visitor to copy the
  5641. * svar *)
  5642. f'.svar.vname <- newname;
  5643. let findStmt (i: int) =
  5644. try H.find stmtmap i
  5645. with Not_found -> E.s (bug "Cannot find the copy of stmt#%d" i)
  5646. in
  5647. let patchstmt (s: stmt) =
  5648. match s.skind with
  5649. Goto (sr, l) ->
  5650. (* Make a copy of the reference *)
  5651. let sr' = ref (findStmt !sr.sid) in
  5652. s.skind <- Goto (sr',l)
  5653. | Switch (e, body, cases, l) ->
  5654. s.skind <- Switch (e, body,
  5655. Util.list_map (fun cs -> findStmt cs.sid) cases, l)
  5656. | _ -> ()
  5657. in
  5658. List.iter patchstmt !patches;
  5659. f'
  5660. in
  5661. patches := [];
  5662. sid := 0;
  5663. H.clear stmtmap;
  5664. ChangeDoChildrenPost (f', patchfunction)
  5665. (* We must create a new varinfo for each declaration. Memoize to
  5666. * maintain sharing *)
  5667. method vvdec (v: varinfo) =
  5668. (* Some varinfo have empty names. Give them some name *)
  5669. if v.vname = "" then begin
  5670. v.vname <- "arg" ^ string_of_int !argid; incr argid
  5671. end;
  5672. try
  5673. ChangeTo (H.find map v.vname)
  5674. with Not_found -> begin
  5675. let v' = {v with vid = newVID () } in
  5676. H.add map v.vname v';
  5677. ChangeDoChildrenPost (v', fun x -> x)
  5678. end
  5679. (* We must replace references to local variables *)
  5680. method vvrbl (v: varinfo) =
  5681. if v.vglob then SkipChildren else
  5682. try
  5683. ChangeTo (H.find map v.vname)
  5684. with Not_found ->
  5685. E.s (bug "Cannot find the new copy of local variable %s" v.vname)
  5686. (* Replace statements. *)
  5687. method vstmt (s: stmt) : stmt visitAction =
  5688. s.sid <- !sid; incr sid;
  5689. let s' = {s with sid = s.sid} in
  5690. H.add stmtmap s.sid s'; (* Remember where we copied this *)
  5691. (* if we have a Goto or a Switch remember them to fixup at end *)
  5692. (match s'.skind with
  5693. (Goto _ | Switch _) -> patches := s' :: !patches
  5694. | _ -> ());
  5695. (* Do the children *)
  5696. ChangeDoChildrenPost (s', fun x -> x)
  5697. (* Copy blocks since they are mutable *)
  5698. method vblock (b: block) =
  5699. ChangeDoChildrenPost ({b with bstmts = b.bstmts}, fun x -> x)
  5700. method vglob _ = E.s (bug "copyFunction should not be used on globals")
  5701. end
  5702. (* We need a function that copies a CIL function. *)
  5703. let copyFunction (f: fundec) (newname: string) : fundec =
  5704. visitCilFunction (new copyFunctionVisitor(newname)) f
  5705. (********* Compute the CFG ********)
  5706. let sid_counter = ref 0
  5707. let new_sid () =
  5708. let id = !sid_counter in
  5709. incr sid_counter;
  5710. id
  5711. let statements : stmt list ref = ref []
  5712. (* Clear all info about the CFG in statements *)
  5713. class clear : cilVisitor = object
  5714. inherit nopCilVisitor
  5715. method vstmt s = begin
  5716. s.sid <- !sid_counter ;
  5717. incr sid_counter ;
  5718. statements := s :: !statements;
  5719. s.succs <- [] ;
  5720. s.preds <- [] ;
  5721. DoChildren
  5722. end
  5723. method vexpr _ = SkipChildren
  5724. method vtype _ = SkipChildren
  5725. method vinst _ = SkipChildren
  5726. end
  5727. let link source dest = begin
  5728. if not (List.mem dest source.succs) then
  5729. source.succs <- dest :: source.succs ;
  5730. if not (List.mem source dest.preds) then
  5731. dest.preds <- source :: dest.preds
  5732. end
  5733. let trylink source dest_option = match dest_option with
  5734. None -> ()
  5735. | Some(dest) -> link source dest
  5736. (** Cmopute the successors and predecessors of a block, given a fallthrough *)
  5737. let rec succpred_block b fallthrough rlabels =
  5738. let rec handle sl = match sl with
  5739. [] -> ()
  5740. | [a] -> succpred_stmt a fallthrough rlabels
  5741. | hd :: ((next :: _) as tl) ->
  5742. succpred_stmt hd (Some next) rlabels;
  5743. handle tl
  5744. in handle b.bstmts
  5745. and succpred_stmt s fallthrough rlabels =
  5746. match s.skind with
  5747. Instr _ -> trylink s fallthrough
  5748. | Return _ -> ()
  5749. | Goto(dest,l) -> link s !dest
  5750. | ComputedGoto(e,l) -> List.iter (link s) rlabels
  5751. | Break _
  5752. | Continue _
  5753. | Switch _ ->
  5754. failwith "computeCFGInfo: cannot be called on functions with break, continue or switch statements. Use prepareCFG first to remove them."
  5755. | If(e1,b1,b2,l) ->
  5756. (match b1.bstmts with
  5757. [] -> trylink s fallthrough
  5758. | hd :: tl -> (link s hd ; succpred_block b1 fallthrough rlabels )) ;
  5759. (match b2.bstmts with
  5760. [] -> trylink s fallthrough
  5761. | hd :: tl -> (link s hd ; succpred_block b2 fallthrough rlabels ))
  5762. | Loop(b,l,_,_) ->
  5763. begin match b.bstmts with
  5764. [] -> failwith "computeCFGInfo: empty loop"
  5765. | hd :: tl ->
  5766. link s hd ;
  5767. succpred_block b (Some(hd)) rlabels
  5768. end
  5769. | Block(b) -> begin match b.bstmts with
  5770. [] -> trylink s fallthrough
  5771. | hd :: tl -> link s hd ;
  5772. succpred_block b fallthrough rlabels
  5773. end
  5774. | TryExcept _ | TryFinally _ ->
  5775. failwith "computeCFGInfo: structured exception handling not implemented"
  5776. let caseRangeFold (l: label list) =
  5777. let rec fold acc = function
  5778. | ((Case _ | Default _ | Label _) as x) :: xs -> fold (x :: acc) xs
  5779. | CaseRange(el, eh, loc) :: xs ->
  5780. let il, ih, ik =
  5781. match constFold true el, constFold true eh with
  5782. Const(CInt64(il, ilk, _)), Const(CInt64(ih, ihk, _)) ->
  5783. mkCilint ilk il, mkCilint ihk ih, commonIntKind ilk ihk
  5784. | _ -> E.s (error "Cannot understand the constants in case range")
  5785. in
  5786. if compare_cilint il ih > 0 then
  5787. E.s (error "Empty case range");
  5788. let rec mkAll (i: cilint) acc =
  5789. if compare_cilint i ih > 0 then acc
  5790. else mkAll (add_cilint i one_cilint) (Case(kintegerCilint ik i, loc) :: acc)
  5791. in
  5792. fold (mkAll il acc) xs
  5793. | [] -> List.rev acc
  5794. in fold [] l
  5795. (* [weimer] Sun May 5 12:25:24 PDT 2002
  5796. * This code was pulled from ext/switch.ml because it looks like we really
  5797. * want it to be part of CIL.
  5798. *
  5799. * Here is the magic handling to
  5800. * (1) replace switch statements with if/goto
  5801. * (2) remove "break"
  5802. * (3) remove "default"
  5803. * (4) remove "continue"
  5804. *)
  5805. (* This alphaTable is used to prevent collision of label names when
  5806. transforming switch statements and loops. It uses a *unit*
  5807. alphaTableData ref because there isn't any information we need to
  5808. carry around. *)
  5809. let labelAlphaTable : (string, unit A.alphaTableData ref) H.t =
  5810. H.create 11
  5811. let freshLabel (base:string) =
  5812. fst (A.newAlphaName labelAlphaTable None base ())
  5813. let rec xform_switch_stmt s break_dest cont_dest = begin
  5814. let suffix e = match getInteger e with
  5815. | Some value ->
  5816. if compare_cilint value zero_cilint < 0 then
  5817. "neg_" ^ string_of_cilint (neg_cilint value)
  5818. else
  5819. string_of_cilint value
  5820. | None -> "exp"
  5821. in
  5822. s.labels <- Util.list_map (fun lab -> match lab with
  5823. Label _ -> lab
  5824. | Case(e,l) ->
  5825. let str = Printf.sprintf "case_%s" (suffix e) in
  5826. Label(freshLabel str,l,false)
  5827. | CaseRange(e1,e2,l) ->
  5828. let str = Printf.sprintf "caserange_%s_%s" (suffix e1) (suffix e2) in
  5829. Label(freshLabel str,l,false)
  5830. | Default(l) -> Label(freshLabel "switch_default",l,false)
  5831. ) s.labels ;
  5832. match s.skind with
  5833. | Instr _ | Return _ | Goto _ | ComputedGoto _ -> ()
  5834. | Break(l) -> begin try
  5835. s.skind <- Goto(break_dest (),l)
  5836. with e ->
  5837. ignore (error "prepareCFG: break: %a@!" d_stmt s) ;
  5838. raise e
  5839. end
  5840. | Continue(l) -> begin try
  5841. s.skind <- Goto(cont_dest (),l)
  5842. with e ->
  5843. ignore (error "prepareCFG: continue: %a@!" d_stmt s) ;
  5844. raise e
  5845. end
  5846. | If(e,b1,b2,l) -> xform_switch_block b1 break_dest cont_dest ;
  5847. xform_switch_block b2 break_dest cont_dest
  5848. | Switch(e,b,sl,l) ->
  5849. (* change
  5850. * switch (se) {
  5851. * case 0: s0 ;
  5852. * case 1: s1 ; break;
  5853. * ...
  5854. * }
  5855. *
  5856. * into:
  5857. *
  5858. * if (se == 0) goto label_0;
  5859. * if (se == 1) goto label_1;
  5860. * ...
  5861. * goto label_default; // If there is a [Default]
  5862. * goto label_break; // If there is no [Default]
  5863. * label_0: s0;
  5864. * label_1: s1; goto label_break;
  5865. * ...
  5866. * label_break: ; // break_stmt
  5867. *
  5868. * The default case, if present, must be used only if *all*
  5869. * non-default cases fail [ISO/IEC 9899:1999, �6.8.4.2, �5]. As
  5870. * a result, we test all cases first, and hit 'default' only if
  5871. * no case matches. However, we do not reorder the switch's
  5872. * body, so fall-through still works as expected.
  5873. *
  5874. *)
  5875. let break_stmt = mkStmt (Instr []) in
  5876. break_stmt.labels <- [Label(freshLabel "switch_break",l,false)] ;
  5877. (* To be changed into goto default if there if a [Default] *)
  5878. let goto_break = mkStmt (Goto (ref break_stmt, l)) in
  5879. (* Return a list of [If] statements, equivalent to the cases of [stmt].
  5880. * Use a single [If] and || operators if useLogicalOperators is true.
  5881. * If [stmt] is a [Default], update goto label_break into goto
  5882. * label_default.
  5883. *)
  5884. let xform_choice stmt =
  5885. let cases = List.filter (function Label _ -> false | _ -> true ) stmt.labels in
  5886. try (* is this the default case? *)
  5887. match List.find (function Default _ -> true | _ -> false) cases with
  5888. | Default dl ->
  5889. (* We found a [Default], update the fallthrough goto *)
  5890. goto_break.skind <- Goto(ref stmt, dl);
  5891. []
  5892. | _ -> E.s (bug "Unexpected pattern-matching failure")
  5893. with
  5894. Not_found -> (* this is a list of specific cases *)
  5895. match cases with
  5896. | ((Case (_, cl) | CaseRange (_, _, cl)) as lab) :: lab_tl ->
  5897. (* assume that integer promotion and type conversion of cases is
  5898. * performed by cabs2cil. *)
  5899. let comp_case_range e1 e2 =
  5900. BinOp(Ge, e, e1, intType), BinOp(Le, e, e2, intType) in
  5901. let make_comp lab = begin match lab with
  5902. | Case (exp, _) -> BinOp(Eq, e, exp, intType)
  5903. | CaseRange (e1, e2, _) when !useLogicalOperators ->
  5904. let c1, c2 = comp_case_range e1 e2 in
  5905. BinOp(LAnd, c1, c2, intType)
  5906. | _ -> E.s (bug "Unexpected pattern-matching failure")
  5907. end in
  5908. let make_or_from_cases () =
  5909. List.fold_left
  5910. (fun pred label -> BinOp(LOr, pred, make_comp label, intType))
  5911. (make_comp lab) lab_tl
  5912. in
  5913. let make_if_stmt pred cl =
  5914. let then_block = mkBlock [ mkStmt (Goto(ref stmt,cl)) ] in
  5915. let else_block = mkBlock [] in
  5916. mkStmt(If(pred,then_block,else_block,cl)) in
  5917. let make_double_if_stmt (pred1, pred2) cl =
  5918. let then_block = mkBlock [ make_if_stmt pred2 cl ] in
  5919. let else_block = mkBlock [] in
  5920. mkStmt(If(pred1,then_block,else_block,cl)) in
  5921. if !useLogicalOperators then
  5922. [make_if_stmt (make_or_from_cases ()) cl]
  5923. else
  5924. List.map (function
  5925. | Case _ as lab -> make_if_stmt (make_comp lab) cl
  5926. | CaseRange (e1, e2, _) -> make_double_if_stmt (comp_case_range e1 e2) cl
  5927. | _ -> E.s (bug "Unexpected pattern-matching failure"))
  5928. cases
  5929. | Default _ :: _ | Label _ :: _ ->
  5930. E.s (bug "Unexpected pattern-matching failure")
  5931. | [] -> E.s (bug "Block missing 'case' and 'default' in switch statement")
  5932. in
  5933. b.bstmts <-
  5934. (List.flatten (List.map xform_choice sl)) @
  5935. [goto_break] @
  5936. b.bstmts @
  5937. [break_stmt];
  5938. s.skind <- Block b;
  5939. xform_switch_block b (fun () -> ref break_stmt) cont_dest
  5940. | Loop(b,l,_,_) ->
  5941. let break_stmt = mkStmt (Instr []) in
  5942. break_stmt.labels <- [Label(freshLabel "while_break",l,false)] ;
  5943. let cont_stmt = mkStmt (Instr []) in
  5944. cont_stmt.labels <- [Label(freshLabel "while_continue",l,false)] ;
  5945. b.bstmts <- cont_stmt :: b.bstmts ;
  5946. let this_stmt = mkStmt
  5947. (Loop(b,l,Some(cont_stmt),Some(break_stmt))) in
  5948. let break_dest () = ref break_stmt in
  5949. let cont_dest () = ref cont_stmt in
  5950. xform_switch_block b break_dest cont_dest ;
  5951. break_stmt.succs <- s.succs ;
  5952. let new_block = mkBlock [ this_stmt ; break_stmt ] in
  5953. s.skind <- Block new_block
  5954. | Block(b) -> xform_switch_block b break_dest cont_dest
  5955. | TryExcept _ | TryFinally _ ->
  5956. failwith "xform_switch_statement: structured exception handling not implemented"
  5957. end and xform_switch_block b break_dest cont_dest =
  5958. try
  5959. let rec link_succs sl = match sl with
  5960. | [] -> ()
  5961. | hd :: tl -> (if hd.succs = [] then hd.succs <- tl) ; link_succs tl
  5962. in
  5963. link_succs b.bstmts ;
  5964. List.iter (fun stmt ->
  5965. xform_switch_stmt stmt break_dest cont_dest) b.bstmts ;
  5966. with e ->
  5967. List.iter (fun stmt -> ignore
  5968. (warn "prepareCFG: %a@!" d_stmt stmt)) b.bstmts ;
  5969. raise e
  5970. (* Enter all the labels in a function into an alpha renaming table to
  5971. prevent duplicate labels when transforming loops and switch
  5972. statements. *)
  5973. class registerLabelsVisitor : cilVisitor = object
  5974. inherit nopCilVisitor
  5975. method vstmt { labels = labels } = begin
  5976. List.iter
  5977. (function
  5978. Label (name,_,_) -> A.registerAlphaName labelAlphaTable None name ()
  5979. | _ -> ())
  5980. labels;
  5981. DoChildren
  5982. end
  5983. method vexpr _ = SkipChildren
  5984. method vtype _ = SkipChildren
  5985. method vinst _ = SkipChildren
  5986. end
  5987. (* Find all labels-as-value in a function to use them as successors of computed
  5988. * gotos. Duplicated in src/ext/cfg.ml. *)
  5989. class addrOfLabelFinder slr = object(self)
  5990. inherit nopCilVisitor
  5991. method vexpr e = match e with
  5992. | AddrOfLabel sref ->
  5993. slr := !sref :: (!slr);
  5994. SkipChildren
  5995. | _ -> DoChildren
  5996. end
  5997. let findAddrOfLabelStmts (b : block) : stmt list =
  5998. let slr = ref [] in
  5999. let vis = new addrOfLabelFinder slr in
  6000. ignore(visitCilBlock vis b);
  6001. !slr
  6002. (* prepare a function for computeCFGInfo by removing break, continue,
  6003. * default and switch statements/labels and replacing them with Ifs and
  6004. * Gotos. *)
  6005. let prepareCFG (fd : fundec) : unit =
  6006. (* Labels are local to a function, so start with a clean slate by
  6007. clearing labelAlphaTable. Then register all labels. *)
  6008. H.clear labelAlphaTable;
  6009. ignore (visitCilFunction (new registerLabelsVisitor) fd);
  6010. xform_switch_block fd.sbody
  6011. (fun () -> failwith "prepareCFG: break with no enclosing loop")
  6012. (fun () -> failwith "prepareCFG: continue with no enclosing loop")
  6013. (* make the cfg and return a list of statements *)
  6014. let computeCFGInfo (f : fundec) (global_numbering : bool) : unit =
  6015. if not global_numbering then
  6016. sid_counter := 0 ;
  6017. statements := [];
  6018. let clear_it = new clear in
  6019. ignore (visitCilBlock clear_it f.sbody) ;
  6020. f.smaxstmtid <- Some (!sid_counter) ;
  6021. let rlabels = findAddrOfLabelStmts f.sbody in
  6022. succpred_block f.sbody None rlabels;
  6023. let res = List.rev !statements in
  6024. statements := [];
  6025. f.sallstmts <- res;
  6026. ()
  6027. let initCIL () =
  6028. if not !initCIL_called then begin
  6029. (* Set the machine *)
  6030. begin
  6031. match !envMachine with
  6032. Some machine -> M.theMachine := machine
  6033. | None -> M.theMachine := if !msvcMode then M.msvc else M.gcc
  6034. end;
  6035. (* Pick type for string literals *)
  6036. stringLiteralType := if !M.theMachine.M.const_string_literals then
  6037. charConstPtrType
  6038. else
  6039. charPtrType;
  6040. (* Find the right ikind given the size *)
  6041. let findIkindSz (unsigned: bool) (sz: int) : ikind =
  6042. try
  6043. intKindForSize sz unsigned
  6044. with Not_found ->
  6045. E.s(E.unimp "initCIL: cannot find the right ikind for size %d\n" sz)
  6046. in
  6047. (* Find the right ikind given the name *)
  6048. let findIkindName (name: string) : ikind =
  6049. (* Test the most common sizes first *)
  6050. if name = "int" then IInt
  6051. else if name = "unsigned int" then IUInt
  6052. else if name = "long" then ILong
  6053. else if name = "unsigned long" then IULong
  6054. else if name = "long long" then ILongLong
  6055. else if name = "unsigned long long" then IULongLong
  6056. else if name = "short" then IShort
  6057. else if name = "unsigned short" then IUShort
  6058. else if name = "char" then IChar
  6059. else if name = "unsigned char" then IUChar
  6060. else E.s(E.unimp "initCIL: cannot find the right ikind for type %s\n" name)
  6061. in
  6062. upointType := TInt(findIkindSz true !M.theMachine.M.sizeof_ptr, []);
  6063. ptrdiffType := TInt(findIkindSz false !M.theMachine.M.sizeof_ptr, []);
  6064. kindOfSizeOf := findIkindName !M.theMachine.M.size_t;
  6065. typeOfSizeOf := TInt(!kindOfSizeOf, []);
  6066. wcharKind := findIkindName !M.theMachine.M.wchar_t;
  6067. wcharType := TInt(!wcharKind, []);
  6068. char_is_unsigned := !M.theMachine.M.char_is_unsigned;
  6069. little_endian := !M.theMachine.M.little_endian;
  6070. underscore_name := !M.theMachine.M.underscore_name;
  6071. (* nextGlobalVID := 1; *)
  6072. (* nextCompinfoKey := 1; *)
  6073. initCIL_called := true;
  6074. if !msvcMode then
  6075. initMsvcBuiltins ()
  6076. else
  6077. initGccBuiltins ();
  6078. ()
  6079. end
  6080. (* We want to bring all type declarations before the data declarations. This
  6081. * is needed for code of the following form:
  6082. int f(); // Prototype without arguments
  6083. typedef int FOO;
  6084. int f(FOO x) { ... }
  6085. In CIL the prototype also lists the type of the argument as being FOO,
  6086. which is undefined.
  6087. There is one catch with this scheme. If the type contains an array whose
  6088. length refers to variables then those variables must be declared before
  6089. the type *)
  6090. let pullTypesForward = true
  6091. (* Scan a type and collect the variables that are refered *)
  6092. class getVarsInGlobalClass (pacc: varinfo list ref) = object
  6093. inherit nopCilVisitor
  6094. method vvrbl (vi: varinfo) =
  6095. pacc := vi :: !pacc;
  6096. SkipChildren
  6097. method vglob = function
  6098. GType _ | GCompTag _ -> DoChildren
  6099. | _ -> SkipChildren
  6100. end
  6101. let getVarsInGlobal (g : global) : varinfo list =
  6102. let pacc : varinfo list ref = ref [] in
  6103. let v : cilVisitor = new getVarsInGlobalClass pacc in
  6104. ignore (visitCilGlobal v g);
  6105. !pacc
  6106. let hasPrefix p s =
  6107. let pl = String.length p in
  6108. (String.length s >= pl) && String.sub s 0 pl = p
  6109. let pushGlobal (g: global)
  6110. ~(types:global list ref)
  6111. ~(variables: global list ref) =
  6112. if not pullTypesForward then
  6113. variables := g :: !variables
  6114. else
  6115. begin
  6116. (* Collect a list of variables that are refered from the type. Return
  6117. * Some if the global should go with the types and None if it should go
  6118. * to the variables. *)
  6119. let varsintype : (varinfo list * location) option =
  6120. match g with
  6121. GType (_, l) | GCompTag (_, l) -> Some (getVarsInGlobal g, l)
  6122. | GEnumTag (_, l) | GPragma (Attr("pack", _), l)
  6123. | GCompTagDecl (_, l) | GEnumTagDecl (_, l) -> Some ([], l)
  6124. (** Move the warning pragmas early
  6125. | GPragma(Attr(s, _), l) when hasPrefix "warning" s -> Some ([], l)
  6126. *)
  6127. | _ -> None (* Does not go with the types *)
  6128. in
  6129. match varsintype with
  6130. None -> variables := g :: !variables
  6131. | Some (vl, loc) ->
  6132. types :=
  6133. (* insert declarations for referred variables ('vl'), before
  6134. * the type definition 'g' itself *)
  6135. g :: (List.fold_left (fun acc v -> GVarDecl(v, loc) :: acc)
  6136. !types vl)
  6137. end
  6138. type formatArg =
  6139. Fe of exp
  6140. | Feo of exp option (** For array lengths *)
  6141. | Fu of unop
  6142. | Fb of binop
  6143. | Fk of ikind
  6144. | FE of exp list (** For arguments in a function call *)
  6145. | Ff of (string * typ * attributes) (** For a formal argument *)
  6146. | FF of (string * typ * attributes) list (* For formal argument lists *)
  6147. | Fva of bool (** For the ellipsis in a function type *)
  6148. | Fv of varinfo
  6149. | Fl of lval
  6150. | Flo of lval option (** For the result of a function call *)
  6151. | Fo of offset
  6152. | Fc of compinfo
  6153. | Fi of instr
  6154. | FI of instr list
  6155. | Ft of typ
  6156. | Fd of int
  6157. | Fg of string
  6158. | Fs of stmt
  6159. | FS of stmt list
  6160. | FA of attributes
  6161. | Fp of attrparam
  6162. | FP of attrparam list
  6163. | FX of string
  6164. let d_formatarg () = function
  6165. Fe e -> dprintf "Fe(%a)" d_exp e
  6166. | Feo None -> dprintf "Feo(None)"
  6167. | Feo (Some e) -> dprintf "Feo(%a)" d_exp e
  6168. | FE _ -> dprintf "FE()"
  6169. | Fk ik -> dprintf "Fk()"
  6170. | Fva b -> dprintf "Fva(%b)" b
  6171. | Ff (an, _, _) -> dprintf "Ff(%s)" an
  6172. | FF _ -> dprintf "FF(...)"
  6173. | FA _ -> dprintf "FA(...)"
  6174. | Fu uo -> dprintf "Fu()"
  6175. | Fb bo -> dprintf "Fb()"
  6176. | Fv v -> dprintf "Fv(%s)" v.vname
  6177. | Fl l -> dprintf "Fl(%a)" d_lval l
  6178. | Flo None -> dprintf "Flo(None)"
  6179. | Flo (Some l) -> dprintf "Flo(%a)" d_lval l
  6180. | Fo o -> dprintf "Fo"
  6181. | Fc ci -> dprintf "Fc(%s)" ci.cname
  6182. | Fi i -> dprintf "Fi(...)"
  6183. | FI i -> dprintf "FI(...)"
  6184. | Ft t -> dprintf "Ft(%a)" d_type t
  6185. | Fd n -> dprintf "Fd(%d)" n
  6186. | Fg s -> dprintf "Fg(%s)" s
  6187. | Fp _ -> dprintf "Fp(...)"
  6188. | FP n -> dprintf "FP(...)"
  6189. | Fs _ -> dprintf "FS"
  6190. | FS _ -> dprintf "FS"
  6191. | FX _ -> dprintf "FX()"
  6192. (* ------------------------------------------------------------------------- *)
  6193. (* DEPRECATED FUNCTIONS *)
  6194. (* These will eventually go away *)
  6195. (* ------------------------------------------------------------------------- *)
  6196. (** Deprecated (can't handle large 64-bit unsigned constants
  6197. correctly) - use getInteger instead. If the given expression
  6198. is a (possibly cast'ed) character or an integer constant, return
  6199. that integer. Otherwise, return None. *)
  6200. let rec isInteger : exp -> int64 option = function
  6201. | Const(CInt64 (n,_,_)) -> Some n
  6202. | Const(CChr c) -> isInteger (Const (charConstToInt c)) (* sign-extend *)
  6203. | Const(CEnum(v, s, ei)) -> isInteger v
  6204. | CastE(_, e) -> isInteger e
  6205. | _ -> None
  6206. (** Deprecated. For compatibility with older programs, these are
  6207. aliases for {!Cil.builtinFunctions} *)
  6208. let gccBuiltins = builtinFunctions
  6209. let msvcBuiltins = builtinFunctions
  6210. (* Deprecated. Represents an integer as for a given kind.
  6211. Returns a flag saying whether the value was changed
  6212. during truncation (because it was too large to fit in k). *)
  6213. let truncateInteger64 (k: ikind) (i: int64) : int64 * bool =
  6214. let nrBits = 8 * (bytesSizeOfInt k) in
  6215. let signed = isSigned k in
  6216. if nrBits = 64 then
  6217. i, false
  6218. else begin
  6219. let i1 = Int64.shift_left i (64 - nrBits) in
  6220. let i2 =
  6221. if signed then Int64.shift_right i1 (64 - nrBits)
  6222. else Int64.shift_right_logical i1 (64 - nrBits)
  6223. in
  6224. let truncated =
  6225. if i2 = i then false
  6226. else
  6227. (* Examine the bits that we chopped off. If they are all zero, then
  6228. * any difference between i2 and i is due to a simple sign-extension.
  6229. * e.g. casting the constant 0x80000000 to int makes it
  6230. * 0xffffffff80000000.
  6231. * Suppress the truncation warning in this case. *)
  6232. let chopped = Int64.shift_right i nrBits in
  6233. chopped <> Int64.zero
  6234. (* matth: also suppress the warning if we only chop off 1s.
  6235. This is probably due to a negative number being cast to an
  6236. unsigned value. While potentially a bug, this is almost
  6237. always what the programmer intended. *)
  6238. && chopped <> Int64.minus_one
  6239. in
  6240. i2, truncated
  6241. end
  6242. (* Convert 2 integer constants to integers with the same type, in preparation
  6243. for a binary operation. See ISO C 6.3.1.8p1 *)
  6244. let convertInts (i1:int64) (ik1:ikind) (i2:int64) (ik2:ikind)
  6245. : int64 * int64 * ikind =
  6246. if ik1 = ik2 then (* nothing to do *)
  6247. i1, i2, ik1
  6248. else begin
  6249. let rank : ikind -> int = function
  6250. (* these are just unique numbers representing the integer
  6251. conversion rank. *)
  6252. | IBool -> 0
  6253. | IChar | ISChar | IUChar -> 1
  6254. | IShort | IUShort -> 2
  6255. | IInt | IUInt -> 3
  6256. | ILong | IULong -> 4
  6257. | ILongLong | IULongLong -> 5
  6258. in
  6259. let r1 = rank ik1 in
  6260. let r2 = rank ik2 in
  6261. let ik' =
  6262. if (isSigned ik1) = (isSigned ik2) then begin
  6263. (* Both signed or both unsigned. *)
  6264. if r1 > r2 then ik1 else ik2
  6265. end
  6266. else begin
  6267. let signedKind, unsignedKind, signedRank, unsignedRank =
  6268. if isSigned ik1 then ik1, ik2, r1, r2 else ik2, ik1, r2, r1
  6269. in
  6270. (* The rules for signed + unsigned get hairy.
  6271. (unsigned short + long) is converted to signed long,
  6272. but (unsigned int + long) is converted to unsigned long.*)
  6273. if unsignedRank >= signedRank then unsignedKind
  6274. else if (bytesSizeOfInt signedKind) > (bytesSizeOfInt unsignedKind) then
  6275. signedKind
  6276. else
  6277. unsignedVersionOf signedKind
  6278. end
  6279. in
  6280. let i1',_ = truncateInteger64 ik' i1 in
  6281. let i2',_ = truncateInteger64 ik' i2 in
  6282. i1', i2', ik'
  6283. end