source: Deliverables/D2.2/8051/src/clight/runtime.ml @ 624

Last change on this file since 624 was 619, checked in by ayache, 9 years ago

Update of D2.2 from Paris.

File size: 1.8 KB
Line 
1
2(** This module adds runtime functions in a [Clight] program. Some of these
3    newly defined functions will be used in the Clight32ToClight8 transformation
4    to replace primitive unary or binary operations. They are returned as
5    association lists. The other functions implement unsupported functions by
6    the target architecture that introduce a branch. We need to define them at
7    the [Clight] level in order to have a correct labelling. *)
8
9
10let cint8 = Clight.Tint (Clight.I8, Clight.Signed)
11let cint32 = Clight.Tint (Clight.I32, Clight.Signed)
12
13
14let div_fun s =
15  "signed char " ^ s ^ " (signed char x, signed char y) {\n" ^
16  "  signed char x1 = x, y1 = y, sign = 1;\n" ^
17  "  if (x1 < 0) { x1 = -x1; sign = -sign; }\n" ^
18  "  if (y1 < 0) { y1 = -y1; sign = -sign; }\n" ^
19  "  return (sign * (x1/y1));\n" ^
20  "}\n\n"
21
22let div = ("_div", div_fun, (Clight.Odiv, cint8, cint8))
23
24
25let make_new_ops_assoc labs l =
26  let f_op (base_name, op_fun, primitive_op) =
27    let fresh_name = StringTools.Gen.fresh_prefix labs base_name in
28    (op_fun fresh_name, (primitive_op, fresh_name)) in
29  List.split (List.map f_op l)
30
31
32let add_new_ops new_ops p =
33  let added_string =
34    List.fold_left (fun s new_op -> s ^ "\n" ^ new_op) "" new_ops in
35  let output = added_string ^ (ClightPrinter.print_program p) in
36  let tmp_file = Filename.temp_file "add_runtime" ".c" in
37  let cout = open_out tmp_file in
38  output_string cout output ;
39  flush cout ;
40  close_out cout ;
41  let p = ClightParser.process tmp_file in
42  Misc.SysExt.safe_remove tmp_file ;
43  p
44
45
46let add p =
47  let labs = ClightAnnotator.all_labels p in
48  let make_new_ops_assoc l = make_new_ops_assoc labs l in
49  let (new_unops, unop_assoc) = make_new_ops_assoc [] in
50  let (new_binops, binop_assoc) = make_new_ops_assoc [div] in
51  let p = add_new_ops (new_unops @ new_binops) p in
52  (p, unop_assoc, binop_assoc)
Note: See TracBrowser for help on using the repository browser.