Spivoxity / obc-3

Oxford Oberon-2 compiler
38 stars 7 forks source link

Alignment of record fields following common part #31

Closed Spivoxity closed 4 years ago

Spivoxity commented 4 years ago

Each record type is rounded up to a multiple of 4 bytes, and this padded size is used as the basis for extensions of the type. In the attached example, moving the kind field of the spatial and temporal types into the shared parent address results in a change in the record layout for spatial, with two byes of padding between the kind and planet fields. The layout of the temporal type is unaffected.

This is a valid translation of the source code, but it's a shame when it makes it more difficult to match the network address layouts that are used for Unix system calls. Consider revising the size calculation for record types so that the alignment is the max of the field alignments, and the total size is rounded up to a multiple of the alignment.

MODULE Bind;

IMPORT Out;

TYPE address = RECORD END;
  spatial = RECORD (address) kind: SHORTINT; planet: ARRAY 14 OF CHAR END;
  temporal = RECORD (address) kind: SHORTINT; year: INTEGER END;

CONST SPACE = 1; TIME = 2;

PROCEDURE mybind(sockfd: INTEGER; addr: address; len: INTEGER): INTEGER
  IS "mybind";

VAR rs: spatial; rt: temporal; x: INTEGER;

BEGIN
  rs.kind := SPACE; rs.planet := "Fooey";
  rt.kind := TIME; rt.year := 1776;
  x := mybind(17, rs, 42);
  Out.String("The result was "); Out.Int(x, 0); Out.Ln;
  x := mybind(17, rt, 42);
  Out.String("The result was "); Out.Int(x, 0); Out.Ln
END Bind.
Spivoxity commented 4 years ago

For rel-3.1.2, I made the alignment of a record type be the max. alignment of any field (or 1 for the empty record).