Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Copyright © 2012, MATHEMA Software GmbH Folie 1 von 54JVM Language Summit 2012
Assembling for the JVMType safe, efcient, and low level programming for the Java Virtual Machine
Michael [email protected]
Copyright © 2012, MATHEMA Software GmbH Folie 2 von 54JVM Language Summit 2012
Independence
● Machine independence
● Problem independence
● Human independence
● Time independence
Daniel J. SalomonFour Dimensions of Programming-Language Independence
ACM SIGPLAN Notices, Volume 27, No. 3, March 1992
Copyright © 2012, MATHEMA Software GmbH Folie 3 von 54JVM Language Summit 2012
Independence (Defnition)
Defnition of Independence
A programming language can be said to be independent of a classifcation of the elements of a domain if it:
(1) supplies the same level of computational power to all groupsin the classifcation, and
(2) meets the computational needs of each of the groupsin the classifcation.
Copyright © 2012, MATHEMA Software GmbH Folie 4 von 54JVM Language Summit 2012
Example: Architecture Independence
Architecture Independence
Applying the given defnition of independence, an architecture-independent language would allow one to:
(1) Run any program written for one architecture on any other architecture.
(2) Take advantage of all the special features of any particular architecture.
Copyright © 2012, MATHEMA Software GmbH Folie 5 von 54JVM Language Summit 2012
A short introduction to theALX Language Family
Copyright © 2012, MATHEMA Software GmbH Folie 6 von 54JVM Language Summit 2012
Unifed Syntax Elements
● Command Statementkeyword token-list
● Block Statement keyword [token-list begin-keyword]?
…end [keyword optional-tokens]?
● Expression (Statement)
● Assignment
● Procedure call
● Everything else
Copyright © 2012, MATHEMA Software GmbH Folie 7 von 54JVM Language Summit 2012
Copyright © 2012, MATHEMA Software GmbH Folie 8 von 54JVM Language Summit 2012
● AL0
random memory access (LLVM, C, …)
● AL1
managed memory (JVM, CLR, …)
● AL2
control structures, records (Pascal, C, …)
● AL3
objects (Java, C#, C++, …)
● AL?
everything else (Fortress, X10, …)
ALx-Levels
Copyright © 2012, MATHEMA Software GmbH Folie 9 von 54JVM Language Summit 2012
Protection Domains
Copyright © 2012, MATHEMA Software GmbH Folie 10 von 54JVM Language Summit 2012
Copyright © 2012, MATHEMA Software GmbH Folie 11 von 54JVM Language Summit 2012
Source File Format
● supported ALx version
● XML like format
● UTF-8 encoding
● supported Unicode version and reference to Private Use Area
● owner, copyright, license, …
● signature, …
Copyright © 2012, MATHEMA Software GmbH Folie 12 von 54JVM Language Summit 2012
Contracts and Style
function f(x : ℤ, y : ℕ) → ℕ requires {x ≤ y, x < 0 ⇒ y ≠ 0}
ensures {result > 0}is
return s ∕ √s where s = x2 + y2
end function
(a, b, x0) ↦ x0 · ∑[1 ≤ i ≤ n] √(a[i] – b[i])
Copyright © 2012, MATHEMA Software GmbH Folie 13 von 54JVM Language Summit 2012
Pattern Matching
function . ̇ : (x : ℤ, n : ℤ) → ℤ is∎ (0, 0):
throw ArithmeticException∎ (0, ?):
return 0∎ (?, 0):
return 1∎ (x, 1):
return x∎ (x, n):
return npow(x, n)end function
Copyright © 2012, MATHEMA Software GmbH Folie 14 von 54JVM Language Summit 2012
Annotated Types
● “covariant”
t : [releasable] Tu : Tt ← u // ✗u ← t // ✓
● “contravariant”
t : [tainted] Tu : Tt ← u // ✓u ← t // ✗
Copyright © 2012, MATHEMA Software GmbH Folie 15 von 54JVM Language Summit 2012
“Batch”
int limit = 500;
Remote<Mailer> connection = …;batch (Mailer mailer : connection) {
for (Message m : mailer.Messages) {if (m.Size > limit) {
print(m.Subject + ": " + m.Sender.Name);m.delete();
}}
}
Copyright © 2012, MATHEMA Software GmbH Folie 16 von 54JVM Language Summit 2012
“Batch”
limit := 500
connection : Remote⟦Mailer⟧← …batch mailer :↞ connection do
for m :↞ mailer.messages doif m.size > limit then
print(m.subject, “: ”, m.sender.name);m.delete();
end ifend for
end batch
Copyright © 2012, MATHEMA Software GmbH Folie 17 von 54JVM Language Summit 2012
“Batch”
syntax Batch extends Block is
‘batch’ e : Expr⟦Remote⟦?⟧⟧ ‘do’
b : Block
end(‘batch’)
semantics
end syntax
Copyright © 2012, MATHEMA Software GmbH Folie 18 von 54JVM Language Summit 2012
Syntax Extensions
syntax Unless extends Statement is’unless‘ condition : ℰ⟦�⟧ ’then‘
block : ℬ⎡’else‘ ⎤?
⎣ else_block : ℬ ⎦end(’unless‘)
semantic ……
end syntax
Copyright © 2012, MATHEMA Software GmbH Folie 19 von 54JVM Language Summit 2012
Syntax Extension
semantic in terms of AL0
if else_block.isDefned() then⦗t :← ⟬condition.eval()⟭⦘⦗if notFalse(t) then goto ①↓ end⦘
⦗⟬block.eval()⟭⦘⦗goto ⓪↓⦘
⦗①:⦘⦗⟬else_block.eval()⟭⦘
⦗⓪:⦘else
Copyright © 2012, MATHEMA Software GmbH Folie 20 von 54JVM Language Summit 2012
Syntax-Erweiterungen (3/3)
else⦗t :← ⟬condition.eval()⟭⦘⦗if notFalse(t) then goto ⓪↓ end⦘
⦗⟬block.eval()⟭⦘⦗⓪:⦘
end ifend syntax
Copyright © 2012, MATHEMA Software GmbH Folie 21 von 54JVM Language Summit 2012
A short Introduction to AL1
Copyright © 2012, MATHEMA Software GmbH Folie 22 von 54JVM Language Summit 2012
Hello World
namespace //mathema.de/hello/Hello
import //alx/compatibility/c/Stdio/print
⟦read_only⟧ data DataSegment is
greetings : Sq⟦Byte⟧ ← “Hello, world!” ∘ 0end data
callable main() is
s ⃗:← ⌖greetings
print(s)⃗
end callable
Copyright © 2012, MATHEMA Software GmbH Folie 23 von 54JVM Language Summit 2012
● � MachineWord
● �1 Byte
● �2 Wyde
● �4 Tetra
● �8 Octa
● �16 Hexadec
● �32 Doxadex
● �64 Texadex
● �128 Oxadex
● �10 Deca
● �12 Dodeca
● � Address
● �2 HalfFloat
● �4 SingleFloat
● �8 (Double)Float
● �10 ExtendedFloat
● �16 QuadrupleFloat
Copyright © 2012, MATHEMA Software GmbH Folie 24 von 54JVM Language Summit 2012
CafeBabe vs BebaFeca
data DataSegment isa0 : Byte ← (01)16
a1 : Byte ← (23)16
a2 : Byte ← (45)16
a3 : Byte ← (67)16
end data
callable main() ist :← load⟦Tetra⟧(⌖a0)// t = (67452301)16 (little-endian)// t = (01234567)16 (big-endian)
end callable
Copyright © 2012, MATHEMA Software GmbH Folie 25 von 54JVM Language Summit 2012
CafeBabe!
assume confg.byte_order = BIG_ENDIAN
data DataSegment isa0 : Byte ← (01)16
a1 : Byte ← (23)16
a2 : Byte ← (45)16
a3 : Byte ← (67)16
end data
callable main() ist :← load⟦Tetra⟧(⌖a0)// t = (01234567)16
end callable
Copyright © 2012, MATHEMA Software GmbH Folie 26 von 54JVM Language Summit 2012
Machine Word Access
● Tetra
● ⟦const⟧ width : ℕ[Bit] = 32
● ⟦const⟧ size : ℕ[Byte] = 4
● ⟦const⟧ element_size : ℕ[Byte] = 4
● ⟦const⟧ alignment : ℕ = 4
● signed : �32
● unsigned : �32
● high : Wyde
● low : Wyde
● wydes : Sq⟦Wyde, 2⟧
● big_endian.wydes : Sq⟦Wyde, 2⟧
● little_endian.wydes : Sq⟦Wyde, 2⟧
● bytes : Sq⟦Byte, 4⟧
● big_endian.bytes : Sq⟦Byte, 4⟧
● big_endian.bytes : Sq⟦Byte, 4⟧
● bits : BitSq⟦32⟧
● big_endian.bits : BitSq⟦32⟧
● little_endian.bits : BitSq⟦32⟧
Copyright © 2012, MATHEMA Software GmbH Folie 27 von 54JVM Language Summit 2012
⟦inline⟧callable markZeroBytes(x : �) → � is
M := (7F…7F)16
r :← and(x, M)r ← addUnsigned(r, M)r ← or(r, x)r ← or(r, M)r ← invert(r)return r
end callable
Copyright © 2012, MATHEMA Software GmbH Folie 28 von 54JVM Language Summit 2012
⟦inline⟧callable fndZeroByte(x : �) → � is
r :← markZeroBytes(x)variant∎ “big endian” …
…∎ “little endian” …
…end variantreturn r
end callable
Copyright © 2012, MATHEMA Software GmbH Folie 29 von 54JVM Language Summit 2012
Varianten
variant∎ “big endian”
requires {confg.byte_order = BIG_ENDIAN}:
nlz :← numberOfLeadingZeroBits(r)
r ← shiftRightUnsigned(nlz, 3)
∎ “little endian”requires {confg.byte_order = LITTLE_ENDIAN}:
ntz :← numberOfTrailingZeroBits(r)
r ← ⟦unchecked⟧ shiftRight(ntz, 3)
end variant
Copyright © 2012, MATHEMA Software GmbH Folie 30 von 54JVM Language Summit 2012
Predicates
● zero
● nonZero
● positive
● nonPositive
● negative
● nonNegative
● even
● odd
Copyright © 2012, MATHEMA Software GmbH Folie 31 von 54JVM Language Summit 2012
Relations
● equal
● nonEqual
● less
● lessOrEqual
● greater
● greaterOrEqual
● lessUnsigned
● lessOrEqualUnsigned
● greaterUnsigned
● greaterOrEqualUnsigned
Copyright © 2012, MATHEMA Software GmbH Folie 32 von 54JVM Language Summit 2012
(Conditional) Jumps and Assignment
● unconditional jump
goto L
● conditional jump
if condition thengoto L
end if
● conditional assignment
if condition thenv ← r
end if
Copyright © 2012, MATHEMA Software GmbH Folie 33 von 54JVM Language Summit 2012
A Note on Labels
goto ①↓①: goto ①↑①: goto ①↑
Copyright © 2012, MATHEMA Software GmbH Folie 34 von 54JVM Language Summit 2012
Basic Functions
● abs
● nabs
● sign
● min
● max
● pow
● sqrt
● log
● log
● signUnsigned
● minUnsigned
● maxUnsigned
● powUnsigned
● sqrtUnsigned
Copyright © 2012, MATHEMA Software GmbH Folie 35 von 54JVM Language Summit 2012
Bit Manipulation
● invert(x)
● and(x, y)
● or(x, y)
● xor(x, y)
● andNot(x, y)
● orNot(x, y)
● xorNot(x, y)
● nand(x, y)
● nor(x, y)
● nxor(x, y)
Copyright © 2012, MATHEMA Software GmbH Folie 36 von 54JVM Language Summit 2012
Bit Counts
● numberOfLeadingZeroBits(x)
● numberOfTrailingZeroBits(x)
● numberOfOneBits(x)
● numberOfOneBits.is_native
Copyright © 2012, MATHEMA Software GmbH Folie 37 von 54JVM Language Summit 2012
Arithmetic
● add(x : �, y : �) → � throws OverfowException
● addUnsigned(x : �, y : �) → �
● addWithCarry(x : �, y : �, c : �{0, 1}) → (�, �{0, 1})
● addWithCarryUnsigned(x : �, y : �, c : �{0, 1}) → (�, �{0, 1})
● ⟦unchecked⟧ add(x : �, y : �) → �
● ⟦checked⟧ addUnsigned(x : �, y : �) → � throws OverfowException
Copyright © 2012, MATHEMA Software GmbH Folie 38 von 54JVM Language Summit 2012
Shift
● shift2(x, i) throws OverfowException
● shift10(x, i) throws OverfowException
● shiftLeft(x, n) throws OverfowException
● shiftLeftUnsigned(x, n)
● shiftRight(x, n)
● shiftRightUnsigned(x, n)
Copyright © 2012, MATHEMA Software GmbH Folie 39 von 54JVM Language Summit 2012
Division and Shift
● x / 2n should be identical to x ≫ n(is not true for Java et al. if x < 0)
● diferent division and modulo functions
● quot(x, y), mod(x, y) // ⌊x / y⌋ ⟶ –∞● ratio(x, y), residue(x, y) // round(x / y) ⟶ nearest● group(x, y), pad(x, y) // ⌈x / y⌉ ⟶ +∞● div(x, y) // sign(x · y) · quot(|x|, |y|) ⟶ 0
Copyright © 2012, MATHEMA Software GmbH Folie 40 von 54JVM Language Summit 2012
Optimized Ofsets
● times2plus(x, y)
● times4plus(x, y)
● times8plus(x, y)
● times16plus(x, y)
● times32plus(x, y)
● times64plus(x, y)
● …
Copyright © 2012, MATHEMA Software GmbH Folie 41 von 54JVM Language Summit 2012
The Syntactical Exceptions
● Iverson operator
[ . ] : (�) → �
sign(x, y) ↦ [x > y] – [x < y]
● Address-of operator
⌖. : (⟦var⟧ T) → Pt⟦T⟧
data X isx : Tetra
end datav ← load⟦Tetra⟧(⌖x) … ¶ v ← X:x
Copyright © 2012, MATHEMA Software GmbH Folie 42 von 54JVM Language Summit 2012
Standard Annotations
● uncached
● unchecked
● checked
● tail_call
● unaligned
● volatile
● …
Copyright © 2012, MATHEMA Software GmbH Folie 43 von 54JVM Language Summit 2012
AL1 and the JVM
Copyright © 2012, MATHEMA Software GmbH Folie 44 von 54JVM Language Summit 2012
Conditional branches
if (x = a) ∨ (x = b) then …
end if
if ((a == x) ∣ (x == b)) { …
}
if ([x = a] ⊗ [x = b]) ≠ 0 then …
end if
Copyright © 2012, MATHEMA Software GmbH Folie 45 von 54JVM Language Summit 2012
Implementing [x < y]
0: iload x
1: iload y
2: if_icmpge +7 (9)
5: iconst_1
6: goto +4 (10)
9: iconst_0
10: ireturn
Copyright © 2012, MATHEMA Software GmbH Folie 46 von 54JVM Language Summit 2012
Implementing [x < y]
0: iload x
1: iload y
2: if_icmpge +5 (7)
5: iconst_1
6: ireturn
7: iconst_0
8: ireturn
Copyright © 2012, MATHEMA Software GmbH Folie 47 von 54JVM Language Summit 2012
Implementing [x < y]
0: iload x
1: iload y
2: iconst_1
3: istore z
4: if_icmplt +5 (9)
7: iconst_0
8: istore z
9: iload z
10: ireturn
Copyright © 2012, MATHEMA Software GmbH Folie 48 von 54JVM Language Summit 2012
Implementing [x < y]
0: iconst_0
1: iload x
2: iload y
3: if_icmpge +5 (8)
6: iconst_1
7: iadd
8: ireturn
Copyright © 2012, MATHEMA Software GmbH Folie 49 von 54JVM Language Summit 2012
Implementing the long Version of [x < y]
0: lload x
1: lload y
2: lcmp
3: ldc2_w 63
6: lushr
7: lreturn
Copyright © 2012, MATHEMA Software GmbH Folie 50 von 54JVM Language Summit 2012
compareAndSet(int a, int b, int c1, int c2)
21: iload 523: iload_324: iadd25: istore 527: iload 529: ireturn
int x = Integer.compare(a, b) ⋙ 31;x = x – 1;x = x & (c1 – c2);x = x + c2;return x;
0: iload_01: iload_12: invokestatic compare(II)I5: bipush 317: iushr8: istore 510: iinc 5, -113: iload 515: iload_216: iload_317: isub18: iand19: istore 5
Copyright © 2012, MATHEMA Software GmbH Folie 51 von 54JVM Language Summit 2012
compareAndSet(int a, int b, int c1, int c2)
0: iload_01: iload_12: if_icmpge 115: iload_26: istore 58: goto 1411: iload_312: istore 514: iload 516: ireturn
int x;if (a < b) {
x = c1;} else {
x = c2;}return x;
return (a < b) ? c1 : c2
0: iload_01: iload_12: if_icmpge 95: iload_26: goto 109: iload_310: ireturn
Copyright © 2012, MATHEMA Software GmbH Folie 52 von 54JVM Language Summit 2012
Missing JVM Idioms
Chapter 3 — Compiling for the Java Virtual Machine
3.3 Arithmetic
… (Recall that ~x == -1^x.) …
… iconst_m1
ixor …
Tim Lindholm, Frank Yellin, Gilad Bracha, Alex BuckleyThe JavaTM Virtual Machine Specifcation – Java SE 7 Edition
Oracle, 2012-02-06
Copyright © 2012, MATHEMA Software GmbH Folie 53 von 54JVM Language Summit 2012
Questions?
Copyright © 2012, MATHEMA Software GmbH Folie 54 von 54JVM Language Summit 2012
Contact
Michael [email protected]
MATHEMA Software GmbHHenkestraße 9191052 ErlangenGermany