Let's Write an LLVM Specializer for Python! Stephen Diehl)LLVM is the engine that drives our effort. It is a modern compiler framework and intermediate representation language together with toolchain for manipulating and optimizing this language.
Initial usage: stl2bin your_ascii_stl_file.stl new_binary_stl_file.stl; stl2ascii your_binary_stl_file.stl new_ascii_stl_file.stl; stl your_ascii_stl_file.stl new.
Basic Types. LLVM types are your typical machine types plus pointers, structs, vectors and arrays. Array of 1. 0 floats. Array of 1. 0 arrays of 2. Vector of width 8 of floatsfloat* ; Pointer to a float. Pointer to an array.
Instructions. All instructions are assignment to a unique virtual register. In SSA (Single Static Assignment) a register is never assigned to more than once.%result = add i. Symbols used in an LLVM module are either global or local. Global symbols begin with @ and local symbols begin with %. The numerical instructions are: add : Integer additionfadd : Floating point additionsub : Integer subtractionfsub : Floating point subtractionmul : Integer multiplicationfmul : Floating point multiplicationudiv : Unsigned integer quotientsdiv : Signed integer quotientfdiv : Floating point quotienturem : Unsigned integer remaindersrem : Signed integer remainderfrem : Floating point integer remainder. Memory. LLVM uses the traditional load/store model: load: Load a typed value from a given referencestore: Store a typed value in a given referencealloca: Allocate a pointer to memory on the virtual stack%ptr = alloca i. Functions. Functions are defined by as a collection of basic blocks, a return type and argument types.
Function names must be unique in the module. Basic Blocks. The function is split across basic blocks which hold sequences of instructions and a terminator instruction which either returns or jumps to another local basic block. Return. A function must have a terminator, one of such instructions is a ret which returns a value to the stack.
Scripting Languages I: Node. PHP, Python, Ruby (Sheet One)a side- by- side reference sheetsheet one: version | grammar and execution | variables and expressions | arithmetic and logic | strings | regexes | dates and time | arrays | dictionaries | functions | execution control | exceptions | concurrencysheet two: streams | files | file formats | directories | processes and environment | option parsing | libraries and namespaces | objects | inheritance and polymorphism | reflection | net and web | gui | unit tests | debugging and profiling | java interopversionnode. V$ python - -version$ ruby - -versionimplicit prologue // npm install underscorevarus = require('underscore'); noneimport os, re, sysnonegrammar and executionnode. Python 3 source is UTF- 8 by default# - *- coding: utf- 8 - *- Ruby 2.
We now create the builder function which we'll use to populate the basic block structure of the module. Again, when we call Python's repr it will print out the LLVM. Introduction¶ There are 5 general mechanisms for creating arrays: Conversion from other Python structures (e.g., lists, tuples) Intrinsic numpy array array creation.
I have a list of bytes as integers, which is something like [120, 3, 255, 0, 100] How can I write this list to a file as binary? Would this work? newFileBytes = [123. Python: We assume that os, re, and sys are always imported. Grammar and Execution. interpreter. The customary name of the interpreter and how to invoke it. How to Read and Write Files in Python. When you're programming in the Python language beyond the most trivial programs, you'll typically be required to read data from. This HOWTO discusses Python 2.x’s support for Unicode, and explains various problems that people commonly encounter when trying to work with Unicode.
UTF- 8 by default# - *- coding: utf- 8 - *- end- of- line comment // comment// comment# comment# comment# commentmultiple line comment /* lineanother line *//* comment lineanother line */use triple quote string literal: '''comment lineanother line'''=begincomment lineanother line=endvariables and expressionsnode. NULL; $a = []; $d = []; $x = 1; list($y, $z) = [2, 3]; # in function body: v = Nonea, d = [], {}x = 1y, z = 2, 3v = nila, d = [], {}x = 1y, z = 2, 3regions which define lexical scopetop level: html pagenestable: functiontop level: function or method bodynestable (with use clause): anonymous function bodynestable (read only): function or method bodytop level: file class block module block method bodynestable: anonymous function body anonymous blockglobal variable// assign without using varg = 1; functionincr_global () { g++; }list($g. PI", 3. 1. 4); const. PI = 3. 1. 4; # uppercase identifiers# constant by convention. PI = 3. 1. 4# warning if capitalized# identifier is reassigned.
PI = 3. 1. 4assignment x = 1; $v = 1; assignments can be chained but otherwise don't return values: v = 1v = 1parallel assignment nonelist($x, $y, $z) = [1 ,2, 3]; # 3 is discarded: list($x, $y) = [1, 2, 3]; # $z set to NULL: list($x, $y, $z) = [1, 2]; x, y, z = 1, 2, 3# raises Value. Error: x, y = 1, 2, 3# raises Value. Error: x, y, z = 1, 2x, y, z = 1, 2, 3# 3 is discarded: x, y = 1, 2, 3# z set to nil: x, y, z = 1, 2swap tmp = x; x = y; y = tmp; list($x, $y) = [$y, $x]; x, y = y, xx, y = y, xcompound assignmentarithmetic, string, logical, bit+= - = *= /= none %=+=none< < = > > = & = |= ^=+= - = *= none /= %= **=.= none& = |= none< < = > > = & = |= ^=# do not return values: += - = *= /= //= %= **=+= *=& = |= ^=< < = > > = & = |= ^=+= - = *= /= none %= **=+= *=& & = ||= ^=< < = > > = & = |= ^=increment and decrement var x = 1; var y = ++x; var z = - -y; $x = 1; $y = ++$x; $z = - -$y; nonex = 1# x and y not mutated: y = x.
NULL# case insensitive. Nonenilnull test v === nullis_null($v)! Nonev is. Nonev == nilv.
NULLraises Name. Errorraises Name. Errorconditional expression x > 0 ? TRUE FALSE# case insensitive. True Falsetrue falsefalsehoods false null undefined"" 0 Na. NFALSE NULL 0 0. 0 "" "0" []False. None 0 0. 0 '' [] {}false nillogical operators & & || !& & || ! Math. min(1, 2, 3)Math.
Math. min. apply(Math, [1, 2, 3])Math. Math, [1, 2, 3])min(1, 2, 3)max(1, 2, 3)$a = [1, 2, 3]min($a)max($a)min(1, 2, 3)max(1, 2, 3)min([1, 2, 3])max([1, 2, 3])[1, 2, 3]. Python 3: cmp(0, 1)cmp('do', 're')0 < => 1"do" < => "re"arithmetic operatorsaddition, subtraction, multiplication, float division, quotient, remainder+ - * / none %+ - * / none %+ - * see note // %Python 2 does not have an operator which performs float division on integers. In Python 3 / always performs float division.+ - * x.
Math. floor(x / y)(int)(1. Infinity, Na. N, or - Infinity depending upon whether dividend is positive, zero, or negative. There are literals for Infinity and Na.
N. returns FALSE with warningraises Zero. Division. Errorraises Zero.
Division. Errorfloat division 1. Python 3: 1. 3 / 5. FALSE with warningraises Zero.
Division. Errorreturns - Infinity, Na. N, or Infinitypower Math. Math. sqrt(2)sqrt(2)import mathmath. Mathsqrt(2)sqrt - 1 Na. NNa. N# raises Value. Error: import mathmath.
Math. sqrt(- 1) raises Math: :Domain. Error unless require 'complex' is in effect.(- 1) ** 0.
Math. exp Math. log Math. Math. cos Math. tan Math. Math. acos Math. atan Math. Mathexp log sin cos tan asin acos atan atan.
Math. PIMath. EM_PI M_Eimport mathmath. Math. PI Efloat truncation none.
Math. round(3. 1)Math. Math. ceil(3. 1)(int)$xround($x)ceil($x)floor($x)import mathint(x)int(round(x))math. Math. abs(- 3)abs($x)abs(x)x. Bignumfloat overflow Infinity. INFraises Overflow. Error. Infinityrational construction nonefrom fractions import Fractionx = Fraction(2. Rational(2. 2, 7)rational decomposition nonex.
Math. floor(Math. Math. random()nonerand(0,9. Python 2. 0x. 2a.
String(7)?? base_convert("4. Stringstringstr. Stringstring literal "don't say \"no\""'don\'t say "no"'"don't say \"no\""'don\'t say "no"''don\'t say "no"'"don't say \"no\"""don't "'say "no"''''don't say "no"'''"""don't say "no\"""""don't say \"no\""'don\'t say "no"'"don't "'say "no"'newline in literal yes'first linesecond line'"first linesecond line"triple quote literals only: '''first linesecond line'''"""first linesecond line"""'first linesecond line'"first linesecond line"literal escapes single and double quotes: \b \f \n \r \t \v \uhhhh \xhh \" \' \\double quoted: \f \n \r \t \v \xhh \$ \" \\ \ooosingle quoted: \' \\single and double quoted: \newline \\ \' \" \a \b \f \n \r \t \v \ooo \xhh. Python 3: \uhhhh \Uhhhhhhhhdouble quoted: \a \b \cx \e \f \n \r \s \t \v \xhh \ooo \uhhhh \u{hhhhh}single quoted: \' \\here document none$word = "amet"; $s = < < < EOFlorem ipsumdolor sit $word. EOF; noneword = "amet"s = < < EOFlorem ipsumdolor sit#{word}EOFvariable interpolation // None; use string concatenation.// Both of these expressions are '1. None; use string concatenation.// Evaluates to "1. Fixed(2)$fmt = "lorem %s %d %f"; sprintf($fmt, "ipsum", 1.
Hello, " + "World!"; $s = "Hello, "; $s. World!"; s = 'Hello, 's. World!'# juxtaposition can be used to# concatenate literals: s. Hello, ' "World!"s = "Hello, "s.
World!"# juxtaposition can be used to# concatenate literals: s. Hello, " 'World!'replicate var hbar = Array(8.
Upper. Case()"LOREM". Lower. Case()strtoupper("lorem")strtolower("LOREM")'lorem'.
LOREM'. lower()"lorem". LOREM". downcasecapitalizestring, wordsnoneucfirst("lorem")ucwords("lorem ipsum")import string'lorem'. Left()"lorem ". trim. Right()trim(" lorem ")ltrim(" lorem")rtrim("lorem ")' lorem '. STR_PAD_LEFT)str_pad("lorem", 1. STR_PAD_BOTH)'lorem'.
Int("1. 2", 1. 0)7. Float(". 0. 37")// 1. Int("1. 2A")// Na. N: parse. Int("A")7 + "1. A"# 0: 0 + "A"7 + int('1. Value. Error: int('1.
A')# raises Value. Error: int('A')7 + "1. A". to_i# 0: "A". Type. Error: ' '. None, 1)"do re mi fa". NULL, PREG_SPLIT_DELIM_CAPTURE)re. With("foo")"foobar".
With("bar")'foobar'. Of("ipsum")# returns FALSE if not found: strpos("do re re", "re")strrpos("do re re", "re")# raises Value. Error if not found: 'do re re'. String. from. Char.
Code(6. 5)"A". char. Code. At(0)chr(6. A")chr(6. 5)ord('A')# Python 3: chr(0x. A". ord. 0x. 3bb. UTF- 8')"\u. 03bb".
None, "aeiou")"disemvowel me". D \s \S \w \W. \d \D \h \H \s \S \v \V \w \W. D \s \S \w \W. \d \D \h \H \s \S \w \Wanchors ^ $ \b \B^ $ \A \b \B \z \Z^ $ \A \b \B \Z^ $ \A \b \B \z \Zmatch test if (s. Lorem". match(/lorem/i)preg_match('/lorem/i', "Lorem")re. Lorem', re. I)/lorem/i. Lorem")modifiers g i me i m s xre.
I re. M re. S re. Xi o m xsubstitution s = "do re mi mi mi"; s. P< file>.+)\.(? P< suffix>.+)$/'; preg_match($rx, $s, $m); $m["file"]$m["suffix"]rx = '^(? P< file>.+)\.(? P< suffix>.+)$'m = re.
R))\)/'none/(?< foo> \(([^()]*|\g< foo> )*\))/dates and timenode. Date. Date. Timedatetime. Timecurrent datetimevart = new. Date(); $t = new. Date. Time("now"); $utc_tmz = new. Date. Time. Zone("UTC"); $utc = new.
Date. Time("now", $utc_tmz); import datetimet = datetime. Time. nowutc = Time. Date()). get. Time() / 1. Time. now. to_ibroken- down datetime to unix epoch. Math. round(t. get. Time() / 1. 00. 0)$epoch = $t- > get. Timestamp(); from datetime import datetime as dtepoch = int(t.
Date(epoch * 1. 00. Date. Time(); $t. Timestamp(1. 30. 44. Time. at(1. 30. 44. Y- %m- %d %H: %M: %S", $epoch); date("Y- m- d H: i: s", $epoch); $t- > format("Y- m- d H: i: s"); t.
Y- %m- %d %H: %M: %S')t. Y- %m- %d %H: %M: %S")parse datetimenone$fmt = "Y- m- d H: i: s"; $s = "2. Date. Time: :create. From. Format($fmt, $s); from datetime import datetimes = '2. Y- %m- %d %H: %M: %S't = datetime. Y- %m- %d %H: %M: %S"t = Date.
Time. strptime(s, fmt). Date("July 7, 1. 99. July 7, 1. 99. 9"); # pip install python- dateutilimport dateutil. July 7, 1. 99. 9't = dateutil. July 7, 1. 99. 9"t = Date. Full. Year()t. get.
Month() + 1t. get. Date() # get. Day() is day of week(int)$t- > format("Y")(int)$t- > format("m")(int)$t- > format("d")t. Hours()t. get. Minutes()t. Seconds()(int)$t- > format("H")(int)$t- > format("i")(int)$t- > format("s")t. Date(yr, mo - 1, dy, hr, mi, ss); import datetimeyr = 1. Time. new(yr, mo, dy, hr, mi, ss)datetime subtractionnumber containing time difference in milliseconds# Date.
Interval object if diff method used: $fmt = "Y- m- d H: i: s"; $s = "2. Date. Time: :create. From. Format($fmt, $s); $now = new.
Date. Time("now"); $interval = $now- > diff($then); datetime. Float containing time difference in secondsadd durationvart. Date(); vardelta = (1. Date(t. 1. get. Time() + delta); $now = new. Date. Time("now"); $now- > add(new. Date. Interval("PT1. M3. S"); import datetimedelta = datetime.
Date: :Delta. parse(s). Time. now + deltalocal time zone determination# Date.