translater

honggarae 15/03/2022 945

Howitworks

Compilationisthetranslationprocessfromsourcecode(usuallyhigh-levellanguage)toobjectcode(usuallylow-levellanguageormachinelanguage)thatcanbedirectlyexecutedbyacomputerorvirtualmachine.However,therearealsocompilersfromlow-levellanguages​​tohigh-levellanguages.Amongthesecompilers,theonesusedtoregeneratehigh-levellanguagecodesfromlow-levellanguagecodesgeneratedbyhigh-levellanguages​​arealsocalleddecompilers.Therearealsocompilersthatgenerateanotherhigh-levellanguagefromonehigh-levellanguage,oracompilerthatgeneratesintermediatecodethatneedsfurtherprocessing(alsocalledcascade).

Atypicalcompileroutputisanobjectfilecomposedofthenameandaddressoftheentrypoint,andthemachinecodeofexternalcalls(tofunctioncallsthatarenotinthisobjectfile).Asetofobjectfilesdoesnotneedtobegeneratedbythesamecompiler,butthecompilerusedmustadoptthesameoutputformat,whichcanbelinkedtogetherandgenerateanEXEthatcanbedirectlyexecutedbytheuser.

SoonourcomputerThefilesarecompiledfiles.

Type

Thecompilercangenerateobjectcodethatisusedtoruninthesameenvironmentasthecomputerandoperatingsystem(platform)wherethecompileritselfislocated.Thistypeofcompilerisalsocalled"Native"compiler.Inaddition,thecompilercanalsogenerateobjectcodeforrunningonotherplatforms.Thistypeofcompilerisalsocalledacross-compiler.Crosscompilersareveryusefulwhengeneratingnewhardwareplatforms."Source-to-sourcecompiler"referstoacompilerthatusesahigh-levellanguageasinputandoutputisalsoahigh-levellanguage.Forexample:Automaticparallelizationcompilersoftenuseahigh-levellanguageasinput,convertthecodeinit,andannotateitwithparallelcodecomments(suchasOpenMP)oruselanguageconstructs(suchasFORTRAN'sDOALLinstruction).

Theprocessor

Thefunctionistocompletethesourceprogrambysubstitutingpredefinedprogramsegments.

Frontend

Thefrontendismainlyresponsibleforparsing(parse)theinputsourcecode,andthesyntacticanalyzerandsemanticanalyzerworktogether.Thegrammaranalyzerisresponsibleforfindingoutthe‘words’(Token)inthesourcecode,andthesemanticanalyzerassemblesthesescatteredwordsintomeaningfulexpressions,sentences,functions,etc.accordingtoapredefinedgrammar.Forexample,"a=b+c;"thefront-endsyntaxanalyzersees"a,=,b,+,c;",thesemanticanalyzerfirstassemblesthemintotheexpression"b+c"accordingtothedefinedgrammar,Andthenassembledintoasentenceof"a=b+c".Thefrontendisalsoresponsibleforsemanticchecking,suchascheckingwhetherthevariablesinvolvedintheoperationareofthesametype,andsimpleerrorhandling.Thefinalresultisoftenanabstractsyntaxtree(abstractsyntaxtree,orAST),sothatthebackendcanbefurtheroptimizedandprocessedonthisbasis.

Backend

Thecompilerbackendismainlyresponsibleforanalyzing,optimizingintermediatecode(Intermediaterepresentation)andgeneratingmachinecode(CodeGeneration).

Generallyspeaking,allcompileranalysis,optimization,andvariantscanbedividedintotwocategories:intraproceduralorinterprocedural.Obviously,theanalysisandoptimizationbetweenfunctionsaremoreaccurate,butittakeslongertocomplete.

Codeanalysis

Theobjectofcompileranalysisistheintermediatecodegeneratedandpassedbythefrontend.ModernoptimizingcompilersoftenuseseveraltypesHierarchicalintermediatecoderepresentstheprogram.Thehigh-levelintermediatecode(highlevelIR)isclosetotheformatoftheinputsourceprogram,islanguagedependent,containsmoreglobalinformation,andthestructureofthesourceprogram;middlelevelTheintermediatecode(middlelevelIR)hasnothingtodowiththeinputlanguage,andthelow-levelintermediatecode(LowlevelIR)issimilartomachinelanguage.Differentanalysis,optimizationoccursonthemostsuitablelayerofintermediatecode.

Commoncompilationanalysisincludesfunctioncalltree(calltree),controlflowgraph(Controlflowgraph),andvariabledefinition-use,use-definechain(define-use/use-defineorud/duchain),variablealiasanalysis(aliasanalysis),pointeranalysis(pointeranalysis),datadependenceanalysis(datadependenceanalysis),etc.

Theresultofprogramanalysisisaprerequisiteforcompileroptimizationandcompilertransformation.Commonoptimizationsandtransformationsinclude:inlining,deadcodeelimination,loopnormalization,loopunrolling,loopfusion,loopfission),arraypadding,etc.Thepurposeofoptimizationanddeformationistoreducethelengthofthecode,increasetheutilizationrateofmemoryandcache,andreducethefrequencyofreadingandwritingdisksandaccessingnetworkdata.Moreadvancedoptimizationcaneventurnserializedcode(serialcode)intoparallelized,multi-threadedcode(parallelized,multi-threadedcode).

Thegenerationofmachinecodeistheprocessofconvertingoptimizedandmodifiedintermediatecodeintomachineinstructions.Moderncompilersmainlyadoptthestrategyofgeneratingassemblycodeinsteadofdirectlygeneratingbinaryobjectcode.Eveninthecodegenerationstage,advancedcompilersstillneedtodoalotofanalysis,optimization,anddeformation.Forexample,howtoallocateregisters(registerallocatioin),howtoselecttheappropriatemachineinstructions(instructionselection),howtomergeafewlinesofcodeintoonesentence,andsoon.

Howtowork

First,thecompilerperformsgrammaticalanalysis,thatis,toseparatethosestrings.

Thensemanticanalysisistoclarifythemeaningofeachgrammaticalunitanalyzedbygrammaticalanalysis.

translater

Thefinalgeneratedistheobjectfile,alsoknownastheobjfile.

Afterlinkingthroughthelinker,thefinalEXEfilecanbegenerated.

Sometimesitisnecessarytolinkthetargetfilesgeneratedbymultiplefilestogeneratethefinalcode.Thisprocessiscalledcross-linking.

Compileroptimization

Thecomplexityofapplicationsisduetotheirabilitytohandlemultipleproblemsandrelateddatasets.Infact,acomplexapplicationislikemanyapplicationswithdifferentfunctions"pasted"together.Mostofthecomplexityinthesourcefilecomesfromhandlinginitializationandproblemsettingcode.Althoughthesefilesusuallyaccountforalargepartofthesourcefilesandareverydifficult,theybasicallydonotcosttheCPUexecutioncycle.

Despitetheabovesituation,mostMakefilefilesonlyhaveasetofcompileroptionstocompileallfilesintheproject.Therefore,thestandardoptimizationmethodsimplyincreasestheintensityoftheoptimizationoptions,generallyfromO2toO3.Inthisway,alotofeffortisneededtodebugtodeterminewhichfilescannotbeoptimized,andtoestablishspecialmakerulesforthesefiles.

Asimplerbutmoreeffectivemethodistoruntheoriginalcodethroughaperformanceanalyzerandgeneratealistofsourcefilesthattakeup85to95%oftheCPU.Normally,thesefilesaccountforonlyabout1%ofallfiles.Ifdevelopersimmediatelyestablishtheirownrulesforeachfileinthelist,theywillbeinamoreflexibleandeffectiveposition.Inthisway,changingtheoptimizationwillonlycauseasmallpartofthefiletoberecompiled.Furthermore,sincetimewillnotbewastedonoptimizingfunctionsthatarenottime-consuming,recompilingallfileswillgreatlyspeedup.

Comparison

Manypeopledividehigh-levelprogramminglanguages​​intotwocategories:compiledlanguages​​andliterallanguages.However,infact,mostoftheselanguages​​canbeimplementedineithercompiledorliteraltranslation,andtheclassificationactuallyreflectsthecommonimplementationofthatlanguage.(However,someliteraltranslationlanguages​​aredifficulttoimplementwithcompilers.Forexample,thoseliteraltranslationlanguages​​thatallowonlinecodechanges.)

>

Inthe1950s,IBMJohnBackusledaresearchteamtodeveloptheFORTRANlanguageanditscompiler.Butbecausepeopledidn'tknowmuchaboutcompilationtheoryatthattime,thedevelopmentworkbecamebothcomplicatedandarduous.Atthesametime,NoamChomskybeganhisresearchonthestructureofnaturallanguage.Hisdiscoveryeventuallymadethestructureofthecompilerextremelysimple,evenwithsomeautomation.Chomsky'sresearchledtotheclassificationoflanguages​​basedonthedifficultyoftheirgrammarandthealgorithmsneededtorecognizethem.JustliketheChomskyHierarchy,itincludesfourlevelsofgrammar:Type0grammar,Type1grammar,Type2grammarandType3grammar,andeachofthemisaspecialcaseoftheformer.Type2grammar(orcontext-freegrammar)provedtobethemostusefulofprogramminglanguages,andtodayitrepresentsthestandardwayofprogramminglanguagestructure.Theanalysisproblem(theeffectivealgorithmforcontext-freegrammarrecognition)wasstudiedinthe1960sand1970s,anditsolvedthisproblemfairlywell.Itisalreadyastandardpartofthecompilationprinciple.

FiniteAutomationandRegularExpressionarecloselyrelatedtocontext-freegrammar,andtheycorrespondtoChomsky'sType3grammar.ResearchonthembeganalmostatthesametimeasChomsky'sresearch,andledtothesymbolicwayofrepresentingwordsinprogramminglanguages.

Peoplewentontodeepenthemethodofgeneratingeffectivetargetcode.Theseweretheoriginalcompilers,andtheyhavebeeninuseeversince.Peopleusuallycallitoptimizationtechnique(OptimizationTechnique),butbecauseithasneverreallygottheoptimizedtargetcodeandonlyimprovesitseffectiveness,itshouldactuallybecalledCodeImprovementTechnique(CodeImprovementTechnique).).

Whentheanalysisproblembecomeseasiertounderstand,peoplespendalotofeffortindevelopingprogramstostudytheautomaticconstructionofthispartofthecompiler.TheseprogramswereoriginallycalledtheCompiler-compiler,butshouldbemorepreciselycalledtheParserGenerator,becausetheycanonlyautomaticallyhandlepartofthecompilation.ThemostfamousoftheseprogramsisYacc(YetAnotherCompiler-compiler),whichwaswrittenbySteveJohnsonin1975forUnixsystems.Similarly,theresearchoffinitestateautomatahasalsodevelopedatoolcalledScannerGenerator.Lex(developedbyMikeLeskforUnixsystemsatthesametimeasYacc)isoneofthebest.

Inthelate1970sandearly1980s,alargenumberofprojectsfocusedontheautomationofotherpartsofthecompiler,includingcodegeneration.Theseattemptshavenotachievedmuchsuccess,probablybecausetheoperationistoocomplicatedandpeopledon'tknowmuchaboutit.

Recentdevelopmentsincompilerdesigninclude:First,thecompilerincludesapplicationswithmorecomplexalgorithms,whichareusedtoinferorsimplifytheinformationintheprogram;thisisinlinewiththedevelopmentofmorecomplexprogramminglanguages.integrate.AtypicalexampleistheHindley-Milnertypecheckingunifiedalgorithmforfunctionallanguagecompilation.Second,thecompilerhasincreasinglybecomeapartofawindow-basedinteractivedevelopmentenvironment(InteractiveDevelopmentEnvironment,IDE),whichincludesaneditor,linker,debugger,andprojectmanagementprogram.TherearenotmanysuchIDEstandards,butthedevelopmentofastandardwindowenvironmenthasbecomethedirection.Ontheotherhand,despitealotofresearchinthefieldofcompilationprinciples,thebasiccompilerdesignprincipleshavenotchangedmuchinthepast20years,anditisrapidlybecomingacentrallinkincomputersciencecourses.

Inthe1990s,manyfreecompilersandcompilerdevelopmenttoolsweredevelopedaspartoftheGNUprojectorotheropensourceprojects.Thesetoolscanbeusedtocompileallcomputerprogramminglanguages.Someofthemareconsideredhigh-quality,andpeopleinterestedinmoderncompilationtheorycaneasilygettheirfreesourcecode.

About1999,SGIreleasedthesourcecodeoftheirindustrializedparalleloptimizingcompilerPro64,whichwaslaterusedasaresearchplatformbymanycompilerresearchgroupsaroundtheworld,andnameditOpen64.Open64hasagooddesignstructureandcomprehensiveanalysisandoptimization.Itisanidealplatformforadvancedresearchoncompilers.

Compilerrelatedtechnicalterms:

1.

td>

compilercompiler;compiler

2.

on-linecompiler

3.

precompiler

4.

serialcompiler

5.

system-specificcompiler

6.

InformationPresentationFacilityCompilerInformationPresentationFacilityCompiler

7.

CompilerMonitorSystem

Latest: Data Frame

Next: Housing security system