25.9 Optimizations. 4tH 4tH Forth compiler
বিজ্ঞাপন
বিজ্ঞাপন
CHAPTER 25. COMPILING THE SOURCE 482
(E)dit (C)ompile (R)un (A)rguments
(Q)uit (G)enerate (B)uild (D)ecompile
>q
If it doesn’t, try another C compiler. Yours may not be compatible
2
. If it does, you can safely proceed with the installation: su make install
25.9
Optimizations
It really depends on your compiler. Some compilers allow optimizations up to level 3
(-O3), others won’t even produce a usable compilant with any optimization enabled. It can even depend on the platform you’re compiling for. It is hard to give a general recommendation, but try compiling 4tH without any optimization first, then crank optimization gradually up until the compilant doesn’t work properly anymore or optimization doesn’t give you any more speed or size advantages. You may have to do some benchmarking to find this out
3
. There is also a program that allows you to test the virtual machine. All listed preferred compilers allow the highest optimization level. The optimization level can be set manually or can be adjusted in the Makefile. Consult your compiler documentation for details.
25.10
GCC specific optimizations
Some people may wonder why 4tH doesn’t come with -fomit-frame-pointer enabled. Well, first because the use of this compilation option is highly debatable
4 and second, because it may have detrimental effects on some platforms. However, if you insist on using it, you can - although we never found any convincing advantages in either size or speed.
What does give you a 15 - 25% speed boost is the use of -DUSEGCCGOTO. This compilation option enables a GCC-specific language extension, which makes the VM a whole lot faster. It requires an extra file, named gcc_exec.h. This file is generated by a 4tH program, makegcch.4th. If you ever want to make extensions to 4tH and you want the best performance under GCC, you will have to regenerate it, since it is not regenerated automatically. Any working 4tH will do:
4th cxq makegcch.4th cmds_4th.h gcc_exec.h
If you want to use any of these options, you will have to edit the CFLAGS section of your
Makefile accordingly, e.g.:
CFLAGS= -DUNIX -DUSEGCCGOTO -fsigned-char -Wall -O3 -s
See section 25.5 for details.
2
See table 25.1 for a list of compatible compilers.
3
4tH comes with a wide selection of benchmarking programs.
4 http://timetobleed.com/gcc-optimization-flag-makes-your-64bit-binary-fatter-and-slower/
CHAPTER 25. COMPILING THE SOURCE
25.11
Using the library
483
Before we dive into the depths of the API, we will tell you how to compile a program that uses the 4tH library. If you are using an advanced MS-DOS or MS-Windows compiler this may or may not apply to you. In that case we advise you to check your documentation. If you are using a plain vanilla compiler this will usually work. Compile the program with: cc -c <program>.c
When you use a non-Unix system, link the objectfile with the startup code and libraries to create an executable program. The <model>.obj is the startup code for C-programs and
<model>.lib is the runtime-library.
link <model>.obj <program.obj>, <program>„ 4th<model>.lib <model>.lib
Unix developers just give the command: cc <program>.c -o <program> -l4th
If you happen to use the GNU Compiler Collection (gcc), just issue: gcc -s -Wall -fsigned-char <program>.c -o <program> -l4th
After you’ve built the library you can also issue these commands to (re)compile 4th. If you happen to use a GNU based platform, you can configure make to work with 4tH. You just have to create a small Makefile in your current working directory
5
:
# GNU Make - implicit rules for 4tH
# Copyright 2006, 2009 Hans Bezemer
%.c : %.4th
4th cgq $< $@
%.c : %.hx
4th lgq $< $@
%.4th : %.4pp
pp4th -o $@ $<
CC=gcc
CFLAGS=-fsigned-char -Wall -O3 -s
LDLIBS=-l4th
LDFLAGS=-s
Note this is only an example, so you may have to change it for your system. Now copy
4th.h
to the directory where the 4tH source is located and you’re done:
$ make examples/eliza
4th cgq examples/eliza.4th examples/eliza.c
gcc -fsigned-char -Wall -O3 -s -c examples/eliza.c -o examples/eliza.o
gcc -s examples/eliza.o -l4th -o examples/eliza rm examples/eliza.c examples/eliza.o
$ examples/eliza
HI! I’M ELIZA. WHAT’S YOUR PROBLEM?
>
5
That is the directory where you are when you call 4tH and is usually pointed to by the environment variable
DIR4TH
.
CHAPTER 25. COMPILING THE SOURCE 484
Isn’t that easy? You can also use this procedure to turn 4tH utilities like the preprocessor into full fledged programs. In fact, the Makefile takes the preprocessor into account if required:
$ make teonw pp4th -o teonw.4th teonw.4pp
4th cgq teonw.4th teonw.c
gcc -fsigned-char -Wall -O3 -s -c -o teonw.o teonw.c
gcc -s teonw.o -l4th -o teonw rm teonw.4th teonw.c teonw.o
ডাউনলোড
বিজ্ঞাপন