| 1 |
#ifndef CARAWAY_H |
| 2 |
#define CARAWAY_H |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
extern FILE *yyin; |
| 10 |
|
| 11 |
int main( int argc, char **argv ); |
| 12 |
void fatal( char *s ); |
| 13 |
|
| 14 |
/* |
| 15 |
* Immediate and Non-Immediate OOP |
| 16 |
* |
| 17 |
* Immediate OOP (bottom 2 bits ~= 0) |
| 18 |
* 01 => SmallInteger, 10 => Character |
| 19 |
* |
| 20 |
* Non-Immediate OOP (bottom 2 bits = 0) |
| 21 |
* |
| 22 |
* Pointer Object Body |
| 23 |
* |
| 24 |
* +-------------------------------------------+ |
| 25 |
* | optional size (if > size bits in flags) | |
| 26 |
* +-------------------------------------------+ |
| 27 |
* | oop in first instance variable | |
| 28 |
* +-------------------------------------------+ |
| 29 |
* | oop in second instance variable | |
| 30 |
* +-------------------------------------------+ |
| 31 |
* |
| 32 |
* Byte Object Body |
| 33 |
* |
| 34 |
* +-------------------------------------------+ |
| 35 |
* | optional size (if > size bits in flags) | |
| 36 |
* +-------------------------------------------+ |
| 37 |
* | byte 1 | byte 2 | byte 3 | byte 4 | <- cast OOP to char * |
| 38 |
* +-------------------------------------------+ |
| 39 |
* | byte 5 | byte 6 | pad to 4 bytes | |
| 40 |
* +-------------------------------------------+ |
| 41 |
* |
| 42 |
*/ |
| 43 |
typedef struct OOP { |
| 44 |
unsigned long flags; |
| 45 |
struct OOP *klass; |
| 46 |
union { |
| 47 |
unsigned long word; |
| 48 |
struct OOP **oops; // instance variables |
| 49 |
struct OOP *forward; // forwarding pointer + flags (2bits) |
| 50 |
} ptr; |
| 51 |
} *OOP; |
| 52 |
|
| 53 |
#define CWCAST(st) (struct st*) |
| 54 |
#define CWPLUGIN(oop) (CWCAST(CWPlugin)(oop)) |
| 55 |
|
| 56 |
#define IV_GET(oop,i) oop->ptr.oops[i] |
| 57 |
#define IV_SET(oop,i,v) oop->ptr.oops[i]=v |
| 58 |
#define BYTE_GET(oop,i) ((unsigned char *)oop->ptr.oops)[i] |
| 59 |
#define BYTE_SET(oop,i,v) ((unsigned char *)oop->ptr.oops)[i]=v |
| 60 |
|
| 61 |
#define NEWOOP(oop) OOP oop = cw_alloc_oop2() |
| 62 |
#define OOPSETUP(oop,c,t) do { \ |
| 63 |
oop->flags = (t); \ |
| 64 |
oop->klass = (c); \ |
| 65 |
INIT_FIELDS_CL(oop, c); \ |
| 66 |
} while (0) |
| 67 |
#define IDXSETUP(oop,c,t,s) do { \ |
| 68 |
INIT_FIELDS(oop, s); \ |
| 69 |
INIT_FL_FIELDS(oop, t, s); \ |
| 70 |
oop->klass = (c); \ |
| 71 |
} while (0) |
| 72 |
#define BYTESSETUP(oop,c,t,s) do { \ |
| 73 |
INIT_FIELDS(oop, s/SIZEOF_LONG + (s%SIZEOF_LONG*SIZEOF_LONG==0?0:1)); \ |
| 74 |
INIT_FL_FIELDS(oop, t, s); \ |
| 75 |
oop->klass = (c); \ |
| 76 |
} while (0) |
| 77 |
#define INIT_FL_FIELDS(oop,t,s) \ |
| 78 |
oop->flags = t | (s > FIELD_MAX ? FIELD_MAX : s) << FIELD_LSHIFT |
| 79 |
#define INIT_FIELDS_CL(oop,c) \ |
| 80 |
INIT_FIELDS(oop, FL_FIELDS(Behavior_superclass(c)->flags)) |
| 81 |
#define INIT_FIELDS(oop,s) cw_init_fields(oop,s) |
| 82 |
inline void cw_init_fields( OOP oop, long size ); |
| 83 |
|
| 84 |
#define ALLOC_N(type,n) (type*)xmalloc2((n),sizeof(type)) |
| 85 |
#define ALLOC(type) (type*)xmalloc(sizeof(type)) |
| 86 |
#define REALLOC_N(var,type,n) (var)=(type*)xrealloc2((char*)(var),(n),sizeof(type)) |
| 87 |
|
| 88 |
#define MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n)) |
| 89 |
#define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n)) |
| 90 |
#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n)) |
| 91 |
#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n)) |
| 92 |
|
| 93 |
#define xmalloc cw_xmalloc |
| 94 |
#define xmalloc2 cw_xmalloc2 |
| 95 |
#define xcalloc cw_xcalloc |
| 96 |
#define xrealloc cw_xrealloc |
| 97 |
#define xrealloc2 cw_xrealloc2 |
| 98 |
#define xfree cw_xfree |
| 99 |
|
| 100 |
inline void *xmalloc(long); |
| 101 |
inline void *xmalloc2(long,long); |
| 102 |
inline void *xcalloc(long,long); |
| 103 |
inline void *xrealloc(void*,long); |
| 104 |
inline void *xrealloc2(void*,long,long); |
| 105 |
inline void xfree(void*); |
| 106 |
|
| 107 |
/* |
| 108 |
* Flags |
| 109 |
* |
| 110 |
* 3 2 1 |
| 111 |
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 |
| 112 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 |
* | # fixed fields | unused |o|r|i|w|m|p|1/0| |
| 114 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 115 |
* |
| 116 |
* (1) FL_PTR - the instance variables are pointers or bytes |
| 117 |
* (1) FL_MARK - the object is marked by GC |
| 118 |
* (1) FL_EPHEM - the object is ephemeron, not weak |
| 119 |
* (1) FL_WEAK - weak reference object |
| 120 |
* (1) FL_IM - the object is immutable |
| 121 |
* (1) FL_RT - in remenbered table |
| 122 |
* (1) FL_OLDRT - in old remenbered table |
| 123 |
* (12) FL_FIELDS - number of fixed fields |
| 124 |
* |
| 125 |
* Bottom 2 bits are kept for immediate objects. |
| 126 |
* Because flags value is used by Behavior>>format as OOP. |
| 127 |
* |
| 128 |
* If number of instance variables is greater than FL_FIELDS, |
| 129 |
* the number is contained in first of instance variables array |
| 130 |
* (OOP->ptr.oops) as integer object. See also OOP. |
| 131 |
* |
| 132 |
*/ |
| 133 |
|
| 134 |
#define FIELD_BITS 12 |
| 135 |
#define FIELD_LSHIFT SIZEOF_LONG*8-FIELD_BITS |
| 136 |
#define FIELD_MAX (1<<FIELD_BITS) |
| 137 |
#define FL_PTR 0x100 |
| 138 |
#define FL_MARK (FL_PTR<<1) |
| 139 |
#define FL_WEAK (FL_PTR<<2) |
| 140 |
#define FL_IM (FL_PTR<<3) |
| 141 |
#define FL_RT (FL_PTR<<4) |
| 142 |
#define FL_OLDRT (FL_PTR<<5) |
| 143 |
|
| 144 |
#define HAS_PTR(x) (x->flags&FL_PTR) |
| 145 |
#define FL_FIELD_SIZE(f) (unsigned long)RSHIFT(f,FIELD_LSHIFT) |
| 146 |
#define FL_CL_FIELD_SIZE(x) FL_FIELD_SIZE(FORMAT(x->klass)) |
| 147 |
#define FL_SP_FIELD_SIZE(x) FL_FIELD_SIZE(FORMAT(Behavior_superclass(x->klass))) |
| 148 |
#define FORMAT(x) (unsigned long)Behavior_format(x) |
| 149 |
#define HAS_OPSIZE(x) FL_FIELD_SIZE(x->flags)==FIELD_MAX |
| 150 |
#define FIELD_SIZE(x) \ |
| 151 |
(HAS_OPSIZE(x)?SML2ULONG(IV_GET(x,0)):FL_FIELD_SIZE(x->flags)) |
| 152 |
|
| 153 |
|
| 154 |
// |
| 155 |
// number |
| 156 |
// |
| 157 |
|
| 158 |
#ifdef __STDC__ |
| 159 |
# include <limits.h> |
| 160 |
#else |
| 161 |
# ifndef LONG_MAX |
| 162 |
# ifdef HAVE_LIMITS_H |
| 163 |
# include <limits.h> |
| 164 |
# else |
| 165 |
/* assuming 32bit(2's compliment) long */ |
| 166 |
# define LONG_MAX 2147483647 |
| 167 |
# endif |
| 168 |
# endif |
| 169 |
# ifndef LONG_MIN |
| 170 |
# define LONG_MIN (-LONG_MAX-1) |
| 171 |
# endif |
| 172 |
# ifndef CHAR_BIT |
| 173 |
# define CHAR_BIT 8 |
| 174 |
# endif |
| 175 |
#endif |
| 176 |
|
| 177 |
#if HAVE_LONG_LONG |
| 178 |
# ifndef LLONG_MAX |
| 179 |
# ifdef LONG_LONG_MAX |
| 180 |
# define LLONG_MAX LONG_LONG_MAX |
| 181 |
# else |
| 182 |
# ifdef _I64_MAX |
| 183 |
# define LLONG_MAX _I64_MAX |
| 184 |
# else |
| 185 |
/* assuming 64bit(2's complement) long long */ |
| 186 |
# define LLONG_MAX 9223372036854775807LL |
| 187 |
# endif |
| 188 |
# endif |
| 189 |
# endif |
| 190 |
# ifndef LLONG_MIN |
| 191 |
# ifdef LONG_LONG_MIN |
| 192 |
# define LLONG_MIN LONG_LONG_MIN |
| 193 |
# else |
| 194 |
# ifdef _I64_MIN |
| 195 |
# define LLONG_MIN _I64_MIN |
| 196 |
# else |
| 197 |
# define LLONG_MIN (-LLONG_MAX-1) |
| 198 |
# endif |
| 199 |
# endif |
| 200 |
# endif |
| 201 |
#endif |
| 202 |
|
| 203 |
#define SMLINT_MAX (LONG_MAX>>1) |
| 204 |
#define SMLINT_MIN RSHIFT((long)LONG_MIN,1) |
| 205 |
|
| 206 |
OOP cw_int2inum( long n ); |
| 207 |
OOP cw_uint2inum( unsigned long n ); |
| 208 |
#define SMLINT_FLAG 0x01 |
| 209 |
#define INT2SML(i) ((OOP)(((long)(i))<<2 | SMLINT_FLAG)) |
| 210 |
#define LONG2SML(i) INT2SML(i) |
| 211 |
#define INT2NUM(v) cw_int2inum(v) |
| 212 |
#define LONG2NUM(v) INT2NUM(v) |
| 213 |
#define UINT2NUM(v) cw_uint2inum(v) |
| 214 |
#define ULONG2NUM(v) UINT2NUM(v) |
| 215 |
|
| 216 |
#define SML2LONG(x) RSHIFT((long)x,2) |
| 217 |
#define SML2ULONG(x) (((unsigned long)(x))>>2) |
| 218 |
#define IS_SMLINT(f) (((long)(f))&SMLINT_FLAG) |
| 219 |
#define POSSMLABLE(f) ((f) <= SMLINT_MAX) |
| 220 |
#define NEGSMLABLE(f) ((f) >= SMLINT_MIN) |
| 221 |
#define SMLABLE(f) (POSSMLABLE(f) && NEGSMLABLE(f)) |
| 222 |
|
| 223 |
long cw_num2long(OOP); |
| 224 |
unsigned long cw_num2ulong(OOP); |
| 225 |
#define NUM2LONG(x) (IS_SMLINT(x)?SML2LONG(x):cw_num2long((OOP)x)) |
| 226 |
#define NUM2ULONG(x) cw_num2ulong((OOP)x) |
| 227 |
#if SIZEOF_INT < SIZEOF_LONG |
| 228 |
long cw_num2int(OOP); |
| 229 |
#define NUM2INT(x) (IS_SMLINT(x)?SML2INT(x):cw_num2int((OOP)x)) |
| 230 |
long cw_sml2int(OOP); |
| 231 |
#define SML2INT(x) cw_sml2int((OOP)x) |
| 232 |
unsigned long cw_num2uint(OOP); |
| 233 |
#define NUM2UINT(x) cw_num2uint(x) |
| 234 |
unsigned long cw_sml2uint(OOP); |
| 235 |
#define SML2UINT(x) cw_sml2uint(x) |
| 236 |
#else |
| 237 |
#define NUM2INT(x) ((int)NUM2LONG(x)) |
| 238 |
#define NUM2UINT(x) ((unsigned int)NUM2ULONG(x)) |
| 239 |
#define SML2INT(x) ((int)SML2LONG(x)) |
| 240 |
#define SML2UINT(x) ((unsigned int)SML2ULONG(x)) |
| 241 |
#endif |
| 242 |
|
| 243 |
#define CHAR_FLAG 0x10 |
| 244 |
#define INT2CHAR(i) ((OOP)(((long)(i))<<2 | CHAR_FLAG)) |
| 245 |
#define LONG2CHAR(i) INT2CHAR(i) |
| 246 |
|
| 247 |
#define IMMEDIATE_MASK 0x03 |
| 248 |
#define IS_IMMEDIATE(x) ((OOP)(x) & IMMEDIATE_MASK) |
| 249 |
|
| 250 |
// |
| 251 |
// OOP |
| 252 |
// |
| 253 |
|
| 254 |
OOP CWObject; |
| 255 |
OOP CWBehavior; |
| 256 |
OOP CWClassDescription; |
| 257 |
OOP CWClass; |
| 258 |
OOP CWMetaclass; |
| 259 |
OOP CWMethodDictionary; |
| 260 |
OOP CWCompiledCode; |
| 261 |
OOP CWCompiledMethod; |
| 262 |
OOP CWCompiledBlock; |
| 263 |
OOP CWBoolean; |
| 264 |
OOP CWTrue; |
| 265 |
OOP CWFalse; |
| 266 |
OOP CWUndefinedObject; |
| 267 |
OOP CWString; |
| 268 |
OOP CWByteString; |
| 269 |
OOP CWSymbol; |
| 270 |
OOP CWByteSymbol; |
| 271 |
OOP CWArray; |
| 272 |
OOP CWVariableBinding; |
| 273 |
OOP CWNameSpace; |
| 274 |
OOP CWNamedNameSpace; |
| 275 |
OOP CWClassNameSpace; |
| 276 |
|
| 277 |
// namespaces |
| 278 |
#define NS_ROOT "Smalltalk" |
| 279 |
#define NS_KERNEL "Kernel" |
| 280 |
#define NS_COLLECTIONS "Collections" |
| 281 |
OOP CWCollections; |
| 282 |
|
| 283 |
// global variables for bootstrap |
| 284 |
OOP CWBootSymbols; |
| 285 |
OOP CWBootClasses; |
| 286 |
|
| 287 |
// unique global variable |
| 288 |
OOP CWRootNameSpace; |
| 289 |
|
| 290 |
// pseudo variables |
| 291 |
#define Pnil ((OOP)0) |
| 292 |
#define Ptrue ((OOP)0111) |
| 293 |
#define Pfalse ((OOP)1011) |
| 294 |
|
| 295 |
|
| 296 |
// specify directly for FIELD_SIZE |
| 297 |
#define Object_FIELD_SIZE 0 |
| 298 |
|
| 299 |
#define Behavior_FIELD_SIZE 4 |
| 300 |
#define Behavior_superclass(oop) oop->ptr.oops[0] |
| 301 |
#define Behavior_methodDict(oop) oop->ptr.oops[1] |
| 302 |
#define Behavior_format(oop) oop->ptr.oops[2] |
| 303 |
#define Behavior_subclasses(oop) oop->ptr.oops[3] |
| 304 |
|
| 305 |
#define ClassDescription_FIELD_SIZE 2 |
| 306 |
#define ClassDescription_instanceVariables(oop) oop->ptr.oops[4] |
| 307 |
#define ClassDescription_organization(oop) oop->ptr.oops[5] |
| 308 |
|
| 309 |
#define Class_FIELD_SIZE 3 |
| 310 |
#define CLOOP_FIELD_SIZE \ |
| 311 |
Object_FIELD_SIZE+Behavior_FIELD_SIZE+ClassDescription_FIELD_SIZE+Class_FIELD_SIZE |
| 312 |
#define Class_name(oop) oop->ptr.oops[6] |
| 313 |
#define Class_classPool(oop) oop->ptr.oops[7] |
| 314 |
#define Class_environment(oop) oop->ptr.oops[8] |
| 315 |
|
| 316 |
#define Metaclass_FIELD_SIZE 1 |
| 317 |
#define Metaclass_thisClass(oop) oop->ptr.oops[6] |
| 318 |
|
| 319 |
#define Namespace_FIELD_SIZE 4 |
| 320 |
#define Namespace_organization(oop) oop->ptr.oops[0] |
| 321 |
#define Namespace_bindings(oop) oop->ptr.oops[1] |
| 322 |
#define Namespace_specificImports(oop) oop->ptr.oops[2] |
| 323 |
#define Namespace_generalImports(oop) oop->ptr.oops[3] |
| 324 |
|
| 325 |
#define NamedNamespace_FIELD_SIZE 2 |
| 326 |
#define NamedNamespace_parent(oop) oop->ptr.oops[4] |
| 327 |
#define NamedNamespace_name(oop) oop->ptr.oops[5] |
| 328 |
|
| 329 |
#define ClassNamespace_FIELD_SIZE 1 |
| 330 |
#define ClassNamespace_owner(oop) oop->ptr.oops[4] |
| 331 |
|
| 332 |
#define VariableBinding_FIELD_SIZE 2 |
| 333 |
#define VariableBinding_path(oop) oop->ptr.oops[0] |
| 334 |
#define VariableBinding_value(oop) oop->ptr.oops[1] |
| 335 |
|
| 336 |
#define Array_FIELD_SIZE 1 |
| 337 |
#define Array_tally(oop) oop->ptr.oops[0] |
| 338 |
|
| 339 |
void cw_init_proto_oops(); |
| 340 |
void cw_init_dictionary(); |
| 341 |
|
| 342 |
// 中身はまだ空のブ・踉札藩僖?薀垢鯤屬 |
| 343 |
OOP cw_class_boot( OOP super, int fields ); |
| 344 |
OOP cw_make_metaclass( OOP oop, OOP super ); |
| 345 |
OOP cw_proto_class( OOP super, int hasPtr, int fields ); |
| 346 |
|
| 347 |
OOP cw_boot_intern( char *str ); |
| 348 |
OOP cw_boot_lookup( char *str ); |
| 349 |
|
| 350 |
typedef enum { |
| 351 |
CWNoneIndexedType, |
| 352 |
CWObjectsIndexedType, |
| 353 |
CWBytesIndexedType, |
| 354 |
CWWeakIndexedType, |
| 355 |
} CWIndexedType; |
| 356 |
|
| 357 |
OOP cw_define_class( OOP namespace, |
| 358 |
OOP super, |
| 359 |
int type, |
| 360 |
int fields, |
| 361 |
char *category ); |
| 362 |
OOP cw_define_namespace( OOP namespace, char *name, char *category ); |
| 363 |
OOP cw_find_namespace( char *name ); |
| 364 |
void cw_class_specific_import( OOP klass, OOP import ); |
| 365 |
void cw_class_general_import( OOP klass, OOP import ); |
| 366 |
void cw_namespace_specific_import( OOP namespace, OOP import ); |
| 367 |
void cw_namespace_general_import( OOP namespace, OOP import ); |
| 368 |
|
| 369 |
char *cw_class_name( OOP klass ); |
| 370 |
OOP cw_iv_get( OOP oop, long index ); |
| 371 |
void cw_iv_set( OOP oop, long index, OOP val ); |
| 372 |
OOP cw_object_get( OOP oop, long index ); |
| 373 |
void cw_object_set( OOP oop, long index, OOP val ); |
| 374 |
unsigned char cw_byte_get( OOP oop, long index ); |
| 375 |
void cw_byte_set( OOP oop, long index, unsigned char val ); |
| 376 |
void cw_swap( OOP oop1, OOP oop2 ); |
| 377 |
|
| 378 |
#define FIELD_PTR(x) (HAS_OPSIZE(x)?x->ptr.oops+1:x->ptr.oops) |
| 379 |
#define BYTE_PTR(type,x) ((type)FIELD_PTR(x)) |
| 380 |
#define BYTESTR_PTR(x) BYTE_PTR(char *,x) |
| 381 |
OOP cw_byte_string_new( const char *ptr, long len ); |
| 382 |
OOP cw_byte_string_new2( const char *ptr ); |
| 383 |
int cw_byte_string_hash( OOP str ); |
| 384 |
int cw_byte_string_cmp( OOP str, OOP other ); |
| 385 |
|
| 386 |
OOP cw_byte_symbol_new( const char *ptr, long len ); |
| 387 |
OOP cw_byte_symbol_new2( const char *ptr ); |
| 388 |
unsigned long cw_symbol_table_size(); |
| 389 |
#define cw_byte_symbol_hash(sym) cw_byte_string_hash(sym) |
| 390 |
#define cw_byte_symbol_cmp(sym,other) cw_byte_string_cmp(sym,other) |
| 391 |
|
| 392 |
OOP cw_array_new( long size ); |
| 393 |
void cw_array_add( OOP ary, OOP obj ); |
| 394 |
OOP cw_data_new( unsigned char *bytes, int size ); |
| 395 |
OOP cw_variable_binding_from_path( char *path ); |
| 396 |
OOP cw_method_dictionary_new( long size ); |
| 397 |
OOP cw_method_dictionary_get( OOP dict, OOP sym ); |
| 398 |
void cw_method_dictionary_set( OOP dict, OOP sym, OOP mth ); |
| 399 |
OOP cw_compiled_method_new(); |
| 400 |
|
| 401 |
#endif /* CARAWAY_H */ |
| 402 |
|