This question is about constructing custom expression trees in .NET using the operators found in C# (or any other language). I provide the question along with some the background information.
For my managed 2-phase 64-bit assembler I need support for expressions. For example, one might want to assemble:
mystring: DB 'hello, world'
TIMES 64-$+mystring DB ' '
The expression 64-$+mystring must not be a string but an actual valid expression with the benefits of syntax and type checking and IntelliSense in VS, something along the lines of:
64 - Reference.CurrentOffset + new Reference("mystring");
This expression is not evaluated when it is constructed. Instead, it is evaluated later in my assembler’s context (when it determines the symbol offsets and such). The .NET framework (since .NET 3.5) provides support for expressions trees, and it seems to me that it is ideal for this kind of expressions which are evaluated later or somewhere else.
But I don’t know how to ensure that I can use the C# syntax (using +, <<, %, etc..) for constructing the expression tree. I want to prevent things like:
var expression = AssemblerExpression.Subtract(64,
AssemblerExpression.Add(AssemblerExpression.CurrentOffset(),
AssemblerExpression.Reference("mystring")))
How would you go about this?
Note: I need an expression tree to be able to convert the expression into an acceptable custom string representation, and at the same time be able to evaluate it at a point in time other than at its definition.
An explanation of my example: 64-$+mystring. The $ is the current offset, so it is a specific number that is unknown in advance (but known at evaluation time). The mystring is a symbol which may or may not be known at evaluation time (for example when it has not yet been defined). Subtracting a constant C from a symbol S is the same as S + -C. Subtracting two symbols S0 and S1 (S1 - S0) gives the integer difference between the two symbol’s values.
However, this question is not really about how to evaluate assembler expressions, but more about how to evaluate any expression that has custom classes in them (for things like the symbols and $ in the example) and how to still ensure that it can be pretty-printed using some visitor (thus keeping the tree). And since the .NET framework has its expression trees and visitors, it would be nice to use those, if possible.
I don’t know what exactly you are aiming for, but the following is some sketchy approach that I think would work.
Note I
$(current offset) feature. It would probably be sure a register (?)operator-at work either. However, the mechanics are largely the same. I stopped short of adding it because I couldn’t work out the semantics of the sample expressions in your question(I’d think that subtracting the address of a known string is not useful, for example)
operator+directly on SymbolicReference would be more appropriate.Has sacrificed coding style for demo purposes (in general, you’ll not want to repeatedly
Compile()your expression trees, and direct evaluation with.Compile()()looks ugly and confusing. It’s left up to the OP to integrate it in a more legible fashionThe demonstration of the explicit conversion operator is really off-topic. I got carried away slighlty (?)
.