Knowledge (XXG)

Thunk

Source 📝

105:, that calculates the value of the argument. The address and environment 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. 724:. 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. 980:
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.… hen a procedure declaration is being compiled, the translator, again by observing syntax, knows what kind of address to expect from a
100:
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
934:
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
674:
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
670:
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
153:
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. The adoption of anonymous functions into other programming languages has made this capability widely
766:). 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. The only case of 32-bit code calling 64-bit code is in the WoW64's thunking of Windows APIs to 32-bit. 797:
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.
49:
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 in
683:
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.
790:. 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. 708:
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 different
1179:
Proceedings of the 1996 ACM SIGPLAN Conference on Object-Oriented Programming Systems, Languages & Applications, OOPSLA 1996, San Jose, California, USA, October 6-10, 1996
659:
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's
692:
Routines for computations such as integration need to calculate an expression at multiple points. Call by name was used for this purpose in languages that didn't support
89:. 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 " 134:, have relied heavily on thunks, with the added feature that the thunks save their initial result so that they can avoid recalculating it; this is known as 130:
programming languages in which some variant of call-by-name is the standard evaluation strategy. Compilers for these languages, such as the
1182: 782:. With overlays, a developer divides a program's code into segments that can be loaded and unloaded independently, and identifies the 1448: 1371: 1340: 1286: 1256: 1190: 959: 1171: 651:
on an object of that type, via a reference that has the same type. Class C will have an additional dispatch table, used to call
1502: 1497: 1274:
Programmer's Guide to Microsoft Windows 95: Key Topics on Programming for Windows from the Microsoft Windows Development Team
911: 693: 314: 93:", calculates all of the arguments before the call and then passes the resulting values to the subroutine. In the rival " 834: 812: 306: 935:
advance with a little compile-time thought In other words, it had 'already been thought of'; thus it was christened a
829: 818: 664: 302: 671:
dispatch tables must then retrieve the offset and use it to adjust the instance address before calling the method.
51: 1135: 755:
memory addressing. As customers have migrated from one platform to another, thunks have been essential to support
1361:
Windows Assembly Language and Systems Programming - 16- and 32-Bit Low-Level Programming for the PC and Windows
131: 1221: 145:
Functional programming languages have also allowed programmers to explicitly generate thunks. This is done in
228:// 'thunk' is a function that takes no arguments and, when invoked, performs a potentially expensive 62: 949: 886: 123: 28: 1475: 876: 779: 1439:. The Morgan Kaufmann Series in Software Engineering and Programming (1 ed.). San Francisco, USA: 1364: 1333: 891: 871: 697: 310: 38: 73:
compilers, which required special analysis (thought) to determine what type of routine to generate.
1477: 1462: 139: 86: 55: 1387: 317:
might be called via any of several interfaces. The following code illustrates such a situation in
1302: 1071: 1022: 914:. The environment passed for the thunk is that of the expression, not that of the called routine. 866: 844: 709: 150: 1406: 231:// operation (computing a square root, in this example) and/or causes some side-effect to occur 1454: 1444: 1367: 1336: 1282: 1252: 1186: 1063: 1042:"Thunks: a way of compiling procedure statements with some comments on procedure declarations" 1014: 955: 740: 119: 97:" approach, the subroutine receives the unevaluated argument expression and must evaluate it. 1053: 1004: 881: 1440: 1314: 1278: 1248: 945: 941: 794: 756: 721: 127: 786:
into each segment. A segment that calls into another segment must do so indirectly via a
1330:
Writing Windows VxDs and Device Drivers - Programming Secrets for Virtual Device Drivers
1196: 1430: 775: 744: 644: 643:
In this example, the code generated for each of the classes A, B and C will include a
1491: 717: 90: 1075: 1026: 1356: 787: 94: 1272: 783: 146: 135: 762:
The transition from 32-bit to 64-bit code on x86 also uses a form of thunking (
46: 1067: 1018: 1458: 993:"Comments on the Implementation of Recursive Procedures and Blocks in ALGOL" 713: 1058: 1041: 1009: 992: 667:
for the entire C object, rather than the inherited B part of that object.
824: 778:
hardware, thunks can implement a limited form of virtual memory known as
118:
Although the software industry largely standardized on call-by-value and
82: 70: 17: 1332:(2nd printing, 2nd ed.). Lawrence, Kansas, USA: R&D Books / 1150: 854: 839: 752: 748: 732: 728: 727:
Much of the literature on interoperability thunks relates to various
939:, which is 'the past tense of "think" at two in the morning'. See: 318: 849: 763: 655:
on an object of type C via a reference of type B. The expression
267:// the thunk can then be passed around without being evaluated... 736: 1434: 688:
Numerical calculations requiring evaluations at multiple points
157:
The following is a simple demonstration in JavaScript (ES6):
1480: 122:
evaluation, active study of call-by-name continued in the
1388:"Why can't you thunk between 32-bit and 64-bit Windows?" 1363:(2nd ed.). Lawrence, Kansas, USA: R&D Books / 1052:(1). Association for Computing Machinery (ACM): 55–58. 1003:(1). Association for Computing Machinery (ACM): 65–69. 680: 660: 656: 652: 648: 1172:"The Direct Cost of Virtual Function Calls in C++" 85:research saw broad experimentation with different 1181:. 11th OOPSLA 1996: San Jose, California, USA. 1222:"Thunking: Using 16-Bit Libraries in OS/2 2.0" 1106:Parallel and Concurrent Programming in Haskell 126:community. This research produced a series of 69:. It refers to the original use of thunks in 8: 1277:(1st ed.). Redmond, Washington, USA: 1247:(2nd ed.). Redmond, Washington, USA: 1057: 1008: 149:by wrapping an argument expression in an 927: 903: 313:, leading to situations where the same 162:// 'hypot' is a binary function 101:generate a helper subroutine, called a 1310: 1300: 7: 1170:Driesen, Karel; Hölzle, Urs (1996). 910:A thunk is an early limited type of 61:The term originated as a whimsical 25: 759:written for the older platforms. 679:along with C's implementation of 1134:Stroustrup, Bjarne (Fall 1989). 1465:from the original on 2012-12-05 1091:Programming Language Pragmatics 774:On systems that lack automatic 1136:"Multiple Inheritance for C++" 1040:Ingerman, P. Z. (1961-01-01). 1: 1407:"Virtual Memory For 640K DOS" 1405:Bright, Walter (1990-07-01). 747:, and to the transition from 1119:Queinnec, Christian (2003). 835:Platform Invocation Services 813:DOS Protected Mode Interface 770:Overlays and dynamic linking 720:, or at least one runs in a 1245:Inside Microsoft Windows 95 951:The New Hacker's Dictionary 830:Microsoft Layer for Unicode 819:DOS Protected Mode Services 663:implementation receives an 311:inherit multiple interfaces 303:object-oriented programming 297:Object-oriented programming 1519: 1220:Calcote, John (May 1995). 991:E. T. Irons (1961-01-01). 954:. MIT Press. p. 445. 26: 1046:Communications of the ACM 997:Communications of the ACM 675:compiler can generate an 647:that can be used to call 793:Similarly, systems that 323: 159: 132:Glasgow Haskell Compiler 52:compiler code generation 1226:OS/2 Developer Magazine 1089:Scott, Michael (2009). 305:platforms that allow a 1503:Functional programming 1328:Hazzah, Karen (1997). 1104:Marlow, Simon (2013). 887:Trampoline (computing) 124:functional programming 114:Functional programming 29:Thunk (disambiguation) 1498:Computing terminology 1243:King, Adrian (1994). 1059:10.1145/366062.366084 1010:10.1145/366062.366084 877:Remote procedure call 731:platforms, including 301:Thunks are useful in 87:evaluation strategies 1365:Miller Freeman, Inc. 1334:Miller Freeman, Inc. 1121:Lisp in Small Pieces 892:Reducible expression 872:Futures and promises 698:procedure parameters 270:doSomethingWithThunk 39:computer programming 27:For other uses, see 1436:Linkers and Loaders 712:, run in different 710:calling conventions 81:The early years of 56:modular programming 1411:Dr. Dobb's Journal 867:Anonymous function 845:Windows on Windows 807:Thunk technologies 282:// ...or evaluated 151:anonymous function 34:Type of subroutine 1392:The Old New Thing 1143:Computing Systems 120:call-by-reference 65:form of the verb 16:(Redirected from 1510: 1482: 1473: 1471: 1470: 1427: 1421: 1420: 1418: 1417: 1402: 1396: 1395: 1384: 1378: 1377: 1353: 1347: 1346: 1325: 1319: 1318: 1312: 1308: 1306: 1298: 1296: 1295: 1269: 1263: 1262: 1240: 1234: 1233: 1217: 1211: 1210: 1208: 1207: 1201: 1195:. Archived from 1176: 1167: 1161: 1160: 1158: 1157: 1140: 1131: 1125: 1124: 1116: 1110: 1109: 1101: 1095: 1094: 1086: 1080: 1079: 1061: 1037: 1031: 1030: 1012: 988: 982: 978: 972: 971: 969: 968: 946:Raymond, Eric S. 942:Raymond, Eric S. 932: 915: 908: 882:Shim (computing) 861:Related concepts 795:dynamically link 704:Interoperability 682: 665:instance address 662: 658: 654: 650: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 292: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 199: 196: 193: 190: 187: 184: 181: 178: 175: 172: 169: 166: 163: 21: 1518: 1517: 1513: 1512: 1511: 1509: 1508: 1507: 1488: 1487: 1486: 1485: 1468: 1466: 1451: 1441:Morgan Kaufmann 1431:Levine, John R. 1429: 1428: 1424: 1415: 1413: 1404: 1403: 1399: 1386: 1385: 1381: 1374: 1359:(August 1997). 1355: 1354: 1350: 1343: 1327: 1326: 1322: 1309: 1299: 1293: 1291: 1289: 1279:Microsoft Press 1271: 1270: 1266: 1259: 1249:Microsoft Press 1242: 1241: 1237: 1219: 1218: 1214: 1205: 1203: 1199: 1193: 1174: 1169: 1168: 1164: 1155: 1153: 1138: 1133: 1132: 1128: 1118: 1117: 1113: 1103: 1102: 1098: 1088: 1087: 1083: 1039: 1038: 1034: 990: 989: 985: 979: 975: 966: 964: 962: 940: 933: 929: 924: 919: 918: 909: 905: 900: 863: 809: 804: 772: 757:legacy software 722:virtual machine 706: 690: 641: 640: 637: 634: 631: 628: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 406: 403: 400: 397: 394: 391: 388: 385: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 299: 294: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 218: 215: 212: 209: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 164: 161: 128:lazy evaluation 116: 111: 79: 35: 32: 23: 22: 15: 12: 11: 5: 1516: 1514: 1506: 1505: 1500: 1490: 1489: 1484: 1483: 1449: 1422: 1397: 1379: 1372: 1348: 1341: 1320: 1287: 1281:. 1995-07-01. 1264: 1257: 1235: 1212: 1191: 1162: 1126: 1123:. p. 176. 1111: 1096: 1093:. p. 395. 1081: 1032: 983: 973: 960: 926: 925: 923: 920: 917: 916: 902: 901: 899: 896: 895: 894: 889: 884: 879: 874: 869: 862: 859: 858: 857: 852: 847: 842: 837: 832: 827: 822: 816: 808: 805: 803: 800: 776:virtual memory 771: 768: 718:address spaces 705: 702: 689: 686: 677:adjustor thunk 657:b->Access() 645:dispatch table 324: 298: 295: 160: 115: 112: 110: 107: 78: 75: 33: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1515: 1504: 1501: 1499: 1496: 1495: 1493: 1481: 1478: 1476: 1464: 1460: 1456: 1452: 1450:1-55860-496-0 1446: 1442: 1438: 1437: 1432: 1426: 1423: 1412: 1408: 1401: 1398: 1394:. 2008-10-20. 1393: 1389: 1383: 1380: 1375: 1373:0-87930-474-X 1369: 1366: 1362: 1358: 1357:Kauler, Barry 1352: 1349: 1344: 1342:0-87930-438-3 1338: 1335: 1331: 1324: 1321: 1316: 1304: 1290: 1288:1-55615-834-3 1284: 1280: 1276: 1275: 1268: 1265: 1260: 1258:1-55615-626-X 1254: 1250: 1246: 1239: 1236: 1231: 1227: 1223: 1216: 1213: 1202:on 2019-12-29 1198: 1194: 1192:0-89791-788-X 1188: 1184: 1180: 1173: 1166: 1163: 1152: 1148: 1144: 1137: 1130: 1127: 1122: 1115: 1112: 1108:. p. 10. 1107: 1100: 1097: 1092: 1085: 1082: 1077: 1073: 1069: 1065: 1060: 1055: 1051: 1047: 1043: 1036: 1033: 1028: 1024: 1020: 1016: 1011: 1006: 1002: 998: 994: 987: 984: 977: 974: 963: 961:9780262680929 957: 953: 952: 947: 943: 938: 931: 928: 921: 913: 907: 904: 897: 893: 890: 888: 885: 883: 880: 878: 875: 873: 870: 868: 865: 864: 860: 856: 853: 851: 848: 846: 843: 841: 838: 836: 833: 831: 828: 826: 823: 820: 817: 814: 811: 810: 806: 801: 799: 796: 791: 789: 785: 781: 777: 769: 767: 765: 760: 758: 754: 750: 746: 742: 738: 734: 730: 725: 723: 719: 715: 711: 703: 701: 699: 695: 687: 685: 678: 672: 668: 666: 646: 524:better_value_ 506:better_value_ 322: 320: 316: 312: 308: 304: 296: 158: 155: 152: 148: 143: 141: 137: 133: 129: 125: 121: 113: 108: 106: 104: 98: 96: 92: 91:call by value 88: 84: 76: 74: 72: 68: 64: 59: 57: 53: 48: 44: 40: 30: 19: 1467:. Retrieved 1435: 1425: 1414:. Retrieved 1410: 1400: 1391: 1382: 1360: 1351: 1329: 1323: 1292:. Retrieved 1273: 1267: 1244: 1238: 1229: 1225: 1215: 1204:. Retrieved 1197:the original 1178: 1165: 1154:. Retrieved 1146: 1142: 1129: 1120: 1114: 1105: 1099: 1090: 1084: 1049: 1045: 1035: 1000: 996: 986: 976: 965:. Retrieved 950: 936: 930: 906: 792: 788:branch table 784:entry points 773: 761: 726: 707: 691: 676: 673: 669: 642: 300: 156: 144: 140:call-by-need 117: 109:Applications 102: 99: 95:call by name 80: 66: 60: 42: 36: 1311:|work= 1232:(3): 48–56. 154:available. 147:source code 136:memoization 1492:Categories 1469:2020-01-12 1416:2014-03-06 1294:2016-05-26 1206:2011-02-24 1156:2014-08-04 967:2015-05-25 922:References 77:Background 47:subroutine 1433:(2000) . 1313:ignored ( 1303:cite book 1068:0001-0782 1019:0001-0782 714:CPU modes 63:irregular 1479:Errata: 1463:Archived 1459:42413382 1076:14646332 1027:14646332 944:(1996). 825:J/Direct 802:See also 780:overlays 694:closures 497:override 291:// === 5 83:compiler 71:ALGOL 60 18:Thunking 981:thunk." 948:(ed.). 912:closure 741:Windows 515:private 434:private 404:virtual 371:private 341:virtual 1474:Code: 1457:  1447:  1370:  1339:  1285:  1255:  1189:  1151:USENIX 1074:  1066:  1025:  1017:  958:  855:libffi 840:Win32s 821:(DPMS) 815:(DPMI) 753:32-bit 749:16-bit 733:MS-DOS 729:Wintel 681:Access 661:Access 653:Access 649:Access 632:some_c 617:some_c 608:some_b 593:some_b 587:// ... 566:Access 557:return 503:return 488:Access 479:public 470:public 461:public 443:value_ 425:value_ 422:return 410:Access 398:public 380:value_ 362:value_ 359:return 347:Access 335:public 315:method 1200:(PDF) 1175:(PDF) 1149:(4). 1139:(PDF) 1072:S2CID 1023:S2CID 937:thunk 898:Notes 850:WoW64 764:WoW64 629:& 605:& 563:-> 494:const 452:class 416:const 389:class 353:const 326:class 307:class 285:thunk 276:thunk 249:hypot 246:=> 237:thunk 234:const 189:=> 168:hypot 165:const 103:thunk 67:think 45:is a 43:thunk 1455:OCLC 1445:ISBN 1368:ISBN 1337:ISBN 1315:help 1283:ISBN 1253:ISBN 1187:ISBN 1064:ISSN 1015:ISSN 956:ISBN 745:.NET 743:and 737:OS/2 578:main 198:sqrt 192:Math 54:and 41:, a 1183:ACM 1054:doi 1005:doi 751:to 716:or 696:or 623:use 599:use 575:int 569:(); 536:use 533:int 521:int 485:int 440:int 407:int 377:int 344:int 319:C++ 309:to 288:(); 138:or 37:In 1494:: 1461:. 1453:. 1443:. 1409:. 1390:. 1307:: 1305:}} 1301:{{ 1251:. 1228:. 1224:. 1185:. 1177:. 1145:. 1141:. 1070:. 1062:. 1048:. 1044:. 1021:. 1013:. 999:. 995:. 739:, 735:, 700:. 635:); 611:); 581:() 530:}; 491:() 449:}; 413:() 386:}; 350:() 321:. 279:); 264:); 243:() 225:); 142:. 58:. 1472:. 1419:. 1376:. 1345:. 1317:) 1297:. 1261:. 1230:7 1209:. 1159:. 1147:1 1078:. 1056:: 1050:4 1029:. 1007:: 1001:4 970:. 638:} 626:( 620:; 614:C 602:( 596:; 590:B 584:{ 572:} 560:b 554:{ 551:) 548:b 545:* 542:B 539:( 527:; 518:: 512:} 509:; 500:{ 482:: 476:{ 473:B 467:, 464:A 458:: 455:C 446:; 437:: 431:} 428:; 419:{ 401:: 395:{ 392:B 383:; 374:: 368:} 365:; 356:{ 338:: 332:{ 329:A 273:( 261:4 258:, 255:3 252:( 240:= 222:y 219:* 216:y 213:+ 210:x 207:* 204:x 201:( 195:. 186:) 183:y 180:, 177:x 174:( 171:= 31:. 20:)

Index

Thunking
Thunk (disambiguation)
computer programming
subroutine
compiler code generation
modular programming
irregular
ALGOL 60
compiler
evaluation strategies
call by value
call by name
call-by-reference
functional programming
lazy evaluation
Glasgow Haskell Compiler
memoization
call-by-need
source code
anonymous function
object-oriented programming
class
inherit multiple interfaces
method
C++
dispatch table
instance address
closures
procedure parameters
calling conventions

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.