File: static_term.m

package info (click to toggle)
mercury 0.10.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 21,984 kB
  • ctags: 11,923
  • sloc: objc: 187,634; ansic: 66,107; sh: 7,570; lisp: 1,568; cpp: 1,337; makefile: 614; perl: 511; awk: 274; asm: 252; exp: 32; xml: 12; fortran: 3; csh: 1
file content (94 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
%---------------------------------------------------------------------------%
% Copyright (C) 2000 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
%
% File: static_term.m.
% Author: zs.
%
% This module handles the conversion of Mercury terms in the compiler
% into rvals we can give to llds_out.m in order to make those terms available
% at runtime in the program being compiled.
%
%---------------------------------------------------------------------------%

:- module static_term.

:- interface.

:- import_module llds.
:- import_module counter, std_util.

:- pred static_term__term_to_rval(univ::in, maybe(rval)::out,
	counter::in, counter::out) is det.

%---------------------------------------------------------------------------%

:- implementation.

:- import_module builtin_ops.
:- import_module list, require.

static_term__term_to_rval(Univ, Rval, CellCounter0, CellCounter) :-
	( std_util__get_functor_info(Univ, FunctorInfo) ->
		static_term__functor_info_to_rval(FunctorInfo, Rval,
			CellCounter0, CellCounter)
	;
		error("static_term__term_to_rval: unexpected kind of term")
	).

:- pred static_term__functor_info_to_rval(functor_tag_info::in,
	maybe(rval)::out, counter::in, counter::out) is det.

static_term__functor_info_to_rval(FunctorInfo, MaybeRval,
		CellCounter0, CellCounter) :-
	(
		FunctorInfo = functor_integer(Int),
		MaybeRval = yes(const(int_const(Int))),
		CellCounter = CellCounter0
	;
		FunctorInfo = functor_float(Float),
		MaybeRval = yes(const(float_const(Float))),
		CellCounter = CellCounter0
	;
		FunctorInfo = functor_string(String),
		MaybeRval = yes(const(string_const(String))),
		CellCounter = CellCounter0
	;
		FunctorInfo = functor_enum(Enum),
		MaybeRval = yes(const(int_const(Enum))),
		CellCounter = CellCounter0
	;
		FunctorInfo = functor_local(Ptag, Sectag),
		MaybeRval = yes(mkword(Ptag,
			unop(mkbody, const(int_const(Sectag))))),
		CellCounter = CellCounter0
	;
		FunctorInfo = functor_remote(Ptag, Sectag, Args),
		MaybeSectagRval = yes(const(int_const(Sectag))),
		list__map_foldl(static_term__term_to_rval,
			Args, MaybeArgRvals, CellCounter0, CellCounter1),
		counter__allocate(CNum, CellCounter1, CellCounter),
		Reuse = no,
		MaybeRval = yes(create(Ptag, [MaybeSectagRval | MaybeArgRvals],
			uniform(no), must_be_static, CNum,
			"static_term", Reuse))
	;
		FunctorInfo = functor_unshared(Ptag, Args),
		list__map_foldl(static_term__term_to_rval,
			Args, MaybeArgRvals, CellCounter0, CellCounter1),
		counter__allocate(CNum, CellCounter1, CellCounter),
		Reuse = no,
		MaybeRval = yes(create(Ptag, MaybeArgRvals,
			uniform(no), must_be_static, CNum,
			"static_term", Reuse))
	;
		FunctorInfo = functor_notag(Univ),
		static_term__term_to_rval(Univ, MaybeRval,
			CellCounter0, CellCounter)
	;
		FunctorInfo = functor_equiv(Univ),
		static_term__term_to_rval(Univ, MaybeRval,
			CellCounter0, CellCounter)
	).