SlideShare a Scribd company logo
Hacking Go Compiler 
Internals 
Moriyoshi Koizumi <mozo@mozo.jp>
Intended Audience 
• An eccentric Go programmer who happens to want 
to add feture XX to the language, knowing her 
patch will never be merged. 
• A keen-minded programmer who wants to know 
how the compiler works.
Overall Architecture 
Lexer 
Parser 
Escape Analysis 
Typegen GCproggen 
Codegen
Phase 1. Lexer
Lexer 
• A lexer scans over the source code and cut it into a 
bunch of meaningful chunks (the first abstraction). 
• Example: 
a := b + c() 
LNAME LASOP LNAME + LNAME ( )
Lexer 
src/go/cmd/gc/lexer.c 
static int32 
_yylex(void) 
{ 
... 
l0: 
c = getc(); 
if(yy_isspace(c)) { 
if(c == 'n' && curio.nlsemi) { 
ungetc(c); 
DBG("lex: implicit semin"); 
return ';'; 
} 
goto l0; 
} 
...
Lexer 
... 
switch(c) { 
... 
case '+': 
c1 = getc(); 
if(c1 == '+') { 
c = LINC; 
goto lx; 
} 
if(c1 == '=') { 
c = OADD; 
goto asop; 
} 
break; 
.... 
}
When do you want to hack the 
lexer 
• Modify the keyword such as func and make. 
• Modify the operator only cosmetically (e.g. != → 
~=) 
• Modify how literals and identifiers are represented. 
• Add a new keyword or operator to the language to 
later use in the parser.
Example: Emojis for identifiers 
• http://moriyoshi.hatenablog.com/entry/2014/06/0 
3/121728 
• Go doesn’t treat emojis as part of identifiers. 
./sushi.go:8: invalid identifier character U+1f363 
• But I wanted to have 寿司(in the source)
Example: Emojis for identifiers 
• Patched the following place to let it accept emojis: 
if(c >= Runeself) { 
ungetc(c); 
rune = getr(); 
// 0xb7 · is used for internal names 
if(!isalpharune(rune) && !isdigitrune(rune) && 
(importpkg == nil || rune != 0xb7)) 
yyerror("invalid identifier character U+%04x", 
rune); 
cp += runetochar(cp, &rune); 
} else if(!yy_isalnum(c) && c != '_') 
break;
Phase 2. Parser
Parser 
• Parser repeatedly calls the lexer to fetch the tokens 
and builds an abstract syntax tree (AST) that 
represents the source code. 
• The AST is retouched (“typecheck”and “walk” sub-phase) 
during type inference and assersion phase 
so it would be less verbose and contain information 
helpful for the later stages. 
• src/cmd/gc/go.y, src/cmd/gc/dcl.c 
src/cmd/gc/typecheck.c, 
src/cmd/gc/walk.c, 
src/cmd/gc/reflect.c
Parser 
LNAME LASOP LNAME + LNAME ( ) 
OAS 
ONAME OADD 
ONAME 
OCALL 
ONAME ∅ 
Tokens 
AST
Parser 
• src/cmd/gc/go.y 
… 
/* 
* expressions 
*/ 
expr: 
uexpr 
| expr LOROR expr 
{ 
$$ = nod(OOROR, $1, $3); 
} 
| expr LANDAND expr 
{ 
$$ = nod(OANDAND, $1, $3); 
} 
…
Example: Bracket operator 
overload! 
• Let the following code (A) expand to (B) 
• https://gist.github.com/moriyoshi/c0e2b2f9be688 
3e33251 
(A) 
(B) 
a := &struct{}{} 
fmt.Println(a[1]) 
a[1] = "test2" 
fmt.Println(a.__getindex(1)) 
a.__setindex(1, "test2")
Example: Bracket operator 
overload! 
• Things to do: 
• Introduce a new AST node type (e.g. OINDEXINTER) 
• Add a branch point in “typecheck” to handle the case 
where the indexed target is neither a string, array, slice 
nor map type. 
• Supply a code in “walk” to specially treat the assignment 
and dereference that involves that kind of node. The 
code synthesizes the node to invoke the special 
functions, then typecheck and walk over themselves in a 
recursive manner. 
• Don’t forget to take care of evaluation order corrector.
Helpful functions to debug your 
hack 
• print(const char *, …) 
• This is actually printf() of standard libc. 
• Accepts the following extra format specifiers: 
• %N (node) 
• %T (type) 
• %E, %J, %H, %L, %O, %S, %V, %Z, %B, %F
Roll-up 
• Go’s compiler internals should look complex at first 
glance, but it would turn out pretty straightforward 
and hacker-friendly ;)
Hacking Go Compiler Internals / GoCon 2014 Autumn

More Related Content

What's hot (20)

C++ via C#
C++ via C#C++ via C#
C++ via C#
Egor Bogatov
 
Go之道
Go之道Go之道
Go之道
刘 晓冬
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
Bryan Helmig
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
CyberPlusIndia
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
Kamil Toman
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers Toolbox
Stefan
 
Modern C++
Modern C++Modern C++
Modern C++
Michael Clark
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
David Beazley (Dabeaz LLC)
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
Solution4Future
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
ESUG
 
Python
PythonPython
Python
Wei-Bo Chen
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
Bartlomiej Filipek
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
The best language in the world
The best language in the worldThe best language in the world
The best language in the world
David Muñoz Díaz
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
Eleanor McHugh
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
Bryan Helmig
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
Kamil Toman
 
A CTF Hackers Toolbox
A CTF Hackers ToolboxA CTF Hackers Toolbox
A CTF Hackers Toolbox
Stefan
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
Solution4Future
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
ESUG
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
The best language in the world
The best language in the worldThe best language in the world
The best language in the world
David Muñoz Díaz
 

Viewers also liked (10)

Develop android application with mono for android
Develop android application with mono for androidDevelop android application with mono for android
Develop android application with mono for android
Nicko Satria Consulting
 
Golang web database3
Golang web database3Golang web database3
Golang web database3
NISCI
 
Pyramidのrendererをカスタマイズする
PyramidのrendererをカスタマイズするPyramidのrendererをカスタマイズする
Pyramidのrendererをカスタマイズする
Moriyoshi Koizumi
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
Victor S. Recio
 
CoreOS Overview
CoreOS OverviewCoreOS Overview
CoreOS Overview
Victor S. Recio
 
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and PyramidAuthentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Moriyoshi Koizumi
 
NSQ-Centric Architecture (GoCon Autumn 2014)
NSQ-Centric Architecture (GoCon Autumn 2014)NSQ-Centric Architecture (GoCon Autumn 2014)
NSQ-Centric Architecture (GoCon Autumn 2014)
guregu
 
PHP7を魔改造した話
PHP7を魔改造した話PHP7を魔改造した話
PHP7を魔改造した話
Moriyoshi Koizumi
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話します
Moriyoshi Koizumi
 
Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
Moriyoshi Koizumi
 
Develop android application with mono for android
Develop android application with mono for androidDevelop android application with mono for android
Develop android application with mono for android
Nicko Satria Consulting
 
Golang web database3
Golang web database3Golang web database3
Golang web database3
NISCI
 
Pyramidのrendererをカスタマイズする
PyramidのrendererをカスタマイズするPyramidのrendererをカスタマイズする
Pyramidのrendererをカスタマイズする
Moriyoshi Koizumi
 
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and PyramidAuthentication, Authorization, OAuth, OpenID Connect and Pyramid
Authentication, Authorization, OAuth, OpenID Connect and Pyramid
Moriyoshi Koizumi
 
NSQ-Centric Architecture (GoCon Autumn 2014)
NSQ-Centric Architecture (GoCon Autumn 2014)NSQ-Centric Architecture (GoCon Autumn 2014)
NSQ-Centric Architecture (GoCon Autumn 2014)
guregu
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話します
Moriyoshi Koizumi
 
Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
Moriyoshi Koizumi
 

Similar to Hacking Go Compiler Internals / GoCon 2014 Autumn (20)

Effective Go
Effective GoEffective Go
Effective Go
Jongseok Choi
 
Gunosy.go #4 go
Gunosy.go #4 goGunosy.go #4 go
Gunosy.go #4 go
Taku Fukushima
 
How to create a programming language
How to create a programming languageHow to create a programming language
How to create a programming language
Robert Mamore
 
Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015
Chris McEniry
 
I don't know what I'm Doing: A newbie guide for Golang for DevOps
I don't know what I'm Doing: A newbie guide for Golang for DevOpsI don't know what I'm Doing: A newbie guide for Golang for DevOps
I don't know what I'm Doing: A newbie guide for Golang for DevOps
Peter Souter
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
Sami Said
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
Sami Said
 
Writing a C Compiler Build a Real Programming Language from Scratch Nora Sandler
Writing a C Compiler Build a Real Programming Language from Scratch Nora SandlerWriting a C Compiler Build a Real Programming Language from Scratch Nora Sandler
Writing a C Compiler Build a Real Programming Language from Scratch Nora Sandler
hathudpunta
 
Static Analysis in Go
Static Analysis in GoStatic Analysis in Go
Static Analysis in Go
Takuya Ueda
 
Template Haskell
Template HaskellTemplate Haskell
Template Haskell
Sergey Stretovich
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
Robert Stern
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
Andres Almiray
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
Andres Almiray
 
Golang勉強会
Golang勉強会Golang勉強会
Golang勉強会
Shin Sekaryo
 
Golang
GolangGolang
Golang
Felipe Mamud
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
Phillip Trelford
 
Flex
FlexFlex
Flex
9029170256
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
Andres Almiray
 
How to create a programming language
How to create a programming languageHow to create a programming language
How to create a programming language
Robert Mamore
 
Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015
Chris McEniry
 
I don't know what I'm Doing: A newbie guide for Golang for DevOps
I don't know what I'm Doing: A newbie guide for Golang for DevOpsI don't know what I'm Doing: A newbie guide for Golang for DevOps
I don't know what I'm Doing: A newbie guide for Golang for DevOps
Peter Souter
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
Sami Said
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
Sami Said
 
Writing a C Compiler Build a Real Programming Language from Scratch Nora Sandler
Writing a C Compiler Build a Real Programming Language from Scratch Nora SandlerWriting a C Compiler Build a Real Programming Language from Scratch Nora Sandler
Writing a C Compiler Build a Real Programming Language from Scratch Nora Sandler
hathudpunta
 
Static Analysis in Go
Static Analysis in GoStatic Analysis in Go
Static Analysis in Go
Takuya Ueda
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
Robert Stern
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
Sean Cribbs
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
Phillip Trelford
 

More from Moriyoshi Koizumi (20)

Uguisudani
UguisudaniUguisudani
Uguisudani
Moriyoshi Koizumi
 
よいことも悪いこともぜんぶPHPが教えてくれた
よいことも悪いこともぜんぶPHPが教えてくれたよいことも悪いこともぜんぶPHPが教えてくれた
よいことも悪いこともぜんぶPHPが教えてくれた
Moriyoshi Koizumi
 
Ik in action
Ik in actionIk in action
Ik in action
Moriyoshi Koizumi
 
mod_himoteからはじめよう
mod_himoteからはじめようmod_himoteからはじめよう
mod_himoteからはじめよう
Moriyoshi Koizumi
 
HPHPは約束の地なのか
HPHPは約束の地なのかHPHPは約束の地なのか
HPHPは約束の地なのか
Moriyoshi Koizumi
 
Phjosh(仮)プロジェクト
Phjosh(仮)プロジェクトPhjosh(仮)プロジェクト
Phjosh(仮)プロジェクト
Moriyoshi Koizumi
 
LLの虎 semifinal: 殺伐Python
LLの虎 semifinal: 殺伐PythonLLの虎 semifinal: 殺伐Python
LLの虎 semifinal: 殺伐Python
Moriyoshi Koizumi
 
Introducing E-Cell 3.2
Introducing E-Cell 3.2Introducing E-Cell 3.2
Introducing E-Cell 3.2
Moriyoshi Koizumi
 
GoでKVSを書けるのか
GoでKVSを書けるのかGoでKVSを書けるのか
GoでKVSを書けるのか
Moriyoshi Koizumi
 
10〜30分で何となく分かるGo
10〜30分で何となく分かるGo10〜30分で何となく分かるGo
10〜30分で何となく分かるGo
Moriyoshi Koizumi
 
PHPのすべらない話
PHPのすべらない話PHPのすべらない話
PHPのすべらない話
Moriyoshi Koizumi
 

Recently uploaded (20)

MagicOS 9 PowerPoint Inspired By AR 4789
MagicOS 9 PowerPoint Inspired By AR 4789MagicOS 9 PowerPoint Inspired By AR 4789
MagicOS 9 PowerPoint Inspired By AR 4789
elliotbuckbyfuncicha
 
Final-TheGuide_Analysed-46430_Final-RR.docx
Final-TheGuide_Analysed-46430_Final-RR.docxFinal-TheGuide_Analysed-46430_Final-RR.docx
Final-TheGuide_Analysed-46430_Final-RR.docx
EarthSoft Foundation of Guidance - EFG
 
Reasons why basketball is my favorite hobby?
Reasons why basketball is my favorite hobby?Reasons why basketball is my favorite hobby?
Reasons why basketball is my favorite hobby?
memi27
 
Season 11 _ EP 03 - The General Quiz.pptx
Season 11 _ EP 03 - The General Quiz.pptxSeason 11 _ EP 03 - The General Quiz.pptx
Season 11 _ EP 03 - The General Quiz.pptx
Quiz Club, Indian Institute of Technology, Patna
 
Presentation1 bio 2.pdf This to know about my life i am rengooz.
Presentation1 bio 2.pdf This to know about my life i am rengooz.Presentation1 bio 2.pdf This to know about my life i am rengooz.
Presentation1 bio 2.pdf This to know about my life i am rengooz.
RenGooZ SBUH
 
Reasons why dancing is my favorite hobby
Reasons why dancing is my favorite hobbyReasons why dancing is my favorite hobby
Reasons why dancing is my favorite hobby
memi27
 
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾  𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾  𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
Ahsan Khan
 
Season 11_EP04 - General Grievous Quiz.pptx
Season 11_EP04 -  General Grievous Quiz.pptxSeason 11_EP04 -  General Grievous Quiz.pptx
Season 11_EP04 - General Grievous Quiz.pptx
Quiz Club, Indian Institute of Technology, Patna
 
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
Enzo Zelocchi Fan Page
 
The Other Side of the Myth: Ereshkigal and the Villainess’s Journey
The Other Side of the Myth: Ereshkigal and the Villainess’s JourneyThe Other Side of the Myth: Ereshkigal and the Villainess’s Journey
The Other Side of the Myth: Ereshkigal and the Villainess’s Journey
Valerie Frankel
 
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
BITS Goa Quiz Club
 
Baby oil m123131aking tutorial (natural ingresients.pptx
Baby oil m123131aking tutorial (natural ingresients.pptxBaby oil m123131aking tutorial (natural ingresients.pptx
Baby oil m123131aking tutorial (natural ingresients.pptx
RanitMal
 
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
Taqyea
 
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
BITS Goa Quiz Club
 
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
BITS Goa Quiz Club
 
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPTWHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
Optimistic18
 
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
BITS Goa Quiz Club
 
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
Taqyea
 
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
BITS Goa Quiz Club
 
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
BITS Goa Quiz Club
 
MagicOS 9 PowerPoint Inspired By AR 4789
MagicOS 9 PowerPoint Inspired By AR 4789MagicOS 9 PowerPoint Inspired By AR 4789
MagicOS 9 PowerPoint Inspired By AR 4789
elliotbuckbyfuncicha
 
Reasons why basketball is my favorite hobby?
Reasons why basketball is my favorite hobby?Reasons why basketball is my favorite hobby?
Reasons why basketball is my favorite hobby?
memi27
 
Presentation1 bio 2.pdf This to know about my life i am rengooz.
Presentation1 bio 2.pdf This to know about my life i am rengooz.Presentation1 bio 2.pdf This to know about my life i am rengooz.
Presentation1 bio 2.pdf This to know about my life i am rengooz.
RenGooZ SBUH
 
Reasons why dancing is my favorite hobby
Reasons why dancing is my favorite hobbyReasons why dancing is my favorite hobby
Reasons why dancing is my favorite hobby
memi27
 
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾  𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾  𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Thug Life" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
Ahsan Khan
 
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
How Enzo Zelocchi Leveraged 28 Million Followers to Build a Global Brand That...
Enzo Zelocchi Fan Page
 
The Other Side of the Myth: Ereshkigal and the Villainess’s Journey
The Other Side of the Myth: Ereshkigal and the Villainess’s JourneyThe Other Side of the Myth: Ereshkigal and the Villainess’s Journey
The Other Side of the Myth: Ereshkigal and the Villainess’s Journey
Valerie Frankel
 
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
The Conspiracies Quiz || QM: Vagarth Dvivedi || Waves 2024 QuizFest || Midnig...
BITS Goa Quiz Club
 
Baby oil m123131aking tutorial (natural ingresients.pptx
Baby oil m123131aking tutorial (natural ingresients.pptxBaby oil m123131aking tutorial (natural ingresients.pptx
Baby oil m123131aking tutorial (natural ingresients.pptx
RanitMal
 
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
最新版西班牙弗朗西斯科·德·维多利亚大学毕业证(UFV毕业证书)原版定制
Taqyea
 
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
Lit Quiz Prelims || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia |...
BITS Goa Quiz Club
 
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
Ganimatoonics Finals || QM: Agastya and Nikhil || Quark'25 || BITS Pilani, KK...
BITS Goa Quiz Club
 
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPTWHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
WHO KILLED ALASKA? #28: Bobby Time - "THE HUNT FOR THE HAND" TRANSCRIPT
Optimistic18
 
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
Internet Quiz - Finals || Aditya Shiva Sharma, Druva and Haaziq || Midnight Q...
BITS Goa Quiz Club
 
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
最新版西班牙巴拿马大学毕业证(Panamá毕业证书)原版定制
Taqyea
 
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
Ganimatoonics Prelims || QM: Agastya Sanyal and Nikhil Vinay || Quark'25 || B...
BITS Goa Quiz Club
 
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
Lit Quiz Finals || QM: Shivansh Verma, Dhruba Chatterjee & Suhani Timbadia ||...
BITS Goa Quiz Club
 

Hacking Go Compiler Internals / GoCon 2014 Autumn

  • 1. Hacking Go Compiler Internals Moriyoshi Koizumi <[email protected]>
  • 2. Intended Audience • An eccentric Go programmer who happens to want to add feture XX to the language, knowing her patch will never be merged. • A keen-minded programmer who wants to know how the compiler works.
  • 3. Overall Architecture Lexer Parser Escape Analysis Typegen GCproggen Codegen
  • 5. Lexer • A lexer scans over the source code and cut it into a bunch of meaningful chunks (the first abstraction). • Example: a := b + c() LNAME LASOP LNAME + LNAME ( )
  • 6. Lexer src/go/cmd/gc/lexer.c static int32 _yylex(void) { ... l0: c = getc(); if(yy_isspace(c)) { if(c == 'n' && curio.nlsemi) { ungetc(c); DBG("lex: implicit semin"); return ';'; } goto l0; } ...
  • 7. Lexer ... switch(c) { ... case '+': c1 = getc(); if(c1 == '+') { c = LINC; goto lx; } if(c1 == '=') { c = OADD; goto asop; } break; .... }
  • 8. When do you want to hack the lexer • Modify the keyword such as func and make. • Modify the operator only cosmetically (e.g. != → ~=) • Modify how literals and identifiers are represented. • Add a new keyword or operator to the language to later use in the parser.
  • 9. Example: Emojis for identifiers • http://moriyoshi.hatenablog.com/entry/2014/06/0 3/121728 • Go doesn’t treat emojis as part of identifiers. ./sushi.go:8: invalid identifier character U+1f363 • But I wanted to have 寿司(in the source)
  • 10. Example: Emojis for identifiers • Patched the following place to let it accept emojis: if(c >= Runeself) { ungetc(c); rune = getr(); // 0xb7 · is used for internal names if(!isalpharune(rune) && !isdigitrune(rune) && (importpkg == nil || rune != 0xb7)) yyerror("invalid identifier character U+%04x", rune); cp += runetochar(cp, &rune); } else if(!yy_isalnum(c) && c != '_') break;
  • 12. Parser • Parser repeatedly calls the lexer to fetch the tokens and builds an abstract syntax tree (AST) that represents the source code. • The AST is retouched (“typecheck”and “walk” sub-phase) during type inference and assersion phase so it would be less verbose and contain information helpful for the later stages. • src/cmd/gc/go.y, src/cmd/gc/dcl.c src/cmd/gc/typecheck.c, src/cmd/gc/walk.c, src/cmd/gc/reflect.c
  • 13. Parser LNAME LASOP LNAME + LNAME ( ) OAS ONAME OADD ONAME OCALL ONAME ∅ Tokens AST
  • 14. Parser • src/cmd/gc/go.y … /* * expressions */ expr: uexpr | expr LOROR expr { $$ = nod(OOROR, $1, $3); } | expr LANDAND expr { $$ = nod(OANDAND, $1, $3); } …
  • 15. Example: Bracket operator overload! • Let the following code (A) expand to (B) • https://gist.github.com/moriyoshi/c0e2b2f9be688 3e33251 (A) (B) a := &struct{}{} fmt.Println(a[1]) a[1] = "test2" fmt.Println(a.__getindex(1)) a.__setindex(1, "test2")
  • 16. Example: Bracket operator overload! • Things to do: • Introduce a new AST node type (e.g. OINDEXINTER) • Add a branch point in “typecheck” to handle the case where the indexed target is neither a string, array, slice nor map type. • Supply a code in “walk” to specially treat the assignment and dereference that involves that kind of node. The code synthesizes the node to invoke the special functions, then typecheck and walk over themselves in a recursive manner. • Don’t forget to take care of evaluation order corrector.
  • 17. Helpful functions to debug your hack • print(const char *, …) • This is actually printf() of standard libc. • Accepts the following extra format specifiers: • %N (node) • %T (type) • %E, %J, %H, %L, %O, %S, %V, %Z, %B, %F
  • 18. Roll-up • Go’s compiler internals should look complex at first glance, but it would turn out pretty straightforward and hacker-friendly ;)