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

Constant propagation #324

Closed
briot opened this issue Sep 11, 2019 · 2 comments
Closed

Constant propagation #324

briot opened this issue Sep 11, 2019 · 2 comments

Comments

@briot
Copy link

briot commented Sep 11, 2019

I am working on a tool to automatically generate input/output from types declared in an Ada package. This includes binary output, for which we want to output the minimum number of bytes to contain all possible values of the type.

Is there any planned support in libadalang for either:

  • computing 'Size for a type (not sure whether this might sometimes require backend knowledge, for instance when fields are reordered in a record -- I personally only care for scalar types but this feature would have to be more general in libadalang I guess)

  • propagating constants, so that I can use the range information from the type declaration. Of course, the range might be the result of some computation, in particular involving constants from other parts of the code or even (sigh) from formal parameters.

I also want to generate python versions of these input/output routines, so I cannot simply do the computation in the generated Ada code, but need some static value to pass to python.

Both of these seem far-fetched for libadalang, but I might as well ask :-)

@raph-amiard
Copy link
Member

Hi Manu!

computing 'Size for a type (not sure whether this might sometimes require backend knowledge, for instance when fields are reordered in a record -- I personally only care for scalar types but this feature would have to be more general in libadalang I guess)

This is planned but not yet implemented for LAL. It would be basically the equivalent of ASIS' data decomposition annex. Note that for ASIS, it will soon be implemented on top of gnat's -gnatr4js switch, and that's basically what will be used by Libadalang too (eg. we don't plan to reimplement that in LAL).

Note that if you really need it, you can definitely emit and read the corresponding JSON files yourself! With help from Libadalang you should be able to map types and data representation info quite easily (hint: using LAL's p_fully_qualified_name property would be a start I guess ...)

propagating constants, so that I can use the range information from the type declaration. Of course, the range might be the result of some computation, in particular involving constants from other parts of the code or even (sigh) from formal parameters.

I'm not sure that's exactly what you're looking for, but Libadalang as a static expression evaluator, that might help you, and that is currently accessible via the p_eval_as_int property.

Using it, you can eval constants. Note that:

  1. It's not necessarily complete
  2. It will raise a property error if you try to use it on expressions that are not statically known.
  3. It might be hard to distinguish between 1. and 2. :)

Here is an example to get first and last values for a set of types, including one example where it will fail because the value is not statically known:

procedure Test_Static_Eval is
   type A is range 1 .. 1248;
   High : constant := 583989 * 34;
   type B is range A'First .. High;
   High_2 : constant Positive := 28359;
   type C is range 1 .. High_2;

   procedure NonStatic (B : Integer) is
      type D is range 1 .. B;
   begin
      null;
   end NonStatic;

begin
   null;
end Test_Static_Eval;

And then using the playground on it:

def eval_range(rc):
    rng = rc.f_range.f_range
    return rng.f_left.p_eval_as_int, rng.f_right.p_eval_as_int

for rc in u.root.findall(lal.SignedIntTypeDef):
    try:
        print(eval_range(rc))
    except lal.PropertyError as p:
        print("Cannot eval range for {}".format(rc.entity_repr))
        print( "Exception:", p.message)

@briot
Copy link
Author

briot commented Sep 11, 2019

Hi Raphael,
Thanks a lot for your very extensive reply. I think that fully answers my questions. I did not know about p_eval_as_int (the dump function which I suggested in #317 filters it out...).
Having to first compile with -gnatr4js might slow things down a bit, so I don't know whether I'll use that. Since this is only for the python output, I might find other solutions for those cases where I can't evaluate the range via p_eval_as_in.

@briot briot closed this as completed Sep 11, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit to raph-amiard/libadalang that referenced this issue Sep 12, 2019
raph-amiard added a commit that referenced this issue Sep 12, 2019
raph-amiard added a commit that referenced this issue Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants