Honors Compilers Run-Time Support for Compilers Mar 21st 2002.

49
Honors Compilers Honors Compilers Run-Time Support for Run-Time Support for Compilers Compilers Mar 21st 2002 Mar 21st 2002
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    228
  • download

    1

Transcript of Honors Compilers Run-Time Support for Compilers Mar 21st 2002.

Honors CompilersHonors Compilers

Run-Time Support for Run-Time Support for CompilersCompilers

Mar 21st 2002Mar 21st 2002

Major IssuesMajor Issues

What is a compiler run-time?What is a compiler run-time?Major components of run-timeMajor components of run-time

Computational SupportComputational SupportI/O interfacingI/O interfacingStorage ManagementStorage ManagementTasking interfacesTasking interfacesException HandlingException HandlingOther operating system issuesOther operating system issues

The Compiler Run-TimeThe Compiler Run-Time

Simple operations generate direct Simple operations generate direct machine language (e.g. addition)machine language (e.g. addition)

But modern languages have many But modern languages have many high level constructshigh level constructs

Compiler generates calls to hidden Compiler generates calls to hidden routines called the run-time.routines called the run-time.

Run-time is included silently in linked Run-time is included silently in linked program (standard libraries used)program (standard libraries used)

The Compiler Run-TimeThe Compiler Run-Time

Part of the implementationPart of the implementationNot available directly to programNot available directly to program

Interface may not be documentedInterface may not be documentedSpecial calling sequences can be usedSpecial calling sequences can be usedTailored asm may be appropriateTailored asm may be appropriateSpecial operating system interfaceSpecial operating system interface

Delivered as part of the compilerDelivered as part of the compilerLicensing considerations applyLicensing considerations apply

Computational SupportComputational Support

Some simple operations may not be Some simple operations may not be supported by instruction setsupported by instruction set64-bit integer arithmetic64-bit integer arithmeticConversion of fpt to integerConversion of fpt to integerOverflow checkingOverflow checkingFloating-point (emulation needed)Floating-point (emulation needed)Long shiftsLong shiftsSquare rootSquare root

Handling Computational Handling Computational SupportSupport

Need very small high efficiency Need very small high efficiency routines, might possibly be inlinableroutines, might possibly be inlinable

Special calling sequences may be Special calling sequences may be useful (e.g. args in registers)useful (e.g. args in registers)

Assembly language may be Assembly language may be reasonable in this casereasonable in this case

Implementation is target dependentImplementation is target dependent

Language ConsiderationsLanguage Considerations

Some languages have no high level Some languages have no high level constructs, so need minimal run timeconstructs, so need minimal run timeFor example, C, everything is done with For example, C, everything is done with

explicit procedure calls.explicit procedure calls.But other languages, e.g.But other languages, e.g.

C++ (stream operations)C++ (stream operations)Ada (tasking)Ada (tasking)Java (automatic storage mgmt)Java (automatic storage mgmt)

The Run-Time in CThe Run-Time in C

Very smallVery small Usually only some computational routines.Usually only some computational routines.

Standard has many standard functions as Standard has many standard functions as part of languagepart of language These are explicit library routinesThese are explicit library routines Often part of the underlying OSOften part of the underlying OS

E.g. fopen, delete, etc.E.g. fopen, delete, etc.

Command line interface neededCommand line interface needed

The Run-Time in AdaThe Run-Time in Ada

Much more extensiveMuch more extensiveTaskingTaskingTimingTimingStorage ManagementStorage ManagementConversions (e.g. string to fpt)Conversions (e.g. string to fpt)

Requires extensive libraryRequires extensive libraryOnly partly target dependentOnly partly target dependent

Other LanguagesOther Languages

C++ nearer to C, but hasC++ nearer to C, but hasStreamsStreamsException HandlingException Handling

Java, more extensive, addsJava, more extensive, addsStorage mgmt (garbage collection)Storage mgmt (garbage collection)Standardized arithmetic (IEEE fpt)Standardized arithmetic (IEEE fpt)Interface to JBC/JVMInterface to JBC/JVM

Other Languages (cont)Other Languages (cont)

COBOL, much more extensiveCOBOL, much more extensiveDetailed complex I/O modelDetailed complex I/O model

Includes full indexed filesIncludes full indexed filesSORT subsystemSORT subsystemINSPECT REPLACINGINSPECT REPLACINGPICTURE editingPICTURE editingDisplay and packed numeric formatsDisplay and packed numeric formats

Other Languages (cont)Other Languages (cont)

Very high level languagesVery high level languagesSetl, Python, GUILE, Visual BasicSetl, Python, GUILE, Visual BasicSet operationsSet operationsGUI operationsGUI operationsHigh level operating systems interfacesHigh level operating systems interfaces

E.g. COM/DCOM interfacingE.g. COM/DCOM interfacingWeb interfacingWeb interfacing

I/O interfaceI/O interface

Language may have explicit I/O Language may have explicit I/O statements, or rely on proc interfacestatements, or rely on proc interface

In either case, have to map the In either case, have to map the language notion of I/O to op syslanguage notion of I/O to op sysEnd of line (record vs stream)End of line (record vs stream)Notion of file system (directories?)Notion of file system (directories?)Exception handlingException handlingCharacter sets (esp wide chars)Character sets (esp wide chars)

Implementing the I/O Implementing the I/O InterfaceInterface

Two partsTwo partsTarget independent codeTarget independent codeTarget dependent codeTarget dependent code

Target Independent codeTarget Independent codeSimply implements the language Simply implements the language

semanticssemanticsUsing some abstractionsUsing some abstractions

Target Dependent I/OTarget Dependent I/O

On top of operating systemOn top of operating systemMap language semantics to OS Map language semantics to OS

semantics, deal with differences as well semantics, deal with differences as well as possible.as possible.

On a bare board, have to actually On a bare board, have to actually implement basic I/Oimplement basic I/OPerhaps at the port levelPerhaps at the port levelBasically compiler includes an O/SBasically compiler includes an O/S

Storage ManagementStorage Management

Stack AllocationStack AllocationHeap AllocationHeap AllocationControlled AllocationControlled AllocationAutomatic Garbage CollectionAutomatic Garbage Collection

Stack ManagementStack Management

Stack must be allocated for each task Stack must be allocated for each task in the programin the programUsually handled by operating systemUsually handled by operating systemHave to worry about size of stackHave to worry about size of stackHow/whether to extend it as neededHow/whether to extend it as neededHow/whether to check stack overflowHow/whether to check stack overflow

May or may not be language requiredMay or may not be language required

Heap ManagementHeap Management

Basic semantics is Allocate/FreeBasic semantics is Allocate/FreeParameters for allocationParameters for allocation

Size in bytesSize in bytesAlignment (often just use max alignment as Alignment (often just use max alignment as

required for example in C by malloc)required for example in C by malloc)Parameters for freeParameters for free

Probably just address (but perhaps size and Probably just address (but perhaps size and aligmnent as well, as in Ada)aligmnent as well, as in Ada)

Heap AlgorithmsHeap Algorithms

Many algorithms availableMany algorithms availableFree listFree listMultiple free listsMultiple free listsBinary allocatorBinary allocator

Performance considerationsPerformance considerationsAverage case timeAverage case timeWorst case time (real-time critical)Worst case time (real-time critical)Fragmentation considerationsFragmentation considerations

OS Heap AllocationOS Heap Allocation

Typical O/S provides malloc/freeTypical O/S provides malloc/freeMalloc takes size in bytesMalloc takes size in bytes

Returns max aligned addressReturns max aligned addressFree takes addressFree takes addressOptimized for average performanceOptimized for average performanceOften algorithm is not documentedOften algorithm is not documented

More on malloc/freeMore on malloc/free

Can map language requirementsCan map language requirementsE.g. new in C++ or Ada maps to mallocE.g. new in C++ or Ada maps to mallocUnchecked_Deallocation to freeUnchecked_Deallocation to free

May want to allocate large blocks May want to allocate large blocks with malloc, subdivide for usewith malloc, subdivide for useBuilt in for PL/1 areasBuilt in for PL/1 areasBuilt in for Ada collections with storage Built in for Ada collections with storage

size clause given.size clause given.

Commit StrategiesCommit Strategies

Physical AllocationPhysical AllocationVirtual AllocationVirtual Allocation

Storage committed at allocate timeStorage committed at allocate timeStorage committed at use timeStorage committed at use time

Overflow checkingOverflow checkingIndication of out of memoryIndication of out of memoryDifficulties with commit on useDifficulties with commit on use

Specialized StrategiesSpecialized Strategies

Trade off fragmentation with time Trade off fragmentation with time performance.performance.

FragmentationFragmentationInternal, block allocated is larger than Internal, block allocated is larger than

needed by the allocation requestneeded by the allocation requestExternal, storage available, but not External, storage available, but not

contiguous.contiguous.

Example, Binary Storage Example, Binary Storage Allocator (Buddy System)Allocator (Buddy System)

N free lists for 2**1, 2**2, … 2**N N free lists for 2**1, 2**2, … 2**N unitsunits

Block of length 2**K is on 2**K Block of length 2**K is on 2**K boundary (last K address bits zero)boundary (last K address bits zero)

To allocate block of length MTo allocate block of length MUse next higher power of 2 (2**K)Use next higher power of 2 (2**K)If available on free list grabIf available on free list grabIf not, allocate block of 2**(K+1), splitIf not, allocate block of 2**(K+1), split

Free recombines if possibleFree recombines if possible

BinaryAllocator Performance BinaryAllocator Performance IssuesIssues

Internal FragmentationInternal FragmentationNeed 40 bytes, get 64 bytes, 24 wastedNeed 40 bytes, get 64 bytes, 24 wasted

External FragmentationExternal FragmentationMay have 2 non-contiguous 128 byte May have 2 non-contiguous 128 byte

blocks, but cannot allocate 256 bytesblocks, but cannot allocate 256 bytesPerformancePerformance

Bounded (at most K allocation steps)Bounded (at most K allocation steps)

Controlled AllocationControlled Allocation

In C++, constructors automatically In C++, constructors automatically allocate storage, destructors free allocate storage, destructors free storage.storage.

In Ada, controlled type Initialize In Ada, controlled type Initialize allocates, Finalize frees storage.allocates, Finalize frees storage.

Can be used for other than storage Can be used for other than storage issues, but 99% of time usage is for issues, but 99% of time usage is for allocate/free of memoryallocate/free of memory

Implementing Controlled Implementing Controlled StorageStorage

Compiler inserts automatic calls to Compiler inserts automatic calls to constructors and destructorsconstructors and destructors

In GNAT, you can use –gnatG to see In GNAT, you can use –gnatG to see this happening in the expanded this happening in the expanded code.code.

Constructors/destructors contain Constructors/destructors contain appropriate storage mgmt calls.appropriate storage mgmt calls.

Automatic Garbage CollectionAutomatic Garbage Collection

Allocate storage as neededAllocate storage as neededFree storage automatically when no Free storage automatically when no

longer needed.longer needed.Concept of reachabilityConcept of reachabilityCan garbage collect when non-Can garbage collect when non-

reachable.reachable.Possibly compact storagePossibly compact storage

Considerations of adjusting pointersConsiderations of adjusting pointers

Determining Blocks in UseDetermining Blocks in Use

Assume that we can tell what blocks Assume that we can tell what blocks are currently allocated.are currently allocated.

We have certainly starting pointsWe have certainly starting pointsGlobal and local variablesGlobal and local variablesRegister contentsRegister contentsThese are called “roots”These are called “roots”

Tracing Blocks in UseTracing Blocks in Use

Need to find all pointers in blocks in Need to find all pointers in blocks in use, and recursively trace other use, and recursively trace other blocks in use, following all pointers.blocks in use, following all pointers.

Two approachesTwo approachesConservativeConservativeType-AccurateType-Accurate

Conservative TracingConservative Tracing

Conservative Garbage CollectionConservative Garbage CollectionIf it looks like a pointer, assume it isIf it looks like a pointer, assume it isWill never release used storageWill never release used storageMay hold onto garbageMay hold onto garbage

Type-Accurate Garbage CollectionType-Accurate Garbage CollectionKnow where pointers areKnow where pointers areTrace only pointers, knowing typesTrace only pointers, knowing types

Further Steps in GCFurther Steps in GC

Once all blocks in use are tracedOnce all blocks in use are tracedFree all remaining blocksFree all remaining blocksPossibly compact blocks in usePossibly compact blocks in useAdjust pointers if compactionAdjust pointers if compaction

Requires type accurate tracingRequires type accurate tracingSince only pointers must be adjustedSince only pointers must be adjustedEliminates external fragmentationEliminates external fragmentation

Concerns with GCConcerns with GC

Stop the world and GCStop the world and GCNot good for a rocket launch!Not good for a rocket launch!Or even for an ATM/Web use if too slowOr even for an ATM/Web use if too slow

Parallel garbage collectionParallel garbage collectionGC as you go alongGC as you go alongHave a separate processor doing GCHave a separate processor doing GC

Requires delicate syncrhonizationRequires delicate syncrhonization

Reference CountsReference Counts

Each block has a count of number of Each block has a count of number of pointers to the block.pointers to the block.

Copying a pointer increments countCopying a pointer increments countDestroying a pointer decrements Destroying a pointer decrements

countcount If count goes to zero, free blockIf count goes to zero, free blockBut cycles can be complete garbage But cycles can be complete garbage

and never freed.and never freed.

TaskingTasking

Might be done with explicit libraryMight be done with explicit library E.g. use of POSIX threads in CE.g. use of POSIX threads in C No special compiler considerationsNo special compiler considerations Except for synchronization issuesExcept for synchronization issues

E.g. when stuff can be held in registersE.g. when stuff can be held in registersVOLATILE/ATOMIC considerationsVOLATILE/ATOMIC considerations

POSIX/Threads is almost standardPOSIX/Threads is almost standard Not perfect, some variationsNot perfect, some variations Not always completely implemented yetNot always completely implemented yet

Language Tasking ConstructsLanguage Tasking Constructs

Threads in JavaThreads in JavaTasks in AdaTasks in AdaBuilt in defined semanticsBuilt in defined semanticsMay be more or less precisely May be more or less precisely

defined with respect todefined with respect toPriority handlingPriority handlingGuaranteed performanceGuaranteed performanceDispatching issues (e.g. time slicing?)Dispatching issues (e.g. time slicing?)

Implementing TaskingImplementing Tasking

Map tasks onto OS threadsMap tasks onto OS threads1/1 (each task is a thread)1/1 (each task is a thread)Many/1 (multiple tasks to a thread)Many/1 (multiple tasks to a thread)All/1 (don’t need OS threads)All/1 (don’t need OS threads)

Cannot map tasks to processesCannot map tasks to processesBecause of address space issuesBecause of address space issues

May be difficult to get exact May be difficult to get exact semantic equivalence.semantic equivalence.

Difficulties in Implementing Difficulties in Implementing Tasking, an ExampleTasking, an Example

Ceiling priority protocolCeiling priority protocolRaise priority to ceilingRaise priority to ceilingGrab resource, do processingGrab resource, do processingLower priority to ceilingLower priority to ceiling

But does last step lose controlBut does last step lose controlIn Ada, never to equal prio taskIn Ada, never to equal prio taskIn POSIX, may well add to end of queue, In POSIX, may well add to end of queue,

thus losing control.thus losing control.

Bare Board Implementations Bare Board Implementations of Taskingof Tasking

If no operating system, then no If no operating system, then no threads to map to.threads to map to.

Basically must write an executive Basically must write an executive that provides this capabilitythat provides this capability

The compiler system ends up The compiler system ends up including a small tasking executive, a including a small tasking executive, a mini-operating system.mini-operating system.

Implementations of Exception Implementations of Exception HandlingHandling

C++, Java and Ada share same same C++, Java and Ada share same same basic model of exceptions.basic model of exceptions.Replacement semanticsReplacement semanticsRaise/Throw an exceptionRaise/Throw an exceptionAbandons current executionAbandons current executionStrips stack framesStrips stack framesTill exception is handled/caughtTill exception is handled/caught

The “setjmp/longjmp” The “setjmp/longjmp” method for exceptions.method for exceptions.

When a handler is encountered, save When a handler is encountered, save enough machine state to restore as enough machine state to restore as required (setjmp)required (setjmp)

When a throw occurs, restore most When a throw occurs, restore most recently saved state (longjmp)recently saved state (longjmp)

Requires a stack of saved statesRequires a stack of saved statesFast throw, but handlers costFast throw, but handlers cost

““Zero-cost” Exception Zero-cost” Exception HandlingHandling

A handler generates only static A handler generates only static tables (PC range and handler ID)tables (PC range and handler ID)

Throw unwinds stack framesThrow unwinds stack framesRestoring all registersRestoring all registersFind return pointsFind return points

Check return point against PC tableCheck return point against PC tableJump to handler on a matchJump to handler on a match

More on “Zero-cost” More on “Zero-cost” approachapproach

Requires tight integration with Requires tight integration with compilercompiler

Stack tracebackStack tracebackRequires traceback code to understand Requires traceback code to understand

generated codegenerated codeEither by looking at itEither by looking at itOr by reading external tablesOr by reading external tablesFor example, DWARF-2 unwind tablesFor example, DWARF-2 unwind tables

Other Operating System Other Operating System IssuesIssues

Calendar/Time interfaceCalendar/Time interfaceDistribution issues (RPC/Streams)Distribution issues (RPC/Streams) Interface to other LanguagesInterface to other Languages Interface to linkerInterface to linkerCertification IssuesCertification Issues

Calendar/Time IssuesCalendar/Time Issues

Match of semanticsMatch of semanticsTime changes, daylight savingTime changes, daylight saving

Formats of dates etc.Formats of dates etc.Use for delays, alarmsUse for delays, alarmsResolutionResolution

Special target dependent capabilitiesSpecial target dependent capabilitiesE.g. high resolution timer on NTE.g. high resolution timer on NT

Distribution IssuesDistribution Issues

CORBA interfacesCORBA interfacesAnnex E in AdaAnnex E in Ada

Requires RPC supportRequires RPC supportStream handlingStream handling

Target dependenceTarget dependenceUse of Use of

Build/synchronization/network issuesBuild/synchronization/network issuesUse of sockets as binding layerUse of sockets as binding layer

Interface To Other LanguagesInterface To Other Languages

Compatibility of Calling SequencesCompatibility of Calling SequencesCompatibility of DataCompatibility of Data

In AdaIn AdaPragma Convention on procedure sets Pragma Convention on procedure sets

appropriate calling sequenceappropriate calling sequencePragma Convention on data sets appropriate Pragma Convention on data sets appropriate

representation, e.g. columnwise arrays for representation, e.g. columnwise arrays for FortranFortran

Interface to LinkerInterface to Linker

Language may require special linker Language may require special linker capabilities forcapabilities forElaboration checking (Ada)Elaboration checking (Ada)Elaboration code (Ada, C++)Elaboration code (Ada, C++)Other consistency checksOther consistency checks

May need special linker, or add-onMay need special linker, or add-onIn GNAT, gnatbind does extra stepsIn GNAT, gnatbind does extra stepsIn C++, collec2 does extra stepsIn C++, collec2 does extra steps

Certification IssuesCertification Issues

Safety-Critical certificationSafety-Critical certification Requires complete testingRequires complete testing And documentation of processAnd documentation of process

For a run-timeFor a run-time Must provide testing materialsMust provide testing materials Certify the processCertify the process One approach: no run-time at all!One approach: no run-time at all!

Applies to OS as wellApplies to OS as well WRS new certifiable kernelWRS new certifiable kernel