1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
|
/**
STBytecodeInterpreter.m
Copyright (c) 2002 Free Software Foundation
This file is part of the StepTalk project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "STBytecodeInterpreter.h"
#import "Externs.h"
#import "STBlock.h"
#import "STBlockContext.h"
#import "STBytecodes.h"
#import "STCompiledMethod.h"
#import "STExecutionContext.h"
#import "STLiterals.h"
#import "STMessage.h"
#import "STMethodContext.h"
#import "STSmalltalkScriptObject.h"
#import "STStack.h"
#import <StepTalk/STEnvironment.h>
#import <StepTalk/STExterns.h>
#import <StepTalk/STFunctions.h>
#import <StepTalk/NSInvocation+additions.h>
#import <StepTalk/STScripting.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSValue.h>
@interface STBytecodeInterpreter(STPrivateMethods)
- (short)fetchBytecode;
- (BOOL)dispatchBytecode:(STBytecode)bytecode;
- (void)invalidBytecode:(STBytecode)bytecode;
- (void)setInstructionPointer:(NSUInteger)newIP;
- (NSUInteger)instructionPointer;
- (void)sendSelectorAtIndex:(NSUInteger)index withArgCount:(NSUInteger)argCount;
- (id)interpret;
- (void)returnValue:(id)value;
@end
static SEL sendSelectorAtIndexSel;
static IMP sendSelectorAtIndexImp;
static SEL pushSel;
static IMP pushImp;
static SEL popSel;
static IMP popImp;
static Class NSInvocation_class = nil;
@implementation STBytecodeInterpreter
+ (void)initialize
{
sendSelectorAtIndexSel = @selector(sendSelectorAtIndex:withArgCount:);
pushSel = @selector(push:);
popSel = @selector(pop);
sendSelectorAtIndexImp = [STBytecodeInterpreter instanceMethodForSelector:sendSelectorAtIndexSel];
pushImp = [STStack instanceMethodForSelector:pushSel];
popImp = [STStack instanceMethodForSelector:popSel];
NSInvocation_class = [NSInvocation class];
}
+ interpreterWithEnvrionment:(STEnvironment *)env
{
return AUTORELEASE([[self alloc] initWithEnvironment:env]);
}
- initWithEnvironment:(STEnvironment *)env
{
if ((self = [super init]) != nil)
environment = RETAIN(env);
return self;
}
- (void)dealloc
{
RELEASE(environment);
RELEASE(activeContext);
[super dealloc];
}
- (void)setEnvironment:(STEnvironment *)env
{
ASSIGN(environment,env);
}
- (id)interpretMethod:(STCompiledMethod *)method
forReceiver:(id)anObject
arguments:(NSArray*)args
{
// NSAutoreleasePool *pool = [NSAutoreleasePool new];
STExecutionContext *oldContext;
STMethodContext *newContext;
id retval;
if (!environment)
{
[NSException raise:STInterpreterGenericException
format:@"No execution environment set"];
return nil;
}
NSDebugLLog(@"STBytecodeInterpreter",@"Executing method %@ with %lu args",
[method selector],(unsigned long)[args count]);
if (!method)
{
return nil;
}
if ([args count] != [method argumentCount])
{
[NSException raise:STInterpreterGenericException
format:@"Invalid argument count %lu (should be %lu)"
@" for method %@ ",
(unsigned long)[args count],
(unsigned long)[method argumentCount],
[method selector]];
}
newContext = [[STMethodContext alloc] initWithMethod:method];
[newContext setArgumentsFromArray:args];
[newContext setReceiver:anObject];
oldContext = activeContext;
[self setContext:newContext];
NS_DURING
retval = [self interpret];
NS_HANDLER
if ([[localException name] isEqualToString:STInterpreterReturnException])
{
NSDictionary *userInfo = [localException userInfo];
if ([userInfo objectForKey: @"Context"] == newContext)
{
retval = [userInfo objectForKey:@"Value"];
}
else
{
RELEASE(newContext);
[localException raise];
}
}
else
{
RELEASE(newContext);
[localException raise];
}
NS_ENDHANDLER
[self setContext:oldContext];
RELEASE(newContext);
// RETAIN(retval);
// [pool release];
// AUTORELEASE(retval);
return retval;
}
/* ---------------------------------------------------------------------------
* Interpret
* ---------------------------------------------------------------------------
*/
- (void)setContext:(STExecutionContext *)newContext
{
NSDebugLLog(@"STExecutionContext", @"Switch from context %@ to context %@",
activeContext, newContext);
if(!newContext)
{
RELEASE(activeContext);
activeContext = nil;
stack = nil;
bytecodes = nil;
instructionPointer = 0;
receiver = nil;
return;
}
if( ![newContext isValid])
{
[NSException raise:STInterpreterGenericException
format:@"Trying to set an invalid context"];
}
[activeContext setInstructionPointer:instructionPointer];
ASSIGN(activeContext,newContext);
stack = [activeContext stack];
receiver = [activeContext receiver];
if(!stack)
{
[NSException raise:STInternalInconsistencyException
format:@"No execution stack"];
}
instructionPointer = [activeContext instructionPointer];
bytecodes = [activeContext bytecodes];
}
- (STExecutionContext *)context
{
return activeContext;
}
- (id)interpret
{
STBytecode bytecode;
id retval;
entry++;
NSDebugLLog(@"STBytecodeInterpreter", @"Interpreter entry %li", (long)entry);
NSDebugLLog(@"STBytecodeInterpreter", @"IP %lx %lx",
(unsigned long)instructionPointer,
(unsigned long)[bytecodes length]);
if(!bytecodes)
{
NSLog(@"Smalltalk: No bytecodes.");
return nil;
}
stopRequested = NO;
do
{
bytecode = [bytecodes fetchNextBytecodeAtPointer:&instructionPointer];
if(stopRequested)
{
break;
}
} while( [self dispatchBytecode:bytecode] );
if(!stopRequested)
{
retval = [stack pop];
}
else
{
NSDebugLLog(@"STBytecodeInterpreter",@"Stop requested");
retval = nil;
}
NSDebugLLog(@"STBytecodeInterpreter",
@"Returning '%@' from interpreter (entry %li)",
retval,(long)entry);
entry --;
return retval;
}
- (void)halt
{
NSDebugLLog(@"STBytecodeInterpreter",@"Halt!");
stopRequested = YES;
}
/* ---------------------------------------------------------------------------
* Return
* ---------------------------------------------------------------------------
*/
- (void)returnValue:(id)value
{
NSDebugLLog(@"STExecutionContext",
@"%@ return value '%@' from method",
activeContext,value);
[activeContext invalidate];
if (activeContext != [activeContext homeContext])
{
NSDictionary *userInfo;
/* FIXME Invalidate all contexts between the active context and the
home context as well. Note that the semantics presented in the
Blue Book doesn't get this right either. */
userInfo =
[NSDictionary dictionaryWithObjectsAndKeys:
/* Attention: Order is important here. The returned value
may be nil, so the context value must come first. */
[activeContext homeContext], @"Context", value, @"Value", nil];
[[NSException exceptionWithName:STInterpreterReturnException
reason:@"Block cannot return"
userInfo:userInfo] raise];
}
[stack push:value];
}
- (void)returnBlockValue:(id)value
{
NSDebugLLog(@"STExecutionContext",
@"%@ return value '%@' from block",
activeContext,value);
[activeContext invalidate];
[stack push:value];
}
- (NSUInteger)instructionPointer
{
return instructionPointer;
}
/* ---------------------------------------------------------------------------
* Block manipulation
* ---------------------------------------------------------------------------
*/
- (void)createBlockWithArgumentCount:(NSUInteger)argCount
stackSize:(NSUInteger)stackSize
{
NSUInteger ptr;
STBlock *block;
ptr = instructionPointer + STLongJumpBytecodeSize;
NSDebugLLog(@"STExecutionContext",
@"%@ Create block: argc:%lu stack:%lu ip:0x%04lx",
activeContext,
(unsigned long)argCount,
(unsigned long)stackSize,
(unsigned long)ptr);
block =
[[STBlock alloc] initWithInterpreter:self
homeContext:[activeContext homeContext]
initialIP:ptr
argumentCount:argCount
stackSize:stackSize];
[stack push:AUTORELEASE(block)];
}
/* ---------------------------------------------------------------------------
send selector (see also STEnvironment class)
* ---------------------------------------------------------------------------
*/
- (void)sendSelectorAtIndex:(NSUInteger)selIndex
withArgCount:(NSUInteger)argCount
{
NSString *selector;
NSInvocation *invocation;
id target;
NSInteger index;
id object;
NSDebugLLog(@"STSending",
@"send selector '%@' with %lu args'",
[activeContext literalObjectAtIndex:selIndex],
(unsigned long)argCount);
target = [stack valueFromTop:argCount];
/* FIXME */
if(!target)
{
target = STNil;
}
selector = [activeContext literalObjectAtIndex:selIndex];
NSDebugLLog(@"STSending",
@" %s receiver:%@ (%@) selector:%@",
[receiver isProxy] ? "proxy for" : "",
target,
NSStringFromClass([target class]),
selector);
/* FIXME: this is too slow */
selector = [environment translateSelector:selector forReceiver:target];
invocation = [NSInvocation_class invocationWithTarget:target
selectorName:selector];
if(!invocation)
{
[NSException raise:STInternalInconsistencyException
format:@"Should not send selector '%@' to "
@"receiver of type %@ (could not create invocation)",
selector,[target className]];
return;
}
for(index = argCount + 1; index > 1; index--)
{
object = [stack pop];
if(object == STNil)
{
object = nil;
}
NSDebugLLog(@"STSending",
@" argument %2li: '%@'",(long)(index - 2), object);
[invocation setArgumentAsObject:object atIndex:index];
}
/* ----------------------------
* invoke it!
* ----------------------------
*/
NSDebugLLog(@"STSending",@" invoking... (%@ %s)",invocation, [[invocation methodSignature] methodReturnType]);
[invocation invoke];
NSDebugLLog(@"STSending",@" done invoking.");
/* FIXME */
if(!stopRequested)
{
/* pop the receiver from the stack */
[stack pop];
[stack push: [invocation returnValueAsObject]];
}
}
/* ---------------------------------------------------------------------------
Bytecode manipulation
* ---------------------------------------------------------------------------
*/
#define STDebugBytecode(bc) \
NSDebugLLog(@"STBytecodeInterpreter", \
@"#%04lx %@", \
(unsigned long)(bc).pointer, \
STDissasembleBytecode(bc))
#define STDebugBytecodeWith(bc,object) \
NSDebugLLog(@"STBytecodeInterpreter", \
@"#%04lx %@ (%@)", \
(unsigned long)(bc).pointer, \
STDissasembleBytecode(bc), \
(object))
#define STPush(s, v) (*pushImp)(s, pushSel, v)
// #define STPush(s, v) [s push:v]
#define STPop(s) (*popImp)(s, popSel)
// #define STPop(s) [s pop]
- (BOOL)dispatchBytecode:(STBytecode)bytecode
{
NSString *refName;
id object;
switch(bytecode.code)
{
case STLongJumpBytecode:
{
/*
NSInteger offset =
STLongJumpOffset(bytecode.arg1,bytecode.arg2)
- STLongJumpBytecodeSize;
*/
NSInteger offset = bytecode.arg1 - STLongJumpBytecodeSize;
STDebugBytecode(bytecode);
instructionPointer+=offset;
break;
}
case STPushReceiverBytecode:
STDebugBytecodeWith(bytecode,receiver);
STPush(stack, receiver);
break;
case STPushNilBytecode:
STDebugBytecodeWith(bytecode,receiver);
STPush(stack,nil);
break;
case STPushTrueBytecode:
STDebugBytecodeWith(bytecode,receiver);
STPush(stack,[NSNumber numberWithBool:YES]);
break;
case STPushFalseBytecode:
STDebugBytecodeWith(bytecode,receiver);
STPush(stack,[NSNumber numberWithBool:NO]);
break;
case STPushRecVarBytecode:
refName = [activeContext referenceNameAtIndex:bytecode.arg1];
object = [receiver valueForKey:refName];
STDebugBytecodeWith(bytecode,object);
STPush(stack,object);
break;
case STPushExternBytecode:
// NSLog(@"PUSH EXTERN ctx(%@): %@ %@", [activeContext className], [[activeContext method] selector], [[activeContext method] namedReferences]);
refName = [activeContext referenceNameAtIndex:bytecode.arg1];
object = [environment objectWithName:refName];
STDebugBytecodeWith(bytecode,object);
STPush(stack,object);
break;
case STPushTemporaryBytecode:
object = [activeContext temporaryAtIndex:bytecode.arg1];
STPush(stack,object);
STDebugBytecodeWith(bytecode,object);
break;
case STPushLiteralBytecode:
object = [activeContext literalObjectAtIndex:bytecode.arg1];
STDebugBytecodeWith(bytecode,object);
STPush(stack,object);
break;
case STPopAndStoreRecVarBytecode:
STDebugBytecode(bytecode);
refName = [activeContext referenceNameAtIndex:bytecode.arg1];
object = STPop(stack);
[receiver setValue:object forKey:refName];
break;
case STPopAndStoreExternBytecode:
STDebugBytecode(bytecode);
refName = [activeContext referenceNameAtIndex:bytecode.arg1];
object = STPop(stack);
[environment setObject:object forName:refName];
break;
case STPopAndStoreTempBytecode:
STDebugBytecode(bytecode);
[activeContext setTemporary:STPop(stack) atIndex:bytecode.arg1];
break;
case STSendSelectorBytecode:
STDebugBytecodeWith(bytecode,
[activeContext literalObjectAtIndex:bytecode.arg1]);
(*sendSelectorAtIndexImp)(self, sendSelectorAtIndexSel,
bytecode.arg1,bytecode.arg2);
/*
[self sendSelectorAtIndex:bytecode.arg1
withArgCount:bytecode.arg2];
*/ break;
case STSuperSendSelectorBytecode: /* FIXME: not implemented */
[self invalidBytecode:bytecode];
break;
case STDupBytecode:
STDebugBytecode(bytecode);
[stack duplicateTop];
break;
case STPopStackBytecode:
STDebugBytecode(bytecode);
[stack pop];
break;
case STReturnBytecode:
STDebugBytecode(bytecode);
[self returnValue:[stack pop]];
return NO;
case STReturnBlockBytecode:
STDebugBytecode(bytecode);
[self returnBlockValue:[stack pop]];
return NO;
case STBlockCopyBytecode:
STDebugBytecode(bytecode);
{
STBlockLiteral *info = [stack pop];
[self createBlockWithArgumentCount:[info argumentCount]
stackSize:[info stackSize]];
}
break;
default:
[self invalidBytecode:bytecode];
break;
};
return YES;
}
- (void)invalidBytecode:(STBytecode)bytecode
{
[NSException raise:STInternalInconsistencyException
format:@"invalid bytecode (0x%02x) at 0x%06lx",
bytecode.code,(unsigned long)bytecode.pointer];
}
@end
|