7
var str = "string"

How does the LLVM detect the variable is a string?

riyaz
  • 1,093
  • 1
  • 8
  • 21

1 Answers1

6

The compiler does its job in steps and type inference is one step in this process.

Step 1: Lexical analysis

Typically, as a first step, the compiler does a lexical analysis in which it splits input file bytes to units like numbers and strings (note: not yet string in the same meaning that you are referring) and throws away whitespace and comments.

For example, it splits the input stream in the example to var, str, =, ", string, ".

Step 2: Syntax analysis

The second step is a syntax analysis or parsing, in which compiler constructs and verifies an abstract syntax tree based on the grammar of the language.

In this case it would construct an abstract syntax tree representing a variable declaration statement (see Declaration statements in the language reference) of the form:

var variable_name = expression

in which expression is a string literal:

var variable_name = string_literal

Step 3: Semantic analysis (with type inference)

The third step is semantic analysis and type inference happens in this step. In this case, type inference infers that the type of string literal is String and because there is no other information about the variable str, it will infer that str must be a String, because the expression, the right hand side of the variable declaration, is of type String.

There are more steps after this step. As next steps, the compiler typically generates intermediate code, then optimizes the intermediate code, and finally generates assembly code. After that the tool chain outside of the compiler usually has at least a linking phase that produces the final executable.

Teemu Kurppa
  • 4,779
  • 2
  • 32
  • 38