1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Common functions for emitting assembly code *)
|
|
|
|
|
|
|
|
let output_channel = ref stdout
|
|
|
|
|
|
|
|
let emit_string s = output_string !output_channel s
|
|
|
|
|
|
|
|
let emit_int n = output_string !output_channel (string_of_int n)
|
|
|
|
|
1995-11-19 08:53:12 -08:00
|
|
|
let emit_char c = output_char !output_channel c
|
|
|
|
|
1997-03-04 02:19:51 -08:00
|
|
|
let emit_nativeint n = output_string !output_channel (Nativeint.to_string n)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let emit_printf fmt =
|
|
|
|
Printf.fprintf !output_channel fmt
|
|
|
|
|
1996-01-06 10:56:39 -08:00
|
|
|
let emit_symbol esc s =
|
1995-06-15 01:17:29 -07:00
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
let c = s.[i] in
|
|
|
|
match c with
|
|
|
|
'A'..'Z' | 'a'..'z' | '0'..'9' | '_' ->
|
|
|
|
output_char !output_channel c
|
|
|
|
| _ ->
|
1996-01-06 10:56:39 -08:00
|
|
|
Printf.fprintf !output_channel "%c%02x" esc (Char.code c)
|
1995-06-15 01:17:29 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
let emit_string_literal s =
|
1995-10-16 09:19:36 -07:00
|
|
|
let last_was_escape = ref false in
|
1995-06-15 01:17:29 -07:00
|
|
|
emit_string "\"";
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
let c = s.[i] in
|
1996-09-18 06:23:56 -07:00
|
|
|
if c >= '0' && c <= '9' then
|
1995-10-16 09:19:36 -07:00
|
|
|
if !last_was_escape
|
|
|
|
then Printf.fprintf !output_channel "\\%o" (Char.code c)
|
|
|
|
else output_char !output_channel c
|
1996-09-18 06:23:56 -07:00
|
|
|
else if c >= ' ' && c <= '~' && c <> '"' (* '"' *) && c <> '\\' then begin
|
1995-10-16 09:19:36 -07:00
|
|
|
output_char !output_channel c;
|
|
|
|
last_was_escape := false
|
|
|
|
end else begin
|
|
|
|
Printf.fprintf !output_channel "\\%o" (Char.code c);
|
|
|
|
last_was_escape := true
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
done;
|
|
|
|
emit_string "\""
|
|
|
|
|
1996-09-18 06:23:56 -07:00
|
|
|
let emit_string_directive directive s =
|
|
|
|
let l = String.length s in
|
|
|
|
if l = 0 then ()
|
|
|
|
else if l < 80 then begin
|
|
|
|
emit_string directive;
|
|
|
|
emit_string_literal s;
|
|
|
|
emit_char '\n'
|
|
|
|
end else begin
|
|
|
|
let i = ref 0 in
|
|
|
|
while !i < l do
|
|
|
|
let n = min (l - !i) 80 in
|
|
|
|
emit_string directive;
|
|
|
|
emit_string_literal (String.sub s !i n);
|
|
|
|
emit_char '\n';
|
|
|
|
i := !i + n
|
|
|
|
done
|
|
|
|
end
|
|
|
|
|
|
|
|
let emit_bytes_directive directive s =
|
|
|
|
let pos = ref 0 in
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
if !pos = 0
|
|
|
|
then emit_string directive
|
|
|
|
else emit_char ',';
|
|
|
|
emit_int(Char.code s.[i]);
|
|
|
|
incr pos;
|
|
|
|
if !pos >= 16 then begin emit_char '\n'; pos := 0 end
|
|
|
|
done;
|
|
|
|
if !pos > 0 then emit_char '\n'
|
|
|
|
|