count pages in pdf without opening c# : Extract pdf pages application control utility azure web page asp.net visual studio antocuni-thesis5-part305

Chapter 6
The PyPy JIT compiler generator
6.1 There is a time for everything
Usually, the lifetime e of a program m is split into compile time, when n the source code is
translatedintoexecutablemachinecode,andruntime,whenthemachinedcodeisexecuted.
However,thisisnotthecasewithPyPywhentheJITisenabled,asthelifetimeissplit
intofour dierentsteps:
Translationtime Thestepinwhichthetranslationtoolchain(seeSection3.2)generates
theactualexecutablefromtheRPythonsourceofourinterpreter. Inthisphase,the
JITcompilergeneratorrunsandgeneratesthetracingJITcompilerforourlanguage.
Interpretationtime Thestepinwhichtheuserprogram
1
isrunbytheinterpreter. It
comprisesboththeInterpretationandtheTracing bubblesinFigure5.1.
Compiletime The e step in which the generatedJITcompiler actually runs. It t corre-
spondstotheCompilationbubbleinFigure5.1.
Runtime The e step in n which the code generated by the JITcompiler is executed. It
correspondstotheRunning bubbleinFigure5.1.
Fromthisdescription,itisclearthattheonlystaticstepisthetranslationtime,whichis
performedonlyoncewheneverthesourcecodeoftheinterpreterismodied.Theremaining
threestepsarealldynamic,i.e.theyareactivewhentheuserprogramisrunning.
1
Inourterminology,thenaluseristheprogrammerwhichwriteprogramsinthelanguage.
42
Extract pdf pages - application control utility:C# PDF Page Extract Library: copy, paste, cut PDF pages in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
Easy to Use C# Code to Extract PDF Pages, Copy Pages from One PDF File and Paste into Others
www.rasteredge.com
Extract pdf pages - application control utility:VB.NET PDF Page Extract Library: copy, paste, cut PDF pages in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
Detailed VB.NET Guide for Extracting Pages from Microsoft PDF Doc
www.rasteredge.com
6.2 Architecture of the JIT compiler generator
Figure6.1showsamoredetailedviewofthearchitecture.Atthetopofthepicturethere
istheinterpreter,whichiswritteninRPythonandfollowstwoindependentpathsthrough
the translation toolchain: the e regular translation stepis s always done, , and produces s an
ecientversionoftheinterpreterwhichisincludedinthenalexecutable. Howeverthe
pathwhichismoreinterestingforourpurposesgoesthroughthecodewriter,andisactive
onlywhentheJITisenabled.
Thecodewritertranslatesthesourcecodeoftheinterpreterintointermediatecodethatwill
exploitedtotracetheexecutionoftheuserprograms. Theresultisasetofjitcodes,one
foreachRPythonfunctionormethodthatcanbetraced
2
fromthemaininterpreterloop:
thesejitcodes arecompiledintothenalexecutableasstaticdata,i.e. prebuiltinstances
thatarealreadyfullyinitializedwhentheprogramstarts. Wecanthinkofthejitcodesas
somethingthatexpressestheinterpreterintheinstructionsetofacustomvirtualmachine,
usedsolelyfortheJIT’sinternaluse.
Itisimportanttounderlinethattherearethreedierentkindsofintermediatecode,which
areexecutedatdierentlevels.InthecaseofthePythoninterpreter,thereisthePython
bytecode,whichrepresentstheuserprogramandisexecutedby theinterpreterwhichis
compiledtoCLIbytecode. Thejitcodes,whichrepresentstheinterpreteritselfinaformat
that is understandablebythe JITfrontend toproduce CLIbytecode for the hot loops.
TheCLIbytecode,whichisexecutedbytheunderlyingvirtualmachine.
Thetracing phase (see Section5.2) is implemented d by y the JIT frontend, , whichcan be
thought of as the e custom virtual l machine which can execute e the jitcodes. To o draw w a
parallelwiththeexamplesseeninChapter5,inPyPyRPythonplaystheroleofJava,the
jitcodesoftheJVMbytecodes,andtheJITfrontendistheequivalentofthetracingpart
oftheJVM.
Duringthetracingphase,thefrontendrecordsahistoryofalltheoperationsitexecutes,
i.e. atrace (see e Section 5.3). Oncecompleted, , the e traceis thenpassedtotheselected
JITbackend,whichtranslatesitintoecientmachinecode,whichisnallyexecutedat
runtime.
Like the interpreter, , the e JIT T frontend and d backends are e written in RPython, and are
compiledto CLI bytecode. Thus, , the e nal l executable e is composed of three parts: the
interpreter,theJITfrontendandbackend,andthestaticdata,whichincludesthejitcodes.
2
OnlyasubsetofalltheRPythonfunctionsoftheinterpreterisseenbytheJIT,andthus traceable:
whenthetracerencountersacall toanon-traceablefunction,itinsertsacall operationinthetrace,
insteadofinliningthebody.
43
application control utility:C# PDF Page Insert Library: insert pages into PDF file in C#.net
Page: Insert PDF Pages. |. Home ›› XDoc.PDF ›› C# PDF: Insert PDF Page. Add and Insert Multiple PDF Pages to PDF Document Using C#.
www.rasteredge.com
application control utility:C# PDF Text Extract Library: extract text content from PDF file in
inputFilePath); PDFTextMgr textMgr = PDFTextHandler.ExportPDFTextManager(doc); // Extract text content C# example code for text extraction from all PDF pages.
www.rasteredge.com
Interpreter
Codewriter
jitcodes
Finalexecutable
JITfrontend
CLIbackend
x86backend
Generatedcode
regulartranslation
staticdata
regulartranslation
analyzedby
produces
emits
Translation time
Compiletime
Compiletime
Runtime
Legend
RPython
Python
Executable
Machine code
Staticdata
Figure6.1: ThearchitectureofthePyPyJITcompilergenerator
44
application control utility:C# PDF Image Extract Library: Select, copy, paste PDF images in C#
Image: Extract Image from PDF. |. Home ›› XDoc.PDF ›› C# PDF: Extract PDF Image. How to C#: Extract Image from PDF Document.
www.rasteredge.com
application control utility:VB.NET PDF Text Extract Library: extract text content from PDF
PDF ›› VB.NET PDF: Extract PDF Text. VB.NET PDF - Extract Text from PDF Using VB. How to Extract Text from PDF with VB.NET Sample Codes in .NET Application.
www.rasteredge.com
6.3 Tracing the meta level
Fromtheexplanationabove,itisclearthatthereisanimportantdierencebetweenthe
PyPyapproachandtheusualapproachusedbytheothertracingJITcompilersdescribed
inthepreviouschapter.
ConsideragaintheexamplesinSections5.4and5.6:duringtheinterpretationphasethe
VMexecutesthebytecodeinstructions,whicharealsoexecutedandrecordedwhiletracing.
Ontheotherhand,inPyPywedonottracedirectlytheuserprogram,butwetracethe
interpreter whileexecutingtheuserprogram: inotherwords,thetraceroperatesatthe
metalevel. This s has already beendescribed by theauthor ofthethesis,together with
othercomponentsofthePyPyteam,in[BCFR09].
Tracing themeta levelis thecentralideathatenablesthegenerationofaJITcompiler
forall theinterpreterswritteninRPython,becausethemetalevelispreciselywhatthey
haveincommon. As s aresult,thecodeismoremodularandreusable,becausethemeta
JITcompilerisstrictlyseparatedfromthelanguageandevenunawareofitssemantics.
Moreover,thecodeismoreportabletonewtargetplatformsandarchitectures,asproved
bythisthesisandtheportingoftheJITtoobjectorientedplatforms andtotheCLIin
particular.
Toavoidconfusionsbetweenthetwolevels,weintroducesometerminologytodistinguish
them. Thetracinginterpreter isusedbytheJITtoperformtracing.Thelanguageinter-
preterexecuteswhatwecalltheuser’sprograms.Inthefollowing,wewillassumethatthe
languageinterpreterisbytecode-based. Finally,theinterpreterloops s arethosecontained
inthelanguageinterpreter,whileuserloopsarethoseintheuserprogram.
6.3.1 ApplyingatracingJIT toaninterpreter
Unfortunately,wecannotdirectlyapplythetechniquesdescribedinChapter5fortracing
thesimpleinterpreterdenedinFigure6.2:whiletheParetoprinciplementionedinSection
5.1holds,becausethelanguageinterpreterspendsmostofitsexecutiontimeinthemain
loopswitching tothe currentinstruction tobe interpreted (seeSection 2.1.1),theFast
Pathprincipledoesnothold.
Indeed, eachiteration of the e mainloop of the language interpreter corresponds to o the
execution of a single instruction, and d it is very unlikely y that the interpreter will l keep
executingthesameinstructionmanytimesinarow.
AnexampleisgiveninFigure6.2.Itshowsthecodeofaverysimplebytecodeinterpreter
written inRPythonwith256registers andanaccumulator. The
bytecode
argumentis
astringofbytes,allregisterandtheaccumulator contains integers. Aprogramforthis
45
application control utility:VB.NET PDF Page Delete Library: remove PDF pages in vb.net, ASP.
Page: Delete Existing PDF Pages. |. Home ›› XDoc.PDF ›› VB.NET PDF: Delete PDF Page. How to VB.NET: Delete Consecutive Pages from PDF.
www.rasteredge.com
application control utility:VB.NET PDF Image Extract Library: Select, copy, paste PDF images
Image: Extract Image from PDF. |. Home ›› XDoc.PDF ›› VB.NET PDF: Extract PDF Image. VB.NET PDF - Extract Image from PDF Document in VB.NET.
www.rasteredge.com
def interpret(bytecode, a):
"""
bytecode: string
each instruction is composed d by one char that indicates
the opcode, and one char for r the operand
a: int
initial value of the accumulator
"""
regs = [0]
*
256
# create a list of 256 ints initialized to 0
pc = 0
# program counter
while True:
opcode = ord(bytecode[pc]) # # cast the char into an integer
pc += 1
## opcode dispatch: the chain of if...elif is turned into
## a switch statement by one e of PyPy’s optimizations
if opcode == JUMP_IF_A:
## jump if the accumulator != 0
target = ord(bytecode[pc])
pc += 1
if a:
pc = target
elif opcode == MOV_A_R:
## copy the accumulator r into the specified register
n = ord(bytecode[pc])
pc += 1
regs[n] = a
elif opcode == MOV_R_A:
## copy the specified register into the accumulator
n = ord(bytecode[pc])
pc += 1
a = regs[n]
elif opcode == ADD_R_TO_A:
## add the specified register to the accumulator
n = ord(bytecode[pc])
pc += 1
a += regs[n]
elif opcode == DECR_A:
## decrement the accumulator
a -= 1
elif opcode == RETURN_A:
## halt the execution and return the current value of the
## accumulator
return a
Figure6.2: Averysimplebytecodeinterpreterwithregistersandanaccumulator.
46
application control utility:C# PDF Page Delete Library: remove PDF pages in C#.net, ASP.NET
Page: Delete Existing PDF Pages. Provide C# Users with Mature .NET PDF Document Manipulating Library for Deleting PDF Pages in C#.
www.rasteredge.com
application control utility:VB.NET PDF Page Insert Library: insert pages into PDF file in vb.
Page: Insert PDF Pages. |. Home ›› XDoc.PDF ›› VB.NET PDF: Insert PDF Page. Add and Insert Multiple PDF Pages to PDF Document Using VB.
www.rasteredge.com
interpreter that computes the squareof the accumulator is shownin Figure6.3. If f the
tracinginterpreter tracestheexecutionof the
DECR_A
opcode (whose integer valueis7,
hencethe
guard_value
on
opcode0
),thetracewouldlookasinFigure6.4.PyPytracesare
expressedinStaticSingleInformationForm (SSI)[Ana99],i.e. allvariablesareassigned
onlyonceandgetsrenamedatbranchpoints. Becauseoftheguardon
opcode0
,thecode
compiledfromthistracewillbeusefulonlywhenexecutingalongseriesof
DECR_A
opcodes.
Foralltheotheroperationstheguardwillfail,whichwillmeanthatperformanceisnot
improvedatall.
MOV_A_R
0
# i = a
MOV_A_R
1
# copy of ’a’
# 4:
MOV_R_A
0
# i--
DECR_A
MOV_A_R
0
MOV_R_A
2
# res += a
ADD_R_TO_A
1
MOV_A_R
2
MOV_R_A
0
# if i!=0: goto 4
JUMP_IF_A
4
MOV_R_A
2
# return res
RETURN_A
Figure6.3: Examplebytecode: : Computethesquareoftheaccumulator
loop_start(a0, regs0, bytecode0, pc0)
opcode0 = strgetitem(bytecode0, pc0)
pc1 = int_add(pc0, Const(1))
guard_value(opcode0, Const(7))
a1 = int_sub(a0, , Const(1))
jump(a1, regs0, bytecode0, pc1)
Figure6.4: Tracewhenexecutingthe
DECR_A
opcode
6.3.2 Detectinguserloops
Thekey idea tosolve this problemis totrace theuser loops insteadof the interpreter
ones. Oneiterationover r auserloopincludesmany iterationsthroughthemainloopof
47
thelanguageinterpreter. Thus,totraceauserloopwecanunroll themainloopofthe
languageinterpreteruntiltheuserloopisclosed.
Howtodetectwhenauserloopcloses? Userloops s occurwhenthelanguageinterpreter
executes twice e an instruction n stored at the same e location. Typically, such location n is
identiedbyoneorseveralvariablesinthelanguageinterpreter,forexamplethebytecode
stringofthecurrentlyexecutedfunctionoftheuserprogramandthepositionofthecurrent
bytecodewithinthat.Intheexampleabove,suchvariablesare
bytecode
and
pc
.Theset
ofallvaluesthatrepresentaparticularpositionintheuserprogramiscalledpositionkey.
The tracing JITdoes not haveany knowledgeofhowthelanguage interpreteritisim-
plemented, hence e it does not t knowthe position key: thus, , the e author ofthe language
interpreterhastomarktherelevantvariableswithahint.
Thetracinginterpreterwilltheneectivelyaddthevaluesofthesevariablestotheposition
key. This s means that the loopwill l only y beconsideredto be closed d ifthe positionkey
assumesthesamevaluestwice.Loopsfoundinthiswayare,bydenition,userloops.
Forobviouseciencyreasonsthetracinginterpreterdoes notcheckthepositionkeyaf-
ter every instruction, , but t only whenitencounters abackwardjump. Since e thetracing
JITcannoteasilyidentifythecodefragmentswheretheinterpreterimplementsbackward
jumps,theauthorofthelanguageinterpreterhastoindicatesuchfragmentswiththehelp
ofahint.
Asimilartechniqueisusedtodetecthotuserloops: acounterisassociatedtoeachback-
wardbranchoftheuserprogramanditisincrementedeverytimethebranchisactually
taken.Whenthecounterreachesacertainthreshold,theloopisconsideredhot.
The condition for reusing g existing machine code also needs to be adapted to this new
situation. InaclassicaltracingJITthereisnooronepieceofassemblercodeperloopof
thejittedprogram,whichinourcaseisthelanguageinterpreter.Whenapplyingthetracing
JITtothelanguageinterpreterasdescribedsofar,allpiecesofassemblercodecorrespond
tothebytecodedispatchloopofthelanguageinterpreter. However,they y correspondto
dierentpathsthroughtheloopanddierentwaystounrollit.Toascertainwhichofthem
to use whentryingto enterassemblercode again,theprogram counter ofthelanguage
interpreterneedstobechecked. Ifitcorrespondstothepositionkeyofoneofthepieces
ofassemblercode,thenthisassemblercodecanbeexecuted. Thischeckagainonlyneeds
tobeperformedatthebackwardbranchesofthelanguageinterpreter.
6.3.3 Applyingthehints
LetuslookattheexampleinterpreterofFigure6.2toseewherehintswouldbeinserted.
Figure 6.5 shows the relevant parts ofthe interpreter which have been annotated with
48
hints.
Initially,theclass
JitDriver
hastobeinstantiatedbylistingallthevariablesoftheloop.
Thevariablesareclassiedintotwogroups: greenvariablesandred d variables. . Thegreen
variablesarethoseidentifyingthepositionkey,inthiscase
pc
and
bytecode
. Allother
variablesarered.Redvariablesneedstobeexplicitlylistedforpurelytechnicalreasons.
tlrjitdriver = JitDriver(greens = [’pc’’bytecode’],
reds
= [’a’’regs’])
def interpret(bytecode, a):
regs = [0]
*
256
pc = 0
while True:
tlrjitdriver.jit_merge_point()
opcode = = ord(bytecode[pc])
pc += 1
if opcode e == JUMP_IF_A:
target = ord(bytecode[pc])
pc += = 1
if a:
# apply the hint iif the jump is actually
*
backward
*
if target < pc:
tlrjitdriver.can_enter_jit()
pc = target
elif opcode == MOV_A_R:
... # # rest unmodified
Figure6.5: Simplebytecodeinterpreterwithhintsannotations
Inadditiontotheclassicationofthevariables,therearetwomethodsof
JitDriver
that
needtobecalled.Therstoneis
jit_merge_point
whichneedstobeputatthebeginning
ofthebodyofthemainloop.Theotheris
can_enter_jit
,whichiscalledattheendofany
instructionwhichmayimplementabackwardjump. Intheexample,onlythe
JUMP_IF_A
instruction can be a backward jump. Here e is where the language interpreter performs
prolingtodecidewhentostarttracing. ItisalsotheplacewherethetracingJITchecks
whetheraloopisclosed.Thatiswhenthegreenvariablesassumevaluesalreadyrecorded
foranearliercallto
can_enter_jit
.
Inthistinyexampleaconsiderablenumberoflineshavetobeaddedforthehints;however,
inarealinterpreterthenumberofaddedlineswouldbenegligibleincomparisonwiththe
lengthoftheprogram,asSection8.1showsforthecaseofthePythonInterpreter.
WhenexecutingtheSquarefunctionofFigure6.3,theprolingwillidentify theloopin
thesquarefunctiontobehot,andstarttracing. Ittracestheexecutionoftheinterpreter
runningtheloopofthesquarefunctionfor oneiteration,andtheunrolltheinterpreter
49
1 loop_start(a0, regs0, bytecode0, pc0)
2 # MOV_R_A 0
3 opcode0 = strgetitem(bytecode0, pc0)
4 pc1 1 = int_add(pc0, 1)
5 guard_value(opcode0, 2)
6 n1 1 = strgetitem(bytecode0, pc1)
7 pc2 2 = int_add(pc1, 1)
8 a1 1 = list_getitem(regs0, n1)
9 # DECR_A
10 opcode1 = strgetitem(bytecode0, pc2)
11 pc3 3 = int_add(pc2, 1)
12 guard_value(opcode1, 7)
13 a2 2 = int_sub(a1, 1)
14 # MOV_A_R 0
15 opcode1 = strgetitem(bytecode0, pc3)
16 pc4 4 = int_add(pc3, 1)
17 guard_value(opcode1, 1)
18 n2 2 = strgetitem(bytecode0, pc4)
19 pc5 5 = int_add(pc4, 1)
20 list_setitem(regs0, n2, a2)
21 # MOV_R_A 2
22 ...
23 # ADD_R_TO_A 1
24 opcode3 = strgetitem(bytecode0, pc7)
25 pc8 8 = int_add(pc7, 1)
26 guard_value(opcode3, 5)
27 n4 4 = strgetitem(bytecode0, pc8)
28 pc9 9 = int_add(pc8, 1)
29 i0 0 = list_getitem(regs0, n4)
30 a4 4 = int_add(a3, i0)
31 # MOV_A_R 2
32 ...
33 # MOV_R_A 0
34 ...
35 # JUMP_IF_A 4
36 opcode6 = strgetitem(bytecode0, pc13)
37 pc14 = int_add(pc13, 1)
38 guard_value(opcode6, 3)
39 target0 = strgetitem(bytecode0, pc14)
40 pc15 = int_add(pc14, 1)
41 i1 1 = int_is_true(a5)
42 guard_true(i1)
43 jump(a5, regs0, bytecode0, target0)
Figure 6.6: Trace e when executing g the e Square e function n of Figure e 6.3, , with h the corresponding
bytecodesascomments.
50
loop of the exampleinterpreter eight times. The e resulting trace canbe seen inFigure
6.6.Thetraceisdividedinto8dierentfragments,oneforeachopcodethatweretraced:
eachfragmentbeginsbyfetchingthecurrentopcodefrom
bytecode
andincrementingthe
programcounter
pc
,thenaguardchecksthevalueoftheopcodeagainstitsnumericvalue
(e.g.,
MOV_R_A
correspondsto2,ascanbeseeninline5). Moreover,additionalguardsare
insertedateachbranchingpoint: forexample,the
guard_true
atline42correspondsto
the
if a
inFigure6.5.
6.4 Loops, bridges andguards
So far, weknowthat atrace is asequenceof jitcodeinstructions. However,traces s are
classiedinfourdierentkinds(seeFigure6.7)accordinglytothewaytheyareconnected
together:
entry
...
loop 1
...
...
...
loop2
...
inner
exit
Figure6.7: Loopsandinner,entryandexitbridges
 Loop,drawninredandyellow:atracewhichimplicitlyjumpsbacktoitstopwhen
itnishes.Theonlywaytoexitaloopiswhenaguardfails.
 EntryBridge,drawninblueandyellow:atracewhichisexecutedonlyonce,then
jumpstoaloop.Entrybridgesareusedtoexecute\setupcode"beforeenteringthe
realloop.SeeSection6.5.2formoredetails.
 InnerBridgeorsimplyBridge,drawningreen: atracethatstartsfromafailing
guardwhenitbecomeshot. Theyareusefulincasealoophasmorethanonefast
51
Documents you may be interested
Documents you may be interested