pawn implementer guide

Click here to load reader

Post on 21-Oct-2015

61 views

Category:

Documents

7 download

Embed Size (px)

DESCRIPTION

Pawn Implementer Guide

TRANSCRIPT

  • Pawn

    embedded scripting language

    Implementers Guide

    August 2007

    ITB CompuPhase

  • ii

    Java is a trademark of Sun Microsystems, Inc.

    Microsoft and Microsoft Windows are registered trademarks of Microsoft Corporation.

    Linux is a registered trademark of Linus Torvalds.

    CompuPhase is a registered trademark of ITB CompuPhase.

    Copyright c 19972007, ITB CompuPhase; Eerste Industriestraat 1921, 1401VLBussum, The Netherlands (Pays Bas); telephone: (+31)-(0)35 6939 261e-mail: info@compuphase.com, WWW: http://www.compuphase.com

    The documentation is licensed under the Creative Commons Attribution-ShareAlike 2.5 License. A

    summary of this license is in appendix J. For more information on this licence, visit

    http://creativecommons.org/licenses/by-sa/2.5/

    or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

    The information in this manual and the associated software are provided as is. There are no

    guarantees, explicit or implied, that the software and the manual are accurate. Requests for corrections

    and additions to the manual and the software can be directed to ITB CompuPhase at the above

    address.

    Typeset with TEX in the Computer Modern and Palatino typefaces at a base size of 11 points.

  • iii

    Table of contents

    Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1The compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The abstract machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    Using the abstract machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Calling public functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Function reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Error codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    Extension modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50Writing wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Dynamically loadable extension modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Error checking in native functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62Customizing the native function dispatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63

    Appendices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67A: Building the compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67B: Building the Abstract Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72C: Using CMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87D: Abstract Machine design and reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89E: Debugging support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108F: Code generation notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112G: Adding a garbage collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116H: Running scripts from ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124I: Running scripts with overlays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127J: License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

  • iv Table of contents

  • 1Introduction

    pawn is a simple, typeless, 32-bit extension language with a C-like syntax. Thelanguage and features are described in the companion booklet with the sub-titleThe Language. This Implementers Guide discusses how to embed the pawnscripting language in a host application.

    The pawn toolkit consists of two major parts: the compiler takes a script andconverts it to P-code (or bytecode), which is subsequently executed on an ab-stract machine (or virtual machine). pawn itself is written mostly in the Cprogramming language (there are a few files in assembler) and it has been portedto Microsoft Windows, Linux, PlayStation 2 and the XBox. When embeddingpawn in host applications that are not written in C or C++, I suggest that youuse the AMX DLLs under Microsoft Windows.

    There is a short chapter on the compiler. Most applications execute the compileras a standalone utility with the appropriate options. Even when you link thecompiler into the host program, its API is still based on options as if they werespecified on the command line.

    The abstract machine is a function library. The chapter devoted to it containsseveral examples for embedding the abstract machine in a host application, inaddition to a reference to all API functions.

    Appendices, finally, give compiling instructions for various platforms and back-ground information amongst others the debugger interface and the instructionset.

    The pawn language and toolset was designed to be an extension language forapplications as opposed to many other scripting languages that primarily aimat the command shell of the operating system. Being an extension language, thetools an libraries of the pawn toolset must be integrated with the product.

    The two main parts of the pawn toolset are the compiler and the abstract ma-chine. The compiler may either be linked into the host application, or it maybe a separate process that is launched from the host application. For perfor-mance reasons, the abstract machine is always embedded (linked-in) inside thehost application.

  • 2 Introduction

    The pawn compiler takes a series of text files containing the code for the userscript and definitions of the environment/the host application. One of the includefiles is implicit: the pawn compiler will automatically include it in any user script,but it will fail silently if that file is not present. The default name for that implicitinclude file (or prefix file) is default.inc. You can override this name witha command line option to the pawn compiler.

    For a host application, it is advised to create an implicit include file containing: all application specific constants;

    all native functions that the host application provides (or a core subset of thesenative functions);

    all overloaded operators (or a core subset of these);

    all stock functions (or a core subset of these); forward declarations of all public functions;

    declarations of public variables (if used).You will have to refer to the pawn booklet The Language for writing the dec-larations mentioned in the above list.

    The rationale behind having these declarations in an implicitly included file isthat the definitions are now always available. This avoids errors, especially inthe case of overloaded operators and public functions. If the definition of anoverloaded operator is missing, in many cases the pawn compiler will use the

  • Introduction 3

    default operator without warning. If a user makes a mistake in the declaration ofa public function, the host application will not be able to call it, or it will passthe wrong parameters. A forward declaration of the public function catches thiserror, because the incorrect public function will not match the earlier declaration.

    Apart from this implicit include file, the user can also write custom include filesand explicitly include these. In addition, a host application may supply additionalsystem include files that are not added to a project automatically and must beincluded explicitly.

    The next two chapters are on the pawn compiler and the abstract machine re-spectively. The most common set-up is the one where the compiler runs as aseparate process that is spawned from the host application.

  • 4The compiler

    The pawn compiler is currently the only translator (or parser) that implementsthe pawn language. The pawn compiler translates a text file with source code toa binary file for an abstract machine. The output file format is in appendix D. Theusage of the pawn compiler is described in the pawn booklet The Language.

    Deployment / installation

    In most operating systems, the compiler is a separate self-contained executableprogram. It can run as is, but it will look for a configuration file in the samedirectory as where the compiler is in itself, and it will locate (system) include filesin a specific directory. For the retail packages, the actual compiler is in a dynam-ically loaded library. This library is called libpawnc.so or libpawnc.dll(for Linux/Unix and pawncc.exe for Microsoft Windows respectively).

    Concretely, to set up the pawn compiler on a system: Copy the program file for the compiler (typically pawncc for Linux/Unix andpawncc.exe for Microsoft Windows) in a directory of your choice, and alsocopy the library libpawnc.so or libpawnc.dll if it exists.

    Optionally copy or create a configuration file, called pawn.cfg, in the samedirectory.

    Add a

View more