# LLVM Tutorial#6: Compilation Trouble

Published on Jun 12th 2020Duration: 31:37

I am learning LLVM. In this series I am planning to walk through the introductory tutorial called Kaleidoscope at https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/. In the last episode we implemented operator precedence. In this one, we attempt to finish writing the parser but ran into some problems compiling the program.

## Transcript

The following transcript was automatically generated by an algorithm.

• 00:03 : all right we're live this is Toby and
• 00:06 : we're continuing this series where we we
• 00:10 : walk through the LLVM my first language
• 00:16 : front-end tutorial we're in the later
• 00:21 : part of Chapter two hopefully this is
• 00:25 : gonna be the episode that finishes
• 00:27 : chapter two chapter two has been a
• 00:29 : thinkthey chapter so in the last episode
• 00:34 : we finished although we haven't tested
• 00:36 : the code but we finished writing the
• 00:39 : code to parse binary operations and we
• 00:43 : did it in a way that hand those operator
• 00:45 : presidents and operator precedence is
• 00:50 : done using this technique which I
• 00:53 : believe is called the shunting yard
• 00:54 : algorithm where we assign a number to
• 00:58 : represent the presidents of each
• 01:00 : operator and then there's this clever
• 01:03 : logic during the parsing that makes use
• 01:07 : of that information to make the tree
• 01:12 : shift correctly to represent the correct
• 01:15 : presidents this time we're gonna do the
• 01:19 : section called parse the rest and let's
• 01:23 : just start by copying the example code
• 01:26 : line by line this one is gonna first
• 01:29 : prototype and what they mean by
• 01:32 : prototype in in their language it is not
• 01:36 : like a JavaScript prototype but is the
• 01:38 : prototype of a function which means the
• 01:40 : function signature so we're going to
• 01:42 : parse the function signature is what
• 01:45 : that means which which I think it looks
• 01:48 : like these are the parameter lists
• 02:02 : yeah you know this this is the function
• 02:06 : name and this this whole thing that's
• 02:09 : what we're parsing there's gonna be as
• 02:12 : identifiers token there's gonna be a
• 02:15 : left paren as a token there's gonna be
• 02:18 : another identifier token or for each of
• 02:21 : the parameter things in the parameter
• 02:22 : list it's gonna be a identifier token
• 02:24 : the comma is gonna be its own token this
• 02:28 : is gonna be an identifier and this is
• 02:31 : going to be a closing parenthesis
• 02:46 : and this function is going to return a
• 02:48 : unique pointer to a proto ast okay so is
• 02:57 : saying if current token is not a toque
• 03:03 : identifier we wear like throwing an
• 03:06 : error basically not use throwing of
• 03:10 : errors so this is the place where it
• 03:18 : actually uses the log error P function
• 03:22 : [Music]
• 03:24 : I'm still not sure why that's needed
• 03:27 : honestly so it can return the same type
• 03:35 : that's a waste that's it I don't like
• 03:38 : this way of doing it but that I won't
• 03:42 : complain too much
• 03:43 : I mean whoever wrote this tutorial thank
• 03:47 : you so much I think I would have a much
• 03:51 : harder time learning LLVM so I will try
• 03:55 : my best not to criticize okay so
• 04:02 : basically expecting the first token to
• 04:06 : be an identifier it is not we're
• 04:08 : throwing in there or we're airing out
• 04:12 : and then and because we are verified
• 04:17 : that the current token is an identifier
• 04:20 : we expect the name of the identifier to
• 04:23 : be in identifier string we're gonna
• 04:25 : stick it in this function name variable
• 04:28 : and this get next token eats D
• 04:32 : identifier okay and now we're gonna
• 04:39 : we're expecting after eating the
• 04:43 : identifier token that the next token
• 04:46 : after that is going to be an open paren
• 04:48 : so I'll just copy this bit so if it's
• 04:54 : not an open paren again
• 04:56 : airing out right away and now we're
• 04:58 : gonna read the list of argument names
• 05:00 : I'm gonna type this out and then explain
• 05:02 : it line by line first we're gonna
• 05:07 : instantiate a vector of strings which
• 05:12 : are gonna be the argument names and then
• 05:16 : we're gonna do a while loop saying while
• 05:24 : let me see
• 05:27 : and so this is not comma delimited
• 05:30 : actually interesting it's not comma
• 05:36 : delimited that means my picture is like
• 05:40 : upar open print and no comma and they
• 05:46 : should look like that that's fine
• 05:49 : so I'll just follow what it does so I'll
• 05:52 : get next token yes identifier then we're
• 05:59 : gonna push push this token that
• 06:04 : identifier string to the back of this
• 06:06 : vector
• 06:11 : if current token is not a closing trend
• 06:17 : than the Derby problems okay because
• 06:21 : after the archer this argument lists
• 06:25 : we're expecting a closing print oh this
• 06:28 : should be our names and then but but if
• 06:34 : it is then we're going to consume that
• 06:36 : token and if everything is successful
• 06:42 : we're gonna return a neat pointer using
• 06:48 : make unique it's gonna be a unique
• 06:52 : pointer to a proto ast with the fun name
• 06:58 : the name and we're gonna need to use
• 07:01 : standard and move to move the art names
• 07:05 : I think okay interestingly it's it's not
• 07:19 : a unique pointer here not sure why but
• 07:24 : it is a unique pointers here okay so
• 07:27 : that parses the prototype of the
• 07:29 : function and now let's parse the
• 07:32 : definition I believe of a function
• 07:51 : so we're assuming that the current token
• 07:54 : is the DEF token so we're eating the DEF
• 07:57 : token
• 08:10 : if we did not successfully parse a
• 08:14 : prototype of the function we're gonna
• 08:16 : return the null pointer to signal that
• 08:18 : stuff failed
• 08:20 : now we're gonna I'm gonna do this
• 08:24 : because that's my style we're gonna
• 08:28 : parse an expression next and if that
• 08:35 : worked
• 08:38 : I'm gonna I think the different probably
• 08:42 : is gonna be the body of the function I
• 08:44 : believe again we're gonna use mix unique
• 08:50 : to new up so to speak a new function ast
• 08:58 : will put the proto in there and also the
• 09:04 : expression in there as the body of the
• 09:07 : function I would have called that body
• 09:12 : but okay whoops thing I just reverted a
• 09:21 : page okay okay so that this part is a
• 09:32 : function definition and now we're gonna
• 09:36 : write the code to parse an extern
• 09:39 : externus like there's a function that's
• 09:43 : just defined externally I think and then
• 09:46 : you're just gonna write like like
• 09:51 : external bar something like this and
• 09:57 : you're saying now somebody defined the
• 09:59 : fubar function not me
• 10:01 : but I know someone did so I want to call
• 10:05 : this function just let me call the
• 10:07 : function
• 10:18 : okay it already assumes that the extern
• 10:23 : token is the current token again in the
• 10:28 : same style and it's gonna oh I see this
• 10:32 : because this is gonna be a function food
• 10:35 : of that pick so it will look actually
• 10:37 : more like external ubar and so yeah okay
• 10:44 : the parse the top level expression and I
• 10:49 : think that's the last parse function we
• 10:52 : have to write
• 11:02 : okay dude that in two separate lines as
• 11:09 : I you don't have to do you get to the
• 11:14 : tutorial code as you want okay so this
• 11:20 : is saying what is a top-level expression
• 11:24 : I misspelled unique pointer pointer is
• 11:27 : spelled PTR obviously parse no space
• 11:34 : tableau expression so what is a
• 11:36 : top-level expression well we're just
• 11:38 : gonna pave an expression oh I see we're
• 11:46 : gonna wrap it inside a top level
• 11:48 : function like there's an implicit
• 11:50 : Poplarville function that's unnamed Wow
• 11:55 : and an empty vector for its arguments
• 12:01 : alright I didn't see that coming but
• 12:04 : that's cool
• 12:20 : do you need to move it no
• 12:26 : all right like that yeah and then we're
• 12:32 : gonna make another unique
• 12:45 : and standard move II and then I guess if
• 12:54 : that didn't work we're returning a null
• 12:58 : pointer all right now we're finally
• 13:00 : ready to test this code that's what the
• 13:03 : driver is about so let me write it out
• 13:09 : so this is like while true basically
• 13:12 : well true oh this is a prompt this is a
• 13:17 : girl repple repple so you can type in
• 13:23 : the code as you go I guess if you get a
• 13:29 : enough file it just returns out of this
• 13:34 : main loop function Oh semicolons will Oh
• 13:42 : get ignored what semicolons get ignored
• 13:50 : if it's a death then we're gonna handle
• 13:53 : definition if it said what does handle
• 13:58 : mean did you mean parse handled
• 14:04 : top-level expression
• 14:19 : where are these handle bla bla bla
• 14:22 : functions - do you even have these
• 14:25 : functions oh I see I see okay these
• 14:41 : handle definition why does he have these
• 14:44 : functions now as far as the definition
• 14:51 : and it'll just say I parse the
• 14:54 : definition but if it didn't work
• 14:57 : skip the token forever recovery okay
• 15:00 : fine
• 15:01 : hello I will copy this code and then
• 15:07 : okay now copy this code that does this
• 15:11 : this is like a Red Bull and then I will
• 15:16 : I'm not gonna do this president's
• 15:19 : diction or anything because I decided to
• 15:22 : do it using switch statements I think
• 15:36 : run the interpreter loops okay
• 15:39 : let's cross our fingers and hope this
• 15:41 : works
• 15:43 : I guess for now we don't actually get to
• 15:47 : see the ast structure that we generated
• 15:52 : oh it's gonna say is tell us that it
• 15:56 : successfully parse something that's this
• 15:59 : compile this code which I had a line to
• 16:05 : compile it previously I will reuse it so
• 16:08 : GCC my legacy see - oh my land I've been
• 16:14 : did not work in the program that's fixed
• 16:23 : six errors
• 16:27 : okay another colon here o space is
• 16:43 : required between consecutive right angle
• 16:47 : brackets Oh seriously
• 16:51 : so line 114 like I need space between
• 17:00 : those things oh that is true so now it's
• 17:13 : complaining about that in here okay all
• 17:18 : right let's go to the next one 150 220
• 17:27 : errors concern warnings
• 17:30 : okay when I use less to see the top of
• 17:34 : this error list might need to do like
• 17:50 : type number two
• 17:55 : [Music]
• 18:10 : make unique November called make unique
• 18:16 : and namespace standard probably I need
• 18:20 : to oh I need memory that's what it is
• 18:22 : that's what I heard and to include
• 18:25 : memory and order they used the unique
• 18:30 : pointer thing the smart pointers is a
• 18:34 : memory memory library still did not work
• 18:42 : not sure why there's still a space
• 18:48 : needed and also used value let's go to
• 19:10 : the top of these errors return null
• 19:13 : pointer 154 mmm
• 19:21 : I don't you want me to return a no-no
• 19:25 : variable constructor returning object of
• 19:29 : type a unique pointer of X / KS t so
• 19:42 : this log error function let me see
• 19:53 : he returns a null planner and and it's
• 19:57 : complaining to me about that
• 20:00 : it says no viable constructor returning
• 20:03 : object of type standard unique pointer
• 20:11 : of expert is T candidate constructor not
• 20:21 : viable expects an L value for a first
• 20:26 : argument so I think it's saying that
• 20:32 : it's not allowed to not allowed to
• 20:37 : return a null pointer isn't it saying
• 20:40 : that I'm just gonna make this be a void
• 20:45 : method and I'm not even gonna bother
• 20:48 : having this look error P I think I think
• 20:53 : we just use this one law error function
• 20:57 : and if we need to return something out
• 21:02 : use a separate statement to return the
• 21:05 : null pointer
• 21:18 : not sure whether my weather like why I
• 21:27 : am deviating from the tutorial I didn't
• 21:37 : have a block to this I should have done
• 21:41 : that
• 21:42 : oh I'm not even gonna have the air fog
• 21:47 : energy function anymore
• 21:49 : there's always gonna use the error
• 21:53 : function I think that's all there okay
• 22:02 : try again
• 22:05 : six warnings on 20 air we still have 20
• 22:09 : years jeez auto-type specifier is a C++
• 22:14 : 11 extension yes
• 22:18 : give that to me please I want to see
• 22:21 : fuzzy learn an extension please so I
• 22:24 : think that means I just have to in my
• 22:27 : command I stick this in there right no
• 22:34 : all right I've done this before you know
• 22:40 : this is pretty easy to do but I just
• 22:43 : need to google it
• 22:48 : how do you do this oh you do that
• 22:51 : standard equals C++ 11 this is for a
• 22:55 : claim I think I am actually using clang
• 23:04 : I think I'm already using clang but I
• 23:09 : don't know you might not be for warnings
• 23:13 : and 19 errors this time all right okay
• 23:18 : now we have no member named Mickey
• 23:24 : unique 157 if it's not cosmic unique
• 23:34 : then what is it called
• 23:43 : it is in the memory module and I didn't
• 23:49 : include the memory module his standard
• 24:01 : make unique standard make unique I
• 24:15 : rolled it like that every single time
• 24:23 : number expert ast use of undeclared
• 24:37 : identifier parse I might need to forward
• 24:46 : declare that let me try that now well I
• 24:55 : can copy the code from the tutorial I
• 24:59 : kind of like figuring it for myself
• 25:03 : figuring out for myself because I feel
• 25:09 : like then I can learn a lot more about
• 25:12 : the language that way
• 25:14 : however we could use the source code as
• 25:19 : a kind of a quick test
• 25:24 : so I'm gonna crap this first Oh
• 25:38 : this uses the LLVM libraries does it
• 25:43 : actually use the hell LP i erase he uses
• 25:46 : some STL extra really we may have a
• 25:59 : compiler
• 26:25 : only two errors generated now use of
• 26:30 : undeclared identifier parse been up
• 26:34 : right hand side wow that got that many
• 26:39 : things 248 so you're saying this is I
• 26:47 : need to forward declare this so in C and
• 26:51 : C++ if you wanna use a function that is
• 26:55 : has not been defined yet you have to
• 26:58 : forward declare the function like
• 27:01 : declare the function signature of the
• 27:06 : function before whereas in JavaScript
• 27:10 : there's not needed because of hoisting
• 27:12 : but not so in C and C++ in one error
• 27:18 : generated now not saying oh sorry that's
• 27:25 : interesting this I'm using clang but I
• 27:30 : think G plus plus is the same thing yeah
• 27:33 : yeah okay so this error is something
• 27:38 : about it's a call to implicitly deleted
• 27:43 : copy constructor of this unique pointer
• 27:48 : in algorithm
• 27:54 : in this push back here 194 I got this
• 28:05 : arc which is a unique pointer to an
• 28:10 : expression est we're using pushback to
• 28:15 : push it to this arch list and implicitly
• 28:19 : delete copy constructor is implicitly
• 28:22 : deleted because unique pointer of an
• 28:28 : expression est has a unique what
• 28:47 : I don't really understand what is saying
• 28:51 : to me mmm
• 28:56 : I guess I could look at their code
• 29:18 : okay and see if it works there
• 29:30 : I'm just
• 29:41 : [Music]
• 29:45 : disable API brick trucks undefined
• 29:55 : symbol linker command failed with exit
• 30:03 : code one being disabled he bi break
• 30:11 : checks
• 30:30 : where is my LOL game config that's there
• 30:36 : there's where I put it
• 30:38 : by the way before I started this I
• 30:45 : from this page and I went and got the
• 30:51 : newest one which was version 10 came in
• 30:57 : March of this year and got the lowest
• 31:00 : ten I extracted it put it in LLVM inside
• 31:06 : my home directory and inside of it
• 31:09 : there's this bin directory I've never
• 31:14 : seen this before so I guess the best I
• 31:18 : can do is Google for
• 31:28 : I do find something related to this
• 31:30 : kaleidoscope tutorial you need to