package goblint-cil
A front-end for the C programming language that facilitates program analysis and transformation
Install
Dune Dependency
Authors
Maintainers
Sources
goblint-cil-2.0.6.tbz
sha256=5577007bfac63c3f0609abdb74119fe674c9bc8529d790e90ef73a85964aa07a
sha512=f1a393fa92614ceaf857bec4df474d3e152c578d0ab5fdf791e9129668861ccaa37efae2f18aa539965d6c2ed4dabb47b4a5262aab55112e181935def06f18da
doc/src/goblint-cil.zrapp/zrapp.ml.html
Source file zrapp.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
open GoblintCil open Escape open Pretty open Feature open Liveness module E = Errormsg module H = Hashtbl module IH = Inthash module M = Machdep module U = Util module RD = Reachingdefs module UD = Usedef module A = Cabs module CH = Cabshelper module GA = GrowArray module RCT = Rmciltmps module DCE = Deadcodeelim module EC = Expcompare let doElimTemps = ref false let debug = ref false let printComments = ref false let envWarnings = ref false (* Stuff for Deputy support *) let deputyAttrs = ref false let thisKeyword = "__this" type paramkind = | PKNone | PKThis | PKOffset of attrparam let rec checkParam (ap: attrparam) : paramkind = match ap with | ACons (name, []) when name = thisKeyword -> PKThis | ABinOp (PlusA, a1, a2) when checkParam a1 = PKThis -> if a2 = AInt 0 then PKThis else PKOffset a2 | _ -> PKNone (* End stuff for Deputy support *) (* Some(-1) => l1 < l2 Some(0) => l1 = l2 Some(1) => l1 > l2 None => different files *) let loc_comp l1 l2 = if String.compare l1.A.filename l2.A.filename != 0 then None else if l1.A.lineno > l2.A.lineno then Some(1) else if l2.A.lineno > l1.A.lineno then Some(-1) else if l1.A.byteno > l2.A.byteno then Some(1) else if l2.A.byteno > l1.A.byteno then Some(-1) else if l1.A.columnno > l2.A.columnno then Some(1) else if l2.A.columnno > l1.A.columnno then Some(-1) else if l1.A.endLineno > l2.A.endLineno then Some(1) else if l2.A.endLineno > l1.A.endLineno then Some(-1) else if l1.A.endByteno > l2.A.endByteno then Some(1) else if l2.A.endByteno > l1.A.endByteno then Some(-1) else if l1.A.endColumnno > l2.A.endColumnno then Some(1) else if l2.A.endColumnno > l1.A.endColumnno then Some(-1) else Some(0) let simpleGaSearch l = let hi = GA.max_init_index CH.commentsGA in let rec loop i = if i < 0 then -1 else let (l',_,_) = GA.get CH.commentsGA i in match loc_comp l l' with None -> loop (i-1) | Some(0) -> i | Some(-1) -> loop (i-1) | Some(1) -> i | _ -> E.s (E.error "simpleGaSearch: unexpected return from loc_comp") in loop hi (* location -> string list *) let get_comments l = let cabsl = {A.lineno = l.line; A.filename = l.file; A.byteno = l.byte; A.columnno = l.column; A.ident = 0; A.endLineno = l.endLine; A.endByteno = l.endByte; A.endColumnno = l.endColumn;} in let s = simpleGaSearch cabsl in let rec loop i cl = if i < 0 then cl else let (l',c,b) = GA.get CH.commentsGA i in if String.compare cabsl.A.filename l'.A.filename != 0 then loop (i - 1) cl else if b then cl else let _ = GA.set CH.commentsGA i (l',c,true) in loop (i - 1) (c::cl) in List.rev (loop s []) (* clean up some of the mess made below *) let rec simpl_cond e = match e with | UnOp(LNot,BinOp(LAnd,e1,e2,t1),t2) -> let e1 = simpl_cond (UnOp(LNot,e1,t1)) in let e2 = simpl_cond (UnOp(LNot,e2,t1)) in BinOp(LOr,e1,e2,t2) | UnOp(LNot,BinOp(LOr,e1,e2,t1),t2) -> let e1 = simpl_cond (UnOp(LNot,e1,t1)) in let e2 = simpl_cond (UnOp(LNot,e2,t1)) in BinOp(LAnd,e1,e2,t2) | UnOp(LNot,UnOp(LNot,e,_),_) -> simpl_cond e | _ -> e (* the argument b is the body of a Loop *) (* returns the loop termination condition *) (* block -> exp option *) let get_loop_condition b = (* returns the first non-empty statement of a statement list *) (* stm list -> stm list *) let rec skipEmpty = function | [] -> [] | {skind = Instr []; labels = []; _}::rest -> skipEmpty rest | x -> x in (* stm -> exp option * instr list *) let rec get_cond_from_if if_stm = match if_stm.skind with If(e,tb,fb,_,_) -> let e = EC.stripNopCasts e in RCT.fold_blocks tb; RCT.fold_blocks fb; let tsl = skipEmpty tb.bstmts in let fsl = skipEmpty fb.bstmts in (match tsl, fsl with {skind = Break _; _} :: _, [] -> Some e | [], {skind = Break _; _} :: _ -> Some(UnOp(LNot, e, intType)) | ({skind = If(_,_,_,_,_); _} as s) :: _, [] -> let teo = get_cond_from_if s in (match teo with None -> None | Some te -> Some(BinOp(LAnd,e,EC.stripNopCasts te,intType))) | [], ({skind = If(_,_,_,_,_); _} as s) :: _ -> let feo = get_cond_from_if s in (match feo with None -> None | Some fe -> Some(BinOp(LAnd,UnOp(LNot,e,intType), EC.stripNopCasts fe,intType))) | {skind = Break _; _} :: _, ({skind = If(_,_,_,_,_); _} as s):: _ -> let feo = get_cond_from_if s in (match feo with None -> None | Some fe -> Some(BinOp(LOr,e,EC.stripNopCasts fe,intType))) | ({skind = If(_,_,_,_,_); _} as s) :: _, {skind = Break _; _} :: _ -> let teo = get_cond_from_if s in (match teo with None -> None | Some te -> Some(BinOp(LOr,UnOp(LNot,e,intType), EC.stripNopCasts te,intType))) | ({skind = If(_,_,_,_,_); _} as ts) :: _ , ({skind = If(_,_,_,_,_); _} as fs) :: _ -> let teo = get_cond_from_if ts in let feo = get_cond_from_if fs in (match teo, feo with Some te, Some fe -> Some(BinOp(LOr,BinOp(LAnd,e,EC.stripNopCasts te,intType), BinOp(LAnd,UnOp(LNot,e,intType), EC.stripNopCasts fe,intType),intType)) | _,_ -> None) | _, _ -> (if !debug then ignore(E.log "cond_finder: branches of %a not good\n" d_stmt if_stm); None)) | _ -> (if !debug then ignore(E.log "cond_finder: %a not an if\n" d_stmt if_stm); None) in let sl = skipEmpty b.bstmts in match sl with ({skind = If(_,_,_,_,_); labels=[]; _} as s) :: rest -> get_cond_from_if s, rest | s :: _ -> (if !debug then ignore(E.log "checkMover: %a is first, not an if\n" d_stmt s); None, sl) | [] -> (if !debug then ignore(E.log "checkMover: no statements in loop block?\n"); None, sl) class zraCilPrinterClass : cilPrinter = object (self) inherit defaultCilPrinterClass as super val genvHtbl : (string, varinfo) H.t = H.create 128 val lenvHtbl : (string, varinfo) H.t = H.create 128 (*** VARIABLES ***) (* give the varinfo for the variable to be printed, returns the varinfo for the varinfo with that name in the current environment. Returns argument and prints a warning if the variable isn't in the environment *) method private getEnvVi (v:varinfo) : varinfo = try if H.mem lenvHtbl v.vname then H.find lenvHtbl v.vname else H.find genvHtbl v.vname with Not_found -> if !envWarnings then ignore (warn "variable %s not in pp environment" v.vname); v (* True when v agrees with the entry in the environment for the name of v. False otherwise *) method private checkVi (v:varinfo) : bool = let v' = self#getEnvVi v in v.vid = v'.vid method private checkViAndWarn (v:varinfo) = if not (self#checkVi v) then ignore (warn "mentioned variable %s and its entry in the current environment have different varinfo." v.vname) (** Get the comment out of a location if there is one *) method! pLineDirective ?(forcefile=false) l = let ld = super#pLineDirective l in if !printComments then let c = String.concat "\n" (get_comments l) in match c with "" -> ld | _ -> ld ++ line ++ text "/*" ++ text c ++ text "*/" ++ line else ld (* variable use *) method! pVar (v:varinfo) = (* warn about instances where a possibly unintentionally conflicting name is used *) if IH.mem RCT.iioh v.vid then let rhso = IH.find RCT.iioh v.vid in match rhso with Some(Call(_,e,el,l,eloc)) -> (* print a call instead of a temp variable *) let oldpit = super#getPrintInstrTerminator() in let _ = super#setPrintInstrTerminator "" in let opc = !printComments in let _ = printComments := false in let c = match unrollType (typeOf e) with TFun(rt,_,_,_) when not (Util.equals (typeSig rt) (typeSig v.vtype)) -> text "(" ++ self#pType None () v.vtype ++ text ")" | _ -> nil in let d = self#pInstr () (Call(None,e,el,l,eloc)) in let _ = super#setPrintInstrTerminator oldpit in let _ = printComments := opc in c ++ d | _ -> if IH.mem RCT.incdecHash v.vid then (* print an post-inc/dec instead of a temp variable *) let redefid, rhsvi, b = IH.find RCT.incdecHash v.vid in match b with PlusA | PlusPI | IndexPI -> text rhsvi.vname ++ text "++" | MinusA | MinusPI -> text rhsvi.vname ++ text "--" | _ -> E.s (E.error "zraCilPrinterClass.pVar: unexpected op for inc/dec") else (self#checkViAndWarn v; text v.vname) else if IH.mem RCT.incdecHash v.vid then (* print an post-inc/dec instead of a temp variable *) let redefid, rhsvi, b = IH.find RCT.incdecHash v.vid in match b with PlusA | PlusPI | IndexPI -> text rhsvi.vname ++ text "++" | MinusA | MinusPI -> text rhsvi.vname ++ text "--" | _ -> E.s (E.error "zraCilPrinterClass.pVar: unexpected op for inc/dec") else (self#checkViAndWarn v; text v.vname) (* variable declaration *) method! pVDecl () (v:varinfo) = (* See if the name is already in the environment with a different varinfo. If so, give a warning. If not, add the name to the environment *) let _ = if (H.mem lenvHtbl v.vname) && not(self#checkVi v) then ignore( warn "name %s has already been declared locally with different varinfo" v.vname) else if (H.mem genvHtbl v.vname) && not(self#checkVi v) then ignore( warn "name %s has already been declared globally with different varinfo" v.vname) else if not v.vglob then (if !debug then ignore(E.log "zrapp: adding %s to local pp environment\n" v.vname); H.add lenvHtbl v.vname v) else (if !debug then ignore(E.log "zrapp: adding %s to global pp envirnoment\n" v.vname); H.add genvHtbl v.vname v) in (* First the storage modifiers *) self#pLineDirective v.vdecl ++ text (if v.vinline then "__inline " else "") ++ d_storage () v.vstorage ++ (self#pType (Some (text v.vname)) () v.vtype) ++ text " " ++ self#pAttrs () v.vattr (* For printing deputy annotations *) method! pAttr (Attr (an, args) : attribute) : doc * bool = if not (!deputyAttrs) then super#pAttr (Attr(an,args)) else match an, args with | "fancybounds", [AInt i1; AInt i2] -> nil, false (*if !showBounds then dprintf "BND(%a, %a)" self#pExp (getBoundsExp i1) self#pExp (getBoundsExp i2), false else text "BND(...)", false*) | "bounds", [a1; a2] -> begin match checkParam a1, checkParam a2 with | PKThis, PKThis -> text "COUNT(0)", false | PKThis, PKOffset (AInt 1) -> text "SAFE", false | PKThis, PKOffset a -> nil, false (*if !showBounds then dprintf "COUNT(%a)" self#pAttrParam a, false else text "COUNT(...)", false*) | _ -> nil, false (* if !showBounds then dprintf "BND(%a, %a)" self#pAttrParam a1 self#pAttrParam a2, false else text "BND(...)", false*) end | "fancysize", [AInt i] -> nil, false (*dprintf "SIZE(%a)" self#pExp (getBoundsExp i), false*) | "size", [a] -> dprintf "SIZE(%a)" self#pAttrParam a, false | "fancywhen", [AInt i] -> nil, false (*dprintf "WHEN(%a)" self#pExp (getBoundsExp i), false*) | "when", [a] -> dprintf "WHEN(%a)" self#pAttrParam a, false | "nullterm", [] -> text "NT", false | "assumeconst", [] -> text "ASSUMECONST", false | "trusted", [] -> text "TRUSTED", false | "poly", [a] -> dprintf "POLY(%a)" self#pAttrParam a, false | "poly", [] -> text "POLY", false | "sentinel", [] -> text "SNT", false | "nonnull", [] -> text "NONNULL", false | "_ptrnode", [AInt n] -> nil, false (*if !Doptions.emitGraphDetailLevel >= 3 then dprintf "NODE(%d)" n, false else nil, false*) | "missing_annot", _-> (* Don't bother printing thess *) nil, false | _ -> super#pAttr (Attr (an, args)) (*** GLOBALS ***) method! pGlobal () (g:global) : doc = (* global (vars, types, etc.) *) match g with | GFun (fundec, l) -> (* If the function has attributes then print a prototype because GCC cannot accept function attributes in a definition *) let oldattr = fundec.svar.vattr in (* Always pring the file name before function declarations *) let proto = if oldattr <> [] then (self#pLineDirective l) ++ (self#pVDecl () fundec.svar) ++ chr ';' ++ line else nil in (* Temporarily remove the function attributes *) fundec.svar.vattr <- []; let body = (self#pLineDirective ~forcefile:true l) ++ (self#pFunDecl () fundec) in fundec.svar.vattr <- oldattr; proto ++ body ++ line | GType (typ, l) -> self#pLineDirective ~forcefile:true l ++ text "typedef " ++ (self#pType (Some (text typ.tname)) () typ.ttype) ++ text ";\n" | GEnumTag (enum, l) -> self#pLineDirective l ++ text "enum" ++ align ++ text (" " ^ enum.ename) ++ self#pAttrs () enum.eattr ++ text " {" ++ line ++ (docList ~sep:(chr ',' ++ line) (fun (n, attrs, i, loc) -> text n ++ self#pAttrs () attrs ++ text (n ^ " = ") ++ self#pExp () i) () enum.eitems) ++ unalign ++ line ++ text "};\n" | GEnumTagDecl (enum, l) -> (* This is a declaration of a tag *) self#pLineDirective l ++ text ("enum " ^ enum.ename ^ ";\n") | GCompTag (comp, l) -> (* This is a definition of a tag *) let n = comp.cname in let su, su1, su2 = if comp.cstruct then "struct", "str", "uct" else "union", "uni", "on" in self#pLineDirective ~forcefile:true l ++ text su1 ++ (align ++ text su2 ++ chr ' ' ++ text n ++ text " {" ++ line ++ ((docList ~sep:line (self#pFieldDecl ())) () comp.cfields) ++ unalign) ++ line ++ text "}" ++ (self#pAttrs () comp.cattr) ++ text ";\n" | GCompTagDecl (comp, l) -> (* This is a declaration of a tag *) self#pLineDirective l ++ text (compFullName comp) ++ text ";\n" | GVar (vi, io, l) -> self#pLineDirective ~forcefile:true l ++ self#pVDecl () vi ++ chr ' ' ++ (match io.init with None -> nil | Some i -> text " = " ++ (let islong = match i with CompoundInit (_, il) when List.length il >= 8 -> true | _ -> false in if islong then line ++ self#pLineDirective l ++ text " " else nil) ++ (self#pInit () i)) ++ text ";\n" (* print global variable 'extern' declarations, and function prototypes *) | GVarDecl (vi, l) -> let builtins = gccBuiltins in if not !printCilAsIs && H.mem builtins vi.vname then begin (* Compiler builtins need no prototypes. Just print them in comments. *) text "/* compiler builtin: \n " ++ (self#pVDecl () vi) ++ text "; */\n" end else self#pLineDirective l ++ (self#pVDecl () vi) ++ text ";\n" | GAsm (s, l) -> self#pLineDirective l ++ text ("__asm__(\"" ^ escape_string s ^ "\");\n") | GPragma (Attr(an, args), l) -> (* sm: suppress printing pragmas that gcc does not understand *) (* assume anything starting with "ccured" is ours *) (* also don't print the 'combiner' pragma *) (* nor 'cilnoremove' *) let suppress = not !print_CIL_Input && ((startsWith "box" an) || (startsWith "ccured" an) || (an = "merger") || (an = "cilnoremove")) in let d = match an, args with | _, [] -> text an | "weak", [ACons (symbol, [])] -> text "weak " ++ text symbol | _ -> text (an ^ "(") ++ docList ~sep:(chr ',') (self#pAttrParam ()) () args ++ text ")" in self#pLineDirective l ++ (if suppress then text "/* " else text "") ++ (text "#pragma ") ++ d ++ (if suppress then text " */\n" else text "\n") | GText s -> if s <> "//" then text s ++ text "\n" else nil method! dGlobal (out: out_channel) (g: global) : unit = (* For all except functions and variable with initializers, use the pGlobal *) match g with GFun (fdec, l) -> (* If the function has attributes then print a prototype because GCC cannot accept function attributes in a definition *) let oldattr = fdec.svar.vattr in let proto = if oldattr <> [] then (self#pLineDirective l) ++ (self#pVDecl () fdec.svar) ++ chr ';' ++ line else nil in fprint out ~width:80 (proto ++ (self#pLineDirective ~forcefile:true l)); (* Temporarily remove the function attributes *) fdec.svar.vattr <- []; fprint out ~width:80 (self#pFunDecl () fdec); fdec.svar.vattr <- oldattr; output_string out "\n" | GVar (vi, {init = Some i}, l) -> begin fprint out ~width:80 (self#pLineDirective ~forcefile:true l ++ self#pVDecl () vi ++ text " = " ++ (let islong = match i with CompoundInit (_, il) when List.length il >= 8 -> true | _ -> false in if islong then line ++ self#pLineDirective l ++ text " " else nil)); self#dInit out 3 i; output_string out ";\n" end | g -> fprint out ~width:80 (self#pGlobal () g) method! pFieldDecl () fi = self#pLineDirective fi.floc ++ (self#pType (Some (text (if fi.fname = missingFieldName then "" else fi.fname))) () fi.ftype) ++ text " " ++ (match fi.fbitfield with None -> nil | Some i -> text ": " ++ num i ++ text " ") ++ self#pAttrs () fi.fattr ++ text ";" method private pFunDecl () f = H.add genvHtbl f.svar.vname f.svar;(* add function to global env *) H.clear lenvHtbl; (* new local environment *) (* add the arguments to the local environment *) List.iter (fun vi -> H.add lenvHtbl vi.vname vi) f.sformals; let nf = if !doElimTemps then RCT.eliminate_temps f else f in let decls = docList ~sep:line (fun vi -> self#pVDecl () vi ++ text ";") () nf.slocals in self#pVDecl () nf.svar ++ line ++ text "{ " ++ (align (* locals. *) ++ decls ++ line ++ line (* the body *) ++ ((* remember the declaration *) super#setCurrentFormals nf.sformals; let body = self#pBlock () nf.sbody in super#setCurrentFormals []; body)) ++ line ++ text "}" method! private pStmtKind (next : stmt) () (sk : stmtkind) = match sk with | Loop(b,l,el,_,_) -> begin (* See if we can turn this into a while(e) {} *) (* TODO: See if we can turn this into a do { } while(e); *) let co, bodystmts = get_loop_condition b in match co with | None -> super#pStmtKind next () sk | Some c -> begin self#pLineDirective l ++ text "wh" ++ (align ++ text "ile (" ++ self#pExp () (simpl_cond (UnOp(LNot,c,intType))) ++ text ") " ++ self#pBlock () {bstmts=bodystmts; battrs=b.battrs}) end end | _ -> super#pStmtKind next () sk end (* class zraCilPrinterClass *) let zraCilPrinter = new zraCilPrinterClass (* pretty print an expression *) let pp_exp (fd : fundec) () (e : exp) = deputyAttrs := true; ignore(RCT.eliminateTempsForExpPrinting fd); let d = zraCilPrinter#pExp () e in deputyAttrs := false; d let feature = { fd_name = "zrapp"; fd_enabled = false; fd_description = "pretty printing with checks for name conflicts and\n\t\t\t\ttemp variable elimination"; fd_extraopt = [ "--zrapp_elim_temps", Arg.Unit (fun n -> doElimTemps := true), "Try to eliminate temporary variables during pretty printing"; "--zrapp_debug", Arg.Unit (fun n -> debug := true; RD.debug := true), "Lots of debugging info for pretty printing and reaching definitions"; "--zrapp_debug_fn", Arg.String (fun s -> RD.debug_fn := s), "Only output debugging info for one function"; "--zrapp_comments", Arg.Unit (fun _ -> printComments := true), "Print comments from source file in output";]; fd_doit = (function (f: file) -> lineDirectiveStyle := None; printerForMaincil := zraCilPrinter); fd_post_check = false } let () = Feature.register feature
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>