Skip to content

Commit 77b6963

Browse files
committed
other c# fixes
1 parent d484cfa commit 77b6963

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
From a single simple description, generate all tools you may need. Test your rules with fuzzers, generate machine learning agents, use it in C or python, embed it in graphical engines and on the web. All automatically.
77

8-
Rulebook contains unique mechanisms that cannot be implemented in mainstream languages such as c, cpp and python that mathematically guarantees asymptotically less code to write the same rules. (paper to be released soon.)
8+
Rulebook contains unique mechanisms that cannot be implemented in mainstream languages such as c, cpp and python that mathematically guarantees asymptotically less code to write the same rules by allowing composition and reuse of sequences of actions.
99

1010
The following table compares the number of lines of code required to implement a given game in Rulebook and CPP, excluding their header files. The number of lines they require scales quadratically with respect to the complexity of the game. Our does not.
1111
![RLC Logo](./imgs/lines_of_code.png)
@@ -54,6 +54,8 @@ It will to learn pass true to `win` to maximize `score`, as reported by the seco
5454

5555
[Tutorial for GYM users](./docs/gym_tutorial.md)
5656

57+
[Paper for Reinforcement Learning users](https://arxiv.org/abs/2504.19625)
58+
5759
[Language reference and stdlib documentation](https://github.com/rl-language/rlc-stdlib-doc/tree/master)
5860

5961
### Contacts

docs/rationale.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `Rulebook` (or `rl` for short) is a domain specific-language that tries to reduce the complexity of writing simulations, such as those used in reinforcement learning, game programming, and similar other domains. This document explains the rationale behind it.
44

5-
If you want to jump directly into the code, try it out as described in the main page instead instead. If you want to read about the project rationale, read it [here](./where_we_are_going.md). If you want to see a quick description of what the `rlc` tool is capable of, instead of the `rl` language, you can see video [here](https://www.youtube.com/watch?v=tMnBo3TGIbU). A less pratical and more rambly philosofical description of why `rl` is usefull can be found [here](./philosophy.md)
5+
If you want to jump directly into the code, try it out as described in the main page instead instead. If you want to read about the project rationale, read it [here](./where_we_are_going.md). If you want to see a quick description of what the `rlc` tool is capable of, instead of the `rl` language, you can see video [here](https://www.youtube.com/watch?v=tMnBo3TGIbU).
66

77
### The Complexity of writing games and rule heavy simulations.
88

lib/conversions/src/RLCToCSharp.cpp

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
#include "rlc/dialect/Passes.hpp"
2323
#include "rlc/dialect/Types.hpp"
2424
#include "rlc/dialect/Visits.hpp"
25+
#include "rlc/dialect/conversion/TypeConverter.h"
2526

2627
namespace mlir::rlc
2728
{
@@ -79,7 +80,7 @@ namespace mlir::rlc
7980
matcher.add([](mlir::rlc::StringLiteralType type,
8081
llvm::raw_string_ostream& OS) { OS << "char*"; });
8182
matcher.add([&](mlir::rlc::ArrayType type, llvm::raw_string_ostream& OS) {
82-
OS << matcher.convert(type.getUnderlying()) << "*";
83+
OS << typeToMangled(type) << ".Content";
8384
});
8485
matcher.add(
8586
[&](mlir::rlc::OwningPtrType type, llvm::raw_string_ostream& OS) {
@@ -213,7 +214,7 @@ namespace mlir::rlc
213214
const int toDrop = int(isMemberFunction);
214215
for (auto [type, name] : llvm::drop_begin(llvm::zip(types, args), toDrop))
215216
{
216-
if (isCSharpBuiltinType(type) or isBuiltinDeclaration)
217+
if (isBuiltinDeclaration)
217218
writer.write("ref ");
218219
writer.writeType(type, isBuiltinDeclaration);
219220
writer.write(" ");
@@ -270,12 +271,7 @@ namespace mlir::rlc
270271
writer.write(" __result");
271272
writer.write(" = new ");
272273
writer.writeType(returnType);
273-
writer.write("((");
274-
writer.writeType(returnType, 1);
275-
writer.write("*)");
276-
writer.write("Marshal.AllocHGlobal(sizeof(");
277-
writer.writeType(returnType, 1);
278-
writer.writenl(")));");
274+
writer.write("();");
279275
}
280276
}
281277
}
@@ -756,7 +752,7 @@ namespace mlir::rlc
756752

757753
if (not table.isTriviallyCopiable(type))
758754
{
759-
writer.write("void assign(", name, " other) {");
755+
writer.write("public void assign(", name, " other) {");
760756
writer.indentOnce(1);
761757
writer.writenl(
762758
"RLCNative.",
@@ -863,7 +859,9 @@ namespace mlir::rlc
863859
static void emitArrayDecl(
864860
mlir::rlc::ArrayType type,
865861
StreamWriter& writer,
866-
MemberFunctionsTable& table)
862+
MemberFunctionsTable& table,
863+
size_t typeSize,
864+
size_t elementSize)
867865
{
868866
writer.write("public unsafe class ");
869867
writer.writeType(type);
@@ -874,15 +872,42 @@ namespace mlir::rlc
874872
writer.writenl("public Content* __content;");
875873
writer.writenl("private bool owning;");
876874
writer.writenl("[StructLayout(LayoutKind.Sequential)]");
877-
writer.write("public struct Content {");
875+
writer.writenl("public struct Content {");
878876
auto _ = writer.indent();
879-
writer.write("public fixed ");
880-
writer.writeType(type.getUnderlying());
877+
writer.write("public fixed byte");
881878
writer.write(" __content");
882-
writer.write("[");
883-
writer.writeType(type.getSize());
884-
writer.writenl("];");
879+
writer.writenl("[", typeSize, "];");
885880
writer.writenl("}").endLine();
881+
882+
writer.write("public ");
883+
if (isCSharpBuiltinType(type.getUnderlying()))
884+
{
885+
writer.write("ref ");
886+
}
887+
writer.writeType(type.getUnderlying());
888+
writer.writenl(" this [int index] {");
889+
writer.writenl("get {");
890+
writer.writenl(
891+
"if ((((uint) index) >= ",
892+
type.getArraySize(),
893+
")) throw new ArgumentOutOfRangeException(nameof(index));");
894+
writer.writenl("return ");
895+
if (isCSharpBuiltinType(type.getUnderlying()))
896+
{
897+
writer.write("ref (*(((");
898+
writer.writeType(type.getUnderlying());
899+
writer.write("*) __content) + index));");
900+
}
901+
else
902+
{
903+
writer.write("new ");
904+
writer.writeType(type.getUnderlying());
905+
writer.write("((((");
906+
writer.writeType(type.getUnderlying(), 1);
907+
writer.write("*) __content) + index));");
908+
}
909+
writer.writenl("}");
910+
writer.writenl("}");
886911
}
887912

888913
emitSpecialFunctions(type, writer, table);
@@ -1166,6 +1191,14 @@ namespace mlir::rlc
11661191
}
11671192
}
11681193

1194+
static size_t getSizeTypeInBytes(
1195+
const mlir::DataLayout& dl,
1196+
mlir::Type type,
1197+
mlir::TypeConverter& converterToLLVMIR)
1198+
{
1199+
return dl.getTypeSize(converterToLLVMIR.convertType(type));
1200+
}
1201+
11691202
#define GEN_PASS_DEF_PRINTCSHARPPASS
11701203
#include "rlc/dialect/Passes.inc"
11711204

@@ -1178,8 +1211,11 @@ namespace mlir::rlc
11781211
PatternMatcher matcher(*OS);
11791212
MemberFunctionsTable table(getOperation());
11801213
mlir::rlc::ModuleBuilder builder(getOperation());
1214+
mlir::TypeConverter converter;
1215+
mlir::rlc::registerConversions(converter, getOperation());
11811216

11821217
llvm::SmallVector<std::string> declaredFunNames;
1218+
const auto& dl = mlir::DataLayout::closest(getOperation());
11831219

11841220
emitPrelude(matcher.getWriter());
11851221
matcher.addTypeSerializer();
@@ -1234,7 +1270,12 @@ namespace mlir::rlc
12341270
}
12351271
else if (auto casted = mlir::dyn_cast<mlir::rlc::ArrayType>(type))
12361272
{
1237-
emitArrayDecl(casted, matcher.getWriter(), table);
1273+
emitArrayDecl(
1274+
casted,
1275+
matcher.getWriter(),
1276+
table,
1277+
getSizeTypeInBytes(dl, casted, converter),
1278+
getSizeTypeInBytes(dl, casted.getUnderlying(), converter));
12381279
}
12391280
}
12401281
}

tool/rlc/test/wrappers/csharp_action.rl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class Tester {
2222
public static int Main() {
2323
RLCNative.setup(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/Lib" + RLCNative.SharedLibExtension);
2424
Game pair = RLC.play();
25-
long arg = 3;
26-
pair.pick(ref arg);
25+
pair.pick(3);
2726
return (int)(pair.asd - 3);
2827
}
2928
}

tool/rlc/test/wrappers/csharp_custom_functions.rl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class Tester {
3333
Outer pair = new Outer();
3434
Console.WriteLine(pair.inner.x);
3535

36-
long x = 2 ;
37-
return ((int) RLC.to_invoke(pair, ref x)) - 5;
36+
return ((int) RLC.to_invoke(pair, 2)) - 5;
3837
}
3938
}
4039

tool/rlc/test/wrappers/csharp_union.rl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class Tester {
2323
public static int Main() {
2424
RLCNative.setup(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/Lib" + RLCNative.SharedLibExtension);
2525
Outer pair = new Outer();
26-
double x = 2.2;
27-
pair.inner.x.assign(ref x);
26+
pair.inner.x.assign(2.2);
2827
if (pair.inner.x.get_long != null) {
2928
return -10;
3029
}

tool/rlc/test/wrappers/csharp_vector.rl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Tester {
2626
RLCNative.setup(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/Lib" + RLCNative.SharedLibExtension);
2727
VectorTint64_tT pair = new VectorTint64_tT();
2828
long x = 2;
29-
pair.append(ref x);
30-
pair.append(ref x);
31-
pair.append(ref x);
29+
pair.append(x);
30+
pair.append(x);
31+
pair.append(x);
3232
return (int)(pair.size() - 3);
3333
}
3434
}

0 commit comments

Comments
 (0)