Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounded generic parameter #866

Closed
dalance opened this issue Aug 8, 2024 · 5 comments · Fixed by #917
Closed

Bounded generic parameter #866

dalance opened this issue Aug 8, 2024 · 5 comments · Fixed by #917
Labels

Comments

@dalance
Copy link
Collaborator

dalance commented Aug 8, 2024

Generic parameter doesn't have any constraint.
So user of the defined generics don't know which value is acceptable as generic parameter.

module ModuleA {
    inst u: GenericModule::<X /* X is ok??? */> (
    };
}

Additionally, giving wrong value as generic parameter causes complex error.

To resolve this issue, many programming languages introduce bounded generic parameter.

  • Rust: trait bound
  • C++: concept
  • Java: bounded type parameter
@dalance dalance added the lang Language design label Aug 8, 2024
@dalance
Copy link
Collaborator Author

dalance commented Aug 22, 2024

Generic parameters can be classified into the following patterns.

  • constant value like 1, "hello", and parameter N
  • user defined type
  • non-type item like module, interface, package, and function

So the following should be required as parameter bound.

  • const
  • type
  • Named prototype
    • Prototype module
    • Prototype interface
    • Prototype package
    • Prototype function

Prototype means that the public interface of the item is only defined.
For example, the public interface of module is parameter and port.

The syntax idea is below:

const

function FuncA::<N: const> (
    a: input logic<N>,
) -> logic {
}

type

struct StructA::<T: type> {
    a: T,
}

Named prototype

To declare prototype, proto keyword is introduced.
Prototype doesn't affect code generation, it is used for checking generic parameter bound and generating document only.

Prototype module

Prototype module has only parameter and port definition.

proto module ProtoA #(
    param X: u32,
) (
    clk: input clock,
);

module ModuleA::<T: ProtoA> {
    inst u: T #(
        X: 1,
    ) (
        clk: 1,
    );
}

Prototype interface and prototype function

Prototype interface is almost same as normal interface except for having prototype function.
Prototype function has arguments and return type only.

proto interface ProtoA #(
    param X: u32,
) {
    var a: logic;

    modport A {
        a: input,
    }

    proto function FuncA (
        x: input logic,
    ) -> logic;
}

module ModuleA::<T: ProtoA> {
    inst u: T #(
        X: 1,
    );
}

Prototype package

Prototype package is almost same as normal package except for having prototype function.

proto package ProtoA {
    local A: u32 = 1;

    proto function FuncA (
        x: input logic,
    ) -> logic;
}

module ModuleA::<T: ProtoA> {
    let x: u32 = T::A;
}

@dalance
Copy link
Collaborator Author

dalance commented Aug 27, 2024

Small adjustments of proto interface and package syntax.

  • value-less local
  • proto of function is not required

Default implementation of local and function will not be supported yet at first.

proto package ProtoA {
    local A: u32;

    function FuncA (
        x: input logic,
    ) -> logic;
}

Syntax of prototype implementation is like below:
This is from trait impl syntax (impl X for Y) of Rust.

module ModuleA for ProtoA #(
) (
) {}

@dalance
Copy link
Collaborator Author

dalance commented Aug 27, 2024

The necessity of proto interface and package is not clear, but the syntax is more complex than proto module.
The existing parameter override may cover the use case.

So in this PR, adding proto module only.

@taichi-ishitani
Copy link
Contributor

For interface prototype following items are also required.

  • const declarations
  • type/enum/struct/union declarations

@dalance
Copy link
Collaborator Author

dalance commented Sep 13, 2024

I created #963.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants