(** * Programs and compilation units *)
(** The definitions in this section are parameterized over a type [F] of internal function definitions, so that they apply both to CompCert C and to Clight. *)
Set Implicit Arguments.
Section PROGRAMS.
Variable F: Type.
(** Functions can either be defined ([Internal]) or declared as external functions ([External]). *)
Inductive fundef : Type :=
| Internal: F -> fundef
| External: external_function -> typelist -> type -> calling_convention -> fundef.
(** A program, or compilation unit, is composed of: - a list of definitions of functions and global variables; - the names of functions and global variables that are public (not static); - the name of the function that acts as entry point ("main" function). - a list of definitions for structure and union names - the corresponding composite environment - a proof that this environment is consistent with the definitions. *)
Record program : Type := {
prog_defs: list (ident * globdef fundef type);
prog_public: list ident;
prog_main: ident;
prog_types: list composite_definition;
prog_comp_env: composite_env;
prog_comp_env_eq: build_composite_env prog_types = OK prog_comp_env
}.
Definition program_of_program (p: program) : AST.program fundef type :=
{| AST.prog_defs := p.(prog_defs);
AST.prog_public := p.(prog_public);
AST.prog_main := p.(prog_main) |}.
Coercion program_of_program: program >-> AST.program.
Program Definition make_program (types: list composite_definition)
(defs: list (ident * globdef fundef type))
(public: list ident)
(main: ident) : res program :=
match build_composite_env types with
| Error e => Error e
| OK ce =>
OK {| prog_defs := defs;
prog_public := public;
prog_main := main;
prog_types := types;
prog_comp_env := ce;
prog_comp_env_eq := _ |}
end.
End PROGRAMS.
Arguments External {F} _ _ _ _.
Unset Implicit Arguments.
(** * Separate compilation and linking *)
(** ** Linking types *)
Instance Linker_types : Linker type := {
link := fun t1 t2 => if type_eq t1 t2 then Some t1 else None;
linkorder := fun t1 t2 => t1 = t2
}.
Instance Linker_composite_defs : Linker (list composite_definition) := {
link := link_composite_defs;
linkorder := @List.incl composite_definition
}.

Lemma composite_eq:
forall su1 m1 a1 sz1 al1 r1 pos1 al2p1 szal1
su2 m2 a2 sz2 al2 r2 pos2 al2p2 szal2,
su1 = su2 -> m1 = m2 -> a1 = a2 -> sz1 = sz2 -> al1 = al2 -> r1 = r2 ->
Build_composite su1 m1 a1 sz1 al1 r1 pos1 al2p1 szal1 = Build_composite su2 m2 a2 sz2 al2 r2 pos2 al2p2 szal2.