- Pocket Smalltalk

Download Report

Transcript - Pocket Smalltalk

Smalltalk Solutions ’99
W8
Pocket Smalltalk
&
Pocket Java
Andrew Brault
Eric Arseneau
Why are we all here ?
• How we got Smalltalk and Java to
– Run on a machine with around 1 MB of RAM
– Have executables that are around 40-70K in
size
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Audience
• We assume that you are relatively
comfortable with
–
–
–
–
Smalltalk
Limited Java
Basic VM concepts
Bytecode and stack execution model
• Interested in building applications for new
platforms
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Agenda
• Why ?
• Palm
– Hardware
– OS
• Pocket Smalltalk
• Break
• Pocket Java
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why Palm?
• First machine that provided a decent screen
and an application development
environment that was readily available
• Form factor was phenomenal
• Large market for shareware and shrinkwrap
• Data entry technology was interesting
• VERY cool toy !!!
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why Smalltalk ?
•
•
•
•
•
I think we can all answer that !!!
Developer friendly
Can be made portable to multiple platforms
Perfect for writing small Palm Pilot apps
Coolness factor to get Smalltalk running on
something that small !!!
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why not Squeak ?
•
•
•
•
Squeak is too big
No need for development at runtime
Trimming VM & image is very problematic
No “native” GUI support
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why Java ?
• More people interested in Java than
Smalltalk
– Sorry, but we all know this as true
• Can leverage people wanting Java to
distribute Smalltalk applications
• People don’t think it can be done
• Coolness factor to get Java running on
something that small !!!
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Palm Hardware
• 2 Mb Flash ROM
– 1.2 Mb used
•
•
•
•
512k to 2048k of RAM
16 MHz M68238 (?) processor
160x160 LCD
Touch Screen
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Palm OS
• Mac OS Like
• Applications: RAM&ROM, Single Thread
– 16-32 K Dynamic heap
– Rest is protected and slow
• Development Tools
– C, CodeWarrior, gcc, Form based, others
– Macintosh, Windows, Linux
– Emulator
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
PalmOS limitations
• Small dynamic RAM area
• Most of RAM is write protected, accessible
only through slow database APIs
• Users expect applications under 100k
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Pocket Smalltalk Agenda
•
•
•
•
•
•
•
Demo
Runtime Environment
Virtual Machine
The Development Environment
Interface to C & Palm OS
When and How will it be released ?
Future
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Demo
•
•
•
•
•
Browsers
Inspector
Constants
Packager
Simulator
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Runtime Environment
•
•
•
•
•
Virtual Machine is about 23k
Base class library is about 18k
Fully ROM-able
“Hello World” runs in 40k ROM
Minimal RAM requirements
– “Hello world” needs about 2k of RAM
– Practical applications usually need between 8k
and 20k of RAM
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Virtual Machine
•
•
•
•
About 4500 lines of C, some assembler
16-bit implementation
Direct bytecode interpreter
Global method lookup cache
(90% effective)
• User-defined primitives
• Debugging and production VMs available
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Object Memory
• Object table based
• Objects structure
– 6 byte header
• Class pointer
• Pointer to self (for garbage collector)
• Length and GC flags
– 2 bytes per instance variable
– Byte indexable objects supported
• Literal objects placed in ROM
• Object memory segments are database records
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Garbage Collection
•
•
•
•
•
•
•
Global mark-compact
Non-generational
No penalty for pointer stores
Minimal memory usage
Very fast memory allocation
Efficient for small heaps
Classes, methods and literals not scanned
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Special Objects
• nil, true, and false have fixed object
numbers
• Small integers are “immediate” values
• Characters are implemented specially
– One character object body
– 256 object headers point to the same body
• Symbol objects do not exist at runtime
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Blocks
• “Clean” blocks are ROM objects
• “Full” blocks are created at runtime
• Blocks cannot use ^ returns after their home
context has returned
– Common to all Smalltalks
• Blocks cannot access variables from their
home context if the home context has
already returned
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Limitations
• Cannot add methods or classes at runtime
• Slower than compiled C
• Initial overhead of about 30k compared to C
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
The Development Environment
• IDE written using Dolphin Smalltalk
• Cross-compiles Smalltalk code
• Executable must be transferred to an
emulator or device
• Provides the usual development tools
• IDE can simulate the runtime VM
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
A Declarative Smalltalk System
• Standard Smalltalk syntax
• The entire program can be exactly
represented in file-out format
• No pre-initialized globals or class variables
• ANSI Smalltalk standard encourages
declarative Smalltalk
• Allows easy version control and source
code management
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Extensions to Smalltalk
• Character literal syntax
– $\space, $\newline, $\145, etc.
• Optimization of ifNil: [] and related
methods
• ::= assignments
• Named constants (##constantName)
• Some extensions to support PalmOS
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Named Constants
•
•
•
•
Similar to pool dictionaries
Can only take “simple” literal values
Used for PalmOS defines and enumerations
Used to control system properties
– Object space size
– Debugging information
– Degree of optimization
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Base Class Library
• Complete yet compact class library
• Supports most standard core Smalltalk classes
–
–
–
–
Number (including floating-point)
Collection
Stream
Blocks, Models, Boolean, Behaviors, UndefinedObject,
etc.
• PalmOS-specific GUI classes
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Reflection
• Reflection semantics are very close to
traditional Smalltalk
• Metaclasses
• Subclassing from nil
• isKindOf::, respondsTo:, become:,
instVarAt:, doesNotUnderstand:, etc.
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Class and Global Variables
• Global variables are class variables of
Object
• Globals must be initialized at runtime
• No class instance variables
– Classes and metaclasses are in ROM
• No VariableBinding objects are needed
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Cross-Compiler
• Combination of incremental and global
compilation
• Allows execution at development time
• Global optimizations during packaging
– Not available in most other Smalltalks
• Possible to target multiple instruction sets
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Global Optimizations
• Replacement of Symbols by SmallIntegers
– Symbol print-strings are discarded
– Greatly reduces compiled code size
• Dead code elimination
• Inlining of methods
• No CompiledMethod or MethodDictionary
objects
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Future Optimizations
•
•
•
•
Type inferencing
Compile-time method lookup
Tail recursion elimination
Abbreviated representation for common
method patterns
– ^self
– ^instVar
– instVar := arg1
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Interfacing with C
•
•
•
•
Primitives can easily be written in C
C functions take and return type Object
No need to “protect” pointers from GC
CPointer class in Smalltalk allows access to
C structures
• Speed-critical loops can be rewritten in C
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
PalmOS Programming
• Features to support PalmOS programming
– Access to over 800 PalmOS API calls
– Database access classes
– User interface framework
• Application framework takes care of many
details of PalmOS programming
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Interfacing with PalmOS
• SYSTRAP keyword
– PalmOS API implemented via m68k TRAPs
• API calls look like normal message sends
• SYSTRAP WinDrawLine: left y: top x2:
right y2: bottom
• Pointer arguments and return values are
supported via CPointer
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Debugging
• Optional debugging information
– Class names
– Symbol print strings
• Simulator in IDE
• Runtime debugger on PalmPilot
– Shows stack trace and variables
– Limited to post mortem analysis
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Future
•
•
•
•
•
Threaded code interpreter
Use database memory for more heap
Remote debugging
Enhance simulator
Other Platforms
– Win CE
– Netscape, IE
– Game consoles
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Licensing
• An Open Source product!
• Source code of IDE and VM is available
• Currently in beta testing
– www.pocketsmalltalk.com
– www.tiac.net/user/ajb/index.html
– www.appliedthought.com
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Break
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Pocket Java
•
•
•
•
NOT a Java IDE
NOT a Java compiler
Runtime environment for Java 1.1.5
Translates compiled Java .class files into Pocket St
bytecodes
• Simple ‘Hello World’ 54k !!!
• No bytecode verifier
• NOT DONE, work in progress
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Pocket Java Agenda
•
•
•
•
•
•
Demo
Why use a Smalltalk VM ?
Development Process
Pocket Packager
How Come so Small ?
Others
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Demo
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why use a Smalltalk VM ?
• Java and Smalltalk are VERY similar
– Both are bytecode and stack based
• We want to do Smalltalk, others want Java
• Creating a hybrid results in more flexibility
• Had access to the source of a Smalltalk VM
and IDE on Palm
– Did not want to write C code !!!
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Development Process
• Develop in a Java IDE
– Visual Age for Java
– J++
• Package into Palm application
– Pocket Packager
• Run on Emulator
• Run on Device
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Pocket Packager
• Create a Code Space for Java application
• Run application
• Generate Palm application
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Code Space
• Tool written in Smalltalk to read Java class
files
• Specify top level application, reads in all
classes referenced
• Includes VM in Smalltalk to execute Java
application
• Provides a huge benefit in reducing the total
size of Java application
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Run Application
• Implemented Java VM in Smalltalk
– Used to work out and get better understanding
of Java VM
• Currently used as a poor man’s dead code
elimination
– Only translate the methods that were executed
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Generate Palm Application
•
•
•
•
File-in base code to support Java
Translate Java code to Pocket St bytecodes
File-in overrides to translated code
Run standard Pocket St Packager
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
File-in Base Code
• Provide base layer for code generation
• javaAt:, javaAt:put:
– Java is 0 based
• Manually implemented the methods Java
classes
– Object
– Character
– String
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Translate Code Space
• For each class in CodeSpace
– Generate Class
– For each method
• Generate Method
• All names mangled to fit Smalltalk style
– Replace ‘/’ ‘.’ with ‘_’
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Generate Class
• Superclass
– JavaObject if it is java/lang/Object
– Appropriate super if otherwise
• Define Class and Instance variables
• Generate class initialize method for static
variables
• Generate instance initialize method for
instance variables
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Generate Method
• Selector
– Use method name and concatenate type
signature
– public static native Class forName(String
className)
– forName_java_lang_ClassArg1_String:
• Code
– Generate Bytecodes
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Generate Bytecodes
• Translate the Java bytecodes into Pocket St
bytecodes using Bytecode Trnslator
• Use bytecode set as is, minimize changes
• Differences between Java and St bytecodes
– Many Java bytecodes support basic types
– More control flow type of bytecodes
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
What are Bytecodes ?
• A machine instruction set that has 256
instructions, and whose operands are byte
oriented
• Stack based, Forth like
• Includes instructions that can access a literal
frame to provide access to non byte oriented
operands
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Bytecode Translator
• Follows the visitor Pattern
• Currently very simplistic
• Each Java bytecode diretly translated to
Pocket St bytecode
• Some cases, Java bytecode is implemented
by Smalltalk code, instead of directly to a
bytecode
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Bytecode Types
•
• Return
Push
– Push values on stack
• Store
– Return from send
• Stack Manipulation
– Store into variables
• Control Flow
– Goto, Branch
• Invoke Message
– Message send
– Dup, Pop
• Monitor
• Exception Handling
• Basic type handling
– Type specific
operations
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Load Bytecodes
• Take a value and push it on top of the
operand stack
• Value can be a constant, global var ref,
literal
• Many are type oriented
– iload, dload
– Translate to push object
– Lose type information
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Store Bytecodes
• Pop object from top of operand stack and
store into variable
• Can store into temps, inst vars, globals
• As for Load, many are type oriented
• Translate to store object
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Control Flow
• Standard branch
– Branch always
– Bran on true false
– Goto
• Java has case bytecode
– Not sure how to implement yet
– Ditionary with blocks Or VM support ?
• Gosub
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Invoke Message
• invokeinterface
– Search using runtime
type
– Use Smalltalk send
• invokespecial
– Only for <init>, private
methods, named super
– Search using compile
time type
– Use directed send
• invokestatic
– Seach for static
selector using class
specified
– Use receiver on top
send
• invokevirtual
– Search using runtime
type
– Use Smalltalk send
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Return
• Exit from current method and return to
sender
• Type oriented
• Return void
– Return nil
– Caller does a pop
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Stack Manipulation
• pop, dup, swap
– Translates as is
• pop2, dup2, …
– Must take special care of pop2
• May mean pop a basic object that uses two slots
– Must figure out whether to translate to single
pop
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Monitor
• Java’s instruction to handle concurrent
threads
• Implemented via Smalltalk objects and
methods, no VM support
• Need to add processes to Pocket Smalltalk
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Exception Handling
• Implement via Smalltalk objects and
methods
• Need some VM support for curtailing stacks
and attaching exceptions on stack frames
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Basic Type Handling
• Not everything is an object in Java
– Integers, Floats, Doubles, Arrays
• Bytecodes like iadd, dadd, imul, etc.
• Translate to message send
– iadd to ‘send #+’
– fmul to ‘send #*’
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Basic Type Handling
• Array
– anew - translate to push Array, send new
– asize - translate to send size
• Double & Long
– Take up two normal 16 bit slots
– Translator must take care for argument, temp,
inst var indeces
– pop2 should sometimes be translated to pop
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Why is it so small ?
• Not entirely sure !!!
• More compact code storage
• Reduce duplicate literals
– Each class file has it’s own literal table, these are
coalesced by Code Space
• Replacing one method in Character with a
Smalltalk literal saved 80K !!!
– Java does not support Array literals, replaced method
with a Smalltalk equivalent
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Future
• Finish support for other packages
– AWT
– Applet
•
•
•
•
Verifier
Server
Translate to other Smalltalk dialects
Win CE
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Where To Get More Info ?
•
•
•
•
www.pocketjava.com
www.pocketsmalltalk.com
www.pocketdeveloper.com
www.ericsworld.com
Pocket Smalltalk & Pocket Java © 1999 Applied Thought
Questions
Pocket Smalltalk & Pocket Java © 1999 Applied Thought