-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
252 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#import "VM.h" | ||
#import <Foundation/Foundation.h> | ||
|
||
#include <stdint.h> | ||
|
||
typedef enum : uint64_t { | ||
SYSCALL_READ = 0, | ||
SYSCALL_WRITE = 1, | ||
SYSCALL_OPEN = 2, | ||
SYSCALL_SEMGET = 64, | ||
SYSCALL_SEMOP = 65, | ||
SYSCALL_SEMCTL = 66, | ||
} SYSCALL_OPCODE; | ||
|
||
typedef enum : uint64_t { | ||
STDIN_FILENO = 0, // File number of stdin; | ||
STDOUT_FILENO = 1, // File number of stdout; | ||
STDERR_FILENO = 2, // File number of stderr; | ||
} WRITE_DEVICE_OPCODE; | ||
|
||
@interface VM (Syscalls) | ||
|
||
- (void)instructionSyscall; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#import "Headers/Utilities.h" | ||
#import "Headers/VM+Syscalls.h" | ||
#import "Headers/VM.h" | ||
#import <Foundation/Foundation.h> | ||
|
||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <sys/sem.h> | ||
|
||
@implementation VM (Syscalls) | ||
|
||
- (void)syscallRead { | ||
@try { | ||
id readDeviceID = [self.dataStack pop]; | ||
id contentPointer = [self.dataStack pop]; | ||
NSUInteger pointer = [contentPointer integerValue]; | ||
NSUInteger length = [[self.memoryRAM objectAtIndex:pointer] integerValue]; | ||
pointer++; | ||
|
||
Word_Manager wm = howManyWords(length); | ||
char stringContent[length]; | ||
ssize_t bytesWritten = read([readDeviceID integerValue], stringContent, length); | ||
NSUInteger upperLimit = wm.howManyWords; | ||
NSArray *treatedInput = padWords(packString(@(stringContent)), upperLimit); | ||
|
||
[self.memoryRAM replaceObjectsInRange:(NSMakeRange (pointer, upperLimit)) | ||
withObjectsFromArray:treatedInput]; | ||
[self.dataStack push:@(bytesWritten)]; | ||
} @catch (NSException *exception) { | ||
@throw exception; | ||
} | ||
} | ||
|
||
- (void)syscallWrite { | ||
@try { | ||
id writeDeviceID = [self.dataStack pop]; | ||
id contentPointer = [self.dataStack pop]; | ||
|
||
NSString *content = unpackString(contentPointer, self.memoryRAM); | ||
const char *stringContent = [content UTF8String]; | ||
|
||
ssize_t bytesWritten = write([writeDeviceID integerValue], stringContent, strlen(stringContent)); | ||
[self.dataStack push:@(bytesWritten)]; | ||
} @catch (NSException *exception) { | ||
@throw exception; | ||
} | ||
} | ||
|
||
- (void)instructionSyscall { | ||
@try { | ||
id syscallOpcode = [self.dataStack pop]; | ||
if ([syscallOpcode isEqualTo:@(SYSCALL_WRITE)]) { | ||
NSLog(@"WRITE SYSCALL"); | ||
[self syscallWrite]; | ||
} else if ([syscallOpcode isEqualTo:@(SYSCALL_READ)]) { | ||
NSLog(@"READ SYSCALL"); | ||
[self syscallRead]; | ||
} else { | ||
@throw [NSException | ||
exceptionWithName:@"Unrecognized syscall opcode" | ||
reason:[NSString stringWithFormat: | ||
@"Could not find syscall with opcode %lu", [syscallOpcode integerValue]] | ||
userInfo:nil]; | ||
} | ||
} @catch (NSException *exception) { | ||
@throw exception; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ plus | |
double | ||
/2 | ||
+* | ||
nop | ||
nop | ||
syscall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
:main literal literal literal syscall | ||
&readSyscallContent | ||
*stdinDevice | ||
*readSyscall | ||
literal literal literal syscall halt | ||
&readSyscallContent | ||
*stdoutDevice | ||
*writeSyscall | ||
|
||
>stdinDevice #0 | ||
>readSyscall #0 | ||
>stdoutDevice #1 | ||
>writeSyscall #1 | ||
:readSyscallContent "000000000000000000000000000000" |
Oops, something went wrong.