Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/fi-gen/ml-gen.h
ViewVC logotype

View of /trunk/sml3d/src/fi-gen/ml-gen.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 166 - (download) (as text) (annotate)
Tue Jun 3 16:48:02 2008 UTC (11 years, 5 months ago) by jhr
Original Path: src/fi-gen/ml-gen.h
File size: 2415 byte(s)
  Fixed generator to generate correct SML code
/* ml-gen.h
 *
 * COPYRIGHT (c) 2007 John Reppy (http://www.cs.uchicago.edu/~jhr)
 * All rights reserved.
 */

#ifndef _ML_GEN_H_

#include <sys/types.h>
#include <stddef.h>
#include <stdio.h>

typedef enum {		/* basic C types */
    U8, U16, U32, U64,	    /* Unsigned ints */
    S8, S16, S32, S64,	    /* Signed ints */
    F32, F64,		    /* Floating-point */
    PTR			    /* Pointers */
} CType_t;

/* macro to test if an integer type is signed or not */
#define isSIGNED(ty)	((ty)-1 < (ty)0)

#define SINT_TY(ty)			\
	((sizeof(ty) == 1) ? S8 :	\
	 (sizeof(ty) == 2) ? S16 :	\
	 (sizeof(ty) == 4) ? S32 :	\
	  S64)
#define UINT_TY(ty)			\
	((sizeof(ty) == 1) ? U8 :	\
	 (sizeof(ty) == 2) ? U16 :	\
	 (sizeof(ty) == 4) ? U32 :	\
	  U64)
#define INT_TY(ty)	(isSIGNED(ty) ? SINT_TY(ty) : UINT_TY(ty))
#define FLT_TY(ty)	((sizeof(ty) == 4) ? F32 : F64)

#define FIELD_TY(structTy, fld)  typeof(((structTy *)0)->fld)
#define FIELD_SZ(structTy, fld)  sizeof(((structTy *)0)->fld)

#define INT_FIELD(structTy, fld)	\
	prField (#fld, INT_TY(FIELD_TY(structTy, fld)), offsetof(structTy, fld))
#define FLOAT_FIELD(structTy, fld)	\
	prField (#fld, FLT_TY(FIELD_TY(structTy, fld)), offsetof(structTy, fld))
#define PTR_FIELD(structTy, fld)	\
	prField (#fld, PTR, offsetof(structTy, fld))

extern void prField (const char *fld, CType_t fldTy, off_t off);
extern void prSize (const char *ty, size_t sz);
extern void prOffset (const char *ty, const char *fld, off_t off);
extern void prIndex (const char *ty, const char *fld, off_t off, size_t fldSz);
extern void prConst (const char *name, CType_t ty, int val);

#define SIZE(ty)	prSize(#ty, sizeof(ty))
#define OFFSET(ty, fld)	prOffset(#ty, #fld, offsetof(ty, fld))
#define INDEX(ty, fld)	prIndex(#ty, #fld, offsetof(ty, fld), sizeof(((ty *)0)->fld[0]));
#define CONST(c)	prConst(#c, U32, c);
#define SCONST(c)	prConst(#c, S32, c);

#define BEGIN_STRUCT(ty)	{				\
	printf("    structure %s =\n", #ty);			\
	printf("      struct\n");				\
	printf("        type t = MLton.Pointer.t\n");		\
	printf("        val size = 0w%d\n", (int)sizeof(ty));
#define STRUCT_OFFSET(ty, fld)	prOffset(0, #fld, offsetof(ty, fld))
#define STRUCT_INDEX(ty, fld)	prIndex(0, #fld, offsetof(ty, fld), sizeof(((ty *)0)->fld[0]));
#define END_STRUCT						\
	printf("      end\n");					\
    }

#define COM(msg)	printf("  (***** " msg " *****)\n")
#define NL		printf("\n");

#endif /* !_ML_GEN_H_ */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0