[M3devel] LONGINT alignment on 32bit targets?

Jay K jay.krell at cornell.edu
Mon Sep 21 09:04:49 CEST 2015


For the record, I didn't do this, way back even in:

https://modula3.elegosoft.com/cgi-bin/cvsweb.cgi/cm3/m3-sys/m3middle/src/Target.m3?rev=1.1;content-type=text%2Fplain

we had:

    (* build a generic 32-bit/IEEE system description *)
    Int64.cg_type    := CGType.Int64;    Int64.size       := 64;    Int64.align      := 64;    Int64.min.x      := IChunks { 00, 00, 00, 16_8000 };    Int64.max.x      := IChunks { FF, FF, FF, 16_7fff };


This is likely wrong on many/most/all 32bit targets.


Int64 is likely to be loaded/stored with two 32bit operations, therefore align probably should be 32.

At the time, I don't think there was any way to use this type, i.e.there was no LONGINT.

Once LONGINT came into being, if you wrote:

foo.m3RECORD  a: Ctypes.int;  b: LONGINTEND;

and foo.c:struct { int a; long long b;};

you would probably get different layout.

Let's see:

jair:src jay$ cat 1.cint a = sizeof(struct { int a; long long b;});jair:src jay$ cc -c -S -m32 1.cjair:src jay$ cat 1.s	.section	__TEXT,__text,regular,pure_instructions	.macosx_version_min 10, 10	.section	__DATA,__data	.globl	_a                      ## @a	.align	2_a:	.long	12                      ## 0xc

jair:1 jay$ cat 1.c src/Main.m3 src/m3makefile #include <stdio.h>
int main(){printf("%d\n", (int)sizeof(struct {int a; long long b;}));return 0;}
MODULE Main;IMPORT IO, Fmt, Ctypes;

BEGINIO.Put(Fmt.Int(BYTESIZE(RECORD a:Ctypes.int; b:LONGINT; END)));IO.Put("\n");END Main.
import("m3core")import("libm3")implementation("Main")build_standalone()program("a")
jair:1 jay$ cc -m32 1.cjair:1 jay$ ./a.out12
jair:1 jay$  cm3 -DTARGET=I386_DARWIN  --- building in I386_DARWIN ---
new source -> compiling Main.m3 -> linking ajair:1 jay$ ./I386_DARWIN/a
16



Now, if you are going to use x86 interlocked instructions on this,you might need higher alignment anyway. Might.


(Which all goes to my point -- while layout rules can't actually be removed,they have to exist, it does behoove one to try to layout data that hasobvious portable layout, if you need interop. i.e. you just would not everhave a record like this. You would make the likely padding explicit.Or sort by size. Though once you put records within records, it becomesdifficult/impossible.)


 - Jay
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20150921/c590557d/attachment-0001.html>


More information about the M3devel mailing list