user2269707
user2269707

Reputation:

How one pass C compiler deal with labels?

Some times labels is used before declared, for example:

void test() {
  goto label;
  label: return;
}

when an one pass compiler parses the first statement, it doesn't know where the label is, until the label: statement comes.

Since one pass compiler only parses the code once, there's no way to leave the label alone and comes back later, right?

So what is the usual way to deal with this in an one compiler?

Upvotes: 1

Views: 357

Answers (1)

rici
rici

Reputation: 241721

Two possibilities:

  1. Backpatch. Use the destination address field in the generated branch operation to create a linked list of unresolved uses of the label, putting the head of the list in the label symbol table. When the label is defined, walk the list, overwriting ("patching") each link with the correct value.

  2. If you're allowed to generate symbolic assembly code, just output the label name and let the assembler deal with it.

Upvotes: 3

Related Questions