src/Tools/code/code_target.ML
author haftmann
Tue Sep 02 20:38:17 2008 +0200 (2008-09-02)
changeset 28090 29af3c712d2b
parent 28064 d4a6460c53d1
child 28663 bd8438543bf2
permissions -rw-r--r--
distributed literal code generation out of central infrastructure
haftmann@24219
     1
(*  Title:      Tools/code/code_target.ML
haftmann@24219
     2
    ID:         $Id$
haftmann@24219
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     4
haftmann@28054
     5
Serializer from intermediate language ("Thin-gol") to target languages.
haftmann@24219
     6
*)
haftmann@24219
     7
haftmann@24219
     8
signature CODE_TARGET =
haftmann@24219
     9
sig
haftmann@28054
    10
  include CODE_PRINTER
haftmann@28054
    11
haftmann@28054
    12
  type serializer
haftmann@28064
    13
  val add_target: string * (serializer * literals) -> theory -> theory
haftmann@28054
    14
  val extend_target: string * (string * (Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    15
    -> theory -> theory
haftmann@28054
    16
  val assert_target: theory -> string -> string
haftmann@24219
    17
haftmann@28054
    18
  type destination
haftmann@28054
    19
  type serialization
haftmann@28054
    20
  val parse_args: (OuterLex.token list -> 'a * OuterLex.token list)
haftmann@28054
    21
    -> OuterLex.token list -> 'a
haftmann@28054
    22
  val stmt_names_of_destination: destination -> string list
haftmann@28054
    23
  val code_of_pretty: Pretty.T -> string
haftmann@28054
    24
  val code_writeln: Pretty.T -> unit
haftmann@28054
    25
  val mk_serialization: string -> ('a -> unit) option
haftmann@28054
    26
    -> (Path.T option -> 'a -> unit)
haftmann@28054
    27
    -> ('a -> string * string list)
haftmann@28054
    28
    -> 'a -> serialization
haftmann@28054
    29
  val serialize: theory -> string -> string option -> Args.T list
haftmann@28054
    30
    -> Code_Thingol.program -> string list -> serialization
haftmann@28064
    31
  val serialize_custom: theory -> string * (serializer * literals)
haftmann@28054
    32
    -> Code_Thingol.program -> string list -> string * string list
haftmann@28090
    33
  val the_literals: theory -> string -> literals
haftmann@28054
    34
  val compile: serialization -> unit
haftmann@28054
    35
  val export: serialization -> unit
haftmann@28054
    36
  val file: Path.T -> serialization -> unit
haftmann@28054
    37
  val string: string list -> serialization -> string
haftmann@28054
    38
haftmann@28054
    39
  val code_of: theory -> string -> string -> string list -> string list -> string
haftmann@28054
    40
  val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit
haftmann@28054
    41
  val code_width: int ref
haftmann@28054
    42
haftmann@28054
    43
  val allow_abort: string -> theory -> theory
haftmann@24219
    44
  val add_syntax_class: string -> class
haftmann@28054
    45
    -> (string * (string * string) list) option -> theory -> theory
haftmann@28054
    46
  val add_syntax_inst: string -> string * class -> bool -> theory -> theory
haftmann@28054
    47
  val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
haftmann@24219
    48
  val add_syntax_tycoP: string -> string -> OuterParse.token list
haftmann@28054
    49
    -> (theory -> theory) * OuterParse.token list
haftmann@28054
    50
  val add_syntax_const: string -> string -> const_syntax option -> theory -> theory
haftmann@24219
    51
  val add_syntax_constP: string -> string -> OuterParse.token list
haftmann@28054
    52
    -> (theory -> theory) * OuterParse.token list
haftmann@28054
    53
  val add_reserved: string -> string -> theory -> theory
haftmann@24219
    54
end;
haftmann@24219
    55
haftmann@28054
    56
structure Code_Target : CODE_TARGET =
haftmann@24219
    57
struct
haftmann@24219
    58
haftmann@28054
    59
open Basic_Code_Thingol;
haftmann@28054
    60
open Code_Printer;
haftmann@24219
    61
haftmann@24219
    62
(** basics **)
haftmann@24219
    63
haftmann@27304
    64
datatype destination = Compile | Export | File of Path.T | String of string list;
haftmann@27436
    65
type serialization = destination -> (string * string list) option;
haftmann@27014
    66
haftmann@27103
    67
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*)
haftmann@27103
    68
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f);
haftmann@27103
    69
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n";
haftmann@27103
    70
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p;
haftmann@26998
    71
haftmann@27103
    72
(*FIXME why another code_setmp?*)
haftmann@27103
    73
fun compile f = (code_setmp f Compile; ());
haftmann@27304
    74
fun export f = (code_setmp f Export; ());
haftmann@27103
    75
fun file p f = (code_setmp f (File p); ());
haftmann@27436
    76
fun string cs f = fst (the (code_setmp f (String cs)));
haftmann@27304
    77
haftmann@27304
    78
fun stmt_names_of_destination (String stmts) = stmts
haftmann@27304
    79
  | stmt_names_of_destination _ = [];
haftmann@27014
    80
haftmann@28054
    81
fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE)
haftmann@28054
    82
  | mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation")
haftmann@28054
    83
  | mk_serialization target _ output _ code Export = (output NONE code ; NONE)
haftmann@28054
    84
  | mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE)
haftmann@28054
    85
  | mk_serialization target _ _ string code (String _) = SOME (string code);
haftmann@27550
    86
haftmann@24219
    87
haftmann@28054
    88
(** theory data **)
haftmann@28054
    89
haftmann@28054
    90
datatype name_syntax_table = NameSyntaxTable of {
haftmann@28054
    91
  class: class_syntax Symtab.table,
haftmann@28054
    92
  inst: unit Symtab.table,
haftmann@28054
    93
  tyco: tyco_syntax Symtab.table,
haftmann@28054
    94
  const: const_syntax Symtab.table
haftmann@28054
    95
};
haftmann@27000
    96
haftmann@28054
    97
fun mk_name_syntax_table ((class, inst), (tyco, const)) =
haftmann@28054
    98
  NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const };
haftmann@28054
    99
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) =
haftmann@28054
   100
  mk_name_syntax_table (f ((class, inst), (tyco, const)));
haftmann@28054
   101
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 },
haftmann@28054
   102
    NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
haftmann@28054
   103
  mk_name_syntax_table (
haftmann@28054
   104
    (Symtab.join (K snd) (class1, class2),
haftmann@28054
   105
       Symtab.join (K snd) (inst1, inst2)),
haftmann@28054
   106
    (Symtab.join (K snd) (tyco1, tyco2),
haftmann@28054
   107
       Symtab.join (K snd) (const1, const2))
haftmann@28054
   108
  );
haftmann@28054
   109
haftmann@28054
   110
type serializer =
haftmann@28054
   111
  string option                         (*module name*)
haftmann@28054
   112
  -> Args.T list                        (*arguments*)
haftmann@28054
   113
  -> (string -> string)                 (*labelled_name*)
haftmann@28054
   114
  -> string list                        (*reserved symbols*)
haftmann@28054
   115
  -> (string * Pretty.T) list           (*includes*)
haftmann@28054
   116
  -> (string -> string option)          (*module aliasses*)
haftmann@28054
   117
  -> (string -> class_syntax option)
haftmann@28054
   118
  -> (string -> tyco_syntax option)
haftmann@28054
   119
  -> (string -> const_syntax option)
haftmann@28054
   120
  -> Code_Thingol.program
haftmann@28054
   121
  -> string list                        (*selected statements*)
haftmann@28054
   122
  -> serialization;
haftmann@27000
   123
haftmann@28064
   124
datatype serializer_entry = Serializer of serializer * literals
haftmann@28054
   125
  | Extends of string * (Code_Thingol.program -> Code_Thingol.program);
haftmann@28054
   126
haftmann@28054
   127
datatype target = Target of {
haftmann@28054
   128
  serial: serial,
haftmann@28054
   129
  serializer: serializer_entry,
haftmann@28054
   130
  reserved: string list,
haftmann@28054
   131
  includes: Pretty.T Symtab.table,
haftmann@28054
   132
  name_syntax_table: name_syntax_table,
haftmann@28054
   133
  module_alias: string Symtab.table
haftmann@28054
   134
};
haftmann@27103
   135
haftmann@28054
   136
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
haftmann@28054
   137
  Target { serial = serial, serializer = serializer, reserved = reserved, 
haftmann@28054
   138
    includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
haftmann@28054
   139
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
haftmann@28054
   140
  mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
haftmann@28054
   141
fun merge_target strict target (Target { serial = serial1, serializer = serializer,
haftmann@28054
   142
  reserved = reserved1, includes = includes1,
haftmann@28054
   143
  name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
haftmann@28054
   144
    Target { serial = serial2, serializer = _,
haftmann@28054
   145
      reserved = reserved2, includes = includes2,
haftmann@28054
   146
      name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
haftmann@28054
   147
  if serial1 = serial2 orelse not strict then
haftmann@28054
   148
    mk_target ((serial1, serializer),
haftmann@28054
   149
      ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
haftmann@28054
   150
        (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
haftmann@28054
   151
          Symtab.join (K snd) (module_alias1, module_alias2))
haftmann@28054
   152
    ))
haftmann@28054
   153
  else
haftmann@28054
   154
    error ("Incompatible serializers: " ^ quote target);
haftmann@27103
   155
haftmann@28054
   156
structure CodeTargetData = TheoryDataFun
haftmann@27436
   157
(
haftmann@28054
   158
  type T = target Symtab.table * string list;
haftmann@28054
   159
  val empty = (Symtab.empty, []);
haftmann@28054
   160
  val copy = I;
haftmann@28054
   161
  val extend = I;
haftmann@28054
   162
  fun merge _ ((target1, exc1) : T, (target2, exc2)) =
haftmann@28054
   163
    (Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2));
haftmann@27436
   164
);
haftmann@27436
   165
haftmann@28054
   166
fun the_serializer (Target { serializer, ... }) = serializer;
haftmann@28054
   167
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@28054
   168
fun the_includes (Target { includes, ... }) = includes;
haftmann@28054
   169
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
haftmann@28054
   170
fun the_module_alias (Target { module_alias , ... }) = module_alias;
haftmann@27437
   171
haftmann@28054
   172
val abort_allowed = snd o CodeTargetData.get;
haftmann@27436
   173
haftmann@28054
   174
fun assert_target thy target =
haftmann@28054
   175
  case Symtab.lookup (fst (CodeTargetData.get thy)) target
haftmann@28054
   176
   of SOME data => target
haftmann@28054
   177
    | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   178
haftmann@28054
   179
fun put_target (target, seri) thy =
haftmann@27304
   180
  let
haftmann@28054
   181
    val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy));
haftmann@28054
   182
    val _ = case seri
haftmann@28054
   183
     of Extends (super, _) => if defined_target super then ()
haftmann@28054
   184
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   185
      | _ => ();
haftmann@28054
   186
    val _ = if defined_target target
haftmann@28054
   187
      then warning ("Overwriting existing target " ^ quote target)
haftmann@28054
   188
      else ();
haftmann@28054
   189
  in
haftmann@28054
   190
    thy
haftmann@28054
   191
    |> (CodeTargetData.map o apfst oo Symtab.map_default)
haftmann@28054
   192
          (target, mk_target ((serial (), seri), (([], Symtab.empty),
haftmann@28054
   193
            (mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
haftmann@28054
   194
              Symtab.empty))))
haftmann@28054
   195
          ((map_target o apfst o apsnd o K) seri)
haftmann@28054
   196
  end;
haftmann@27436
   197
haftmann@28054
   198
fun add_target (target, seri) = put_target (target, Serializer seri);
haftmann@28054
   199
fun extend_target (target, (super, modify)) =
haftmann@28054
   200
  put_target (target, Extends (super, modify));
haftmann@27436
   201
haftmann@28054
   202
fun map_target_data target f thy =
haftmann@27436
   203
  let
haftmann@28054
   204
    val _ = assert_target thy target;
haftmann@28054
   205
  in
haftmann@28054
   206
    thy
haftmann@28054
   207
    |> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
haftmann@28054
   208
  end;
haftmann@27304
   209
haftmann@28054
   210
fun map_reserved target =
haftmann@28054
   211
  map_target_data target o apsnd o apfst o apfst;
haftmann@28054
   212
fun map_includes target =
haftmann@28054
   213
  map_target_data target o apsnd o apfst o apsnd;
haftmann@28054
   214
fun map_name_syntax target =
haftmann@28054
   215
  map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
haftmann@28054
   216
fun map_module_alias target =
haftmann@28054
   217
  map_target_data target o apsnd o apsnd o apsnd;
haftmann@27000
   218
haftmann@27000
   219
haftmann@28054
   220
(** serializer configuration **)
haftmann@27000
   221
haftmann@27000
   222
(* data access *)
haftmann@24219
   223
haftmann@24219
   224
local
haftmann@24219
   225
haftmann@24992
   226
fun cert_class thy class =
haftmann@24992
   227
  let
haftmann@24992
   228
    val _ = AxClass.get_info thy class;
haftmann@24992
   229
  in class end;
haftmann@24992
   230
haftmann@27103
   231
fun read_class thy = cert_class thy o Sign.intern_class thy;
haftmann@24992
   232
haftmann@24992
   233
fun cert_tyco thy tyco =
haftmann@24992
   234
  let
haftmann@24992
   235
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@24992
   236
      else error ("No such type constructor: " ^ quote tyco);
haftmann@24992
   237
  in tyco end;
haftmann@24992
   238
haftmann@27103
   239
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
haftmann@24219
   240
haftmann@24219
   241
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
haftmann@24219
   242
  let
haftmann@24841
   243
    val class = prep_class thy raw_class;
haftmann@28054
   244
    val class' = Code_Name.class thy class;
haftmann@24841
   245
    fun mk_classparam c = case AxClass.class_of_param thy c
haftmann@28054
   246
     of SOME class'' => if class = class'' then Code_Name.const thy c
haftmann@24219
   247
          else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
haftmann@24219
   248
      | NONE => error ("Not a class operation: " ^ quote c);
haftmann@24841
   249
    fun mk_syntax_params raw_params = AList.lookup (op =)
haftmann@24841
   250
      ((map o apfst) (mk_classparam o prep_const thy) raw_params);
haftmann@24219
   251
  in case raw_syn
haftmann@24841
   252
   of SOME (syntax, raw_params) =>
haftmann@24219
   253
      thy
haftmann@27024
   254
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   255
           (Symtab.update (class', (syntax, mk_syntax_params raw_params)))
haftmann@24219
   256
    | NONE =>
haftmann@24219
   257
      thy
haftmann@27024
   258
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   259
           (Symtab.delete_safe class')
haftmann@24219
   260
  end;
haftmann@24219
   261
haftmann@24219
   262
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
haftmann@24219
   263
  let
haftmann@28054
   264
    val inst = Code_Name.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
haftmann@24219
   265
  in if add_del then
haftmann@24219
   266
    thy
haftmann@27024
   267
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   268
        (Symtab.update (inst, ()))
haftmann@24219
   269
  else
haftmann@24219
   270
    thy
haftmann@27024
   271
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   272
        (Symtab.delete_safe inst)
haftmann@24219
   273
  end;
haftmann@24219
   274
haftmann@24219
   275
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
haftmann@24219
   276
  let
haftmann@24219
   277
    val tyco = prep_tyco thy raw_tyco;
haftmann@28054
   278
    val tyco' = if tyco = "fun" then "fun" else Code_Name.tyco thy tyco;
haftmann@24219
   279
    fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
haftmann@24219
   280
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@24219
   281
      else syntax
haftmann@24219
   282
  in case raw_syn
haftmann@24219
   283
   of SOME syntax =>
haftmann@24219
   284
      thy
haftmann@27024
   285
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   286
           (Symtab.update (tyco', check_args syntax))
haftmann@24219
   287
   | NONE =>
haftmann@24219
   288
      thy
haftmann@27024
   289
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   290
           (Symtab.delete_safe tyco')
haftmann@24219
   291
  end;
haftmann@24219
   292
haftmann@24219
   293
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
haftmann@24219
   294
  let
haftmann@24219
   295
    val c = prep_const thy raw_c;
haftmann@28054
   296
    val c' = Code_Name.const thy c;
haftmann@28054
   297
    fun check_args (syntax as (n, _)) = if n > Code_Unit.no_args thy c
haftmann@24423
   298
      then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@24219
   299
      else syntax;
haftmann@24219
   300
  in case raw_syn
haftmann@24219
   301
   of SOME syntax =>
haftmann@24219
   302
      thy
haftmann@27024
   303
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   304
           (Symtab.update (c', check_args syntax))
haftmann@24219
   305
   | NONE =>
haftmann@24219
   306
      thy
haftmann@27024
   307
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   308
           (Symtab.delete_safe c')
haftmann@24219
   309
  end;
haftmann@24219
   310
haftmann@24219
   311
fun add_reserved target =
haftmann@24219
   312
  let
haftmann@24219
   313
    fun add sym syms = if member (op =) syms sym
haftmann@24219
   314
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@24219
   315
      else insert (op =) sym syms
haftmann@27103
   316
  in map_reserved target o add end;
haftmann@24992
   317
haftmann@24992
   318
fun add_include target =
haftmann@24992
   319
  let
haftmann@24992
   320
    fun add (name, SOME content) incls =
haftmann@24992
   321
          let
haftmann@24992
   322
            val _ = if Symtab.defined incls name
haftmann@24992
   323
              then warning ("Overwriting existing include " ^ name)
haftmann@24992
   324
              else ();
haftmann@24992
   325
          in Symtab.update (name, str content) incls end
haftmann@24992
   326
      | add (name, NONE) incls =
haftmann@24992
   327
          Symtab.delete name incls;
haftmann@27103
   328
  in map_includes target o add end;
haftmann@24219
   329
haftmann@27103
   330
fun add_module_alias target =
haftmann@28054
   331
  map_module_alias target o Symtab.update o apsnd Code_Name.check_modulename;
haftmann@24219
   332
haftmann@27103
   333
fun gen_allow_abort prep_cs raw_c thy =
haftmann@24841
   334
  let
haftmann@24841
   335
    val c = prep_cs thy raw_c;
haftmann@28054
   336
    val c' = Code_Name.const thy c;
haftmann@24841
   337
  in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
haftmann@24841
   338
haftmann@24219
   339
fun zip_list (x::xs) f g =
haftmann@24219
   340
  f
haftmann@24219
   341
  #-> (fn y =>
haftmann@24219
   342
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@24219
   343
    #-> (fn xys => pair ((x, y) :: xys)));
haftmann@24219
   344
haftmann@27000
   345
haftmann@27103
   346
(* concrete syntax *)
haftmann@27000
   347
haftmann@24219
   348
structure P = OuterParse
haftmann@24219
   349
and K = OuterKeyword
haftmann@24219
   350
haftmann@24219
   351
fun parse_multi_syntax parse_thing parse_syntax =
haftmann@24219
   352
  P.and_list1 parse_thing
haftmann@24219
   353
  #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
haftmann@24219
   354
        (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
haftmann@24219
   355
haftmann@24219
   356
in
haftmann@24219
   357
haftmann@24219
   358
val parse_syntax = parse_syntax;
haftmann@24219
   359
haftmann@24219
   360
val add_syntax_class = gen_add_syntax_class cert_class (K I);
haftmann@24219
   361
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
haftmann@24219
   362
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
haftmann@24219
   363
val add_syntax_const = gen_add_syntax_const (K I);
haftmann@27103
   364
val allow_abort = gen_allow_abort (K I);
haftmann@28054
   365
val add_reserved = add_reserved;
haftmann@24219
   366
haftmann@28054
   367
val add_syntax_class_cmd = gen_add_syntax_class read_class Code_Unit.read_const;
haftmann@24219
   368
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
haftmann@24219
   369
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
haftmann@28054
   370
val add_syntax_const_cmd = gen_add_syntax_const Code_Unit.read_const;
haftmann@28054
   371
val allow_abort_cmd = gen_allow_abort Code_Unit.read_const;
haftmann@24219
   372
haftmann@28054
   373
fun add_syntax_tycoP target tyco = parse_syntax I
haftmann@28054
   374
  >> add_syntax_tyco_cmd target tyco;
haftmann@28054
   375
fun add_syntax_constP target c = parse_syntax fst
haftmann@28054
   376
  >> (add_syntax_const_cmd target c o Code_Printer.simple_const_syntax);
haftmann@24219
   377
haftmann@28064
   378
fun the_literals thy =
haftmann@28064
   379
  let
haftmann@28064
   380
    val (targets, _) = CodeTargetData.get thy;
haftmann@28064
   381
    fun literals target = case Symtab.lookup targets target
haftmann@28064
   382
     of SOME data => (case the_serializer data
haftmann@28064
   383
         of Serializer (_, literals) => literals
haftmann@28064
   384
          | Extends (super, _) => literals super)
haftmann@28064
   385
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28064
   386
  in literals end;
haftmann@28064
   387
wenzelm@24867
   388
haftmann@28054
   389
(** serializer usage **)
haftmann@28054
   390
haftmann@28054
   391
(* montage *)
haftmann@28054
   392
haftmann@28054
   393
fun invoke_serializer thy modify abortable serializer reserved includes 
haftmann@28054
   394
    module_alias class inst tyco const module args program1 cs1 =
haftmann@28054
   395
  let
haftmann@28054
   396
    val program2 = modify program1;
haftmann@28054
   397
    val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const;
haftmann@28054
   398
    val cs2 = subtract (op =) hidden cs1;
haftmann@28054
   399
    val program3 = Graph.subgraph (not o member (op =) hidden) program2;
haftmann@28054
   400
    val all_cs = Graph.all_succs program2 cs2;
haftmann@28054
   401
    val program4 = Graph.subgraph (member (op =) all_cs) program3;
haftmann@28054
   402
    val empty_funs = filter_out (member (op =) abortable)
haftmann@28054
   403
      (Code_Thingol.empty_funs program3);
haftmann@28054
   404
    val _ = if null empty_funs then () else error ("No defining equations for "
haftmann@28054
   405
      ^ commas (map (Code_Name.labelled_name thy) empty_funs));
haftmann@28054
   406
  in
haftmann@28054
   407
    serializer module args (Code_Name.labelled_name thy) reserved includes
haftmann@28054
   408
      (Symtab.lookup module_alias) (Symtab.lookup class)
haftmann@28054
   409
      (Symtab.lookup tyco) (Symtab.lookup const)
haftmann@28054
   410
      program4 cs2
haftmann@28054
   411
  end;
haftmann@28054
   412
haftmann@28054
   413
fun mount_serializer thy alt_serializer target =
haftmann@28054
   414
  let
haftmann@28054
   415
    val (targets, abortable) = CodeTargetData.get thy;
haftmann@28054
   416
    fun collapse_hierarchy target =
haftmann@28054
   417
      let
haftmann@28054
   418
        val data = case Symtab.lookup targets target
haftmann@28054
   419
         of SOME data => data
haftmann@28054
   420
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   421
      in case the_serializer data
haftmann@28054
   422
       of Serializer _ => (I, data)
haftmann@28054
   423
        | Extends (super, modify) => let
haftmann@28054
   424
            val (modify', data') = collapse_hierarchy super
haftmann@28054
   425
          in (modify' #> modify, merge_target false target (data', data)) end
haftmann@28054
   426
      end;
haftmann@28054
   427
    val (modify, data) = collapse_hierarchy target;
haftmann@28064
   428
    val (serializer, _) = the_default (case the_serializer data
haftmann@28054
   429
     of Serializer seri => seri) alt_serializer;
haftmann@28054
   430
    val reserved = the_reserved data;
haftmann@28054
   431
    val includes = Symtab.dest (the_includes data);
haftmann@28054
   432
    val module_alias = the_module_alias data;
haftmann@28054
   433
    val { class, inst, tyco, const } = the_name_syntax data;
haftmann@28054
   434
  in
haftmann@28054
   435
    invoke_serializer thy modify abortable serializer reserved
haftmann@28054
   436
      includes module_alias class inst tyco const
haftmann@28054
   437
  end;
haftmann@28054
   438
haftmann@28054
   439
fun serialize thy = mount_serializer thy NONE;
haftmann@28054
   440
haftmann@28054
   441
fun serialize_custom thy (target_name, seri) program cs =
haftmann@28054
   442
  mount_serializer thy (SOME seri) target_name NONE [] program cs (String [])
haftmann@28054
   443
  |> the;
haftmann@28054
   444
haftmann@28054
   445
fun parse_args f args =
haftmann@28054
   446
  case Scan.read OuterLex.stopper f args
haftmann@28054
   447
   of SOME x => x
haftmann@28054
   448
    | NONE => error "Bad serializer arguments";
haftmann@28054
   449
haftmann@28054
   450
haftmann@28054
   451
(* code presentation *)
haftmann@28054
   452
haftmann@28054
   453
fun code_of thy target module_name cs stmt_names =
haftmann@28054
   454
  let
haftmann@28054
   455
    val (cs', program) = Code_Thingol.consts_program thy cs;
haftmann@28054
   456
  in
haftmann@28054
   457
    string stmt_names (serialize thy target (SOME module_name) [] program cs')
haftmann@28054
   458
  end;
haftmann@28054
   459
haftmann@28054
   460
haftmann@28054
   461
(* code generation *)
haftmann@28054
   462
haftmann@28054
   463
fun read_const_exprs thy cs =
haftmann@28054
   464
  let
haftmann@28054
   465
    val (cs1, cs2) = Code_Name.read_const_exprs thy cs;
haftmann@28054
   466
    val (cs3, program) = Code_Thingol.consts_program thy cs2;
haftmann@28054
   467
    val cs4 = Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy);
haftmann@28054
   468
    val cs5 = map_filter
haftmann@28054
   469
      (fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3);
haftmann@28054
   470
  in fold (insert (op =)) cs5 cs1 end;
haftmann@28054
   471
haftmann@28054
   472
fun cached_program thy = 
haftmann@28054
   473
  let
haftmann@28054
   474
    val program = Code_Thingol.cached_program thy;
haftmann@28054
   475
  in (Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy), program) end
haftmann@28054
   476
haftmann@28054
   477
fun export_code thy cs seris =
haftmann@28054
   478
  let
haftmann@28054
   479
    val (cs', program) = if null cs then cached_program thy
haftmann@28054
   480
      else Code_Thingol.consts_program thy cs;
haftmann@28054
   481
    fun mk_seri_dest dest = case dest
haftmann@28054
   482
     of NONE => compile
haftmann@28054
   483
      | SOME "-" => export
haftmann@28054
   484
      | SOME f => file (Path.explode f)
haftmann@28054
   485
    val _ = map (fn (((target, module), dest), args) =>
haftmann@28054
   486
      (mk_seri_dest dest (serialize thy target module args program cs'))) seris;
haftmann@28054
   487
  in () end;
haftmann@28054
   488
haftmann@28054
   489
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
haftmann@28054
   490
haftmann@27103
   491
haftmann@27304
   492
(** Isar setup **)
haftmann@27103
   493
haftmann@27103
   494
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
haftmann@27103
   495
haftmann@27103
   496
fun code_exprP cmd =
wenzelm@27757
   497
  (Scan.repeat P.term_group
haftmann@27103
   498
  -- Scan.repeat (P.$$$ inK |-- P.name
haftmann@27103
   499
     -- Scan.option (P.$$$ module_nameK |-- P.name)
haftmann@27103
   500
     -- Scan.option (P.$$$ fileK |-- P.name)
wenzelm@27809
   501
     -- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") []
haftmann@27103
   502
  ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
haftmann@27103
   503
haftmann@28054
   504
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK];
wenzelm@24867
   505
wenzelm@24867
   506
val _ =
haftmann@24992
   507
  OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
haftmann@24219
   508
    parse_multi_syntax P.xname
haftmann@24219
   509
      (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
wenzelm@27757
   510
        (P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) []))
haftmann@24219
   511
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   512
          fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
haftmann@24219
   513
  );
haftmann@24219
   514
wenzelm@24867
   515
val _ =
haftmann@24992
   516
  OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
haftmann@24219
   517
    parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
haftmann@24219
   518
      ((P.minus >> K true) || Scan.succeed false)
haftmann@24219
   519
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   520
          fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
haftmann@24219
   521
  );
haftmann@24219
   522
wenzelm@24867
   523
val _ =
haftmann@24992
   524
  OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
haftmann@24219
   525
    parse_multi_syntax P.xname (parse_syntax I)
haftmann@24219
   526
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   527
          fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
haftmann@24219
   528
  );
haftmann@24219
   529
wenzelm@24867
   530
val _ =
haftmann@24992
   531
  OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
wenzelm@27757
   532
    parse_multi_syntax P.term_group (parse_syntax fst)
haftmann@24219
   533
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@28054
   534
      fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const
haftmann@28054
   535
        (Code_Printer.simple_const_syntax syn)) syns)
haftmann@24219
   536
  );
haftmann@24219
   537
wenzelm@24867
   538
val _ =
haftmann@24992
   539
  OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
haftmann@24219
   540
    P.name -- Scan.repeat1 P.name
haftmann@24219
   541
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
haftmann@24841
   542
  );
haftmann@24219
   543
wenzelm@24867
   544
val _ =
haftmann@24992
   545
  OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
haftmann@24992
   546
    P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s))
haftmann@24992
   547
    >> (fn ((target, name), content) => (Toplevel.theory o add_include target)
haftmann@24992
   548
      (name, content))
haftmann@24992
   549
  );
haftmann@24992
   550
haftmann@24992
   551
val _ =
haftmann@24992
   552
  OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
haftmann@24219
   553
    P.name -- Scan.repeat1 (P.name -- P.name)
haftmann@27103
   554
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
haftmann@27103
   555
  );
haftmann@27103
   556
haftmann@27103
   557
val _ =
haftmann@27103
   558
  OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
wenzelm@27757
   559
    Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd)
haftmann@24841
   560
  );
haftmann@24219
   561
wenzelm@24867
   562
val _ =
haftmann@27103
   563
  OuterSyntax.command "export_code" "generate executable code for constants"
haftmann@27103
   564
    K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@27103
   565
haftmann@27103
   566
fun shell_command thyname cmd = Toplevel.program (fn _ =>
wenzelm@27845
   567
  (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd))
wenzelm@27845
   568
    ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
haftmann@27103
   569
   of SOME f => (writeln "Now generating code..."; f (theory thyname))
haftmann@27103
   570
    | NONE => error ("Bad directive " ^ quote cmd)))
haftmann@27103
   571
  handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
haftmann@27103
   572
haftmann@24219
   573
end; (*local*)
haftmann@24219
   574
haftmann@24219
   575
end; (*struct*)