Skip to content

Commit

Permalink
Add C++ WebAssembly ABI (mono#1711)
Browse files Browse the repository at this point in the history
* Minor code refactorings.

* Fix debug assert issue with vtable methods.

* Add support for WebAssembly C++ ABI to parser and AST converter.

(cherry picked from commit 9b06e7b)
  • Loading branch information
tritao authored and JordanL8 committed Aug 29, 2023
1 parent e2141f1 commit a1a6fd1
Show file tree
Hide file tree
Showing 18 changed files with 872 additions and 880 deletions.
3 changes: 2 additions & 1 deletion src/AST/ASTContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum CppAbi
Microsoft,
ARM,
iOS,
iOS64
iOS64,
WebAssembly
}

/// <summary>
Expand Down
9 changes: 1 addition & 8 deletions src/AST/ClassLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ public struct VTableComponent
public Declaration Declaration;

/// Method declaration (if Kind == FunctionPointer).
public Method Method
{
get
{
Debug.Assert(Kind == VTableComponentKind.FunctionPointer);
return Declaration as Method;
}
}
public Method Method => Declaration as Method;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CLI/CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static void Main(string[] args)

Generator gen = new Generator(options);

bool validOptions = gen.ValidateOptions(errorMessages);
var validOptions = gen.ValidateOptions(errorMessages);
PrintErrorMessages(errorMessages);

if (errorMessages.Any() || !validOptions)
Expand Down
26 changes: 4 additions & 22 deletions src/CLI/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,13 @@ namespace CppSharp
{
class Generator : ILibrary
{
private Options options = null;
private readonly Options options;
private string triple = "";
private CppAbi abi = CppAbi.Microsoft;

public Generator(Options options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

this.options = options;
}

static TargetPlatform GetCurrentPlatform()
{
if (Platform.IsWindows)
return TargetPlatform.Windows;

if (Platform.IsMacOS)
return TargetPlatform.MacOS;

if (Platform.IsLinux)
return TargetPlatform.Linux;

throw new System.NotImplementedException("Unknown host platform");
this.options = options ?? throw new ArgumentNullException(nameof(options));
}

void SetupTargetTriple()
Expand Down Expand Up @@ -78,8 +61,7 @@ public bool ValidateOptions(List<string> messages)
return false;
}

if (!options.Platform.HasValue)
options.Platform = GetCurrentPlatform();
options.Platform ??= Platform.Host;

if (string.IsNullOrEmpty(options.OutputDir))
{
Expand Down Expand Up @@ -189,7 +171,7 @@ public void Postprocess(Driver driver, ASTContext ctx)

public void Run()
{
StringBuilder messageBuilder = new StringBuilder();
var messageBuilder = new StringBuilder();
messageBuilder.Append($"Generating {GetGeneratorKindName(options.Kind)}");
messageBuilder.Append($" bindings for {GetPlatformName(options.Platform)} {options.Architecture}");

Expand Down
3 changes: 2 additions & 1 deletion src/CppParser/Bindings/CLI/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ namespace CppSharp
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
};

public enum class RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6301,7 +6301,8 @@ public enum CppAbi
Microsoft = 1,
ARM = 2,
iOS = 3,
iOS64 = 4
iOS64 = 4,
WebAssembly = 5
}

public enum RecordArgABI
Expand Down
Loading

0 comments on commit a1a6fd1

Please sign in to comment.