1(* Emitting phrases *)
2
3local
4  open BasicIO Nonstdio Mixture Const Instruct Buffcode;
5  open Code_dec Reloc Emitcode;
6in
7
8val abs_out_position = ref 0;
9
10val compiled_phrase_index = ref ([] : compiled_phrase list);
11
12fun start_emit_phrase os =
13(
14  output_binary_int os 0;
15  abs_out_position := 4;
16  compiled_phrase_index := []
17);
18
19fun emit_phrase os (phr : ZamPhrase) =
20(
21  reloc_reset();
22  init_out_code();
23  Labels.reset_label_table();
24  if #kph_funcs phr = [] then
25    emit (#kph_inits phr)
26  else
27    (emit (#kph_inits phr);
28     emit [Kbranch 0];
29     emit (#kph_funcs phr);
30     emit [Klabel 0]);
31  buff_output os (!out_buffer) 0 (!out_position);
32  compiled_phrase_index :=
33    { cph_pos   = !abs_out_position,
34      cph_len   = !out_position,
35      cph_reloc = get_reloc_info(),
36      cph_pure  = #kph_is_pure phr}
37        :: !compiled_phrase_index;
38  abs_out_position := !abs_out_position + !out_position
39);
40
41fun end_emit_phrase
42  excRenList valRenList sigStamp mentions os =
43(
44  output_value os
45    { cu_phrase_index = !compiled_phrase_index,
46      cu_exc_ren_list = excRenList,
47      cu_val_ren_list = valRenList,
48      cu_sig_stamp = sigStamp,
49      cu_mentions = mentions };
50  compiled_phrase_index := [];
51  seek_out os 0;
52  output_binary_int os (!abs_out_position)
53);
54
55end;
56