SlideShare a Scribd company logo
1 of 35
Download to read offline
WebAssembly
A portable compilation target for the
web
Compiling
X
Transpiling
Compiling: What?
General term for taking source code written in one language and transforming
into another language
1. C/C++ to x86 machine code
2. Java to JVM Bytecode
Transpiling: What?
Specific term for taking source code written in one language and transforming
into another language that has a similar level of abstraction
1. Java to Javascript
2. JVM Bytecode to x86 machine code
Assembly
Assembly: What?
Assembly (or assembler) language (abbreviated asm), is any low-level
programming language in which there is a very strong correspondence between
the program's statements (mnemonic) and the architecture's machine code
instructions (opcodes).
MOV AL, 0x61 ; Load register AL with 97 (61 hex)
Assembly: What?
Assembly Language for x86 (Mnemonic and operands):
0xB0 0x61 ; Load register AL with 97 (61 hex)
Machine code (Opcode Operand)
Assembler Disassembler
StrongCorrespondence
StrongCorrespondence
MOV AL <=> 0xB0
LLVM
https://lowlevelbits.org/bitcode-demystified/
1. C / C++
2. Rust
3. Go
4.
5.
6.
7.
Compilation Targets
1. x86
2. x86-64
3. ARM
4. MIPS
5. PowerPC
High-Level
Languages Machine Code
Common
Targets
1. C / C++
2. Rust
3. Go
4. Java
5. C#
6. Kotlin
7. Javascript
Compilation Targets
1. JVM Bytecode
2. MS CIL
3. LLVM-IR
(BitCode)
4. ASM.JS
5. WASM
High-Level
Languages
Low-Level
Languages
More Targets
1. JVM Bytecode
2.
3.
4.
5.
JVM Bytecode 1.
2. x86-64
3.
4.
5.
Low-Level
Languages
Machine Code
● JVM
Desktop Virtual
Machines
Graal Substrate
1.
2. MS CIL
3.
4.
5.
MS CIL
Low-Level
Languages
1.
2.
3.
4. MS CLR
Desktop Virtual
Machines
1.
2.
3. LLVM-IR
(BitCode)
4.
5.
LLVM BitCode 1. x86
2. x86-64
3. ARM
4. MIPS
5. PowerPC
Low-Level
Languages
Machine Code
● Graal Sulong
Desktop Virtual
Machines
● asm.js
● WASM
Virtual
Machine Code
1.
2.
3.
4. ASM.JS
5.
ASM.JS
Low-Level
Languages
1. SpiderMonkey
2. V8
3.
4.
Virtual Machines
WebAssembly: What?
WebAssembly (abbreviated wasm)
● A stack-based virtual machine architecture (no registers).
WebAssembly: What?
WebAssembly (abbreviated wasm)
● A stack-based virtual machine architecture (no registers).
● In a way, the next evolution of asm.js and PNaCl
WebAssembly X asm.js
asm.js (text) Wasm (bytes)
(x+y)|0 => i32.add
f()|0 => call
HEAP32[i>>2]|0 => i32.load
WebAssembly: What?
WebAssembly (abbreviated wasm)
● A stack-based virtual machine architecture (no registers).
● In a way, the next evolution of asm.js and PNaCl
● An easy, portable compilation target of high-level languages like
C/C++/Rust, enabling deployment on the web for client and server
applications.
WebAssembly: What?
WebAssembly (abbreviated wasm)
● A stack-based virtual machine architecture (no registers).
● In a way, the next evolution of asm.js and PNaCl
● An easy, portable compilation target of high-level languages like
C/C++/Rust, enabling deployment on the web for client and server
applications.
● A set of 1-byte opcodes encoded in a size- and load-time-efficient binary
format and corresponding mnemonics
WebAssembly: What?
WebAssembly (abbreviated wasm)
● A stack-based virtual machine architecture (no registers).
● In a way, the next evolution of asm.js and PNaCl
● An easy, portable compilation target of high-level languages like
C/C++/Rust, enabling deployment on the web for client and server
applications.
● A set of 1-byte opcodes encoded in a size- and load-time-efficient binary
format and corresponding mnemonics
● Able to provide predictable performance (difficult for languages w/ GC and
really difficult with dynamically-typed languages)
WebAssembly: Features
● Transpiling to native machine code is super fast
● No runtime library
● Can call and use exported functions and linear memory from JS
● Can export functions and linear memory to JS
● No GC for now. Single linear memory space (a byte array).
That’s why Go compiled to WASM is bigger than C/C++ and Rust (needs to
also compile its own garbage collector to WASM, just like Kotlin and Java).
● Runs inside a sandboxed environment, so it's inherently more secure than
running a C source code compiled to x86 or ARM architectures
WebAssembly: Uses
● Avoid plugins like Flash, Java Applets (deprecation, friction, security)
● Avoid rewriting large code bases in C/C++/etc - Just compile it
● Port high-performance C/C++ libs to be called by JS
● Ship audio/video codecs for rare formats
● Audio / Video / Image editing / processing
(https://github.com/agnivade/shimmer)
● 3D editing (AutoCAD: https://web.autocad.com/ - 36.8 MB)
● 3D Games (Tanks! w/ Unity3D: https://webassembly.org/demo/Tanks/)
1.
2.
3.
4.
5. WASM
WASM: Where to run?
Low-Level
Languages 1. Browsers
2. Desktop
3. Smart Contract VM
Virtual Machines
1. EOS
2. Parity
Substrate
3. Dfinity
4. TrueBit
Blockchain
Text format (.wat)
● Encodes a WASM module with
all its contained definitions in a
way that is equivalent to the
binary format.
● Uses S-Expressions
C++ Binary Text
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
20 00
42 00
51
04 7e
42 01
05
20 00
20 00
42 01
7d
10 00
7e
0b
get_local 0
i64.const 0
i64.eq
if i64
i64.const 1
else
get_local 0
get_local 0
i64.const 1
i64.sub
call 0
i64.mul
end
Text format (.wat)
The shortes possible WASM module:
Text
(module)
Text format (.wat)
The shortes possible WASM module:
Binary
0000000: 0061 736d ; WASM_BINARY_MAGIC
0000004: 0100 0000 ; WASM_BINARY_VERSION
C Example
C
// demo.c
DLL_EXPORT
int add(int lhs, int rhs) {
return lhs + rhs;
}
#define DLL_EXPORT __attribute__
((visibility ("default")))
C Example
Compile to WASM:
clang -mwasm demo.c -o
demo.wasm
C
// demo.c
DLL_EXPORT
int add(int lhs, int rhs) {
return lhs + rhs;
}
#define DLL_EXPORT __attribute__
((visibility ("default")))
Loading and
calling
fetch('demo.wasm').then(response =>
Wasm.compile(response.body.getReader())
).then(instance => {
alert("1 + 2 = " + instance.exports.add( 1, 2));
});
C Example
WAT
(module
(func $add (param $lhs i32) (param $rhs
i32) (result i32)
(i32.add (get_local $lhs) (get_local
$rhs))
)
(export "add" $add)
)
Go Example
● Go 1.11 (2018-08-24)
added an
experimental port to
WebAssembly.
Go
package main
import "fmt"
func main() {
fmt.Println("Hello, WebAssembly!")
}
$ GOOS=js GOARCH=wasm go build -o main.wasm
Go Example
index.html
<html>
<head>
<meta charset="utf-8">
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"),
go.importObject).then((result) => {
go.run(result.instance);
});
</script>
</head>
<body></body>
</html>
$ GOOS=js GOARCH=wasm go build -o
main.wasm
$ cp "$(go env
GOROOT)/misc/wasm/wasm_exec.js" .
$ goexec 'http.ListenAndServe(":8080",
http.FileServer(http.Dir(".")))'
Go + LLVM
https://go.googlesource.com/gollvm/
Links
1. https://github.com/mbasso/awesome-wasm
2. https://chinagdg.org/2018/08/liftoff-a-new-baseline-compiler-for-webasse
mbly-in-v8/
3. https://github.com/golang/go/wiki/WebAssembly
Thanks!
Contact me:
Elifarley Cruz
elifarley@gmail.com
http://about.me/elifarley

More Related Content

What's hot

Porting C++ apps to FLASCC
Porting C++ apps to FLASCCPorting C++ apps to FLASCC
Porting C++ apps to FLASCCPavel Nakaznenko
 
Porting and Maintaining your C++ Game on Android without losing your mind
Porting and Maintaining your C++ Game on Android without losing your mindPorting and Maintaining your C++ Game on Android without losing your mind
Porting and Maintaining your C++ Game on Android without losing your mindBeMyApp
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget coursecrazyaxe
 
Data Management and Streaming Strategies in Drakensang Online
Data Management and Streaming Strategies in Drakensang OnlineData Management and Streaming Strategies in Drakensang Online
Data Management and Streaming Strategies in Drakensang OnlineAndre Weissflog
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019corehard_by
 
QBASIC
QBASICQBASIC
QBASICnivi88
 
Are AAA 3D Games for the Web Possible?
Are AAA 3D Games for the Web Possible?Are AAA 3D Games for the Web Possible?
Are AAA 3D Games for the Web Possible?Renaun Erickson
 
Onivim: Modal Editing from the Future
Onivim: Modal Editing from the FutureOnivim: Modal Editing from the Future
Onivim: Modal Editing from the FutureBryan Phelps
 
Open API (aka Swagger) - DDD by Night May 2020
Open API (aka Swagger) - DDD by Night May 2020Open API (aka Swagger) - DDD by Night May 2020
Open API (aka Swagger) - DDD by Night May 2020Pratik Khasnabis
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftTalentica Software
 
Venkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In GroovyVenkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In Groovydeimos
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project noelchris3
 
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseSteve Reiner
 

What's hot (20)

Porting C++ apps to FLASCC
Porting C++ apps to FLASCCPorting C++ apps to FLASCC
Porting C++ apps to FLASCC
 
Porting and Maintaining your C++ Game on Android without losing your mind
Porting and Maintaining your C++ Game on Android without losing your mindPorting and Maintaining your C++ Game on Android without losing your mind
Porting and Maintaining your C++ Game on Android without losing your mind
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget course
 
Data Management and Streaming Strategies in Drakensang Online
Data Management and Streaming Strategies in Drakensang OnlineData Management and Streaming Strategies in Drakensang Online
Data Management and Streaming Strategies in Drakensang Online
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
 
Programming
ProgrammingProgramming
Programming
 
QBASIC
QBASICQBASIC
QBASIC
 
HaXe Demo
HaXe DemoHaXe Demo
HaXe Demo
 
Are AAA 3D Games for the Web Possible?
Are AAA 3D Games for the Web Possible?Are AAA 3D Games for the Web Possible?
Are AAA 3D Games for the Web Possible?
 
ruby-cocoa
ruby-cocoaruby-cocoa
ruby-cocoa
 
Onivim: Modal Editing from the Future
Onivim: Modal Editing from the FutureOnivim: Modal Editing from the Future
Onivim: Modal Editing from the Future
 
Coroutines in Kotlin
Coroutines in KotlinCoroutines in Kotlin
Coroutines in Kotlin
 
Open API (aka Swagger) - DDD by Night May 2020
Open API (aka Swagger) - DDD by Night May 2020Open API (aka Swagger) - DDD by Night May 2020
Open API (aka Swagger) - DDD by Night May 2020
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thrift
 
Venkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In GroovyVenkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In Groovy
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project
 
Week1 dq3
Week1 dq3Week1 dq3
Week1 dq3
 
Conscious Coupling
Conscious CouplingConscious Coupling
Conscious Coupling
 
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
 
GCC, GNU compiler collection
GCC, GNU compiler collectionGCC, GNU compiler collection
GCC, GNU compiler collection
 

Similar to Wasm intro

WebAssembly - czy dzisiaj mi się to przyda do pracy?
WebAssembly - czy dzisiaj mi się to przyda do pracy?WebAssembly - czy dzisiaj mi się to przyda do pracy?
WebAssembly - czy dzisiaj mi się to przyda do pracy?Brainhub
 
Web assembly: a brief overview
Web assembly: a brief overviewWeb assembly: a brief overview
Web assembly: a brief overviewPavlo Iatsiuk
 
Introduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutionsIntroduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutionsQUONTRASOLUTIONS
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assemblyShakacon
 
Presentation1
Presentation1Presentation1
Presentation1kpkcsc
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net frameworkArun Prasad
 
List of programming_languages_by_type
List of programming_languages_by_typeList of programming_languages_by_type
List of programming_languages_by_typePhoenix
 
.Net overviewrajnish
.Net overviewrajnish.Net overviewrajnish
.Net overviewrajnishRajnish Kalla
 
Introduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsIntroduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsQuontra Solutions
 
jhkghj
jhkghjjhkghj
jhkghjAdmin
 
test2PPT
test2PPTtest2PPT
test2PPTAdmin
 
Web Development Environments: Choose the best or go with the rest
Web Development Environments:  Choose the best or go with the restWeb Development Environments:  Choose the best or go with the rest
Web Development Environments: Choose the best or go with the restgeorge.james
 
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...Flink Forward
 
Past, present, and future of web assembly - Devfest Nantes 2017
Past, present, and future of web assembly - Devfest Nantes 2017Past, present, and future of web assembly - Devfest Nantes 2017
Past, present, and future of web assembly - Devfest Nantes 2017Alexandre Morgaut
 

Similar to Wasm intro (20)

WebAssembly - czy dzisiaj mi się to przyda do pracy?
WebAssembly - czy dzisiaj mi się to przyda do pracy?WebAssembly - czy dzisiaj mi się to przyda do pracy?
WebAssembly - czy dzisiaj mi się to przyda do pracy?
 
Web assembly: a brief overview
Web assembly: a brief overviewWeb assembly: a brief overview
Web assembly: a brief overview
 
Introduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutionsIntroduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutions
 
Compilers
CompilersCompilers
Compilers
 
Web (dis)assembly
Web (dis)assemblyWeb (dis)assembly
Web (dis)assembly
 
Presentation1
Presentation1Presentation1
Presentation1
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 
List of programming_languages_by_type
List of programming_languages_by_typeList of programming_languages_by_type
List of programming_languages_by_type
 
.Net overviewrajnish
.Net overviewrajnish.Net overviewrajnish
.Net overviewrajnish
 
Synapse india reviews sharing asp.net
Synapse india reviews sharing  asp.netSynapse india reviews sharing  asp.net
Synapse india reviews sharing asp.net
 
c vs java (2).pptx
c vs java (2).pptxc vs java (2).pptx
c vs java (2).pptx
 
Introduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsIntroduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutions
 
jhkghj
jhkghjjhkghj
jhkghj
 
test2PPT
test2PPTtest2PPT
test2PPT
 
Asp net
Asp netAsp net
Asp net
 
Web Development Environments: Choose the best or go with the rest
Web Development Environments:  Choose the best or go with the restWeb Development Environments:  Choose the best or go with the rest
Web Development Environments: Choose the best or go with the rest
 
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...
Flink Forward Berlin 2018: Robert Bradshaw & Maximilian Michels - "Universal ...
 
Past, present, and future of web assembly - Devfest Nantes 2017
Past, present, and future of web assembly - Devfest Nantes 2017Past, present, and future of web assembly - Devfest Nantes 2017
Past, present, and future of web assembly - Devfest Nantes 2017
 
Asm.js introduction
Asm.js introductionAsm.js introduction
Asm.js introduction
 
.Net overview
.Net overview.Net overview
.Net overview
 

Recently uploaded

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Recently uploaded (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

Wasm intro

  • 3. Compiling: What? General term for taking source code written in one language and transforming into another language 1. C/C++ to x86 machine code 2. Java to JVM Bytecode
  • 4. Transpiling: What? Specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction 1. Java to Javascript 2. JVM Bytecode to x86 machine code
  • 6. Assembly: What? Assembly (or assembler) language (abbreviated asm), is any low-level programming language in which there is a very strong correspondence between the program's statements (mnemonic) and the architecture's machine code instructions (opcodes).
  • 7. MOV AL, 0x61 ; Load register AL with 97 (61 hex) Assembly: What? Assembly Language for x86 (Mnemonic and operands): 0xB0 0x61 ; Load register AL with 97 (61 hex) Machine code (Opcode Operand) Assembler Disassembler StrongCorrespondence StrongCorrespondence MOV AL <=> 0xB0
  • 9. 1. C / C++ 2. Rust 3. Go 4. 5. 6. 7. Compilation Targets 1. x86 2. x86-64 3. ARM 4. MIPS 5. PowerPC High-Level Languages Machine Code Common Targets
  • 10. 1. C / C++ 2. Rust 3. Go 4. Java 5. C# 6. Kotlin 7. Javascript Compilation Targets 1. JVM Bytecode 2. MS CIL 3. LLVM-IR (BitCode) 4. ASM.JS 5. WASM High-Level Languages Low-Level Languages More Targets
  • 11. 1. JVM Bytecode 2. 3. 4. 5. JVM Bytecode 1. 2. x86-64 3. 4. 5. Low-Level Languages Machine Code ● JVM Desktop Virtual Machines Graal Substrate
  • 12. 1. 2. MS CIL 3. 4. 5. MS CIL Low-Level Languages 1. 2. 3. 4. MS CLR Desktop Virtual Machines
  • 13. 1. 2. 3. LLVM-IR (BitCode) 4. 5. LLVM BitCode 1. x86 2. x86-64 3. ARM 4. MIPS 5. PowerPC Low-Level Languages Machine Code ● Graal Sulong Desktop Virtual Machines ● asm.js ● WASM Virtual Machine Code
  • 15. WebAssembly: What? WebAssembly (abbreviated wasm) ● A stack-based virtual machine architecture (no registers).
  • 16. WebAssembly: What? WebAssembly (abbreviated wasm) ● A stack-based virtual machine architecture (no registers). ● In a way, the next evolution of asm.js and PNaCl
  • 17. WebAssembly X asm.js asm.js (text) Wasm (bytes) (x+y)|0 => i32.add f()|0 => call HEAP32[i>>2]|0 => i32.load
  • 18. WebAssembly: What? WebAssembly (abbreviated wasm) ● A stack-based virtual machine architecture (no registers). ● In a way, the next evolution of asm.js and PNaCl ● An easy, portable compilation target of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.
  • 19. WebAssembly: What? WebAssembly (abbreviated wasm) ● A stack-based virtual machine architecture (no registers). ● In a way, the next evolution of asm.js and PNaCl ● An easy, portable compilation target of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications. ● A set of 1-byte opcodes encoded in a size- and load-time-efficient binary format and corresponding mnemonics
  • 20. WebAssembly: What? WebAssembly (abbreviated wasm) ● A stack-based virtual machine architecture (no registers). ● In a way, the next evolution of asm.js and PNaCl ● An easy, portable compilation target of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications. ● A set of 1-byte opcodes encoded in a size- and load-time-efficient binary format and corresponding mnemonics ● Able to provide predictable performance (difficult for languages w/ GC and really difficult with dynamically-typed languages)
  • 21. WebAssembly: Features ● Transpiling to native machine code is super fast ● No runtime library ● Can call and use exported functions and linear memory from JS ● Can export functions and linear memory to JS ● No GC for now. Single linear memory space (a byte array). That’s why Go compiled to WASM is bigger than C/C++ and Rust (needs to also compile its own garbage collector to WASM, just like Kotlin and Java). ● Runs inside a sandboxed environment, so it's inherently more secure than running a C source code compiled to x86 or ARM architectures
  • 22. WebAssembly: Uses ● Avoid plugins like Flash, Java Applets (deprecation, friction, security) ● Avoid rewriting large code bases in C/C++/etc - Just compile it ● Port high-performance C/C++ libs to be called by JS ● Ship audio/video codecs for rare formats ● Audio / Video / Image editing / processing (https://github.com/agnivade/shimmer) ● 3D editing (AutoCAD: https://web.autocad.com/ - 36.8 MB) ● 3D Games (Tanks! w/ Unity3D: https://webassembly.org/demo/Tanks/)
  • 23. 1. 2. 3. 4. 5. WASM WASM: Where to run? Low-Level Languages 1. Browsers 2. Desktop 3. Smart Contract VM Virtual Machines 1. EOS 2. Parity Substrate 3. Dfinity 4. TrueBit Blockchain
  • 24. Text format (.wat) ● Encodes a WASM module with all its contained definitions in a way that is equivalent to the binary format. ● Uses S-Expressions C++ Binary Text int factorial(int n) { if (n == 0) return 1; else return n * factorial(n-1); } 20 00 42 00 51 04 7e 42 01 05 20 00 20 00 42 01 7d 10 00 7e 0b get_local 0 i64.const 0 i64.eq if i64 i64.const 1 else get_local 0 get_local 0 i64.const 1 i64.sub call 0 i64.mul end
  • 25. Text format (.wat) The shortes possible WASM module: Text (module)
  • 26. Text format (.wat) The shortes possible WASM module: Binary 0000000: 0061 736d ; WASM_BINARY_MAGIC 0000004: 0100 0000 ; WASM_BINARY_VERSION
  • 27. C Example C // demo.c DLL_EXPORT int add(int lhs, int rhs) { return lhs + rhs; } #define DLL_EXPORT __attribute__ ((visibility ("default")))
  • 28. C Example Compile to WASM: clang -mwasm demo.c -o demo.wasm C // demo.c DLL_EXPORT int add(int lhs, int rhs) { return lhs + rhs; } #define DLL_EXPORT __attribute__ ((visibility ("default")))
  • 30. C Example WAT (module (func $add (param $lhs i32) (param $rhs i32) (result i32) (i32.add (get_local $lhs) (get_local $rhs)) ) (export "add" $add) )
  • 31. Go Example ● Go 1.11 (2018-08-24) added an experimental port to WebAssembly. Go package main import "fmt" func main() { fmt.Println("Hello, WebAssembly!") } $ GOOS=js GOARCH=wasm go build -o main.wasm
  • 32. Go Example index.html <html> <head> <meta charset="utf-8"> <script src="wasm_exec.js"></script> <script> const go = new Go(); WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => { go.run(result.instance); }); </script> </head> <body></body> </html> $ GOOS=js GOARCH=wasm go build -o main.wasm $ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" . $ goexec 'http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))'