Skip to content

Explicitly mark global variable definition as having 'undefined' value#13

Open
heksterb wants to merge 1 commit into
PacktPublishing:masterfrom
heksterb:fix-uninitialized-global
Open

Explicitly mark global variable definition as having 'undefined' value#13
heksterb wants to merge 1 commit into
PacktPublishing:masterfrom
heksterb:fix-uninitialized-global

Conversation

@heksterb
Copy link
Copy Markdown

With the code as is, global variable definitions have no initializer, producing invalid IR. For example, for the following input program to tinylang:

MODULE Point;

TYPE Point = RECORD X, Y: INTEGER END;

VAR p: Point;

PROCEDURE AssignX(a: INTEGER);
BEGIN
  p.X := a;
END AssignX;

END Point.

invoking build/tools/driver/tinylang --emit-llvm Point.mod produces this IR for the global variable definition:

@_t5Point1p = private global %Point

Attempting to process this produces an error:

$ llc Point.ll
llc: error: llc: Point.ll:10:1: error: expected value token
define void @_t5Point7AssignX(i64 %a) {
^

The simplest way to resolve this is to provide the variable with an 'undefined' initializer; now the IR reads

@_t5Point1p = private global %Point undef

which can be processed.

@OrchidRock
Copy link
Copy Markdown

OrchidRock commented Jan 9, 2023

How about use zeroinitializer instead of undef? I had pulled a PR #20 for this bug.

@heksterb
Copy link
Copy Markdown
Author

heksterb commented Jan 9, 2023

Hi @OrchidRock ; thanks for looking at this. I guess the answer to your question depends on what the semantics of global variables in 'tinylang' are, which are probably similar to those of Modula-2, which itself are probably similar to those of Pascal. I couldn't easily find any information about variable initialization in Modula-2, but in Pascal variables appear to be uninitialized (see for example freepascal.org).

Ultimately it doesn't matter so much for a toy language. and I know this isn't rock-solid evidence, but I think "uninitialized" looks like the correct answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants