Skip to content

Commit

Permalink
Port port ctor expression to new model.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmmr committed Dec 9, 2022
1 parent c30e16c commit 81d72a8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/autogen/types/port.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

.. rubric:: Operators

.. spicy:operator:: port::Call port port(uint<16>, enum)
Creates a port instance.

.. spicy:operator:: port::Equal bool t:port <sp> op:== <sp> t:port
Compares two port values.
Expand Down
10 changes: 10 additions & 0 deletions hilti/toolchain/include/ast/operators/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

#pragma once

#include <vector>

#include <hilti/ast/builder/type.h>
#include <hilti/ast/operators/common.h>
#include <hilti/ast/types/enum.h>
#include <hilti/ast/types/integer.h>
#include <hilti/ast/types/port.h>

namespace hilti::operator_ {

STANDARD_OPERATOR_2(port, Equal, type::Bool(), type::Port(), type::Port(), "Compares two port values.")
STANDARD_OPERATOR_2(port, Unequal, type::Bool(), type::Port(), type::Port(), "Compares two port values.")

BEGIN_KEYWORD_CTOR(port, Ctor, "port", type::Port(), "Creates a port instance.")
std::vector<Operand> parameters() const {
return {{"port", hilti::type::UnsignedInteger(16)}, {"protocol", type::Enum(type::Wildcard())}};
}
END_KEYWORD_CTOR

BEGIN_METHOD(port, Protocol)
const auto& signature() const {
static auto _signature = Signature{.self = type::Port(),
Expand Down
6 changes: 6 additions & 0 deletions hilti/toolchain/src/compiler/codegen/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ struct Visitor : hilti::visitor::PreOrder<cxx::Expression, Visitor> {

result_t operator()(const operator_::port::Equal& n) { return binary(n, "=="); }
result_t operator()(const operator_::port::Unequal& n) { return binary(n, "!="); }

result_t operator()(const operator_::port::Ctor& n) {
auto args = tupleArguments(n, n.op1());
return fmt("::hilti::rt::Port(%s, %s)", args[0], args[1]);
}

result_t operator()(const operator_::port::Protocol& n) { return fmt("%s.protocol()", op0(n)); }

// Set
Expand Down
6 changes: 4 additions & 2 deletions hilti/toolchain/src/compiler/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ expr_e : BEGIN_ '(' expr ')' { $$ = hilti::expression::Unres

expr_f : ctor { $$ = hilti::expression::Ctor(std::move($1), __loc__); }
| ctor_expr { $$ = std::move($1); }
| PORT '(' expr ',' expr ')' { $$ = hilti::builder::port(std::move($3), std::move($5), __loc__); }
| '-' expr_g { $$ = hilti::expression::UnresolvedOperator(hilti::operator_::Kind::SignNeg, {std::move($2)}, __loc__); }
| '[' expr FOR local_id IN expr ']'
{ $$ = hilti::expression::ListComprehension(std::move($6), std::move($2), std::move($4), {}, __loc__); }
Expand Down Expand Up @@ -824,10 +823,12 @@ ctor : CBOOL { $$ = hilti::ctor::Bool($1, __
| CADDRESS '/' CUINTEGER { $$ = hilti::ctor::Network(hilti::ctor::Network::Value($1, $3), __loc__); }
| CPORT { $$ = hilti::ctor::Port(hilti::ctor::Port::Value($1), __loc__); }

/* There are more here that we could move into ctor_expr and have them use namedCtor.
But not sure if that'd change much so leaving here for now.
*/
| OPTIONAL '(' expr ')' { $$ = hilti::ctor::Optional(std::move($3), __loc__); }
| DEFAULT type_param_begin type type_param_end '(' opt_exprs ')'
{ $$ = hilti::ctor::Default(std::move($3), std::move($6), __loc__); }

| list { $$ = std::move($1); }
| map { $$ = std::move($1); }
| regexp { $$ = std::move($1); }
Expand All @@ -850,6 +851,7 @@ ctor_expr : INTERVAL '(' expr ')' { $$ = hilti::builder::namedCto
| UINT16 '(' expr ')' { $$ = hilti::builder::namedCtor("uint16", { std::move($3) }, __loc__); }
| UINT32 '(' expr ')' { $$ = hilti::builder::namedCtor("uint32", { std::move($3) }, __loc__); }
| UINT64 '(' expr ')' { $$ = hilti::builder::namedCtor("uint64", { std::move($3) }, __loc__); }
| PORT '(' expr ',' expr ')' { $$ = hilti::builder::namedCtor("port", {std::move($3), std::move($5)}, __loc__); }
;

tuple : '(' opt_tuple_elems1 ')' { $$ = hilti::ctor::Tuple(std::move($2), __loc__); }
Expand Down
6 changes: 6 additions & 0 deletions hilti/toolchain/src/compiler/visitors/normalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ struct VisitorNormalizer : public visitor::PreOrder<void, VisitorNormalizer> {
});
}

void operator()(const operator_::port::Ctor& op, position_t p) {
tryReplaceCtorExpression<ctor::Port>(op, p, [](const auto& ctor) {
return ctor::Port(ctor::Port::Value(ctor.value()));
});
}

void operator()(const operator_::signed_integer::CtorSigned8& op, position_t p) {
tryReplaceCtorExpression<ctor::SignedInteger>(op, p, [this, &p](const auto& ctor) {
return ctor::SignedInteger(to_int64(ctor.value(), p), 8);
Expand Down

0 comments on commit 81d72a8

Please sign in to comment.