Hi,
I use a simple hack in SLICC's grammar to allow referring to enum values
inside SLICC (.sm) files.
Add the following inside `src/mem/slicc/parser.py`:
def p_literal__enumvalue(self, p):
"literal : IDENT DOUBLE_COLON IDENT"
p[0] = ast.LiteralExprAST(self, p[1] + p[2] + p[3], p[1])
This definition allows using things like `MyEnum::A_VALUE" inside SLICC.
However, it also allows invalid code to be successfully parsed, since it
only requires two valid
identifiers separated by a double colon.
I'm sure that with enough time it would be possible to make a definition
that only accepts enum
types as the first identifier, but checking if the second identifier is a
valid value of the given
enum would be a nightmare. You probably would need to repeat the definition
of the enum inside
SLICC, the same way you need to repeat method declarations for external
classes.
I gave up on doing something better when I realized that. Having to
redefine every enum inside of
.sm files kinda defeats the purpose of having external enums in the first
place.
Anyway, with that hack and declaring `external_type(MyEnum,
primitive="yes")` inside the .sm file
allowed me to use enums created through python with the classes
m5.params.Enum/ScopedEnum.
For enums defined directly in C++, things become a bit more complex.
Instead of `external_type(MyEnum, primitive="yes")` you need to use
`external_type(MyEnum)`.
Next, you need to add a MakeInclude call inside `src/mem/ruby/SConscript`
and here you must pay
attention to 2 things:
- MakeInclude assumes the file will be inside `src/mem/`.
- Some piece of code somewhere assumes the name of the file matches the
name of the external type.
So, for instance, if your enum is named "MyEnum" and is placed in
`src/myfiles/`, your MakeInclude
will be like `MakeInclude("../../myfiles/MyEnum.hh")`.
With all that done, you still need to do one more thing:
in the file `src/mem/slicc/symbols/StateMachine.py` you need to add an item
to the
`python_class_map` in the form "MyEnum":"MyEnum".
If I didn't forget anything, that's all.
To me, as an outside user, SLICC seems to be a feature designed with good
intentions in mind, but
that ended up causing more harm than good. Pure C++ would be much more
straightforward to model
protocols, state machines and such (given an adequate level of abstraction
with pre-defined
classes, of course).
Marleson
On Tue, 28 Jan 2020 at 13:48, Jason Lowe-Power <[email protected]> wrote:
> Hi Giacomo,
>
> You cannot include a header in an .sm file. As far as I know, you also
> can't use general enums in SLICC either. You could take advantage of the
> fact that enums are compatible with integers, possibly, to get around this.
> You can also declare enums in the SLICC, but that will create another enum
> type. See src/mem/ruby/protocol/RubySlicc_Exports.sm for some examples of
> SLICC enums. For example, RubyRequestType is used in the gem5 C++ code, but
> the enum is declared in SLICC.
>
> Generally, if you need to reference a type outside of SLICC, you have to
> expose it by using the `external_type` keyword or by creating a SLICC
> `structure` and declaring it as `external=True`. You can do this wherever
> you want in the SLICC code before you use the type (i.e., you don't have to
> do it in RubySlicc_Types.sm). SLICC `structures` are equivalent to C++
> classes and `external_type`s can be classes, typedefs, etc. You can try to
> declare your enum as an external type, but you won't be able to use the
> enumeration constants in SLICC, as far as I know.
>
> This is a good example of what I was taught about SLICC/Ruby when I was
> first learning about it from Brad: Whenever you do anything new with SLICC,
> you'll have to make modifications to the language.
>
> I'm not certain of this, but it's most likely that if you want to use a
> gem5 enum in SLICC, you'll have to extend the compiler to support that (or
> just find a way around it).
>
> Cheers,
> Jason
>
> On Tue, Jan 28, 2020 at 3:36 AM Giacomo Travaglini <
> [email protected]> wrote:
>
> > A question to the Ruby experts:
> >
> > How can I include a header file in a .sm one?
> > More specifically I would like to refer to a gem5 enum from within the sm
> > file.
> >
> > Thanks
> >
> > Giacomo
> > IMPORTANT NOTICE: The contents of this email and any attachments are
> > confidential and may also be privileged. If you are not the intended
> > recipient, please notify the sender immediately and do not disclose the
> > contents to any other person, use it for any purpose, or store or copy
> the
> > information in any medium. Thank you.
> > _______________________________________________
> > gem5-dev mailing list
> > [email protected]
> > http://m5sim.org/mailman/listinfo/gem5-dev
> _______________________________________________
> gem5-dev mailing list
> [email protected]
> http://m5sim.org/mailman/listinfo/gem5-dev
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev