SlideShare a Scribd company logo
1 of 23
Computer Languages
From Programmer to the CPU
• We've written small Javascript programs
• We've seen large program like Firefox
• Computer language used by a person (e.g. Javascript)
• vs. the simple machine code instructions in the CPU
• What's the connection?
• (Here the basic themes, not the details)
Computer Languages
• It is extremely rare to write machine code by hand. Instead, a
programmer writes code in a more "high level" computer
language with features that are more useful and powerful than
the simple operations found in machine code.
• For CS101, we write code in Javascript which supports high
level features such as strings, loops, and the print() function.
• None of those high level features are directly present in the
low level machine code; they are added by the Javascript
language. There are two major ways that a computer language
can work.
Source Code and Compiler
• One common computer language strategy is based on a
"compiler".
• The computer languages C and its derivative C++ are old and
popular computer languages that use this strategy, although
they tend to have fewer features than dynamic languages
(below).
• In C++, the programmer writes C++ code which includes high
level facilities such as strings and loops (much as we have
seen in Javascript).
• Here is some C++ code to append a "!" at the end of a string.
Conti..,
• Computer languages -- "high level" features
--e.g. loops, if-statements, strings
• e.g. C, C++, Javascript, Java
• Programmer writes "source code" of a program in a language,
say, C++
• Example C++ code -- how to get to the CPU?
• // C++ code a = "hi"; b = a + "!";
• This code appends the string "!" on to the end of "hi", resulting
in the string "hi!" stored into the variable b.
• The machine code instructions in the CPU are too primitive to
implement this append operation as one or two instructions.
Compiler
• "Compiler" looks at the source code
• Compiler translates the source code into a large number of
machine code instructions
• Suppose a high level construct, like an if-statement, can be
implemented by a sequence of 5 machine code instructions
• e.g. Firefox -- written in C++
--Compiler takes in Firefox C++ source code, produces
Firefox.exe
• The compilation step can be done once and long before the
program is run (e.g. produce Firefox.exe at Mozilla
headquarters)
Conti..,
• The end user does not need to the source code or the compiler.
Distribute the program.exe file in working form
• Does not work backwards -- having the .exe, you cannot
recover the source code (well)
Conti..,
• The Compiler for the C++ language, reads that C++ code and
translates and expands it to a larger sequence of the machine
code instructions to implement the sequence of actions
specified by the C++ code.
• The output of the compiler is, essentially, a program file
Source Code
• Having the .exe allows one to run the program
• To add feature or fix a bug, ideally you want the source code
--Add a feature in the source code, then run the compiler again
to make a new version of the .exe
• Open Source software
• The source code is available to all, along with the right to
make modifications
• 1. Typically the software does not cost anything
• 2. Freedom the end user is not dependent on the original
vendor to fix bugs, or perhaps the vendor goes out of business
• The user can make the change themselves (since they have
access to the source)
Conti..,
• Insurance/freedom policy
• Often the license terms will require the change to made
available to the wider community in some cases ... sharing
back to the community
• Open source is a very successful model for some cases
• Talk about this more later, mentioning now since it is so close
to the idea of what "source code" is
• The "source code" is the high level code authored by the
programmer and fed into the compiler.
• Generally just the program.exe file is distributed to users. The
programmer retains the source code. Changing the program in
the future generally requires access to the source code.
Open Source
• "Open Source" refers to software where the program includes
access to its source code, and a license where the user can
make their own modifications.
• Typically open source software is distributed for free.
Critically, beyond the free price, open source software also
includes freedom/independence since the user is not
dependent on the original vendor to make changes or fixes or
whatever to the source code.
Dynamic (interpreter) Languages
• Dynamic languages (big tent here)
• e.g. Java, Javascript, Python
• Can be implemented by an "interpreter"
• There is a broad category of more modern languages such as
Java (the world's most popular language, used in Stanford
CS106A), Javascript, and Python, which do not use the
compiler/machine-code strategy.
• Instead, these languages can be implemented by an
"interpreter", and I will lump them into the category of
"dynamic" languages.
Interpreter
• Interpreter is a program which "runs" other code
• e.g. web browsers includes a Javascript interpreter
--Browser "runs" bits of Javascript code in a web page, such as
ours
• Interpreter looks at one line at a time
• Deconstructs what each line says to do
• The interpreter then does that action, in the moment
• Then proceeds to the next line
• // Javascript code a = 1; b = a + 2;
Conti..,
• e.g. Interpreter looks at a = 1;, does it
• e.g. Interpreter looks at b = a + 2;, does it
• The compiler translates source code to equivalent machine
code
• The interpreter does the code, looking at each line and doing it
• An interpreter is a program which reads in source code as its
input, and "runs" the input code. The interpreter proceeds
through the code given to it, line by line
Conti..,
• A compiler translates all the source code into equivalent
machine code program.exe to be run later -- it is a bulk
translation.
• An interpreter looks at each line of code, and translates and
runs it in the moment, and then proceeds to the next line of
source code.
• The interpreter does not produce a program.exe, instead it
performs the actions specified in the source code directly.
Compiler vs. Interpreter Evaluation
• Disclaimer: there are many languages, no one "best" language,
it depends!
• Compiled code tends to run faster
• The .exe tends to be "lean" .. compiler has removed overhead,
some decisions
• Dynamic/interpreter languages
--Tend to have a greater number of programmer-friendly
features
--i.e. programmers are often more productive in dynamic
languages
--The resulting program tends to run somewhat slower than
compiled code
Conti..,
• Compiled code generally runs faster than interpreted code.
This is because many questions -- how to append to this string,
how many bytes do I need here -- are resolved by the compiler
at compile time, long before the program runs.
• The compiler has, in effect, pre-processed the source code,
stripping out many questions and complications, leaving the
program.exe as lean and direct as it can be to just run.
Dynamic Languages - More Features
/ Slower
• Dynamic languages tend to have more features (programmer-
friendly)
• e.g. Memory Management
--C and C++: partially manual, some programmer input
required
--Dynamic languages: automatic memory management, no
programmer input needed
--Automatic memory management not free: spending CPU
cycles to lighten programmer workload
• Tradeoff
--Dynamic languages often allow the programmer to get things
done faster
Conti..,
--However the dynamic code runs a bit more slowly compared to
compiled code
• Current trend is towards dynamic languages
--Programmers are scarce
--It's attractive to save some programmer time at the expense
of some CPU/memory use
--Moore's law reinforces: CPU cheap, programmer relatively
rare/expensive
Conti..,
• Supporting features tends to be easier in dynamic languages
compared to compiled languages, which is why dynamic
languages tend to have a greater number of programmer-
friendly features.
• "Memory management" is the problem in a program of
knowing, over time, when bytes of RAM are needed and when
they can be reclaimed and use for something else.
• Every program must solve this problem.
• Memory management is an excellent example of a feature
different between compiled and dynamic languages -- most
modern dynamic languages manage memory automatically.
Conti..,
• The programmer can focus on the problem to be solved, and
the dynamic language will take care of managing the memory.
JIT Just In Time Compiler
• JIT -- compile code of a dynamic language on the fly
• All major browsers now have a JIT for the Javascript code
they run (Chrome)
• Best of both worlds
• Flexibility of dynamic languages
• Combined with most of the performance of the compiled
world
• Active area of research, works pretty well
Conti..,
• The most modern form of dynamic language is implemented
with an interpreter paired with a Just In Time compiler (JIT)
trying to get the best of both worlds.
• The JIT looks a sections of dynamic code that are being run
very frequently, and for those, does a compile to native code
for that section on the fly.

More Related Content

What's hot

1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
Harish Kumawat
 
Programming Languages An Intro
Programming Languages An IntroProgramming Languages An Intro
Programming Languages An Intro
Kimberly De Guzman
 

What's hot (20)

FIT-Unit3 chapter2- Computer Languages
FIT-Unit3 chapter2- Computer LanguagesFIT-Unit3 chapter2- Computer Languages
FIT-Unit3 chapter2- Computer Languages
 
High level and Low level Language
High level and Low level Language High level and Low level Language
High level and Low level Language
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
 
Types of Programming Languages
Types of Programming LanguagesTypes of Programming Languages
Types of Programming Languages
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
Computer Language
Computer LanguageComputer Language
Computer Language
 
Computer languages 11
Computer languages 11Computer languages 11
Computer languages 11
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
 
Computer Languages.
Computer Languages.Computer Languages.
Computer Languages.
 
System software
System softwareSystem software
System software
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Program & language generation
Program & language generationProgram & language generation
Program & language generation
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
 
Programming Languages An Intro
Programming Languages An IntroProgramming Languages An Intro
Programming Languages An Intro
 
Introduction Programming Languages
Introduction Programming LanguagesIntroduction Programming Languages
Introduction Programming Languages
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
 
Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languages
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
High Level Language (HLL)
High Level Language (HLL)High Level Language (HLL)
High Level Language (HLL)
 

Viewers also liked

Programming languages
Programming languagesProgramming languages
Programming languages
Akash Varaiya
 
Computer Languages....ppt
Computer Languages....pptComputer Languages....ppt
Computer Languages....ppt
hashgeneration
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
Varun Garg
 

Viewers also liked (20)

Computer Languages
Computer Languages Computer Languages
Computer Languages
 
Chapter 02 Computer Languages (re-upload)
Chapter 02 Computer Languages (re-upload)Chapter 02 Computer Languages (re-upload)
Chapter 02 Computer Languages (re-upload)
 
Copmuter Languages
Copmuter LanguagesCopmuter Languages
Copmuter Languages
 
Introduction to Computer Softwares
Introduction to Computer SoftwaresIntroduction to Computer Softwares
Introduction to Computer Softwares
 
Computer languages
Computer languagesComputer languages
Computer languages
 
Programming languages of computer
Programming languages of computerProgramming languages of computer
Programming languages of computer
 
Chapter 4 computer language
Chapter 4 computer languageChapter 4 computer language
Chapter 4 computer language
 
Lec 01 basic concepts
Lec 01 basic conceptsLec 01 basic concepts
Lec 01 basic concepts
 
Software Basics
Software BasicsSoftware Basics
Software Basics
 
Presentation on computer language
Presentation on computer languagePresentation on computer language
Presentation on computer language
 
Presentation on generation of languages
Presentation on generation of languagesPresentation on generation of languages
Presentation on generation of languages
 
Introduction to computer software
Introduction to computer softwareIntroduction to computer software
Introduction to computer software
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
 
Report-computer hardware,system, and software
Report-computer hardware,system, and softwareReport-computer hardware,system, and software
Report-computer hardware,system, and software
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Computer Languages....ppt
Computer Languages....pptComputer Languages....ppt
Computer Languages....ppt
 
computer languages
computer languagescomputer languages
computer languages
 
Computer languages
Computer languagesComputer languages
Computer languages
 
Computer Software & its Types
Computer Software & its Types Computer Software & its Types
Computer Software & its Types
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 

Similar to computer languages

Compilers and interpreters
Compilers and interpretersCompilers and interpreters
Compilers and interpreters
RAJU KATHI
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
crAmth
 

Similar to computer languages (20)

Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Compilers and interpreters
Compilers and interpretersCompilers and interpreters
Compilers and interpreters
 
PCSG_Computer_Science_Unit_1_Lecture_2.pptx
PCSG_Computer_Science_Unit_1_Lecture_2.pptxPCSG_Computer_Science_Unit_1_Lecture_2.pptx
PCSG_Computer_Science_Unit_1_Lecture_2.pptx
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdf
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introduction
 
CD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptxCD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptx
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Putting Compilers to Work
Putting Compilers to WorkPutting Compilers to Work
Putting Compilers to Work
 
compiler vs interpreter
compiler vs interpretercompiler vs interpreter
compiler vs interpreter
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 

More from Rajendran

More from Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hash table
Hash tableHash table
Hash table
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
proving non-computability
proving non-computabilityproving non-computability
proving non-computability
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

computer languages

  • 2. From Programmer to the CPU • We've written small Javascript programs • We've seen large program like Firefox • Computer language used by a person (e.g. Javascript) • vs. the simple machine code instructions in the CPU • What's the connection? • (Here the basic themes, not the details)
  • 3. Computer Languages • It is extremely rare to write machine code by hand. Instead, a programmer writes code in a more "high level" computer language with features that are more useful and powerful than the simple operations found in machine code. • For CS101, we write code in Javascript which supports high level features such as strings, loops, and the print() function. • None of those high level features are directly present in the low level machine code; they are added by the Javascript language. There are two major ways that a computer language can work.
  • 4. Source Code and Compiler • One common computer language strategy is based on a "compiler". • The computer languages C and its derivative C++ are old and popular computer languages that use this strategy, although they tend to have fewer features than dynamic languages (below). • In C++, the programmer writes C++ code which includes high level facilities such as strings and loops (much as we have seen in Javascript). • Here is some C++ code to append a "!" at the end of a string.
  • 5. Conti.., • Computer languages -- "high level" features --e.g. loops, if-statements, strings • e.g. C, C++, Javascript, Java • Programmer writes "source code" of a program in a language, say, C++ • Example C++ code -- how to get to the CPU? • // C++ code a = "hi"; b = a + "!"; • This code appends the string "!" on to the end of "hi", resulting in the string "hi!" stored into the variable b. • The machine code instructions in the CPU are too primitive to implement this append operation as one or two instructions.
  • 6. Compiler • "Compiler" looks at the source code • Compiler translates the source code into a large number of machine code instructions • Suppose a high level construct, like an if-statement, can be implemented by a sequence of 5 machine code instructions • e.g. Firefox -- written in C++ --Compiler takes in Firefox C++ source code, produces Firefox.exe • The compilation step can be done once and long before the program is run (e.g. produce Firefox.exe at Mozilla headquarters)
  • 7. Conti.., • The end user does not need to the source code or the compiler. Distribute the program.exe file in working form • Does not work backwards -- having the .exe, you cannot recover the source code (well)
  • 8. Conti.., • The Compiler for the C++ language, reads that C++ code and translates and expands it to a larger sequence of the machine code instructions to implement the sequence of actions specified by the C++ code. • The output of the compiler is, essentially, a program file
  • 9. Source Code • Having the .exe allows one to run the program • To add feature or fix a bug, ideally you want the source code --Add a feature in the source code, then run the compiler again to make a new version of the .exe • Open Source software • The source code is available to all, along with the right to make modifications • 1. Typically the software does not cost anything • 2. Freedom the end user is not dependent on the original vendor to fix bugs, or perhaps the vendor goes out of business • The user can make the change themselves (since they have access to the source)
  • 10. Conti.., • Insurance/freedom policy • Often the license terms will require the change to made available to the wider community in some cases ... sharing back to the community • Open source is a very successful model for some cases • Talk about this more later, mentioning now since it is so close to the idea of what "source code" is • The "source code" is the high level code authored by the programmer and fed into the compiler. • Generally just the program.exe file is distributed to users. The programmer retains the source code. Changing the program in the future generally requires access to the source code.
  • 11. Open Source • "Open Source" refers to software where the program includes access to its source code, and a license where the user can make their own modifications. • Typically open source software is distributed for free. Critically, beyond the free price, open source software also includes freedom/independence since the user is not dependent on the original vendor to make changes or fixes or whatever to the source code.
  • 12. Dynamic (interpreter) Languages • Dynamic languages (big tent here) • e.g. Java, Javascript, Python • Can be implemented by an "interpreter" • There is a broad category of more modern languages such as Java (the world's most popular language, used in Stanford CS106A), Javascript, and Python, which do not use the compiler/machine-code strategy. • Instead, these languages can be implemented by an "interpreter", and I will lump them into the category of "dynamic" languages.
  • 13. Interpreter • Interpreter is a program which "runs" other code • e.g. web browsers includes a Javascript interpreter --Browser "runs" bits of Javascript code in a web page, such as ours • Interpreter looks at one line at a time • Deconstructs what each line says to do • The interpreter then does that action, in the moment • Then proceeds to the next line • // Javascript code a = 1; b = a + 2;
  • 14. Conti.., • e.g. Interpreter looks at a = 1;, does it • e.g. Interpreter looks at b = a + 2;, does it • The compiler translates source code to equivalent machine code • The interpreter does the code, looking at each line and doing it • An interpreter is a program which reads in source code as its input, and "runs" the input code. The interpreter proceeds through the code given to it, line by line
  • 15. Conti.., • A compiler translates all the source code into equivalent machine code program.exe to be run later -- it is a bulk translation. • An interpreter looks at each line of code, and translates and runs it in the moment, and then proceeds to the next line of source code. • The interpreter does not produce a program.exe, instead it performs the actions specified in the source code directly.
  • 16. Compiler vs. Interpreter Evaluation • Disclaimer: there are many languages, no one "best" language, it depends! • Compiled code tends to run faster • The .exe tends to be "lean" .. compiler has removed overhead, some decisions • Dynamic/interpreter languages --Tend to have a greater number of programmer-friendly features --i.e. programmers are often more productive in dynamic languages --The resulting program tends to run somewhat slower than compiled code
  • 17. Conti.., • Compiled code generally runs faster than interpreted code. This is because many questions -- how to append to this string, how many bytes do I need here -- are resolved by the compiler at compile time, long before the program runs. • The compiler has, in effect, pre-processed the source code, stripping out many questions and complications, leaving the program.exe as lean and direct as it can be to just run.
  • 18. Dynamic Languages - More Features / Slower • Dynamic languages tend to have more features (programmer- friendly) • e.g. Memory Management --C and C++: partially manual, some programmer input required --Dynamic languages: automatic memory management, no programmer input needed --Automatic memory management not free: spending CPU cycles to lighten programmer workload • Tradeoff --Dynamic languages often allow the programmer to get things done faster
  • 19. Conti.., --However the dynamic code runs a bit more slowly compared to compiled code • Current trend is towards dynamic languages --Programmers are scarce --It's attractive to save some programmer time at the expense of some CPU/memory use --Moore's law reinforces: CPU cheap, programmer relatively rare/expensive
  • 20. Conti.., • Supporting features tends to be easier in dynamic languages compared to compiled languages, which is why dynamic languages tend to have a greater number of programmer- friendly features. • "Memory management" is the problem in a program of knowing, over time, when bytes of RAM are needed and when they can be reclaimed and use for something else. • Every program must solve this problem. • Memory management is an excellent example of a feature different between compiled and dynamic languages -- most modern dynamic languages manage memory automatically.
  • 21. Conti.., • The programmer can focus on the problem to be solved, and the dynamic language will take care of managing the memory.
  • 22. JIT Just In Time Compiler • JIT -- compile code of a dynamic language on the fly • All major browsers now have a JIT for the Javascript code they run (Chrome) • Best of both worlds • Flexibility of dynamic languages • Combined with most of the performance of the compiled world • Active area of research, works pretty well
  • 23. Conti.., • The most modern form of dynamic language is implemented with an interpreter paired with a Just In Time compiler (JIT) trying to get the best of both worlds. • The JIT looks a sections of dynamic code that are being run very frequently, and for those, does a compile to native code for that section on the fly.