util.mli 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. (** Utility functions used by multiple phases. *)
  2. (** {2 Generating variables} *)
  3. (** Generate a new identifier from a given base, e.g. ["foo"] becomes
  4. ["_foo_1"]. Uses an [int] reference defined in the implementation file as a
  5. counter to assert uniqueness of the generated variable. *)
  6. val fresh_id : string -> string
  7. (** Generate a new constant (a variable that is known to be assigned only once
  8. in total) from a given id, e.g. ["foo"] becomes ["_foo_1_"]. Uses the same
  9. counter as {!fresh_id}. *)
  10. val fresh_const : string -> string
  11. (** Check if an identifier is generated by the compiler. *)
  12. val is_generated_id : string -> bool
  13. (** Check if an identifier is a constant generated by the compiler. *)
  14. val is_const_id : string -> bool
  15. (** Generate an identifier from a base and a number. E.g., [generate_id "foo"
  16. 1] returns ["_foo_1"]*)
  17. val generate_id : string -> int -> string
  18. (** {2 AST traversal} *)
  19. (** Default transformation traversal for AST nodes of arbitrary constructor.
  20. For each constructor that has one or more children of type {!Types.node},
  21. the children are transformed with the given transformation function, and a
  22. new node is returned with the transformed children. For [Program] nodes,
  23. {!flatten_blocks} is called on the resulting declaration list. This allows
  24. the transformation function to return multiple nodes as a replacement, in
  25. the form of a [Block] node with multiple children. *)
  26. val transform_children : (Types.node -> Types.node) -> Types.node -> Types.node
  27. (** Flatten [Block] nodes into containing node lists.
  28. E.g., [[A; Block [B; Block [C; D]]; E] -> [A; B; C; D; E]].
  29. Traverses into elements of the list recursively first, so this is
  30. essentially a traversal, as can be seen in the example. Therefore the best
  31. practice is to call this function once on the children of a [Program] node
  32. after any traversal that may generate [Block] nodes that need to be
  33. flattened. *)
  34. val flatten_blocks : Types.node list -> Types.node list
  35. (** Extract the list of child nodes from a [Block] node. *)
  36. val block_body : Types.node -> Types.node list
  37. (** {2 AST node annotations} *)
  38. (** Add a single annotation to a node. *)
  39. val annotate : Types.annotation -> Types.node -> Types.node
  40. (** Extract annotations from a node of arbitrary constructor. *)
  41. val annof : Types.node -> Types.annotation list
  42. (** Get the value of the [Loc] annotation of a node, or {!noloc} if no location
  43. can be found. *)
  44. val locof : Types.node -> Types.location
  45. (** Empty node location: [("", 0, 0, 0, 0)]. Used then location of a node is
  46. unknown (if the annotation was removed at some point) or
  47. non-existant/irrelevant (for generated nodes on which no errors will occur --
  48. hopefully...). *)
  49. val noloc : Types.location
  50. (** Get the value of the [Depth] annotation of a node. Raises
  51. {!Types.InvalidNode} if the annotation can not be found. *)
  52. val depthof : Types.node -> int
  53. (** Get the value of the [Index] annotation of a node. Raises
  54. {!Types.InvalidNode} if the annotation can not be found. *)
  55. val indexof : Types.node -> int
  56. (** Get the value of the [Type] annotation of a node. Some node types
  57. do not need to be annotated since they have inherent types. For example, a
  58. [VarDec] node has a type attribute, and a [Dim] node is always an [Int]
  59. (because array dimensions are integers). All nodes which have inherent types
  60. are [VarDec], [Param], [FunDec], [FunDef], [GlobalDec], [GlobalDef],
  61. [TypeCast], and [Dim]. Raises a {!Types.InvalidNode} if the annotation can
  62. not be found, and the type has no inherent type. *)
  63. val typeof : Types.node -> Types.ctype
  64. (** Get the value of the [LabelName] annotation of a node. Raises
  65. {!Types.InvalidNode} if the annotation can not be found. *)
  66. val labelof : Types.node -> string
  67. (** Get the basic type of a declaration, removing array dimensions *)
  68. val basetypeof : Types.node -> Types.ctype
  69. (** Get the value of the name attribute from a variable or function declaration
  70. (similar to {!typeof} for nodes that have types attributes). Raises
  71. {!Types.InvalidNode} if the node is not one of [GlobalDec], [GlobalDef],
  72. [FunDec], [FunDef], [VarDec], [Param], or [Dim]. *)
  73. val nameof : Types.node -> string
  74. (** Get the CiviC data type of a constant value. *)
  75. val const_type : Types.const -> Types.ctype
  76. (** Check if a constant value is eligible for creating optimized assembly
  77. instructions. E.g. [Intval (-1)] is eligible because the instruction
  78. [iloadc_m1] exists. Used to decide on which {!Types.instr} constructor to
  79. use during the assembly phases. This function always returns [false] when
  80. optimizations are disabled (when {!Globals.args}[.optimize = false]). *)
  81. val is_immediate_const : Types.const -> bool
  82. (** Check if a node has an array type. I.e., [Array] or [ArrayDims]. *)
  83. val is_array : Types.node -> bool
  84. (** {2 Logging} *)
  85. (** Horizontal line of ['-'] characters, used to separate output sections. *)
  86. val hline : string
  87. (** Print the stringification of a node to [stderr] (uses
  88. {!Stringify.node2str}). *)
  89. val prt_node : Types.node -> unit
  90. (** Output a line to stderr if the verbosity level in {!Globals.args} is at
  91. least as high as the specified verbosity level. The line is indented with a
  92. number of spaces to match the longest phase identifier (so that logged lines
  93. align with ideitifiers logged by {!Main.main}). A newline is added
  94. automatically. *)
  95. val log_line : int -> string -> unit
  96. (** Print a line to [stderr] without indent (but do add a newline). *)
  97. val log_plain_line : int -> string -> unit
  98. (** Same as {!log_line}, but prints a node stringification instead of a literal
  99. string. *)
  100. val log_node : int -> Types.node -> unit
  101. (** Generate an Types.location tuple from Lexing data structures *)
  102. val loc_from_lexpos : Lexing.position -> Lexing.position -> Types.location
  103. (** Print location tuple to stderr. Produces
  104. something like the following: {v
  105. int foo;
  106. ^^^ v}
  107. The location tuple is likely to originate from a node annotation, extracted
  108. using {!locof}.*)
  109. val prerr_loc : Types.location -> unit
  110. (** Print location tuple to stderr, along with an error message. Produces
  111. something like the following: {v
  112. File "foo.cvc", line 10, characters 8-11:
  113. <message>
  114. int foo;
  115. ^^^ v} *)
  116. val prerr_loc_msg : Types.location -> string -> unit
  117. (** Print an error message for a node. Calls {!prerr_loc_msg}. *)
  118. val node_error : Types.node -> string -> unit
  119. (** Print a warning message for a node. Calls {!prerr_loc_msg}. *)
  120. val node_warning : Types.node -> string -> unit
  121. (** {2 String utilities} *)
  122. (** [repeat s n] returns a new string of [n] times [s]. *)
  123. val repeat : string -> int -> string
  124. (** [expand n s] adds spaces to [s] until the resulting string is at least [n]
  125. characters long. *)
  126. val expand : int -> string -> string
  127. (** {2 List utilities} *)
  128. (** [optmap f opt] maps [f] to the list value of [opt] if [opt] exists, and
  129. [None] otherwise. *)
  130. val optmap : ('a -> 'b) -> 'a list option -> 'b list option
  131. (** Same as {!optmap}, but returns the list value instead, or an empty list if
  132. [opt] is [None]. *)
  133. val optmapl : ('a -> 'b) -> 'a list option -> 'b list
  134. (** [List.mapi] clone (only available in OCaml version >= 4.00. Maps a function
  135. to a list like [List.map] does, but the iterator function is called with the
  136. element's index as an additional argument. *)
  137. val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list