Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Apr 30, 2024
1 parent c1924d0 commit 9bf4931
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
38 changes: 20 additions & 18 deletions toolchain.common/Internal/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,124 +49,126 @@ namespace chibicc.toolchain.Internal
{
internal static class Utilities
{
private static readonly IFormatProvider invariantCulture = CultureInfo.InvariantCulture;

public static bool TryParseUInt8(
string word,
out byte value) =>
byte.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
byte.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseInt8(string word, out sbyte value) =>
sbyte.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
sbyte.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseInt16(string word, out short value) =>
short.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
short.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseUInt16(string word, out ushort value) =>
ushort.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
ushort.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseInt32(string word, out int value) =>
int.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
int.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseUInt32(string word, out uint value) =>
uint.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
uint.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseInt64(string word, out long value) =>
long.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
long.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseUInt64(string word, out ulong value) =>
ulong.TryParse(
word,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
invariantCulture,
out value) ||
(word.StartsWith("0x") &&
ulong.TryParse(
word.Substring(2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
invariantCulture,
out value));

public static bool TryParseFloat32(string word, out float value) =>
float.TryParse(
word,
NumberStyles.Float,
CultureInfo.InvariantCulture,
invariantCulture,
out value);

public static bool TryParseFloat64(string word, out double value) =>
double.TryParse(
word,
NumberStyles.Float,
CultureInfo.InvariantCulture,
invariantCulture,
out value);

public static bool TryParseEnum<TEnum>(string word, out TEnum value)
Expand Down
61 changes: 36 additions & 25 deletions toolchain.common/Parsing/CilParser_FunctionBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibicc.toolchain.Internal;
using chibicc.toolchain.Tokenizing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

// System.Reflection.Emit declarations are used only referring OpCode metadata.
using System.Reflection.Emit;
using chibicc.toolchain.Internal;

namespace chibicc.toolchain.Parsing;

Expand Down Expand Up @@ -283,22 +282,14 @@ private bool ParseHiddenDirective(
$"Too many operand: {tokens[3]}");
return null;
}

var identityToken = tokens[1];
if (identityToken.Type != TokenTypes.Identity)
{
this.OutputError(
identityToken,
$"Invalid metadata token: {identityToken}");
return null;
}

if (tokens.ElementAtOrDefault(2) is { } signatureToken)

if (tokens.Length == 3)
{
var signatureToken = tokens[1];
if (signatureToken.Type != TokenTypes.Identity)
{
this.OutputError(
identityToken,
signatureToken,
$"Invalid metadata token signature: {signatureToken}");
return null;
}
Expand All @@ -312,20 +303,40 @@ private bool ParseHiddenDirective(
return null;
}

var identityToken = tokens[2];
if (identityToken.Type != TokenTypes.Identity)
{
this.OutputError(
identityToken,
$"Invalid metadata token: {identityToken}");
return null;
}

return new MetadataTokenInstructionNode(
new(tokens[0]),
new(identityToken),
fsn,
labels,
location);
}
else
{
var identityToken = tokens[1];
if (identityToken.Type != TokenTypes.Identity)
{
this.OutputError(
identityToken,
$"Invalid metadata token: {identityToken}");
return null;
}

return new MetadataTokenInstructionNode(
new(tokens[0]),
new(identityToken),
null,
labels,
location);
return new MetadataTokenInstructionNode(
new(tokens[0]),
new(identityToken),
null,
labels,
location);
}
}

private NumericValueInstructionNode? ParseNumericValueInstruction(
Expand All @@ -348,7 +359,7 @@ private bool ParseHiddenDirective(
$"Too many operand: {tokens[2]}");
return null;
}

var valueToken = tokens[1];
if (valueToken.Type != TokenTypes.Identity ||
numericParser(valueToken.Text) is not { } value)
Expand Down Expand Up @@ -385,7 +396,7 @@ private bool ParseHiddenDirective(
$"Too many operand: {tokens[2]}");
return null;
}

var valueToken = tokens[1];
if (valueToken.Type != TokenTypes.String)
{
Expand Down Expand Up @@ -422,7 +433,7 @@ private bool ParseHiddenDirective(
$"Too many operand: {tokens[2]}");
return null;
}

var variableIdentityToken = tokens[1];
if (variableIdentityToken.Type != TokenTypes.Identity)
{
Expand All @@ -446,7 +457,7 @@ private bool ParseHiddenDirective(
labels,
location);
}

if (!isShort && Utilities.TryParseUInt32(
variableIdentityToken.Text,
out var u32))
Expand All @@ -458,7 +469,7 @@ private bool ParseHiddenDirective(
labels,
location);
}

return new NameReferenceInstructionNode(
new(tokens[0]),
isArgumentIndirection,
Expand Down

0 comments on commit 9bf4931

Please sign in to comment.