Compiling records by treating them as collections of variables

pk@cs.tut.fi (Kellom{ki Pertti)
21 Feb 1996 12:24:16 -0500

          From comp.compilers

Related articles
Compiling records by treating them as collections of variables pk@cs.tut.fi (1996-02-21)
Re: Compiling records by treating them as collections of variables cwilson@Xenon.Stanford.EDU (1996-02-23)
Compiling records by treating them as collections of variables dave@occl-cam.demon.co.uk (Dave Lloyd) (1996-02-23)
| List of all articles for this month |
From: pk@cs.tut.fi (Kellom{ki Pertti)
Newsgroups: comp.compilers
Followup-To: comp.compilers
Date: 21 Feb 1996 12:24:16 -0500
Organization: Tampere University of Technology (CS)
Keywords: question, code, comment

I am writing a sample compiler for a compiler construction course. The
language to be compiled is close to Pascal, i.e. a fairly standard
imperative language.


Currently the compiler generates intermediate code for a virtual
machine with an unlimited supply of registers. I am now adding
composite types to the compiler, and I was wondering about the best
way of doing this. For example, if a and b are of a record type, I
might want to allocate a virtual register for both of them in the
statement


a := b; (* allocate r0 for a, r1 for b *)


I might also want to assign a virtual register to a field of a value
of record type, as in


a.x := 0; (* allocate r2 for a.x *)


If I do this, it becomes necessary somehow to express that r2 is
really a part of r0. Many compiler books seem to assume that records
are always stored in a contiguous block of memory, and instead of r2
above, one would use r0+offset to get to a.x. This is all fine and
works, but in a way it breaks the nice register machine abstraction.


Does anyone know of a compiler, which would treat records simply as
syntactic sugar for a collection of variables? For example, given the
declarations


type foo = record
x : real;
i : integer;
end;


var a,b : foo;


one would expand the definition of a and b to


var a_x : real; a_i : integer;
b_x : real; b_i : integer;


and
a := b;


to
a_x := b_x;
a_i := a_i;


In this way, most of the compiler would not see records at all. I can
see problems with records as return values of functions, but other
than that it seems that this would seem to work rather nicely.
--
Pertti Kellom\"aki (TeX format)
    Tampere Univ. of Technology
            Software Systems Lab
[This approach looks to me like it would make arrays and pointers somewhat
more difficult as well. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.