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

Static initializer for class members #113

Merged
merged 4 commits into from Jul 21, 2021

Conversation

s-hadinger
Copy link
Contributor

@s-hadinger s-hadinger commented Jul 19, 2021

Here is a first try to implement inline static class initializers

> class A static a = 42 end
> A.a
42

Code is generated after the class initializer code is identical to explicit initializers:

> import debug
> debug.codedump(compile("class A static a = 42, b = 1 end"))
source 'string', function 'main':
; line 1
  0000  LDCONST	R0	K0
  0001  SETGBL	R0	G24
  0002  CLASS	K0
  0003  GETGBL	R0	G24
  0004  LDINT	R1	42
  0005  SETMBR	R0	R257	R1
  0006  GETGBL	R0	G24
  0007  SETMBR	R0	R258	R259
  0008  RET	0	R0

> debug.codedump(compile("class A static a, b end A.a = 42 A.b = 1"))
source 'string', function 'main':
; line 1
  0000  LDCONST	R0	K0
  0001  SETGBL	R0	G24
  0002  CLASS	K0
  0003  GETGBL	R0	G24
  0004  LDINT	R1	42
  0005  SETMBR	R0	R257	R1
  0006  GETGBL	R0	G24
  0007  SETMBR	R0	R258	R259
  0008  RET	0	R0

However we could think of an optimization, as to remove redundant GETGBL R0 G24

Ideally it should be:

source 'string', function 'main':
; line 1
  0000  LDCONST	R0	K0
  0001  SETGBL	R0	G24
  0002  CLASS	K0
  0003  LDINT	R1	42
  0004  SETMBR	R0	R257	R1
  0005  SETMBR	R0	R258	R259
  0006  RET	0	R0

But my understanding of code generator it too limited.

@skiars
Copy link
Member

skiars commented Jul 20, 2021

Please update the EBNF file.

@s-hadinger
Copy link
Contributor Author

Ah yes, I forgot this one. Done.

}
}

static void classstatic_stmt(bparser *parser, bclass *c, bexpdesc *e)
{
bstring *name;
/* 'static' ID {',' ID} */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done in #115

@skiars skiars merged commit 040988d into berry-lang:master Jul 21, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants