[M3devel] offsetof, et. al?

Jay K jay.krell at cornell.edu
Sun Dec 26 11:28:40 CET 2010


construction various data?

So, I have this C:


typedef struct _enum_t {
  const char* name;
  uint64_t value;
} enum_t;

typedef struct _field_t {
    char name[16];
    uchar offset;
    uchar size;
    uchar element_size;
    uchar str;
    uchar macho_string;
    uchar enum_table_count;
    const enum_t* enum_table;
} field_t;

#define FIELD(t, f) {STRINGIZE(f), offsetof(t, f), sizeof((((t*)0)->f))}
#define FIELD_ARRAY(t, f) {STRINGIZE(f), offsetof(t, f), sizeof((((t*)0)->f)), sizeof((((t*)0)->f)[0]) }
#define FIELD_STRING(t, f) {STRINGIZE(f), offsetof(t, f), sizeof((((t*)0)->f)), sizeof((((t*)0)->f)[0]), 1 }
#define FIELD_ENUM(t, f, e) {STRINGIZE(f), offsetof(t, f), sizeof((((t*)0)->f)), 0, 0, 0, sizeof(e)/sizeof((e)[0]), e }
#define FIELD_MACHO_STRING(t, f) {STRINGIZE(f), offsetof(t, f), sizeof((((t*)0)->f)), 0, 0, 1 }

typedef struct _struct_t {
    const char* name;
    uint    size;
    uint    nfields;
    const field_t* fields;
    int widest_field;
} struct_t;

#define STRUCT(a) {STRINGIZE(a), sizeof(a), NUMBER_OF(PASTE(a,_fields)), PASTE(a,_fields)}

extern_const enum_t
macho_magic_names[] = {
    { "magic32", macho_magic32 },
    { "magic64", macho_magic64 }
};

extern_const enum_t
macho_cputype_names[] = {
    { "x86", macho_cpu_type_x86 },
    { "amd64", macho_cpu_type_amd64 },
    { "powerpc", macho_cpu_type_powerpc },
    { "powerpc64", macho_cpu_type_powerpc64 }
};

extern_const enum_t
macho_cpusubtype_names[] = {
    { "powerpc_all", macho_cpu_subtype_powerpc_all },
    { "x86_all", macho_cpu_subtype_x86_all }
};

extern_const enum_t
macho_filetype_names[] = {
    { "object", macho_type_object },
    { "execute", macho_type_execute },
    { "fixed_vm_library", macho_type_fixed_vm_library },
    { "core", macho_type_core },
    { "preload", macho_type_preload },
    { "dylib", macho_type_dylib },
    { "dylinker", macho_type_dylinker },
    { "bundle", macho_type_bundle },
    { "dylib_stub", macho_type_dylib_stub },
    { "dsym", macho_type_dsym } };
        
extern_const field_t
macho_header32_t_fields[] = {
    FIELD_ENUM(macho_header32_t, magic, macho_magic_names),
    FIELD_ENUM(macho_header32_t, cputype, macho_cputype_names),
    FIELD_ENUM(macho_header32_t, cpusubtype, macho_cpusubtype_names),
    FIELD_ENUM(macho_header32_t, filetype, macho_filetype_names),
    FIELD(macho_header32_t, ncmds),
    FIELD(macho_header32_t, sizeofcmds),
    FIELD(macho_header32_t, flags)
};


how do I "best" convert this to Modula-3?

I don't see a good analog to offsetof.
I don't see a good analog of variably sized non-copying arrays, esp. constants.
I have something kind of close, but I have to use VAR in places instead
of CONST, and I have to make copies of the arrays, and I have to compute
the offsets/sizes myself, which isn't difficult.


I understand that using offset of is necesssarily unsafe.

What I have, C and Modula-3, is checked in, in scratch/macho.


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


More information about the M3devel mailing list