Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
noahw2021 committed Nov 27, 2023
1 parent 9833595 commit 81e7a6d
Show file tree
Hide file tree
Showing 24 changed files with 516 additions and 34 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,38 @@
endingLineNumber = "122"
landmarkName = "__nonvideo_main(Args)"
landmarkType = "9">
<Locations>
<Location
uuid = "C621FE7E-15A7-4BA2-BCE8-A879AEE26F44 - f65a579f866f7b20"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "__nonvideo_main"
moduleName = "plasm2_emu"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/noahwooten/Desktop/Desktop%20-%20Noah%E2%80%99s%20MacBook%20Pro/source/plasm2/plasm2_emu/main.c"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "122"
endingLineNumber = "122"
offsetFromSymbolStart = "1012">
</Location>
<Location
uuid = "C621FE7E-15A7-4BA2-BCE8-A879AEE26F44 - f65a579f866f7b20"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "__nonvideo_main"
moduleName = "plasm2_emu"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/noahwooten/Desktop/Desktop%20-%20Noah%E2%80%99s%20MacBook%20Pro/source/plasm2/plasm2_emu/main.c"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "122"
endingLineNumber = "122"
offsetFromSymbolStart = "928">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
Expand All @@ -163,5 +195,53 @@
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "30080A4F-9AD1-4FCE-9CD8-2130CF0D36E9"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "plasm2_emu/main.c"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "73"
endingLineNumber = "73"
landmarkName = "__nonvideo_main(Args)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "FB713B83-CA5A-4D48-9F6D-213481C59423"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "plasm2_emu/decoder/decoder_debug.c"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "decoder_go(Instruction)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "29B97569-0F1B-4371-A44D-85E3673442A5"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "plasm2_disassembler/main.c"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "21"
endingLineNumber = "21"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
19 changes: 10 additions & 9 deletions plasm2_asm/link/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
void LinkInit(void);
void LinkShutdown(void);

WORD64 LinkGetSymbol(const char* Name, WORD32 Offset);
WORD64 LinkGetSymbol(const char* Name, WORD32 Offset, BYTE Opcode);
void LinkResolve(const char* Name, WORD64 Pointer);
void LinkGo(void);

typedef struct _LINK_CTX {
WORD64 SymbolCount;
struct {
WORD64 Resolution;
char* SymbolName;
BYTE Resolved;

WORD64 LocationCount;
WORD64* Locations;
}*Symbols;
struct {
WORD64 Resolution;
char* SymbolName;
BYTE Resolved;

WORD64 LocationCount;
WORD64* Locations;
BYTE* LocationOpcodes;
}*Symbols;
}LINK_CTX, *PLINK_CTX;
extern PLINK_CTX LinkCtx;
63 changes: 56 additions & 7 deletions plasm2_asm/link/link_go.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "link.h"
#include "../cg/cg.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//
// link_go.c
// plasm2_asm
//
// Created by Noah Wooten on 4/21/23.
//
#include "link.h"
#include "../cg/cg.h"
#include "../symbols/symbols.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

extern FILE* PrimaryOutput;
#pragma warning(disable: 6001 6386 6387)

Expand All @@ -21,13 +23,60 @@ void LinkGo(void) {
free(ErrorMsg);
}

WORD32 VariableCount = 0, FunctionCount = 0;
for (int l = 0; l < LinkCtx->Symbols[s].LocationCount; l++) {
fseek(PrimaryOutput, (WORD32)LinkCtx->Symbols[s].Locations[l], SEEK_SET);
// linkctx->Symbols[s].Resolution; // += cgctx->ReferencePtr; WHY??
fwrite(&LinkCtx->Symbols[s].Resolution, 8, 1, PrimaryOutput);

BYTE ThisOpcode = LinkCtx->Symbols[s].LocationOpcodes[l];

switch (ThisOpcode) {
case 0x02:
case 0x08:
case 0x0F:
case 0x11:
case 0x91:
FunctionCount++;
break;
default:
VariableCount++;
break;
}
}

if (LinkCtx->Symbols[s].Locations)

WORD64 BestEndingSize = 0xFFFFFFFFFFFFFFFF;
for (int s2 = 0; s2 < LinkCtx->SymbolCount; s2++) {
if (LinkCtx->Symbols[s2].Resolution > LinkCtx->Symbols[s].Resolution) {
WORD64 Difference = LinkCtx->Symbols[s2].Resolution -
LinkCtx->Symbols[s].Resolution;

if (Difference < BestEndingSize)
BestEndingSize = Difference;
}
}

if (BestEndingSize == 0xFFFFFFFFFFFFFFFF)
BestEndingSize = CgCtx->HighestPosition - LinkCtx->Symbols[s].Resolution;

WORD64 SymbolTypePrefix = 0;
if (FunctionCount > (VariableCount / 4) ||
FunctionCount == VariableCount
) { // Function
SymbolsCreateFunction(LinkCtx->Symbols[s].Resolution, BestEndingSize,
LinkCtx->Symbols[s].SymbolName);
} else if ((FunctionCount + VariableCount) == 0) { // Unused
SymbolsCreateUnused(LinkCtx->Symbols[s].Resolution, BestEndingSize,
LinkCtx->Symbols[s].SymbolName);
} else { // Variable
SymbolsCreateVariable(LinkCtx->Symbols[s].Resolution, BestEndingSize,
LinkCtx->Symbols[s].SymbolName);
}



// @TODO why are we not freeing this??? - nw 11/21/23
//if (LinkCtx->Symbols[s].Locations)
//free(linkctx->Symbols[s].Locations);
LinkCtx->Symbols[s].LocationCount = 0;
}
Expand Down
39 changes: 27 additions & 12 deletions plasm2_asm/link/link_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@
#include <string.h>
#pragma warning(disable: 6308 26451)

WORD64 LinkGetSymbol(const char* Name, WORD32 Offset) {
WORD64 LinkGetSymbol(const char* Name, WORD32 Offset, BYTE Opcode) {
for (int i = 0; i < LinkCtx->SymbolCount; i++) {
if (!strcmp(LinkCtx->Symbols[i].SymbolName, Name)) {
if (LinkCtx->Symbols[i].Resolved)
return LinkCtx->Symbols[i].Resolution;
if (!LinkCtx->Symbols[i].Locations) {
LinkCtx->Symbols[i].Locations = malloc(sizeof(LinkCtx->Symbols[i].Locations[0]));
} else {
LinkCtx->Symbols[i].Locations = realloc(LinkCtx->Symbols[i].Locations, (sizeof(WORD64) * (LinkCtx->Symbols[i].LocationCount + 1)));
}

if (!strcmp(LinkCtx->Symbols[i].SymbolName, Name)) {
if (LinkCtx->Symbols[i].Resolved)
return LinkCtx->Symbols[i].Resolution;
if (!LinkCtx->Symbols[i].Locations) {
LinkCtx->Symbols[i].Locations = malloc(sizeof(LinkCtx->Symbols[i].Locations[0]));
} else {
LinkCtx->Symbols[i].Locations = realloc(LinkCtx->Symbols[i].Locations, (sizeof(WORD64) * (LinkCtx->Symbols[i].LocationCount + 1)));
}

if (!LinkCtx->Symbols[i].LocationOpcodes) {
LinkCtx->Symbols[i].LocationOpcodes = malloc(sizeof(BYTE));
} else {
LinkCtx->Symbols[i].LocationOpcodes =
realloc(LinkCtx->Symbols[i].LocationOpcodes,
sizeof(BYTE) *
(LinkCtx->Symbols[i].LocationCount + 1));
}
BYTE* ThisOpcode = &LinkCtx->Symbols[i].LocationOpcodes
[LinkCtx->Symbols[i].LocationCount++];

WORD64 CodePos = CgCtx->DataPosition + Offset;
int Loc = LinkCtx->Symbols[i].LocationCount;
WORD32 Loc = LinkCtx->Symbols[i].LocationCount;
LinkCtx->Symbols[i].Locations[Loc] = CodePos;
LinkCtx->Symbols[i].LocationCount++;
*ThisOpcode = Opcode;

return LinkCtx->Symbols[i].Resolution;
}
}
Expand All @@ -38,7 +51,9 @@ WORD64 LinkGetSymbol(const char* Name, WORD32 Offset) {

LinkCtx->Symbols[LinkCtx->SymbolCount].LocationCount = 1;
LinkCtx->Symbols[LinkCtx->SymbolCount].Locations = malloc(sizeof(WORD64));
LinkCtx->Symbols[LinkCtx->SymbolCount].Locations[0] = CgCtx->DataPosition + Offset;
LinkCtx->Symbols[LinkCtx->SymbolCount].LocationOpcodes = malloc(sizeof(BYTE));
LinkCtx->Symbols[LinkCtx->SymbolCount].LocationOpcodes[0] = Opcode;
LinkCtx->Symbols[LinkCtx->SymbolCount].Locations[0] = CgCtx->DataPosition + Offset;
LinkCtx->Symbols[LinkCtx->SymbolCount].Resolution = 0;
LinkCtx->Symbols[LinkCtx->SymbolCount].Resolved = 0;
LinkCtx->Symbols[LinkCtx->SymbolCount].SymbolName = malloc(strlen(Name) + 1);
Expand Down
16 changes: 16 additions & 0 deletions plasm2_asm/plasm2_asm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
8C4E22282B0CD24C006D70ED /* symbols.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = symbols.h; sourceTree = "<group>"; };
8C4E22292B0CD2BC006D70ED /* symbols_init.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = symbols_init.c; sourceTree = "<group>"; };
8C4E222A2B0CD2C6006D70ED /* symbols_generate.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = symbols_generate.c; sourceTree = "<group>"; };
8C4E222B2B0CD32B006D70ED /* symbols_io.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = symbols_io.c; sourceTree = "<group>"; };
8C6C48F72B097DE500B73C87 /* cg_init.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = cg_init.c; sourceTree = "<group>"; };
8C6C48F82B097DE500B73C87 /* cg_put.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = cg_put.c; sourceTree = "<group>"; };
8C6C48F92B097DE500B73C87 /* cg_errors.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = cg_errors.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -73,6 +77,17 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
8C4E22272B0CD242006D70ED /* symbols */ = {
isa = PBXGroup;
children = (
8C4E22282B0CD24C006D70ED /* symbols.h */,
8C4E22292B0CD2BC006D70ED /* symbols_init.c */,
8C4E222A2B0CD2C6006D70ED /* symbols_generate.c */,
8C4E222B2B0CD32B006D70ED /* symbols_io.c */,
);
path = symbols;
sourceTree = "<group>";
};
8C6C48F62B097DCF00B73C87 /* cg */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -125,6 +140,7 @@
children = (
8C6C49142B097E9E00B73C87 /* main.c */,
8C6C49132B097E9000B73C87 /* basetypes.h */,
8C4E22272B0CD242006D70ED /* symbols */,
8C6C49122B097E7600B73C87 /* vf */,
8C6C49082B097E5B00B73C87 /* psin2 */,
8C6C49002B097DEF00B73C87 /* link */,
Expand Down
36 changes: 36 additions & 0 deletions plasm2_asm/symbols/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,41 @@
#ifndef symbols_h
#define symbols_h

#include <stdio.h>

#define SYMBOL_TYPE_FUNCTION 0x01
#define SYMBOL_TYPE_VARIABLE 0x02
#define SYMBOL_TYPE_UNUSED 0x03

typedef struct _SYMBOL {
char SymbolName[256];
WORD64 SymbolType;
WORD64 VirtualIp;
WORD64 AssignedValue;
WORD64 VirtualSize;
}SYMBOL, *PSYMBOL;

typedef struct _SYMBOL_FILE {
WORD64 CompileDate;
WORD32 SymbolCount;
WORD32 SymbolVirtualAddress;
}SYMBOL_FILE, *PSYMBOL_FILE;

typedef struct _SYMBOL_CTX {
FILE* SymbolFile;

PSYMBOL ResolvedSymbols;
WORD32 SymbolCount;
}SYMBOL_CTX, *PSYMBOL_CTX;
extern PSYMBOL_CTX SymbolsCtx;

void SymbolsInit(void);
void SymbolsShutdown(void);
void SymbolsSetFile(FILE* OutFile);
void SymbolsGenerate(void);

void SymbolsCreateFunction(WORD64 VirtualIp, WORD64 VirtualSize, char* Name);
void SymbolsCreateVariable(WORD64 AssignedValue, WORD64 Size, char* Name);
void SymbolsCreateUnused(WORD64 VirtualIp, WORD64 VirtualSize, char* Name);

#endif /* symbols_h */
24 changes: 24 additions & 0 deletions plasm2_asm/symbols/symbols_generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,27 @@
//

#include <stdio.h>
#include <string.h>
#include <time.h>
#include "symbols.h"

void SymbolsGenerate(void) {
if (!SymbolsCtx)
return;

SYMBOL_FILE FileHeader;
memset(&FileHeader, 0, sizeof(SYMBOL_FILE));

time(&FileHeader.CompileDate);
FileHeader.SymbolCount = SymbolsCtx->SymbolCount;
FileHeader.SymbolVirtualAddress = sizeof(SYMBOL_FILE);

fseek(SymbolsCtx->SymbolFile, 0, SEEK_SET);
fwrite(&FileHeader, sizeof(SYMBOL_FILE), 1, SymbolsCtx->SymbolFile);

fseek(SymbolsCtx->SymbolFile, FileHeader.SymbolVirtualAddress, SEEK_SET);
fwrite(SymbolsCtx->ResolvedSymbols, sizeof(SYMBOL), SymbolsCtx->SymbolCount,
SymbolsCtx->SymbolFile);

return;
}
Loading

0 comments on commit 81e7a6d

Please sign in to comment.