Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Thunk

From Wikipedia, the free encyclopedia
(Redirected fromThunk (functional programming))
For other uses, seeThunk (disambiguation).
Look upthunk in Wiktionary, the free dictionary.
Type of subroutine

Incomputer programming, athunk is asubroutine used to inject a calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or to insert operations at the beginning or end of the other subroutine. They have many other applications incompiler code generation andmodular programming.

The term originated as a whimsicalirregular form of the verbthink. It refers to the original use of thunks inALGOL 60 compilers, which required special analysis (thought) to determine what type of routine to generate.[1][2]

Background

[edit]

The early years ofcompiler research saw broad experimentation with differentevaluation strategies. A key question was how to compile a subroutine call if the arguments can be arbitrary mathematical expressions rather than constants. One approach, known as "call by value", calculates all of the arguments before the call and then passes the resulting values to the subroutine. In the rival "call by name" approach, the subroutine receives the unevaluated argument expression and must evaluate it.

A simple implementation of "call by name" might substitute the code of an argument expression for each appearance of the corresponding parameter in the subroutine, but this can produce multiple versions of the subroutine and multiple copies of the expression code. As an improvement, the compiler can generate a helper subroutine, called athunk, that calculates the value of the argument. The address and environment[a] of this helper subroutine are then passed to the original subroutine in place of the original argument, where it can be called as many times as needed. Peter Ingerman first described thunks in reference to the ALGOL 60 programming language, which supports call-by-name evaluation.[4]

Applications

[edit]

Functional programming

[edit]

Although the software industry largely standardized on call-by-value andcall-by-reference evaluation,[5] active study of call-by-name continued in thefunctional programming community. This research produced a series oflazy evaluation programming languages in which some variant of call-by-name is the standard evaluation strategy. Compilers for these languages, such as theGlasgow Haskell Compiler, have relied heavily on thunks, with the added feature that the thunks save their initial result so that they can avoid recalculating it;[6] this is known asmemoization orcall-by-need.

Functional programming languages have also allowed programmers to explicitly generate thunks. This is done insource code by wrapping an argument expression in ananonymous function that has no parameters of its own. This prevents the expression from being evaluated until a receiving function calls the anonymous function, thereby achieving the same effect as call-by-name.[7] The adoption of anonymous functions into other programming languages has made this capability widely available.

Object-oriented programming

[edit]

Thunks are useful inobject-oriented programming platforms that allow aclass toinherit multiple interfaces, leading to situations where the samemethod might be called via any of several interfaces. The following code illustrates such a situation inC++.

classA{public:virtualintAccess()const{returnvalue_;}private:intvalue_;};classB{public:virtualintAccess()const{returnvalue_;}private:intvalue_;};classC:publicA,publicB{public:intAccess()constoverride{returnbetter_value_;}private:intbetter_value_;};intuse(B*b){returnb->Access();}intmain(){// ...Bsome_b;use(&some_b);Csome_c;use(&some_c);}

In this example, the code generated for each of the classes A, B and C will include adispatch table that can be used to callAccess on an object of that type, via a reference that has the same type. Class C will have an additional dispatch table, used to callAccess on an object of type C via a reference of type B. The expressionb->Access() will use B's own dispatch table or the additional C table, depending on the type of object b refers to. If it refers to an object of type C, the compiler must ensure that C'sAccess implementation receives aninstance address for the entire C object, rather than the inherited B part of that object.[8]

As a direct approach to this pointer adjustment problem, the compiler can include an integer offset in each dispatch table entry. This offset is the difference between the reference's address and the address required by the method implementation. The code generated for each call through these dispatch tables must then retrieve the offset and use it to adjust the instance address before calling the method.

The solution just described has problems similar to the naïve implementation of call-by-name described earlier: the compiler generates several copies of code to calculate an argument (the instance address), while also increasing the dispatch table sizes to hold the offsets. As an alternative, the compiler can generate anadjustor thunk along with C's implementation ofAccess that adjusts the instance address by the required amount and then calls the method. The thunk can appear in C's dispatch table for B, thereby eliminating the need for callers to adjust the address themselves.[9]

Interoperability

[edit]

Thunks have been widely used to provide interoperability between software modules whose routines cannot call each other directly. This may occur because the routines have differentcalling conventions, run in differentCPU modes oraddress spaces, or at least one runs in avirtual machine. A compiler (or other tool) can solve this problem by generating a thunk that automates the additional steps needed to call the target routine, whether that is transforming arguments, copying them to another location, or switching the CPU mode. A successful thunk minimizes the extra work the caller must do compared to a normal call.

Much of the literature on interoperability thunks relates to variousWintel platforms, includingMS-DOS,OS/2,[10]Windows[11][12][13][14] and.NET, and to the transition from16-bit to32-bit memory addressing. As customers have migrated from one platform to another, thunks have been essential to supportlegacy software written for the older platforms.UEFI CSM is another example to do thunk for legacyboot loaders.

The transition from 32-bit to 64-bit code on x86 also uses a form of thunking (WoW64). However, because the x86-64 address space is larger than the one available to 32-bit code, the old "generic thunk" mechanism could not be used to call 64-bit code from 32-bit code.[15] The only case of 32-bit code calling 64-bit code is in the WoW64's thunking of Windows APIs to 32-bit.

Overlays and dynamic linking

[edit]

On systems that lack automaticvirtual memory hardware, thunks can implement a limited form of virtual memory known asoverlays. With overlays, a developer divides a program's code into segments that can be loaded and unloaded independently, and identifies theentry points into each segment. A segment that calls into another segment must do so indirectly via abranch table. When a segment is in memory, its branch table entries jump into the segment. When a segment is unloaded, its entries are replaced with "reload thunks" that can reload it on demand.[16]

Similarly, systems thatdynamically link modules of a program together at run-time can use thunks to connect the modules. Each module can call the others through a table of thunks that the linker fills in when it loads the module. This way the modules can interact without prior knowledge of where they are located in memory.[17]

See also

[edit]

Thunk technologies

[edit]

Related concepts

[edit]

Notes

[edit]
  1. ^A thunk is an early limited type ofclosure. The environment passed for the thunk is that of the expression, not that of the called routine.[3]

References

[edit]
  1. ^Eric Raymond rejects "a couple of onomatopoeic myths circulating about the origin of this term" and cites the inventors of the thunk recalling that the term "was coined after they realized (in the wee hours after hours of discussion) that the type of an argument in Algol-60 could be figured out in advance with a little compile-time thought [...] In other words, it had 'already been thought of'; thus it was christened athunk, which is 'the past tense of "think" at two in the morning'. See:Raymond, Eric S. (1996).Raymond, Eric S. (ed.).The New Hacker's Dictionary. MIT Press. p. 445.ISBN 9780262680929. Retrieved2015-05-25.
  2. ^See Ingerman (1961): "The translator knows what kind of thunk to create by considering the formation of the actual parameter and the previously scanned declarations.… [W]hen a procedure declaration is being compiled, the translator, again by observing syntax, knows what kind of address to expect from a thunk."
  3. ^E. T. Irons (1961-01-01)."Comments on the Implementation of Recursive Procedures and Blocks in ALGOL".Communications of the ACM.4 (1). Association for Computing Machinery (ACM):65–69.doi:10.1145/366062.366090.ISSN 0001-0782.S2CID 42778823.
  4. ^Ingerman, P. Z. (1961-01-01)."Thunks: a way of compiling procedure statements with some comments on procedure declarations".Communications of the ACM.4 (1). Association for Computing Machinery (ACM):55–58.doi:10.1145/366062.366084.ISSN 0001-0782.S2CID 14646332.
  5. ^Scott, Michael (2009).Programming Language Pragmatics. p. 395.
  6. ^Marlow, Simon (2013).Parallel and Concurrent Programming in Haskell. p. 10.
  7. ^Queinnec, Christian (2003).Lisp in Small Pieces. p. 176.
  8. ^Stroustrup, Bjarne (Fall 1989)."Multiple Inheritance for C++"(PDF).Computing Systems.1 (4).USENIX. Retrieved2014-08-04.
  9. ^Driesen, Karel; Hölzle, Urs (1996)."The Direct Cost of Virtual Function Calls in C++"(PDF).Proceedings of the 1996 ACM SIGPLAN Conference on Object-Oriented Programming Systems, Languages & Applications, OOPSLA 1996, San Jose, California, USA, October 6-10, 1996. 11th OOPSLA 1996: San Jose, California, USA.ACM.ISBN 0-89791-788-X. Archived fromthe original(PDF) on 2019-12-29. Retrieved2011-02-24.
  10. ^Calcote, John (May 1995)."Thunking: Using 16-Bit Libraries in OS/2 2.0".OS/2 Developer Magazine.7 (3):48–56.
  11. ^King, Adrian (1994).Inside Microsoft Windows 95 (2nd ed.). Redmond, Washington, USA:Microsoft Press.ISBN 1-55615-626-X.
  12. ^Programmer's Guide to Microsoft Windows 95: Key Topics on Programming for Windows from the Microsoft Windows Development Team (1st ed.). Redmond, Washington, USA:Microsoft Press. 1995-07-01.ISBN 1-55615-834-3. Retrieved2016-05-26.{{cite book}}:|work= ignored (help)
  13. ^Hazzah, Karen (1997).Writing Windows VxDs and Device Drivers - Programming Secrets for Virtual Device Drivers (2nd printing, 2nd ed.). Lawrence, Kansas, USA: R&D Books /Miller Freeman, Inc.ISBN 0-87930-438-3.
  14. ^Kauler, Barry (August 1997).Windows Assembly Language and Systems Programming - 16- and 32-Bit Low-Level Programming for the PC and Windows (2nd ed.). Lawrence, Kansas, USA: R&D Books /Miller Freeman, Inc.ISBN 0-87930-474-X.
  15. ^"Why can't you thunk between 32-bit and 64-bit Windows?".The Old New Thing. 2008-10-20.
  16. ^Bright, Walter (1990-07-01)."Virtual Memory For 640K DOS".Dr. Dobb's Journal. Retrieved2014-03-06.
  17. ^Levine, John R. (2000) [October 1999].Linkers and Loaders. The Morgan Kaufmann Series in Software Engineering and Programming (1 ed.). San Francisco, USA:Morgan Kaufmann.ISBN 1-55860-496-0.OCLC 42413382.Archived from the original on 2012-12-05. Retrieved2020-01-12. Code:[1][2][dead link] Errata:[3]
  18. ^Julliard, Alexandre."Wine 9.0".WineHQ Gitlab. Retrieved2025-03-10.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Thunk&oldid=1317280749"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp