Skip to content

Commit

Permalink
got derefs working!
Browse files Browse the repository at this point in the history
  • Loading branch information
marceldobehere committed Oct 30, 2022
1 parent d32e686 commit 7d1a8bc
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 35 deletions.
3 changes: 0 additions & 3 deletions MAAL.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MAAL", "MAAL\MAAL.csproj",
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
asdfasdfasdfsdafsd|Any CPU = asdfasdfasdfsdafsd|Any CPU
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8292F520-AE71-4EAE-81EB-7F40BF0CE602}.asdfasdfasdfsdafsd|Any CPU.ActiveCfg = asdfasdfasdfsdafsd|Any CPU
{8292F520-AE71-4EAE-81EB-7F40BF0CE602}.asdfasdfasdfsdafsd|Any CPU.Build.0 = asdfasdfasdfsdafsd|Any CPU
{8292F520-AE71-4EAE-81EB-7F40BF0CE602}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8292F520-AE71-4EAE-81EB-7F40BF0CE602}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8292F520-AE71-4EAE-81EB-7F40BF0CE602}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
56 changes: 53 additions & 3 deletions MAAL/Compiling/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,19 +493,24 @@ public static BasicValueToken.BasicValueTypeEnum GetTypeFromExpression(Expressio
if (tok.Cast.CastType.PointerCount > 0)
return BasicValueToken.BasicValueTypeEnum.ULONG;

if (tok.Cast.CastType.PointerCount < 0)
throw new Exception($"POINTER CANT BE BELOW 0! {tok}");

string typeAsString = tok.Cast.CastType.BaseType;
return TypeToken.TypeEnumList[TypeToken.TypeList.IndexOf(typeAsString)];
}
if (tok.IsVariable)
{
if (allowCharPointer && !tok.Variable.UseAddr && tok.Variable.VarType.BaseType.Equals("char") &&
tok.Variable.VarType.PointerCount - tok.Variable.DereferenceCount == 1)
tok.Variable.VarType.PointerCount == 1)
return BasicValueToken.BasicValueTypeEnum.CHAR_POINTER;

if (tok.Variable.UseAddr || tok.Variable.DereferenceCount < tok.Variable.VarType.PointerCount)
if (tok.Variable.UseAddr || 0 < tok.Variable.VarType.PointerCount)
return BasicValueToken.BasicValueTypeEnum.ULONG;

if (tok.Variable.VarType.PointerCount < 0)
throw new Exception($"POINTER CANT BE BELOW 0! {tok}");

string typeAsString = tok.Variable.VarType.BaseType;
return TypeToken.TypeEnumList[TypeToken.TypeList.IndexOf(typeAsString)];
}
Expand All @@ -515,6 +520,27 @@ public static BasicValueToken.BasicValueTypeEnum GetTypeFromExpression(Expressio
return GetTypeFromExpression(tok.Left);
}

if (tok.IsDeref)
{
ExpressionToken toDeref = tok.Deref.ToDeref;
if (toDeref.IsVariable)
{
toDeref.Variable.VarType.PointerCount--;
var t = GetTypeFromExpression(toDeref);
toDeref.Variable.VarType.PointerCount++;
return t;
}
if (toDeref.IsCast)
{
toDeref.Cast.CastType.PointerCount--;
var t = GetTypeFromExpression(toDeref);
toDeref.Cast.CastType.PointerCount++;
return t;
}

throw new Exception($"CANT DEREF NON-VAR OR NON-CAST! {tok} {toDeref}");
}

// tok is Expression
{
BasicValueToken.BasicValueTypeEnum l = GetTypeFromExpression(tok.Left);
Expand Down Expand Up @@ -688,6 +714,30 @@ public static void CompileExpression(ExpressionToken tok, List<AlmostByte> almos
return;
}

if (tok.IsDeref)
{
var oType = GetTypeFromExpression(tok.Deref.ToDeref);
byte oSize = DaTyToSize[oType];
if (oType != BasicValueToken.BasicValueTypeEnum.ULONG && oType != BasicValueToken.BasicValueTypeEnum.CHAR_POINTER)
throw new Exception($"CANT DEREF NON-POINTER OR ULONG!");

var dType = GetTypeFromExpression(tok);
byte dSize = DaTyToSize[dType];

AlmostByte cmdByte = new AlmostByte(IToByte[InstructionEnum.COPY_FIX_SIZE_FROM_FIX_MEM_TO_FIX_MEM]);

CompileExpression(tok.Deref.ToDeref, almostCompiledCode, strLocs, new AlmostByte(cmdByte, 2, 8));

almostCompiledCode.Add(new AlmostByte($"DEREFING {tok.Deref.ToDeref} into {dType}"));
almostCompiledCode.Add(cmdByte);
almostCompiledCode.Add(new AlmostByte(dSize));
almostCompiledCode.Add(new AlmostByte(new byte[8]));
almostCompiledCode.Add(resAddr);

//throw new Exception($"BRO HOW TO DO DEREFS? {dType} <- {tok.Deref.ToDeref} | {oType} <- {tok}");

return;
}

if (tok.IsCast)
{
Expand Down Expand Up @@ -1560,7 +1610,7 @@ public static byte[] BasicValueToArr(BasicValueToken val, Dictionary<string, ulo

public static int GetSizeFromVarToken(VarNameToken var)
{
if (var.VarType.PointerCount - var.DereferenceCount > 0)
if (var.VarType.PointerCount > 0)
return 8; // Pointers are 8 bytes
if (var.UseAddr)
return 8;
Expand Down
31 changes: 23 additions & 8 deletions MAAL/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private static ExpressionToken ConvToExpressionToken(Token tok)
return new ExpressionToken(tok as BasicValueToken);
else if (tok is VarNameToken)
return new ExpressionToken(tok as VarNameToken);
else if (tok is DerefToken)
return new ExpressionToken(tok as DerefToken);
else if (tok is ExpressionToken)
return (tok as ExpressionToken);
else
Expand All @@ -67,7 +69,7 @@ private static ExpressionToken ConvToExpressionToken(Token tok)

private static bool CouldBeExpessionToken(Token tok)
{
return tok is ExpressionToken || tok is BasicValueToken || tok is VarNameToken;
return tok is ExpressionToken || tok is BasicValueToken || tok is VarNameToken || tok is DerefToken;
}
#endregion

Expand Down Expand Up @@ -397,14 +399,27 @@ public static ParsedStuff ParseStringAdvanced(List<Token> data, ParsedStuff stuf
#endregion


#region DEREF
//#region OLD DEREF
////*test
//if (false && cTok is OperatorToken && ((cTok as OperatorToken).Operator == OperatorToken.OperatorEnum.Star) && mIndex + 1 < data.Count && data[mIndex + 1] is VarNameToken
// && (mIndex == 0 || !CouldBeExpessionToken(data[mIndex - 1])))
//{
// VarNameToken tok = data[mIndex + 1] as VarNameToken;
// tok.DereferenceCount++;
// data.RemoveAt(mIndex);
// change = true;
// break;
//}
//#endregion
#region DEREF EXPR
//*test
if (cTok is OperatorToken && ((cTok as OperatorToken).Operator == OperatorToken.OperatorEnum.Star) && mIndex + 1 < data.Count && data[mIndex + 1] is VarNameToken
&& (mIndex == 0 || !CouldBeExpessionToken(data[mIndex - 1])))
if (cTok is OperatorToken && ((cTok as OperatorToken).Operator == OperatorToken.OperatorEnum.Star) &&
mIndex + 1 < data.Count &&
CouldBeExpessionToken(data[mIndex + 1]) &&
(mIndex == 0 || !CouldBeExpessionToken(data[mIndex - 1])))
{
VarNameToken tok = data[mIndex + 1] as VarNameToken;
tok.DereferenceCount++;
data.RemoveAt(mIndex);
data[mIndex] = new DerefToken(ConvToExpressionToken(data[mIndex + 1]));
data.RemoveAt(mIndex + 1);
change = true;
break;
}
Expand Down Expand Up @@ -599,7 +614,7 @@ public static ParsedStuff ParseStringAdvanced(List<Token> data, ParsedStuff stuf
if (cTok is TypeToken && mIndex + 4 < data.Count &&
(data[mIndex + 1] is GenericNameToken) && (data[mIndex + 4] is EndCommandToken) &&
(data[mIndex + 2] is OperatorToken) && (data[mIndex + 2] as OperatorToken).Operator == OperatorToken.OperatorEnum.Set &&
((data[mIndex + 3] is GenericNameToken) || (data[mIndex + 3] is VarNameToken) || (data[mIndex + 3] is BasicValueToken) || (data[mIndex + 3] is ExpressionToken)))
CouldBeExpessionToken(data[mIndex + 3]))
{
string varName = (data[mIndex + 1] as GenericNameToken).Name;
DeclareVarToken tok = new DeclareVarToken(
Expand Down
6 changes: 5 additions & 1 deletion MAAL/Parsing/ParserHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ public static bool TryOptimizeExpressionToken(ExpressionToken tok)
{
bool change = false;
change |= TryOptimizeExpressionToken(tok.Cast.ToCast);
Console.WriteLine($"Trying to opt {tok.Cast.ToCast}");
//Console.WriteLine($"Trying to opt {tok.Cast.ToCast}");
if (tok.Cast.ToCast.IsConstValue)
{
var val = tok.Cast.ToCast.ConstValue;
Expand All @@ -584,6 +584,10 @@ public static bool TryOptimizeExpressionToken(ExpressionToken tok)

return change;
}
if (tok.IsDeref)
{
return TryOptimizeExpressionToken(tok.Deref.ToDeref);
}
if (tok.OnlyUseLeft)
{
if (tok.Operator.Operator == OperatorToken.OperatorEnum.Not)
Expand Down
32 changes: 24 additions & 8 deletions MAAL/Parsing/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,30 +322,23 @@ public class VarNameToken : Token
{
public string VarName;
public TypeToken VarType;
public int DereferenceCount = 0;
public bool UseAddr = false;

public VarNameToken(string name, TypeToken typeToken)
{
VarName = name;
VarType = typeToken;
DereferenceCount = 0;
UseAddr = false;
}

public override string ToString()
{
int dif = VarType.PointerCount - DereferenceCount;
if (dif < 0)
throw new Exception("Too many Dereferences!");

VarType.PointerCount -= DereferenceCount;
string res;
if (UseAddr)
res = $"<VAR: \"{VarName}\" (&{VarType})>";
else
res = $"<VAR: \"{VarName}\" ({VarType})>";
VarType.PointerCount += DereferenceCount;

return res;
}

Expand Down Expand Up @@ -389,17 +382,33 @@ public override string ToString()
=> $"<{VarType} \"{VarName}\">";
}

public class DerefToken : Token
{
public ExpressionToken ToDeref;

public DerefToken(ExpressionToken toDeref)
{
ToDeref = toDeref;
}

public override string ToString()
=> $"<*{ToDeref}>";
}


public class ExpressionToken : Token
{
public OperatorToken Operator = null;
public ExpressionToken Left = null, Right = null;
public BasicValueToken ConstValue = null;
public VarNameToken Variable = null;
public CastToken Cast = null;
public DerefToken Deref = null;
public bool IsConstValue = false;
public bool OnlyUseLeft = false;
public bool IsVariable = false;
public bool IsCast = false;
public bool IsDeref = false;

public ExpressionToken(ExpressionToken left, OperatorToken op, ExpressionToken right)
{
Expand Down Expand Up @@ -429,6 +438,11 @@ public ExpressionToken(BasicValueToken val)
IsVariable = false;
IsCast = false;
}
public ExpressionToken(DerefToken val)
{
Deref = val;
IsDeref = true;
}
public ExpressionToken(VarNameToken val)
{
Left = null;
Expand Down Expand Up @@ -480,6 +494,8 @@ public override string ToString()
return $"{Variable}";
if (IsCast)
return $"{Cast}";
if (IsDeref)
return $"{Deref}";

if (OnlyUseLeft)
return $"<{Operator} {Left}>";
Expand Down
Binary file modified MAAL/bin/Debug/MAAL.exe
Binary file not shown.
29 changes: 17 additions & 12 deletions MAAL/bin/Debug/test.maal
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
loc MAIN:
char* str = "This is a test!";

int x = 5;
print "\nX: ";
print x;
ulong strAddr = (ulong)str;
print "\nADDR1: ";
print strAddr;

strAddr++;
print "\nADDR2: ";
print strAddr;

sub TEST;
char* str2 = (char*)strAddr;
print "\nADDR3: ";
print (ulong)str2;

print "\nX: ";
print x;

exit;
char chr = *str2;
print "\nCHR: ";
print chr;


sub TEST:
x = 10;

ret;
print "\nCHR: ";
print *((char*)((ulong)strAddr - 1));

exit;
Binary file modified MAAL/bin/Debug/test.maal.maab
Binary file not shown.
Binary file modified MAAL/obj/Debug/MAAL.exe
Binary file not shown.
8 changes: 8 additions & 0 deletions MAAL/obj/asdfasdfasdfsdafsd/MAAL.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
E:\C SHARP\MAAL\MAAL\bin\asdfasdfasdfsdafsd\MAAL.exe.config
E:\C SHARP\MAAL\MAAL\bin\asdfasdfasdfsdafsd\MAAL.exe
E:\C SHARP\MAAL\MAAL\bin\asdfasdfasdfsdafsd\MAAL.pdb
E:\C SHARP\MAAL\MAAL\obj\asdfasdfasdfsdafsd\MAAL.csproj.AssemblyReference.cache
E:\C SHARP\MAAL\MAAL\obj\asdfasdfasdfsdafsd\MAAL.csproj.SuggestedBindingRedirects.cache
E:\C SHARP\MAAL\MAAL\obj\asdfasdfasdfsdafsd\MAAL.csproj.CoreCompileInputs.cache
E:\C SHARP\MAAL\MAAL\obj\asdfasdfasdfsdafsd\MAAL.exe
E:\C SHARP\MAAL\MAAL\obj\asdfasdfasdfsdafsd\MAAL.pdb
Binary file added MAAL/obj/asdfasdfasdfsdafsd/MAAL.exe
Binary file not shown.

0 comments on commit 7d1a8bc

Please sign in to comment.