Compare commits

...

66 Commits

Author SHA1 Message Date
Godzil
b58f315c98 cleanup things, stop trying with CS 2018-09-19 17:59:25 +01:00
Godzil
7687c2b7d2 Trying to make parser to correctly handle CS 2018-09-18 18:50:13 +01:00
Artur K
0abbce6f4e
Merge pull request #32 from nemerle/add-license-1
Create LICENSE
2018-03-23 19:59:48 +01:00
Artur K
8ffdf657ec
Create LICENSE
Original code was using GPL.
2018-03-23 19:59:34 +01:00
nemerle
2232a76033 Disregard signed/unsigned difference in AstIdent::idID 2017-02-13 16:14:38 +01:00
nemerle
d6af9c1555 Add iInvalid enum value for invalid instructions
Various cleanups.
2017-02-13 13:24:54 +01:00
nemerle
d7acc8cd4d rename otherLongRegi to getPairedRegisterAt, make it a method of
LOCAL_ID struct.
2017-02-13 12:31:30 +01:00
nemerle
a5f1d17e83 Various code cleanups. 2017-02-13 12:11:29 +01:00
nemerle
29efcd5be1 Remove references to malloc.h closes #28 2017-02-07 12:09:58 +01:00
Artur K
4656db9484 Merge pull request #25 from gitter-badger/gitter-badge
Add a Gitter chat badge to Readme.md
2016-05-23 13:30:14 +02:00
The Gitter Badger
b33d7239e5 Add Gitter badge 2016-05-23 11:28:47 +00:00
Artur K
e1f0c084f8 Merge pull request #24 from lab313ru/patch-1
Fixed symbol name collision in parsehdr
2016-05-20 10:13:45 +02:00
Lab 313
e2a6b25345 Fixed collision with round math.h function 2016-05-20 11:01:00 +03:00
nemerle
f210ed78c2 Add the last of the original tools dispsig and srchsig
Closes #22
2016-05-19 20:15:37 +02:00
nemerle
ccc8cc526b Add readsig tool to build
As requested in #22
2016-05-19 20:03:49 +02:00
nemerle
cd6797499f Add parsehdr to the build
As requested in #22

(cherry picked from commit d5985b4b97cb06a89050c34674878c6430432fe1)
2016-05-19 19:52:48 +02:00
nemerle
b60903306f Fix memset-of-non-POD bug.
PROG contains vector, but was memset.
2016-05-19 16:17:08 +02:00
nemerle
1df7cb3be4 Fix msvc detection + prevent range library from pulling in boost::regex 2016-05-19 14:45:03 +02:00
nemerle
a7265f06b0 Fix msvc detection in cmake 2016-05-19 14:14:26 +02:00
nemerle
58532f4402 Removing LLVM dependency contd. 2016-05-19 12:40:59 +02:00
nemerle
7d986ef661 Removing LLVM dependencies WIP 2016-05-19 11:14:46 +02:00
nemerle
7f4197acc1 Use cmake features to mark c++11 as a required standard 2016-05-19 10:44:45 +02:00
nemerle
e71c8051c3 Rename icodeType entries
Following 2f80f16e6f76e3794ddc35ac9a6c1a86cb11cd28
2016-05-19 10:27:15 +02:00
nemerle
73cf949e25 Undefine PASCAL if it's defined as a macro
Following 0af07017b9c2f14435bd0fd103ae199cbf840bb0
2016-05-19 10:20:51 +02:00
nemerle
b2be1cf2da Simplify: SetupLibCheck does not depend on PROG or Project classes.
It also does not exit the program when ".sig" loader fails.

Use QString to build signature filename.
2016-04-26 13:42:41 +02:00
nemerle
94e3016a5b Fix SetCurFunc_by_Name implementation 2016-04-26 13:36:57 +02:00
nemerle
145a50369e Fix: Jump labels were printed incorrectly. 2016-04-26 13:35:20 +02:00
Vladimir Kryvian
d77927c608 Added new line for "removeRegFromLong not supproted" message in icode.h. 2016-04-26 10:06:00 +02:00
Vladimir Kryvian
3bb72987a6 Fixed double import with correct one in makedsig. 2016-04-26 10:05:10 +02:00
nemerle
c782892db4 Prevent the use of msvc's min/max macros 2016-04-26 09:26:28 +02:00
nemerle
a944ea5da8 Implement some of the methods in DccImpl 2016-04-26 09:23:34 +02:00
nemerle
d1738ea630 New feature: option to decompile only a specific function.
Similar to boomerang's -E option:

```
dcc -E 0x1222 ./TARGET.EXE
```

Will only decompile function at given address.
This might help in isolating dcc crashes.
2016-04-26 00:46:56 +02:00
nemerle
5f39236ba2 Fix LOCAL_ID::newLongIdx 2016-04-26 00:27:49 +02:00
nemerle
ede09ddae3 Record native function address as part of the name.
Might help while debugging dcc's handling of large programs.
2016-04-25 16:08:51 +02:00
nemerle
34b1f4f4fe Fix: long processing regression introduced in
bb007ddefc0465d36d394fbc4dbe811a633fc72e

Invalidated instructions should still be processed in findBBExps
2016-04-25 16:02:58 +02:00
nemerle
d6249916e1 More logic operator replacements.
Use Qt string classes.
2016-04-25 15:51:58 +02:00
nemerle
9cd3226536 Normalize logic operation keywords and add use msvc fix
Logical or should be only 'or','and','not', and not error prone
'||','&&','!'
2016-04-25 11:39:07 +02:00
nemerle
3f217e83da Add header that will contain msvc fixes 2016-04-25 10:08:25 +02:00
nemerle
652cfb67c3 Add simple gitattributes 2016-04-25 10:03:55 +02:00
nemerle
c0e9ba2fb3 Add addOutEdgesForConditionalJump to header 2016-04-25 10:03:30 +02:00
nemerle
5963f5fd4d Thanks to @lab313ru : fix bad iterator usage
Trying to increment past the end in graph.cpp

Also removed a goto by extracting a common function.
2016-04-24 12:22:15 +02:00
nemerle
12ee08f87e Implement two new switch idioms closes #14 2016-04-23 20:05:11 +02:00
nemerle
5c85c92d1a Replace tabs with spaces 2016-04-22 11:45:23 +02:00
nemerle
b509d0fcf0 Extend disassembly failure reporting a bit. 2016-04-22 10:47:14 +02:00
nemerle
bb007ddefc Stop processing invalidated instructions in dataflow 2016-04-22 10:36:29 +02:00
nemerle
9129d48429 Comment out a debugging printf 2016-04-22 10:35:41 +02:00
nemerle
d105182051 Add missing \n to error printf 2016-04-22 10:35:14 +02:00
nemerle
157a968372 Assume TYPE_STR is returned in exactly the same way as TYPE_PTR is 2016-04-22 10:34:36 +02:00
Artur K
bae2a582f1 Merge pull request #11 from lab313ru/lab313ru-patch-1
Lab313ru patch 1
2016-04-22 09:19:02 +02:00
Lab 313
19191876e2 Update libdis.h
Fixed negative address getting.
2016-04-22 02:37:22 +03:00
Lab 313
fcfe3c1f4b Update scanner.cpp
Fixed negative address calculating.
2016-04-22 02:35:40 +03:00
nemerle
97f093feaa This build requires LLVM, does not need ncurses - modify CMakeLists.txt to match 2016-02-13 15:14:14 +01:00
Artur K.
3561de6e12 Merge pull request #5 from Arthur2e5/patch-1
README: Recognizing code segments
2015-10-20 06:18:10 +00:00
Mingye Wang
e84d09b97c README: Recognizing code segments 2015-10-20 01:15:59 -04:00
Artur K.
d8a4fe1c04 Merge pull request #4 from Arthur2e5/patch-1
README: tweak formatting by a bit
2015-10-20 05:15:49 +00:00
Mingye Wang
e4e6ad6415 README: tweak formatting by a bit
Trying to get a nice balance between Markdown rendering and plain text readability. And I think I got it.
2015-10-20 01:11:34 -04:00
nemerle
2543617930 Remove llvm as a build requirement 2015-08-13 20:46:54 +02:00
nemerle
bc5784a8f2 Fix #1 - just use QFileInfo. 2015-05-28 15:13:43 +02:00
Artur K
842687726f Update the dcc tools code 2015-04-28 14:59:00 +02:00
nemerle
c5c9196561 Fix for functional tests when running on clean checkout 2015-02-10 17:31:57 +01:00
nemerle
a697ad05c0 Add original dcc tools to repository
* makedsig has been integrated with makedstp, it should handle both LIB and TPL files
* other tools have not been modified
2015-02-10 17:28:50 +01:00
Artur K.
d8c66e7791 Update Readme.md 2014-06-05 15:01:12 +02:00
nemerle
337a6c44aa Added original readme 2014-05-25 12:36:39 +02:00
nemerle
cde4484821 Remove unused local 2014-05-25 12:33:18 +02:00
nemerle
36b063c183 Working towards gui integration with exetoc_qt 2014-05-24 17:08:05 +02:00
nemerle
3603877f42 Qt5 command options processing 2014-03-07 20:01:36 +01:00
111 changed files with 12968 additions and 2869 deletions

6
.gitattributes vendored Normal file
View File

@ -0,0 +1,6 @@
* text=auto
*.c text
*.cpp text
*.ui text
*.qrc text
*.h text

68
3rd_party/libdisasm/INTEL_BUGS vendored Normal file
View File

@ -0,0 +1,68 @@
PMOVMSKB
Gd, Pq1H
PMOVMSKB
(66)
Gd, Vdq1H
should be
PMOVMSKB
Gd, Qq1H
PMOVMSKB
(66)
Gd, Wdq1H
The instruction represented by this opcode expression does not support any
operand to be a memory location.
MASKMOVQ
Pq, Pq1H
MASKMOVDQU
(66)
Vdq, Vdq1H
should be
MASKMOVQ
Pq, Pq1H
MASKMOVDQU
(66)
Vdq, Wdq1H
MOVMSKPS
Gd, Vps1H
MOVMSKPD
(66)
Gd, Vpd1H
should be
MOVMSKPS
Gd, Wps1H
MOVMSKPD
(66)
Gd, Wpd1H
The opcode table entries for LFS, LGS, and LSS
L[FGS]S
Mp
should be
L[FGS]S
Gv,Mp
MOVHLPS
Vps, Vps
MOVLHPS
Vps, Vps
should be
MOVHLPS
Vps, Wps
MOVLHPS
Vps, Wps

137
3rd_party/libdisasm/LICENSE vendored Normal file
View File

@ -0,0 +1,137 @@
The "Clarified Artistic License"
Preamble
The intent of this document is to state the conditions under which a
Package may be copied, such that the Copyright Holder maintains some
semblance of artistic control over the development of the package,
while giving the users of the package the right to use and distribute
the Package in a more-or-less customary fashion, plus the right to make
reasonable modifications.
Definitions:
"Package" refers to the collection of files distributed by the
Copyright Holder, and derivatives of that collection of files
created through textual modification.
"Standard Version" refers to such a Package if it has not been
modified, or has been modified in accordance with the wishes
of the Copyright Holder as specified below.
"Copyright Holder" is whoever is named in the copyright or
copyrights for the package.
"You" is you, if you're thinking about copying or distributing
this Package.
"Distribution fee" is a fee you charge for providing a copy of this
Package to another party.
"Freely Available" means that no fee is charged for the right to use
the item, though there may be fees involved in handling the item.
1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.
2. You may apply bug fixes, portability fixes and other modifications
derived from the Public Domain, or those made Freely Available, or from
the Copyright Holder. A Package modified in such a way shall still be
considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided
that you insert a prominent notice in each changed file stating how and
when you changed that file, and provided that you do at least ONE of the
following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or
an equivalent medium, or placing the modifications on a major archive
site allowing unrestricted access to them, or by allowing the Copyright
Holder to include your modifications in the Standard Version of the
Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict
with standard executables, which must also be provided, and provide
a separate manual page for each non-standard executable that clearly
documents how it differs from the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
e) permit and encourge anyone who receives a copy of the modified Package
permission to make your modifications Freely Available in some specific
way.
4. You may distribute the programs of this Package in object code or
executable form, provided that you do at least ONE of the following:
a) distribute a Standard Version of the executables and library files,
together with instructions (in the manual page or equivalent) on where
to get the Standard Version.
b) accompany the distribution with the machine-readable source of
the Package with your modifications.
c) give non-standard executables non-standard names, and clearly
document the differences in manual pages (or equivalent), together
with instructions on where to get the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
e) offer the machine-readable source of the Package, with your
modifications, by mail order.
5. You may charge a distribution fee for any distribution of this Package.
If you offer support for this Package, you may charge any fee you choose
for that support. You may not charge a license fee for the right to use
this Package itself. You may distribute this Package in aggregate with
other (possibly commercial and possibly nonfree) programs as part of a
larger (possibly commercial and possibly nonfree) software distribution,
and charge license fees for other parts of that software distribution,
provided that you do not advertise this Package as a product of your own.
If the Package includes an interpreter, You may embed this Package's
interpreter within an executable of yours (by linking); this shall be
construed as a mere form of aggregation, provided that the complete
Standard Version of the interpreter is so embedded.
6. The scripts and library files supplied as input to or produced as
output from the programs of this Package do not automatically fall
under the copyright of this Package, but belong to whoever generated
them, and may be sold commercially, and may be aggregated with this
Package. If such scripts or library files are aggregated with this
Package via the so-called "undump" or "unexec" methods of producing a
binary executable image, then distribution of such an image shall
neither be construed as a distribution of this Package nor shall it
fall under the restrictions of Paragraphs 3 and 4, provided that you do
not represent such an executable image as a Standard Version of this
Package.
7. C subroutines (or comparably compiled subroutines in other
languages) supplied by you and linked into this Package in order to
emulate subroutines and variables of the language defined by this
Package shall not be considered part of this Package, but are the
equivalent of input as in Paragraph 6, provided these subroutines do
not change the language in any way that would cause it to fail the
regression tests for the language.
8. Aggregation of the Standard Version of the Package with a commercial
distribution is always permitted provided that the use of this Package is
embedded; that is, when no overt attempt is made to make this Package's
interfaces visible to the end user of the commercial distribution.
Such use shall not be construed as a distribution of this Package.
9. The name of the Copyright Holder may not be used to endorse or promote
products derived from this software without specific prior written permission.
10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The End

12
3rd_party/libdisasm/NAMESPACE.TXT vendored Normal file
View File

@ -0,0 +1,12 @@
The rewritten libdisasm code uses the following namespaces:
Prefix Namespace
----------------------------------------------------
x86_ Global 'libdisasm' namespace
ia32_ Internal IA32 ISA namespace
ia64_ Internal IA64 ISA namespace
ix64_ Internal X86-64 ISA namespace
Note that the 64-bit ISAs are not yet supported/written.

2
3rd_party/libdisasm/README vendored Normal file
View File

@ -0,0 +1,2 @@
This is a cut-up version of libdisasm originally from the bastard project http://bastard.sourceforge.net/

43
3rd_party/libdisasm/TODO vendored Normal file
View File

@ -0,0 +1,43 @@
x86_format.c
------------
intel: jmpf -> jmp, callf -> call
att: jmpf -> ljmp, callf -> lcall
opcode table
------------
finish typing instructions
fix flag clear/set/toggle types
ix64 stuff
----------
document output file formats in web page
features doc: register aliases, implicit operands, stack mods,
ring0 flags, eflags, cpu model/isa
ia32_handle_* implementation
fix operand 0F C2
CMPPS
* sysenter, sysexit as CALL types -- preceded by MSR writes
* SYSENTER/SYSEXIT stack : overwrites SS, ESP
* stos, cmps, scas, movs, ins, outs, lods -> OP_PTR
* OP_SIZE in implicit operands
* use OP_SIZE to choose reg sizes!
DONE?? :
implicit operands: provide action ?
e.g. add/inc for stach, write, etc
replace table numbers in opcodes.dat with
#defines for table names
replace 0 with INSN_INVALID [or maybe FF for imnvalid and 00 for Not Applicable */
no wait that is only for prefix tables -- n/p
if ( prefx) only use if insn != invalid
these should cover all the wacky disasm exceptions
for the rep one we can chet, match only a 0x90
todo: privilege | ring

36
3rd_party/libdisasm/ia32_fixup.cpp vendored Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
static const char * mem_fixup[256] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 00 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 08 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 10 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 18 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 20 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 28 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 30 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 38 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 40 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 48 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 60 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 68 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 70 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 78 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 90 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 98 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* A0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* A8 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* B0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* B8 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* C0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* C8 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* D0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* D8 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* E0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* E8 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* F0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* F8 */
};

3206
3rd_party/libdisasm/ia32_opcode.dat vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -317,7 +317,10 @@ struct x86_op_t{
case op_offset:
return data.offset;
case op_relative_far:
return (int32_t) data.relative_far;
if (data.relative_far & 0x8000)
return (data.relative_far & 0xFFFF) | 0xFFFF0000;
else
return (int32_t)data.relative_far;
default:
assert(false);
break;

49
3rd_party/libdisasm/libdisasm.def vendored Normal file
View File

@ -0,0 +1,49 @@
;libdisasm.def : Declares the module parameters
LIBRARY "libdisasm.dll"
DESCRIPTION "libdisasm exported functions"
EXPORTS
x86_addr_size @1
x86_cleanup @2
x86_disasm @3
x86_disasm_forward @4
x86_disasm_range @5
x86_endian @6
x86_format_header @7
x86_format_insn @8
x86_format_mnemonic @9
x86_format_operand @10
x86_fp_reg @11
x86_get_branch_target @12
x86_get_imm @13
x86_get_options @14
x86_get_raw_imm @15
x86_get_rel_offset @16
x86_imm_signsized @17
x86_imm_sized @18
x86_init @19
x86_insn_is_tagged @20
x86_insn_is_valid @21
x86_invariant_disasm @22
x86_ip_reg @23
x86_max_insn_size @24
x86_op_size @25
x86_operand_1st @26
x86_operand_2nd @27
x86_operand_3rd @28
x86_operand_count @29
x86_operand_foreach @30
x86_operand_new @31
x86_operand_size @32
x86_oplist_free @33
x86_reg_from_id @34
x86_report_error @35
x86_set_insn_addr @36
x86_set_insn_block @37
x86_set_insn_function @38
x86_set_insn_offset @39
x86_set_options @40
x86_set_reporter @41
x86_size_disasm @42
x86_sp_reg @43
x86_tag_insn @44

View File

@ -1,39 +1,47 @@
PROJECT(dcc_original)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
cmake_minimum_required(VERSION 3.1)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
OPTION(dcc_build_tests "Enable unit tests." OFF)
#SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS)
IF(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D_CRT_NONSTDC_NO_DEPRECATE)
IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D__UNIX__ -D_CRT_NONSTDC_NO_DEPRECATE -DNOMINMAX)
ADD_DEFINITIONS(/W4)
ELSE()
#-D_GLIBCXX_DEBUG
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++0x")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} " ) #--coverage
ENDIF()
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeScripts;${CMAKE_MODULE_PATH})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
include(cotire)
FIND_PACKAGE(LLVM)
FIND_PACKAGE(Boost)
IF(dcc_build_tests)
enable_testing()
FIND_PACKAGE(GMock)
ENDIF()
ADD_SUBDIRECTORY(3rd_party)
llvm_map_components_to_libraries(REQ_LLVM_LIBRARIES jit native mc support tablegen)
INCLUDE_DIRECTORIES(
3rd_party/libdisasm
include
include/idioms
common
${Boost_INCLUDE_DIRS}
${LLVM_INCLUDE_DIRS}
)
ADD_SUBDIRECTORY(3rd_party)
ADD_SUBDIRECTORY(common)
ADD_SUBDIRECTORY(tools)
set(dcc_LIB_SOURCES
src/CallConvention.cpp
src/ast.cpp
src/backend.cpp
src/bundle.cpp
@ -42,9 +50,9 @@ set(dcc_LIB_SOURCES
src/control.cpp
src/dataflow.cpp
src/disassem.cpp
src/DccFrontend.cpp
src/error.cpp
src/fixwild.cpp
src/frontend.cpp
src/graph.cpp
src/hlicode.cpp
src/hltype.cpp
@ -63,7 +71,6 @@ set(dcc_LIB_SOURCES
src/locident.cpp
src/liveness_set.cpp
src/parser.cpp
src/perfhlib.cpp
src/procs.cpp
src/project.cpp
src/Procedure.cpp
@ -73,7 +80,7 @@ set(dcc_LIB_SOURCES
src/symtab.cpp
src/udm.cpp
src/BasicBlock.cpp
src/CallConvention.cpp
src/dcc_interface.cpp
)
set(dcc_SOURCES
src/dcc.cpp
@ -82,6 +89,8 @@ set(dcc_HEADERS
include/ast.h
include/bundle.h
include/BinaryImage.h
include/DccFrontend.h
include/Enums.h
include/dcc.h
include/disassem.h
include/dosdcc.h
@ -100,7 +109,7 @@ set(dcc_HEADERS
include/idioms/shift_idioms.h
include/idioms/xor_idioms.h
include/locident.h
include/perfhlib.h
include/CallConvention.h
include/project.h
include/scanner.h
include/state.h
@ -109,20 +118,25 @@ set(dcc_HEADERS
include/Procedure.h
include/StackFrame.h
include/BasicBlock.h
include/CallConvention.h
include/dcc_interface.h
)
SOURCE_GROUP(Source FILES ${dcc_SOURCES})
SOURCE_GROUP(Headers FILES ${dcc_HEADERS})
ADD_LIBRARY(dcc_lib STATIC ${dcc_LIB_SOURCES} ${dcc_HEADERS})
qt5_use_modules(dcc_lib Core)
#cotire(dcc_lib)
ADD_EXECUTABLE(dcc_original ${dcc_SOURCES} ${dcc_HEADERS})
ADD_DEPENDENCIES(dcc_original dcc_lib)
TARGET_LINK_LIBRARIES(dcc_original LLVMSupport dcc_lib disasm_s ${REQ_LLVM_LIBRARIES} LLVMSupport)
TARGET_LINK_LIBRARIES(dcc_original dcc_lib dcc_hash disasm_s)
qt5_use_modules(dcc_original Core)
SET_PROPERTY(TARGET dcc_original PROPERTY CXX_STANDARD 11)
SET_PROPERTY(TARGET dcc_original PROPERTY CXX_STANDARD_REQUIRED ON)
#ADD_SUBDIRECTORY(gui)
if(dcc_build_tests)
ADD_SUBDIRECTORY(src)
endif()

File diff suppressed because it is too large Load Diff

339
LICENSE Normal file
View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

129
Readme.md Normal file
View File

@ -0,0 +1,129 @@
I've fixed many issues in this codebase, among other things - memory reallocation during decompilation.
To reflect those fixes, I've edited the original readme a bit.
* * *
dcc Distribution
================
[![Join the chat at https://gitter.im/nemerle/dcc](https://badges.gitter.im/nemerle/dcc.svg)](https://gitter.im/nemerle/dcc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
The code provided in this distribution is (C) by their authors:
- Cristina Cifuentes (most of dcc code)
- Mike van Emmerik (signatures and prototype code)
- Jeff Ledermann (some disassembly code)
and is provided "as is". Additional contributor list is available
[on GitHub](https://github.com/nemerle/dcc/graphs/contributors).
The following files are included in the dccoo.tar.gz distribution:
- dcc.zip (dcc.exe DOS program, 1995)
- dccsrc.zip (source code *.c, *.h for dcc, 1993-1994)
- dcc32.zip (dcc_oo.exe 32 bit console (Win95/Win-NT) program, 1997)
- dccsrcoo.zip (source code *.cpp, *.h for "oo" dcc, 1993-1997)
- dccbsig.zip (library signatures for Borland C compilers, 1994)
- dccmsig.zip (library signatures for Microsoft C compilers, 1994)
- dcctpsig.zip (library signatures for Turbo Pascal compilers, 1994)
- dcclibs.dat (prototype file for C headers, 1994)
- test.zip (sample test files: *.c *.exe *.b, 1993-1996)
- makedsig.zip (creates a .sig file from a .lib C file, 1994)
- makedstp.zip (creates a .sig file from a Pascal library file, 1994)
- readsig.zip (reads signatures in a .sig file, 1994)
- dispsrch.zip (displays the name of a function given a signature, 1994)
- parsehdr.zip (generates a prototype file (dcclibs.dat) from C *.h files, 1994)
Note that the dcc_oo.exe program (in dcc32.zip) is a 32 bit program,
so it won't work under Windows 3.1. Also, it is a console mode program,
meaning that it has to be run in the "Command Prompt" window (sometimes
known as the "Dos Box"). It is not a GUI program.
The following files are included in the test.zip file: fibo,
benchsho, benchlng, benchfn, benchmul, byteops, intops, longops,
max, testlong, matrixmu, strlen, dhamp.
The version of dcc included in this distribution (dccsrcoo.zip and
dcc32.exe) is a bit better than the first release, but it is still
broken in some cases, and we do not have the time to work in this
project at present so we cannot provide any changes.
Comments on individual files:
- fibo (fibonacci): the small model (fibos.exe) decompiles correctly,
the large model (fibol.exe) expects an extra argument for
`scanf()`. This argument is the segment and is not displayed.
- benchsho: the first `scanf()` takes loc0 as an argument. This is
part of a long variable, but dcc does not have any clue at that
stage that the stack offset pushed on the stack is to be used
as a long variable rather than an integer variable.
- benchlng: as part of the `main()` code, `LO(loc1) | HI(loc1)` should
be displayed instead of `loc3 | loc9`. These two integer variables
are equivalent to the one long loc1 variable.
- benchfn: see benchsho.
- benchmul: see benchsho.
- byteops: decompiles correctly.
- intops: the du analysis for `DIV` and `MOD` is broken. dcc currently
generates code for a long and an integer temporary register that
were used as part of the analysis.
- longops: decompiles correctly.
- max: decompiles correctly.
- testlong: this example decompiles correctly given the algorithms
implemented in dcc. However, it shows that when long variables
are defined and used as integers (or long) without giving dcc
any hint that this is happening, the variable will be treated as
two integer variables. This is due to the fact that the assembly
code is in terms of integer registers, and long registers are not
available in 80286, so a long variable is equivalent to two integer
registers. dcc only knows of this through idioms such as add two
long variables.
- matrixmu: decompiles correctly. Shows that arrays are not supported
in dcc.
- strlen: decompiles correctly. Shows that pointers are partially
supported by dcc.
- dhamp: this program has far more data types than what dcc recognizes
at present.
Our thanks to Gary Shaffstall for some debugging work. Current bugs
are:
- [ ] if the code generated in the one line is too long, the (static)
buffer used for that line is clobbered. Solution: make the buffer
larger (currently 200 chars).
- [ ] the large memory model problem & `scanf()`
- [ ] dcc's error message shows a p option available which doesn't
exist, and doesn't show an i option which exists.
- [x] there is a nasty problem whereby some arrays can get reallocated
to a new address, and some pointers can become invalid. This mainly
tends to happen to larger executable files. A major rewrite will
probably be required to fix this.
For more information refer to the thesis "Reverse Compilation
Techniques" by Cristina Cifuentes, Queensland University of
Technology, 1994, and the dcc home page:
http://www.it.uq.edu.au/groups/csm/dcc_readme.html
Please note that the executable version of dcc provided in this
distribution does not necessarily match the source code provided,
some changes were done without us keeping track of every change.
Using dcc
---------
Here is a very brief summary of switches for dcc:
* `a1`, `a2`: assembler output, before and after re-ordering of input code
* `c`: Attempt to follow control through indirect call instructions
* `i`: Enter interactive disassembler
* `m`: Memory map
* `s`: Statistics summary
* `v`, `V`: verbose (and Very verbose)
* `o` filename: Use filename as assembler output file
If dcc encounters illegal instructions, it will attempt to enter the so called
interactive disassembler. The idea of this was to allow commands to fix the
problem so that dcc could continue, but no such changes are implemented
as yet. (Note: the Unix versions do not have the interactive disassembler). If
you get into this, you can get out of it by pressing `^X` (control-X). Once dcc
has entered the interactive disassembler, however, there is little chance that
it will recover and produce useful output.
If dcc loads the signature file `dccxxx.sig`, this means that it has not
recognised the compiler library used. You can place the signatures in a
different direcory to where you are working if you set the DCC environment
variable to point to their path. Note that if dcc can't find its signature
files, it will be severely handicapped.

View File

@ -2,5 +2,6 @@
#cd bld
#make -j5
#cd ..
mkdir -p tests/outputs
./test_use_base.sh
./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff -wB tests/prev/ tests/outputs/

7
common/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
set(SRC
perfhlib.cpp
perfhlib.h
PatternCollector.h
)
add_library(dcc_hash STATIC ${SRC})

82
common/PatternCollector.h Normal file
View File

@ -0,0 +1,82 @@
#ifndef PATTERNCOLLECTOR
#define PATTERNCOLLECTOR
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#define SYMLEN 16 /* Number of chars in the symbol name, incl null */
#define PATLEN 23 /* Number of bytes in the pattern part */
struct HASHENTRY
{
char name[SYMLEN]; /* The symbol name */
uint8_t pat [PATLEN]; /* The pattern */
uint16_t offset; /* Offset (needed temporarily) */
};
struct PatternCollector {
uint8_t buf[100], bufSave[7]; /* Temp buffer for reading the file */
uint16_t readShort(FILE *f)
{
uint8_t b1, b2;
if (fread(&b1, 1, 1, f) != 1)
{
printf("Could not read\n");
exit(11);
}
if (fread(&b2, 1, 1, f) != 1)
{
printf("Could not read\n");
exit(11);
}
return (b2 << 8) + b1;
}
void grab(FILE *f,int n)
{
if (fread(buf, 1, n, f) != (size_t)n)
{
printf("Could not read\n");
exit(11);
}
}
uint8_t readByte(FILE *f)
{
uint8_t b;
if (fread(&b, 1, 1, f) != 1)
{
printf("Could not read\n");
exit(11);
}
return b;
}
uint16_t readWord(FILE *fl)
{
uint8_t b1, b2;
b1 = readByte(fl);
b2 = readByte(fl);
return b1 + (b2 << 8);
}
/* Called by map(). Return the i+1th key in *pKeys */
uint8_t *getKey(int i)
{
return keys[i].pat;
}
/* Display key i */
void dispKey(int i)
{
printf("%s", keys[i].name);
}
std::vector<HASHENTRY> keys; /* array of keys */
virtual int readSyms(FILE *f)=0;
};
#endif // PATTERNCOLLECTOR

438
common/perfhlib.cpp Normal file
View File

@ -0,0 +1,438 @@
/*
*$Log: perfhlib.c,v $
* Revision 1.5 93/09/29 14:45:02 emmerik
* Oops, didn't do the casts last check in
*
* Revision 1.4 93/09/29 14:41:45 emmerik
* Added casts to mod instructions to keep the SVR4 compiler happy
*
*
* Perfect hashing function library. Contains functions to generate perfect
* hashing functions
*/
#include "perfhlib.h"
#include "PatternCollector.h"
#include "msvc_fixes.h"
#include <stdio.h>
#include <cassert>
#include <stdlib.h>
#include <string.h>
/* Private data structures */
//static int NumEntry; /* Number of entries in the hash table (# keys) */
//static int EntryLen; /* Size (bytes) of each entry (size of keys) */
//static int SetSize; /* Size of the char set */
//static char SetMin; /* First char in the set */
//static int NumVert; /* c times NumEntry */
//static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *T1, *T2; /* Pointers to T1[i], T2[i] */
static int *graphNode; /* The array of edges */
static int *graphNext; /* Linked list of edges */
static int *graphFirst;/* First edge at a vertex */
static int numEdges; /* An edge counter */
static bool *visited; /* Array of bools: whether visited */
static bool *deleted; /* Array of bools: whether deleted */
/* Private prototypes */
static void duplicateKeys(int v1, int v2);
void PerfectHash::setHashParams(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,
int _NumVert)
{
/* These parameters are stored in statics so as to obviate the need for
passing all these (or defererencing pointers) for every call to hash()
*/
NumEntry = _NumEntry;
EntryLen = _EntryLen;
SetSize = _SetSize;
SetMin = _SetMin;
NumVert = _NumVert;
/* Allocate the variable sized tables etc */
if ((T1base = (uint16_t *)malloc(EntryLen * SetSize * sizeof(uint16_t))) == 0)
{
goto BadAlloc;
}
if ((T2base = (uint16_t *)malloc(EntryLen * SetSize * sizeof(uint16_t))) == 0)
{
goto BadAlloc;
}
if ((graphNode = (int *)malloc((NumEntry*2 + 1) * sizeof(int))) == 0)
{
goto BadAlloc;
}
if ((graphNext = (int *)malloc((NumEntry*2 + 1) * sizeof(int))) == 0)
{
goto BadAlloc;
}
if ((graphFirst = (int *)malloc((NumVert + 1) * sizeof(int))) == 0)
{
goto BadAlloc;
}
if ((g = (short *)malloc((NumVert+1) * sizeof(short))) == 0)
{
goto BadAlloc;
}
if ((visited = (bool *)malloc((NumVert+1) * sizeof(bool))) == 0)
{
goto BadAlloc;
}
if ((deleted = (bool *)malloc((NumEntry+1) * sizeof(bool))) == 0)
{
goto BadAlloc;
}
return;
BadAlloc:
printf("Could not allocate memory\n");
hashCleanup();
exit(1);
}
void PerfectHash::hashCleanup(void)
{
/* Free the storage for variable sized tables etc */
if (T1base) free(T1base);
if (T2base) free(T2base);
if (graphNode) free(graphNode);
if (graphNext) free(graphNext);
if (graphFirst) free(graphFirst);
if (g) free(g);
if (visited) free(visited);
if (deleted) free(deleted);
}
void PerfectHash::map(PatternCollector *collector)
{
m_collector = collector;
assert(nullptr!=collector);
int i, j, c;
uint16_t f1, f2;
bool cycle;
uint8_t *keys;
c = 0;
do
{
initGraph();
cycle = false;
/* Randomly generate T1 and T2 */
for (i=0; i < SetSize*EntryLen; i++)
{
T1base[i] = rand() % NumVert;
T2base[i] = rand() % NumVert;
}
for (i=0; i < NumEntry; i++)
{
f1 = 0; f2 = 0;
keys = m_collector->getKey(i);
for (j=0; j < EntryLen; j++)
{
T1 = T1base + j * SetSize;
T2 = T2base + j * SetSize;
f1 += T1[keys[j] - SetMin];
f2 += T2[keys[j] - SetMin];
}
f1 %= (uint16_t)NumVert;
f2 %= (uint16_t)NumVert;
if (f1 == f2)
{
/* A self loop. Reject! */
printf("Self loop on vertex %d!\n", f1);
cycle = true;
break;
}
addToGraph(numEdges++, f1, f2);
}
if (cycle or (cycle = isCycle())) /* OK - is there a cycle? */
{
printf("Iteration %d\n", ++c);
}
else
{
break;
}
}
while (/* there is a cycle */ 1);
}
/* Initialise the graph */
void PerfectHash::initGraph()
{
int i;
for (i=1; i <= NumVert; i++)
{
graphFirst[i] = 0;
}
for (i= -NumEntry; i <= NumEntry; i++)
{
/* No need to init graphNode[] as they will all be filled by successive
calls to addToGraph() */
graphNext[NumEntry+i] = 0;
}
numEdges = 0;
}
/* Add an edge e between vertices v1 and v2 */
/* e, v1, v2 are 0 based */
void PerfectHash::addToGraph(int e, int v1, int v2)
{
e++; v1++; v2++; /* So much more convenient */
graphNode[NumEntry+e] = v2; /* Insert the edge information */
graphNode[NumEntry-e] = v1;
graphNext[NumEntry+e] = graphFirst[v1]; /* Insert v1 to list of alphas */
graphFirst[v1]= e;
graphNext[NumEntry-e] = graphFirst[v2]; /* Insert v2 to list of omegas */
graphFirst[v2]= -e;
}
bool PerfectHash::DFS(int parentE, int v)
{
int e, w;
/* Depth first search of the graph, starting at vertex v, looking for
cycles. parent and v are origin 1. Note parent is an EDGE,
not a vertex */
visited[v] = true;
/* For each e incident with v .. */
for (e = graphFirst[v]; e; e = graphNext[NumEntry+e])
{
uint8_t *key1;
if (deleted[abs(e)])
{
/* A deleted key. Just ignore it */
continue;
}
key1 = m_collector->getKey(abs(e)-1);
w = graphNode[NumEntry+e];
if (visited[w])
{
/* Did we just come through this edge? If so, ignore it. */
if (abs(e) != abs(parentE))
{
/* There is a cycle in the graph. There is some subtle code here
to work around the distinct possibility that there may be
duplicate keys. Duplicate keys will always cause unit
cycles, since f1 and f2 (used to select v and w) will be the
same for both. The edges (representing an index into the
array of keys) are distinct, but the key values are not.
The logic is as follows: for the candidate edge e, check to
see if it terminates in the parent vertex. If so, we test
the keys associated with e and the parent, and if they are
the same, we can safely ignore e for the purposes of cycle
detection, since edge e adds nothing to the cycle. Cycles
involving v, w, and e0 will still be found. The parent
edge was not similarly eliminated because at the time when
it was a candidate, v was not yet visited.
We still have to remove the key from further consideration,
since each edge is visited twice, but with a different
parent edge each time.
*/
/* We save some stack space by calculating the parent vertex
for these relatively few cases where it is needed */
int parentV = graphNode[NumEntry-parentE];
if (w == parentV)
{
uint8_t *key2;
key2=m_collector->getKey(abs(parentE)-1);
if (memcmp(key1, key2, EntryLen) == 0)
{
printf("Duplicate keys with edges %d and %d (",
e, parentE);
m_collector->dispKey(abs(e)-1);
printf(" & ");
m_collector->dispKey(abs(parentE)-1);
printf(")\n");
deleted[abs(e)] = true; /* Wipe the key */
}
else
{
/* A genuine (unit) cycle. */
printf("There is a unit cycle involving vertex %d and edge %d\n", v, e);
return true;
}
}
else
{
/* We have reached a previously visited vertex not the
parent. Therefore, we have uncovered a genuine cycle */
printf("There is a cycle involving vertex %d and edge %d\n", v, e);
return true;
}
}
}
else /* Not yet seen. Traverse it */
{
if (DFS(e, w))
{
/* Cycle found deeper down. Exit */
return true;
}
}
}
return false;
}
bool PerfectHash::isCycle(void)
{
int v, e;
for (v=1; v <= NumVert; v++)
{
visited[v] = false;
}
for (e=1; e <= NumEntry; e++)
{
deleted[e] = false;
}
for (v=1; v <= NumVert; v++)
{
if (not visited[v])
{
if (DFS(-32767, v))
{
return true;
}
}
}
return false;
}
void PerfectHash::traverse(int u)
{
int w, e;
visited[u] = true;
/* Find w, the neighbours of u, by searching the edges e associated with u */
e = graphFirst[1+u];
while (e)
{
w = graphNode[NumEntry+e]-1;
if (not visited[w])
{
g[w] = (abs(e)-1 - g[u]) % NumEntry;
if (g[w] < 0) g[w] += NumEntry; /* Keep these positive */
traverse(w);
}
e = graphNext[NumEntry+e];
}
}
void PerfectHash::assign(void)
{
int v;
for (v=0; v < NumVert; v++)
{
g[v] = 0; /* g is sparse; leave the gaps 0 */
visited[v] = false;
}
for (v=0; v < NumVert; v++)
{
if (not visited[v])
{
g[v] = 0;
traverse(v);
}
}
}
int PerfectHash::hash(uint8_t *string)
{
uint16_t u, v;
int j;
u = 0;
for (j=0; j < EntryLen; j++)
{
T1 = T1base + j * SetSize;
u += T1[string[j] - SetMin];
}
u %= NumVert;
v = 0;
for (j=0; j < EntryLen; j++)
{
T2 = T2base + j * SetSize;
v += T2[string[j] - SetMin];
}
v %= NumVert;
return (g[u] + g[v]) % NumEntry;
}
#if 0
void dispRecord(int i);
void
duplicateKeys(int v1, int v2)
{
int i, j;
uint8_t *keys;
int u, v;
v1--; v2--; /* These guys are origin 1 */
printf("Duplicate keys:\n");
for (i=0; i < NumEntry; i++)
{
getKey(i, &keys);
u = 0;
for (j=0; j < EntryLen; j++)
{
T1 = T1base + j * SetSize;
u += T1[keys[j] - SetMin];
}
u %= NumVert;
if ((u != v1) and (u != v2)) continue;
v = 0;
for (j=0; j < EntryLen; j++)
{
T2 = T2base + j * SetSize;
v += T2[keys[j] - SetMin];
}
v %= NumVert;
if ((v == v2) or (v == v1))
{
printf("Entry #%d key: ", i+1);
for (j=0; j < EntryLen; j++) printf("%02X ", keys[j]);
printf("\n");
dispRecord(i+1);
}
}
exit(1);
}
#endif

38
common/perfhlib.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include <stdint.h>
/** Perfect hashing function library. Contains functions to generate perfect
hashing functions */
struct PatternCollector;
struct PerfectHash {
uint16_t *T1base;
uint16_t *T2base; /* Pointers to start of T1, T2 */
short *g; /* g[] */
int NumEntry; /* Number of entries in the hash table (# keys) */
int EntryLen; /* Size (bytes) of each entry (size of keys) */
int SetSize; /* Size of the char set */
char SetMin; /* First char in the set */
int NumVert; /* c times NumEntry */
/** Set the parameters for the hash table */
void setHashParams(int _numEntry, int _entryLen, int _setSize, char _setMin, int _numVert);
public:
void map(PatternCollector * collector); /* Part 1 of creating the tables */
void hashCleanup(); /* Frees memory allocated by setHashParams() */
void assign(); /* Part 2 of creating the tables */
int hash(uint8_t *string); /* Hash the string to an int 0 .. NUMENTRY-1 */
const uint16_t *readT1(void) const { return T1base; }
const uint16_t *readT2(void) const { return T2base; }
const uint16_t *readG(void) const { return (uint16_t *)g; }
uint16_t *readT1(void){ return T1base; }
uint16_t *readT2(void){ return T2base; }
uint16_t *readG(void) { return (uint16_t *)g; }
private:
void initGraph();
void addToGraph(int e, int v1, int v2);
bool isCycle();
bool DFS(int parentE, int v);
void traverse(int u);
PatternCollector *m_collector; /* used to retrieve the keys */
};

View File

@ -1,3 +1,4 @@
#!/bin/bash
makedir -p tests/outputs
./test_use_all.sh
./regression_tester.rb ./dcc_original -s -c 2>stderr >stdout; diff -wB tests/prev/ tests/outputs/

View File

@ -3,8 +3,6 @@
#include <vector>
#include <bitset>
#include <string>
#include <llvm/ADT/ilist.h>
#include <llvm/ADT/ilist_node.h>
#include <boost/range/iterator_range.hpp>
#include "icode.h"
#include "types.h"
@ -27,7 +25,7 @@ struct TYPEADR_TYPE
TYPEADR_TYPE(interval *v) : ip(0),BBptr(nullptr),intPtr(v)
{}
};
struct BB : public llvm::ilist_node<BB>
struct BB
{
friend struct Function;
private:
@ -59,10 +57,10 @@ public:
ICODE &front();
ICODE &back();
size_t size();
uint8_t nodeType; /* Type of node */
eDFS traversed; /* last traversal id is held here traversed yet? */
int numHlIcodes; /* No. of high-level icodes */
uint32_t flg; /* BB flags */
uint8_t nodeType; /* Type of node */
eDFS traversed; /* last traversal id is held here traversed yet? */
int numHlIcodes; /* No. of high-level icodes */
uint32_t flg; /* BB flags */
/* In edges and out edges */
std::vector<BB *> inEdges; // does not own held pointers
@ -80,10 +78,10 @@ public:
interval *correspInt; //!< Corresponding interval in derived graph Gi-1
// For live register analysis
// LiveIn(b) = LiveUse(b) U (LiveOut(b) - Def(b))
LivenessSet liveUse; /* LiveUse(b) */
LivenessSet def; /* Def(b) */
LivenessSet liveIn; /* LiveIn(b) */
LivenessSet liveOut; /* LiveOut(b) */
LivenessSet liveUse; /* LiveUse(b) */
LivenessSet def; /* Def(b) */
LivenessSet liveIn; /* LiveIn(b) */
LivenessSet liveOut; /* LiveOut(b) */
/* For structuring analysis */
int dfsFirstNum; /* DFS #: first visit of node */
@ -111,7 +109,7 @@ public:
///
const Function *getParent() const { return Parent; }
Function *getParent() { return Parent; }
void writeBB(std::ostream &ostr, int lev, Function *pProc, int *numLoc);
void writeBB(QTextStream & ostr, int lev, Function *pProc, int *numLoc);
BB * rmJMP(int marker, BB *pBB);
void genDU1();
void findBBExps(LOCAL_ID &locals, Function *f);

View File

@ -1,22 +1,25 @@
#pragma once
#include <stdint.h>
#include <vector>
struct PROG /* Loaded program image parameters */
{
int16_t initCS;
int16_t initIP; /* These are initial load values */
int16_t initSS; /* Probably not of great interest */
uint16_t initSP;
bool fCOM; /* Flag set if COM program (else EXE)*/
int cReloc; /* No. of relocation table entries */
uint32_t * relocTable; /* Ptr. to relocation table */
uint8_t * map; /* Memory bitmap ptr */
int cProcs; /* Number of procedures so far */
int offMain; /* The offset of the main() proc */
uint16_t segMain; /* The segment of the main() proc */
bool bSigs; /* True if signatures loaded */
int cbImage; /* Length of image in bytes */
uint16_t initCS=0;
uint16_t initIP=0; /* These are initial load values */
uint16_t initSS=0; /* Probably not of great interest */
uint16_t initSP=0;
bool fCOM=false; /* Flag set if COM program (else EXE)*/
int cReloc=0; /* No. of relocation table entries */
std::vector<uint32_t> relocTable; /* Ptr. to relocation table */
uint8_t * map=nullptr; /* Memory bitmap ptr */
int cProcs=0; /* Number of procedures so far */
int offMain=0; /* The offset of the main() proc */
uint16_t segMain=0; /* The segment of the main() proc */
bool bSigs=false; /* True if signatures loaded */
int cbImage=0; /* Length of image in bytes */
uint8_t * Imagez=nullptr; /* Allocated by loader to hold entire program image */
int addressingMode=0;
public:
const uint8_t *image() const {return Imagez;}
uint8_t * Imagez; /* Allocated by loader to hold entire program image */
int addressingMode;
void displayLoadInfo();
};

View File

@ -1,14 +1,16 @@
#pragma once
#include "ast.h"
class QTextStream;
struct CConv {
enum Type {
UNKNOWN=0,
C,
PASCAL
eUnknown=0,
eCdecl,
ePascal
};
virtual void processHLI(Function *func, Expr *_exp, iICODE picode)=0;
virtual void writeComments(std::ostream &)=0;
virtual void writeComments(QTextStream &)=0;
static CConv * create(Type v);
protected:
@ -16,16 +18,16 @@ protected:
struct C_CallingConvention : public CConv {
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
virtual void writeComments(std::ostream &);
virtual void writeComments(QTextStream &);
private:
int processCArg(Function *callee, Function *pProc, ICODE *picode, size_t numArgs);
};
struct Pascal_CallingConvention : public CConv {
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
virtual void writeComments(std::ostream &);
virtual void writeComments(QTextStream &);
};
struct Unknown_CallingConvention : public CConv {
void processHLI(Function *func, Expr *_exp, iICODE picode) {}
virtual void writeComments(std::ostream &);
virtual void writeComments(QTextStream &);
};

19
include/CallGraph.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "Procedure.h"
/* CALL GRAPH NODE */
struct CALL_GRAPH
{
ilFunction proc; /* Pointer to procedure in pProcList */
std::vector<CALL_GRAPH *> outEdges; /* array of out edges */
public:
void write();
CALL_GRAPH()
{
}
public:
void writeNodeCallGraph(int indIdx);
bool insertCallGraph(ilFunction caller, ilFunction callee);
bool insertCallGraph(Function *caller, ilFunction callee);
void insertArc(ilFunction newProc);
};
//extern CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */

17
include/DccFrontend.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <QObject>
class Project;
class DccFrontend : public QObject
{
Q_OBJECT
void LoadImage();
void parse(Project &proj);
std::string m_fname;
public:
explicit DccFrontend(QObject *parent = 0);
bool FrontEnd(); /* frontend.c */
signals:
public slots:
};

View File

@ -90,16 +90,16 @@ enum eLLFlags
/* Types of icodes */
enum icodeType
{
NOT_SCANNED = 0, // not even scanned yet
LOW_LEVEL, // low-level icode
HIGH_LEVEL // high-level icode
NOT_SCANNED_ICODE = 0, // not even scanned yet
LOW_LEVEL_ICODE, // low-level icode
HIGH_LEVEL_ICODE // high-level icode
};
/* LOW_LEVEL icode opcodes */
enum llIcode
{
//iINVALID,
iINVALID=-1,
iCBW, /* 0 */
iAAA,
iAAD,

View File

@ -1,14 +1,18 @@
#pragma once
#include <llvm/ADT/ilist.h>
//#include <llvm/ADT/ilist_node.h>
#include <bitset>
#include <map>
#include "BasicBlock.h"
#include "locident.h"
#include "state.h"
#include "icode.h"
#include "StackFrame.h"
#include "CallConvention.h"
#include <QtCore/QString>
#include <bitset>
#include <map>
class QIODevice;
class QTextStream;
/* PROCEDURE NODE */
struct CALL_GRAPH;
struct Expr;
@ -19,29 +23,6 @@ struct PROG;
struct Function;
namespace llvm
{
// Traits for intrusive list of basic blocks...
template<>
struct ilist_traits<BB> : public ilist_default_traits<BB>
{
// createSentinel is used to get hold of the node that marks the end of the
// list... (same trick used here as in ilist_traits<Instruction>)
BB *createSentinel() const {
return static_cast<BB*>(&Sentinel);
}
static void destroySentinel(BB*) {}
BB *provideInitialHead() const { return createSentinel(); }
BB *ensureHead(BB*) const { return createSentinel(); }
static void noteHead(BB*, BB*) {}
//static ValueSymbolTable *getSymTab(Function *ItemParent);
private:
mutable ilist_half_node<BB> Sentinel;
};
}
/* Procedure FLAGS */
enum PROC_FLAGS
{
@ -107,9 +88,9 @@ public:
}
void push_back(BB *v) { m_listBB.push_back(v);}
};
struct Function : public llvm::ilist_node<Function>
struct Function
{
typedef llvm::iplist<BB> BasicBlockListType;
typedef std::list<BB *> BasicBlockListType;
// BasicBlock iterators...
typedef BasicBlockListType::iterator iterator;
typedef BasicBlockListType::const_iterator const_iterator;
@ -119,15 +100,15 @@ protected:
hasCase(false),liveAnal(0)
{
type = new FunctionType;
callingConv(CConv::UNKNOWN);
callingConv(CConv::eUnknown);
}
public:
FunctionType * type;
CConv * m_call_conv;
uint32_t procEntry; /* label number */
std::string name; /* Meaningful name for this proc */
STATE state; /* Entry state */
QString name; /* Meaningful name for this proc */
STATE state; /* Entry state */
int depth; /* Depth at which we found it - for printing */
uint32_t flg; /* Combination of Icode & Proc flags */
int16_t cbParam; /* Probable no. of bytes of parameters */
@ -153,7 +134,7 @@ public:
delete type;
}
public:
static Function *Create(FunctionType *ty=0,int /*Linkage*/=0,const std::string &nm="",void */*module*/=0)
static Function *Create(FunctionType *ty=0,int /*Linkage*/=0,const QString &nm="",void */*module*/=0)
{
Function *r=new Function(ty);
r->name = nm;
@ -185,14 +166,14 @@ public:
bool process_JMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
bool process_CALL(ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
void freeCFG();
void codeGen(std::ostream &fs);
void codeGen(QIODevice & fs);
void mergeFallThrough(BB *pBB);
void structIfs();
void structLoops(derSeq *derivedG);
void buildCFG(Disassembler &ds);
void controlFlowAnalysis();
void newRegArg(iICODE picode, iICODE ticode);
void writeProcComments(std::ostream &ostr);
void writeProcComments(QTextStream & ostr);
void displayCFG();
void displayStats();
@ -200,7 +181,7 @@ public:
void preprocessReturnDU(LivenessSet &_liveOut);
Expr * adjustActArgType(Expr *_exp, hlType forType);
std::string writeCall(Function *tproc, STKFRAME &args, int *numLoc);
QString writeCall(Function *tproc, STKFRAME &args, int *numLoc);
void processDosInt(STATE *pstate, PROG &prog, bool done);
ICODE *translate_DIV(LLInst *ll, ICODE &_Icode);
ICODE *translate_XCHG(LLInst *ll, ICODE &_Icode);
@ -233,26 +214,12 @@ protected:
void genLiveKtes();
bool findDerivedSeq(derSeq &derivedGi);
bool nextOrderGraph(derSeq &derivedGi);
};
namespace llvm {
template<> struct ilist_traits<typename ::Function>
: public ilist_default_traits<typename ::Function> {
// createSentinel is used to get hold of the node that marks the end of the
// list... (same trick used here as in ilist_traits<Instruction>)
typename ::Function *createSentinel() const {
return static_cast<typename ::Function*>(&Sentinel);
}
static void destroySentinel(typename ::Function*) {}
typename ::Function *provideInitialHead() const { return createSentinel(); }
typename ::Function *ensureHead(::Function*) const { return createSentinel(); }
static void noteHead(typename ::Function*, typename ::Function*) {}
void addOutEdgesForConditionalJump(BB* pBB, int next_ip, LLInst *ll);
private:
mutable ilist_node<typename ::Function> Sentinel;
bool decodeIndirectJMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
bool decodeIndirectJMP2(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
};
}
typedef llvm::iplist<Function> FunctionListType;
typedef std::list<Function> FunctionListType;
typedef FunctionListType lFunction;
typedef lFunction::iterator ilFunction;

View File

@ -5,11 +5,15 @@
* (C) Cristina Cifuentes
*/
#pragma once
#include "Enums.h"
#include "msvc_fixes.h"
#include <boost/range/iterator_range.hpp>
#include <stdint.h>
#include <cstring>
#include <list>
#include <boost/range/iterator_range.hpp>
#include "Enums.h"
static const int operandSize=20;
/* The following definitions and types define the Conditional Expression
* attributed syntax tree, as defined by the following EBNF:
@ -54,7 +58,7 @@ public:
/** Recursively deallocates the abstract syntax tree rooted at *exp */
virtual ~Expr() {}
public:
virtual std::string walkCondExpr (Function * pProc, int* numLoc) const=0;
virtual QString walkCondExpr (Function * pProc, int* numLoc) const=0;
virtual Expr *inverse() const=0; // return new COND_EXPR that is invarse of this
virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId)=0;
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)=0;
@ -96,10 +100,12 @@ struct UnaryOperator : public Expr
}
public:
int hlTypeSize(Function *pproc) const;
virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
virtual QString walkCondExpr(Function *pProc, int *numLoc) const;
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
virtual hlType expType(Function *pproc) const;
virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
private:
QString wrapUnary(Function *pProc, int *numLoc, QChar op) const;
};
struct BinaryOperator : public Expr
@ -120,7 +126,7 @@ struct BinaryOperator : public Expr
}
~BinaryOperator()
{
assert(m_lhs!=m_rhs || m_lhs==nullptr);
assert(m_lhs!=m_rhs or m_lhs==nullptr);
delete m_lhs;
delete m_rhs;
m_lhs=m_rhs=nullptr;
@ -184,7 +190,7 @@ struct BinaryOperator : public Expr
condOp op() const { return m_op;}
/* Changes the boolean conditional operator at the root of this expression */
void op(condOp o) { m_op=o;}
std::string walkCondExpr (Function * pProc, int* numLoc) const;
QString walkCondExpr(Function * pProc, int* numLoc) const;
public:
hlType expType(Function *pproc) const;
int hlTypeSize(Function *pproc) const;
@ -211,13 +217,10 @@ struct AstIdent : public UnaryOperator
virtual int hlTypeSize(Function *pproc) const;
virtual hlType expType(Function *pproc) const;
virtual Expr * performLongRemoval(eReg regi, LOCAL_ID *locId);
virtual std::string walkCondExpr(Function *pProc, int *numLoc) const;
virtual QString walkCondExpr(Function *pProc, int *numLoc) const;
virtual Expr *insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym);
virtual Expr *insertSubTreeLongReg(Expr *_expr, int longIdx);
virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
protected:
eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl);
};
struct GlobalVariable : public AstIdent
{
@ -228,7 +231,7 @@ struct GlobalVariable : public AstIdent
return new GlobalVariable(*this);
}
GlobalVariable(int16_t segValue, int16_t off);
std::string walkCondExpr(Function *pProc, int *numLoc) const;
QString walkCondExpr(Function *pProc, int *numLoc) const;
int hlTypeSize(Function *pproc) const;
hlType expType(Function *pproc) const;
};
@ -242,7 +245,7 @@ struct GlobalVariableIdx : public AstIdent
return new GlobalVariableIdx(*this);
}
GlobalVariableIdx(int16_t segValue, int16_t off, uint8_t regi, const LOCAL_ID *locSym);
std::string walkCondExpr(Function *pProc, int *numLoc) const;
QString walkCondExpr(Function *pProc, int *numLoc) const;
int hlTypeSize(Function *pproc) const;
hlType expType(Function *pproc) const;
};
@ -264,9 +267,9 @@ struct Constant : public AstIdent
{
return new Constant(*this);
}
std::string walkCondExpr(Function *pProc, int *numLoc) const;
QString walkCondExpr(Function *pProc, int *numLoc) const;
int hlTypeSize(Function *pproc) const;
hlType expType(Function *pproc) const;
hlType expType(Function *pproc) const { return TYPE_CONST; }
};
struct FuncNode : public AstIdent
{
@ -284,7 +287,7 @@ struct FuncNode : public AstIdent
{
return new FuncNode(*this);
}
std::string walkCondExpr(Function *pProc, int *numLoc) const;
QString walkCondExpr(Function *pProc, int *numLoc) const;
int hlTypeSize(Function *pproc) const;
hlType expType(Function *pproc) const;
};
@ -310,7 +313,7 @@ struct RegisterNode : public AstIdent
{
return new RegisterNode(*this);
}
std::string walkCondExpr(Function *pProc, int *numLoc) const;
QString walkCondExpr(Function *pProc, int *numLoc) const;
int hlTypeSize(Function *) const;
hlType expType(Function *pproc) const;
bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);

View File

@ -7,8 +7,10 @@
#pragma once
#include <stdio.h>
#include <vector>
#include <string>
struct strTable : std::vector<std::string>
#include <QtCore/QString>
#include <QtCore/QIODevice>
struct strTable : std::vector<QString>
{
/* Returns the next available index into the table */
size_t nextIdx() {return size();}
@ -20,9 +22,9 @@ struct bundle
{
public:
void appendCode(const char *format, ...);
void appendCode(const std::string &s);
void appendCode(const QString &s);
void appendDecl(const char *format, ...);
void appendDecl(const std::string &);
void appendDecl(const QString &);
void init()
{
decl.clear();
@ -37,6 +39,6 @@ extern bundle cCode;
#define lineSize 360 /* 3 lines in the mean time */
//void newBundle (bundle *procCode);
void writeBundle (std::ostream &ios, bundle procCode);
void writeBundle (QIODevice & ios, bundle procCode);
void freeBundle (bundle *procCode);

View File

@ -5,10 +5,10 @@
#pragma once
//TODO: Remove boolT
#include <llvm/ADT/ilist.h>
#include <utility>
#include <algorithm>
#include <bitset>
#include <QtCore/QString>
#include "Enums.h"
#include "types.h"
@ -26,26 +26,27 @@ extern bundle cCode; /* Output C procedure's declaration and code */
/**** Global variables ****/
extern char *asm1_name, *asm2_name; /* Assembler output filenames */
extern QString asm1_name, asm2_name; /* Assembler output filenames */
typedef struct { /* Command line option flags */
unsigned verbose : 1;
unsigned VeryVerbose : 1;
unsigned asm1 : 1; /* Early disassembly listing */
unsigned asm2 : 1; /* Disassembly listing after restruct */
unsigned Map : 1;
unsigned Stats : 1;
unsigned Interact : 1; /* Interactive mode */
unsigned Calls : 1; /* Follow register indirect calls */
char filename[80]; /* The input filename */
} OPTION;
/** Command line option flags */
struct OPTION
{
bool verbose;
bool VeryVerbose;
bool asm1; /* Early disassembly listing */
bool asm2; /* Disassembly listing after restruct */
bool Map;
bool Stats;
bool Interact; /* Interactive mode */
bool Calls; /* Follow register indirect calls */
QString filename; /* The input filename */
uint32_t CustomEntryPoint;
};
extern OPTION option; /* Command line options */
#include "BinaryImage.h"
/* Memory map states */
enum eAreaType
{
@ -71,22 +72,11 @@ extern STATS stats; /* Icode statistics */
/**** Global function prototypes ****/
class DccFrontend
{
void LoadImage(Project &proj);
void parse(Project &proj);
std::string m_fname;
public:
DccFrontend(const std::string &fname) : m_fname(fname)
{
}
bool FrontEnd(); /* frontend.c */
};
void udm(void); /* udm.c */
void freeCFG(BB * cfg); /* graph.c */
BB * newBB(BB *, int, int, uint8_t, int, Function *); /* graph.c */
void BackEnd(char *filename, CALL_GRAPH *); /* backend.c */
void BackEnd(CALL_GRAPH *); /* backend.c */
extern char *cChar(uint8_t c); /* backend.c */
eErrorId scan(uint32_t ip, ICODE &p); /* scanner.c */
void parse (CALL_GRAPH * *); /* parser.c */
@ -97,20 +87,18 @@ void interactDis(Function *, int initIC); /* disassem.c */
bool JmpInst(llIcode opcode); /* idioms.c */
queue::iterator appendQueue(queue &Q, BB *node); /* reducible.c */
void SetupLibCheck(void); /* chklib.c */
bool SetupLibCheck(void); /* chklib.c */
void CleanupLibCheck(void); /* chklib.c */
bool LibCheck(Function &p); /* chklib.c */
/* Exported functions from hlicode.c */
const char *writeJcond(const HLTYPE &, Function *, int *);
const char *writeJcondInv (HLTYPE, Function *, int *);
QString writeJcond(const HLTYPE &, Function *, int *);
QString writeJcondInv(HLTYPE, Function *, int *);
/* Exported funcions from locident.c */
bool checkLongEq(LONG_STKID_TYPE, iICODE, int, Function *, Assignment &asgn, LLInst &atOffset);
bool checkLongRegEq(LONGID_TYPE, iICODE, int, Function *, Assignment &asgn, LLInst &);
eReg otherLongRegi(eReg, int, LOCAL_ID *);
extern const char *indentStr(int level);

24
include/dcc_interface.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include "Procedure.h"
#include <QtCore/QObject>
#include <QtCore/QDir>
class IXmlTarget;
struct IDcc {
static IDcc *get();
virtual void BaseInit()=0;
virtual void Init(QObject *tgt)=0;
virtual lFunction::iterator GetFirstFuncHandle()=0;
virtual lFunction::iterator GetCurFuncHandle()=0;
virtual void analysis_Once()=0;
virtual void load(QString name)=0; // load and preprocess -> find entry point
virtual void prtout_asm(IXmlTarget *,int level=0)=0;
virtual void prtout_cpp(IXmlTarget *,int level=0)=0;
virtual size_t getFuncCount()=0;
virtual const lFunction &validFunctions() const =0;
virtual void SetCurFunc_by_Name(QString )=0;
virtual QDir installDir()=0;
virtual QDir dataDir(QString kind)=0;
};

View File

@ -5,17 +5,22 @@
***************************************************************************
*/
#pragma once
#include "bundle.h"
#include <fstream>
#include <vector>
#include "bundle.h"
#include <QString>
#include <QTextStream>
struct LLInst;
struct Function;
struct Disassembler
{
protected:
int pass;
int g_lab;
//bundle &cCode;
std::ofstream m_fp;
QIODevice *m_disassembly_target;
QTextStream m_fp;
std::vector<std::string> m_decls;
std::vector<std::string> m_code;

View File

@ -3,6 +3,16 @@
* (C) Cristina Cifuentes
****************************************************************************/
#pragma once
#include "msvc_fixes.h"
#include "BinaryImage.h"
#include "libdis.h"
#include "Enums.h"
#include "state.h" // State depends on INDEXBASE, but later need STATE
#include "CallConvention.h"
#include <boost/range/iterator_range.hpp>
#include <QtCore/QString>
#include <memory>
#include <vector>
#include <list>
@ -10,15 +20,6 @@
#include <set>
#include <algorithm>
#include <initializer_list>
#include <llvm/ADT/ilist.h>
#include <llvm/ADT/ilist_node.h>
#include <llvm/MC/MCInst.h>
#include <llvm/IR/Instruction.h>
#include <boost/range/iterator_range.hpp>
#include "libdis.h"
#include "Enums.h"
#include "state.h" // State depends on INDEXBASE, but later need STATE
#include "CallConvention.h"
//enum condId;
@ -58,12 +59,7 @@ public:
}
friend void swap(LivenessSet& first, LivenessSet& second) // nothrow
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
// by swapping the members of two classes,
// the two classes are effectively swapped
swap(first.registers, second.registers);
std::swap(first.registers, second.registers);
}
LivenessSet &operator|=(const LivenessSet &other)
{
@ -141,8 +137,8 @@ struct UnaryOperator;
struct HlTypeSupport
{
//hlIcode opcode; /* hlIcode opcode */
virtual bool removeRegFromLong(eReg regi, LOCAL_ID *locId)=0;
virtual std::string writeOut(Function *pProc, int *numLoc) const=0;
virtual bool removeRegFromLong(eReg regi, LOCAL_ID *locId)=0;
virtual QString writeOut(Function *pProc, int *numLoc) const=0;
protected:
Expr * performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree);
};
@ -157,37 +153,37 @@ struct CallType : public HlTypeSupport
void placeStkArg(Expr *exp, int pos);
virtual Expr * toAst();
public:
bool removeRegFromLong(eReg /*regi*/, LOCAL_ID */*locId*/)
bool removeRegFromLong(eReg /*regi*/, LOCAL_ID * /*locId*/)
{
printf("CallType : removeRegFromLong not supproted");
printf("CallType : removeRegFromLong not supproted\n");
return false;
}
std::string writeOut(Function *pProc, int *numLoc) const;
QString writeOut(Function *pProc, int *numLoc) const;
};
struct AssignType : public HlTypeSupport
{
/* for HLI_ASSIGN */
protected:
public:
Expr *m_lhs;
Expr *rhs;
Expr * m_lhs;
Expr * m_rhs;
AssignType() {}
Expr *lhs() const {return m_lhs;}
void lhs(Expr *l);
bool removeRegFromLong(eReg regi, LOCAL_ID *locId);
std::string writeOut(Function *pProc, int *numLoc) const;
QString writeOut(Function *pProc, int *numLoc) const;
};
struct ExpType : public HlTypeSupport
{
/* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
Expr *v;
ExpType() : v(0) {}
Expr * v;
ExpType() : v(nullptr) {}
bool removeRegFromLong(eReg regi, LOCAL_ID *locId)
{
v=performLongRemoval(regi,locId,v);
return true;
}
std::string writeOut(Function *pProc, int *numLoc) const;
QString writeOut(Function *pProc, int *numLoc) const;
};
struct HLTYPE
@ -241,7 +237,7 @@ public:
return *this;
}
public:
std::string write1HlIcode(Function *pProc, int *numLoc) const;
QString write1HlIcode(Function *pProc, int *numLoc) const;
void setAsgn(Expr *lhs, Expr *rhs);
} ;
/* LOW_LEVEL icode operand record */
@ -275,12 +271,12 @@ struct LLOperand
}
bool operator==(const LLOperand &with) const
{
return (seg==with.seg) &&
(segOver==with.segOver) &&
(segValue==with.segValue) &&
(regi == with.regi) &&
(off == with.off) &&
(opz==with.opz) &&
return (seg==with.seg) and
(segOver==with.segOver) and
(segValue==with.segValue) and
(regi == with.regi) and
(off == with.off) and
(opz==with.opz) and
(proc.proc==with.proc.proc);
}
int64_t getImm2() const {return opz;}
@ -314,9 +310,10 @@ struct LLOperand
bool compound() const {return is_compound;} // dx:ax pair
size_t byteWidth() const { assert(width<=4); return width;}
};
struct LLInst : public llvm::MCInst //: public llvm::ilist_node<LLInst>
struct LLInst
{
protected:
llIcode m_opcode; // Low level opcode identifier
uint32_t flg; /* icode flags */
LLOperand m_src; /* source operand */
public:
@ -328,22 +325,25 @@ public:
int caseEntry;
std::vector<uint32_t> caseTbl2;
int hllLabNum; /* label # for hll codegen */
bool conditionalJump()
{
return (getOpcode() >= iJB) && (getOpcode() < iJCXZ);
}
bool testFlags(uint32_t x) const { return (flg & x)!=0;}
void setFlags(uint32_t flag) {flg |= flag;}
void clrFlags(uint32_t flag)
{
if(getOpcode()==iMOD)
{
assert(false);
}
flg &= ~flag;
}
uint32_t getFlag() const {return flg;}
uint32_t GetLlLabel() const { return label;}
llIcode getOpcode() const { return m_opcode;}
void setOpcode(uint32_t op) { m_opcode=(llIcode)op; }
bool conditionalJump()
{
return (getOpcode() >= iJB) and (getOpcode() < iJCXZ);
}
bool testFlags(uint32_t x) const { return (flg & x)!=0;}
void setFlags(uint32_t flag) {flg |= flag;}
void clrFlags(uint32_t flag)
{
if(getOpcode()==iMOD)
{
assert(false);
}
flg &= ~flag;
}
uint32_t getFlag() const {return flg;}
uint32_t GetLlLabel() const { return label;}
void SetImmediateOp(uint32_t dw) {m_src.SetImmediateOp(dw);}
@ -366,11 +366,11 @@ public:
}
bool match(llIcode op,eReg dest,eReg src_reg)
{
return (getOpcode()==op)&&(m_dst.regi==dest)&&(m_src.regi==src_reg);
return (getOpcode()==op) and (m_dst.regi==dest) and (m_src.regi==src_reg);
}
bool match(eReg dest,eReg src_reg)
{
return (m_dst.regi==dest)&&(m_src.regi==src_reg);
return (m_dst.regi==dest) and (m_src.regi==src_reg);
}
bool match(eReg dest)
{
@ -400,11 +400,11 @@ public:
}
void emitGotoLabel(int indLevel);
void findJumpTargets(CIcodeRec &_pc);
void writeIntComment(std::ostringstream &s);
void writeIntComment(QTextStream & s);
void dis1Line(int loc_ip, int pass);
std::ostringstream &strSrc(std::ostringstream &os,bool skip_comma=false);
QTextStream & strSrc(QTextStream & os, bool skip_comma=false);
void flops(std::ostringstream &out);
void flops(QTextStream & out);
bool isJmpInst();
HLTYPE createCall();
LLInst(ICODE *container) : flg(0),codeIdx(0),numBytes(0),m_link(container)
@ -460,17 +460,17 @@ public:
template<int TYPE>
struct TypeFilter
{
bool operator()(ICODE *ic) {return ic->type==HIGH_LEVEL;}
bool operator()(ICODE &ic) {return ic.type==HIGH_LEVEL;}
bool operator()(ICODE *ic) {return ic->type==TYPE;}
bool operator()(ICODE &ic) {return ic.type==TYPE;}
};
template<int TYPE>
struct TypeAndValidFilter
{
bool operator()(ICODE *ic) {return (ic->type==HIGH_LEVEL)&&(ic->valid());}
bool operator()(ICODE &ic) {return (ic.type==HIGH_LEVEL)&&ic.valid();}
bool operator()(ICODE *ic) {return (ic->type==TYPE) and (ic->valid());}
bool operator()(ICODE &ic) {return (ic.type==TYPE) and ic.valid();}
};
static TypeFilter<HIGH_LEVEL> select_high_level;
static TypeAndValidFilter<HIGH_LEVEL> select_valid_high_level;
static TypeFilter<HIGH_LEVEL_ICODE> select_high_level;
static TypeAndValidFilter<HIGH_LEVEL_ICODE> select_valid_high_level;
/* Def/Use of registers and stack variables */
struct DU_ICODE
{
@ -506,7 +506,7 @@ public:
if(iter==uses.end())
return;
uses.erase(iter);
assert("Same user more then once!" && uses.end()==std::find(uses.begin(),uses.end(),us));
assert("Same user more then once!" and uses.end()==std::find(uses.begin(),uses.end(),us));
}
};
@ -544,8 +544,8 @@ public:
}
};
icodeType type; /* Icode type */
DU_ICODE du; /* Def/use regs/vars */
DU1 du1; /* du chain 1 */
DU_ICODE du; /* Def/use regs/vars */
DU1 du1; /* du chain 1 */
int loc_ip; // used by CICodeRec to number ICODEs
LLInst * ll() { return &m_ll;}
@ -572,7 +572,7 @@ public:
// set this icode to be an assign
void setAsgn(Expr *lhs, Expr *rhs)
{
type=HIGH_LEVEL;
type=HIGH_LEVEL_ICODE;
hlU()->setAsgn(lhs,rhs);
}
void setUnary(hlIcode op, Expr *_exp);
@ -589,7 +589,7 @@ public:
{
return hlU()->call.newStkArg(exp,opcode,pproc);
}
ICODE() : m_ll(this),Parent(0),invalid(false),type(NOT_SCANNED),loc_ip(0)
ICODE() : m_ll(this),Parent(0),invalid(false),type(NOT_SCANNED_ICODE),loc_ip(0)
{
}
public:
@ -600,24 +600,23 @@ public:
};
/** Map n low level instructions to m high level instructions
*/
struct MappingLLtoML
{
typedef llvm::iplist<llvm::Instruction> InstListType;
typedef boost::iterator_range<iICODE> rSourceRange;
typedef boost::iterator_range<InstListType::iterator> rTargetRange;
rSourceRange m_low_level;
rTargetRange m_middle_level;
};
//struct MappingLLtoML
//{
// typedef boost::iterator_range<iICODE> rSourceRange;
// typedef boost::iterator_range<InstListType::iterator> rTargetRange;
// rSourceRange m_low_level;
// rTargetRange m_middle_level;
//};
// This is the icode array object.
class CIcodeRec : public std::list<ICODE>
{
public:
CIcodeRec(); // Constructor
ICODE * addIcode(ICODE *pIcode);
void SetInBB(rCODE &rang, BB* pnewBB);
bool labelSrch(uint32_t target, uint32_t &pIndex);
ICODE * addIcode(ICODE *pIcode);
void SetInBB(rCODE &rang, BB* pnewBB);
bool labelSrch(uint32_t target, uint32_t &pIndex);
iterator labelSrch(uint32_t target);
ICODE * GetIcode(size_t ip);
ICODE * GetIcode(size_t ip);
bool alreadyDecoded(uint32_t target);
};

View File

@ -6,14 +6,17 @@
*/
#pragma once
#include "msvc_fixes.h"
#include "types.h"
#include "Enums.h"
#include "machine_x86.h"
#include <QtCore/QString>
#include <stdint.h>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include "types.h"
#include "Enums.h"
#include "machine_x86.h"
/* Type definition */
// this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
@ -25,7 +28,7 @@ struct LLInst;
typedef std::list<ICODE>::iterator iICODE;
struct IDX_ARRAY : public std::vector<iICODE>
{
bool inList(iICODE idx)
bool inList(iICODE idx) const
{
return std::find(begin(),end(),idx)!=end();
}
@ -93,23 +96,22 @@ protected:
LONGID_TYPE m_longId; /* For TYPE_LONG_(UN)SIGN registers */
public:
hlType type; /* Probable type */
bool illegal; /* Boolean: not a valid field any more */
//std::vector<iICODE> idx;
IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */
frameType loc; /* Frame location */
bool illegal; /* Boolean: not a valid field any more */
bool hasMacro; /* Identifier requires a macro */
char macro[10]; /* Macro for this identifier */
std::string name; /* Identifier's name */
QString name; /* Identifier's name */
union ID_UNION { /* Different types of identifiers */
friend struct ID;
protected:
LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
public:
eReg regi; /* For TYPE_BYTE(uint16_t)_(UN)SIGN registers */
struct { /* For TYPE_BYTE(uint16_t)_(UN)SIGN on the stack */
eReg regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */
struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */
uint8_t regOff; /* register offset (if any) */
int off; /* offset from BP */
} bwId;
} bwId;
BWGLB_TYPE bwGlb; /* For TYPE_BYTE(uint16_t)_(UN)SIGN globals */
LONGGLB_TYPE longGlb;
struct { /* For TYPE_LONG_(UN)SIGN constants */
@ -118,27 +120,30 @@ public:
} longKte;
ID_UNION() { /*new (&longStkId) LONG_STKID_TYPE();*/}
} id;
LONGID_TYPE & longId() {assert(isLong() && loc==REG_FRAME); return m_longId;}
const LONGID_TYPE & longId() const {assert(isLong() && loc==REG_FRAME); return m_longId;}
LONG_STKID_TYPE & longStkId() {assert(isLong() && loc==STK_FRAME); return id.longStkId;}
const LONG_STKID_TYPE & longStkId() const {assert(isLong() && loc==STK_FRAME); return id.longStkId;}
LONGID_TYPE & longId() {assert(isLong() and loc==REG_FRAME); return m_longId;}
const LONGID_TYPE & longId() const {assert(isLong() and loc==REG_FRAME); return m_longId;}
LONG_STKID_TYPE & longStkId() {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
const LONG_STKID_TYPE & longStkId() const {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
ID();
ID(hlType t, frameType f);
ID(hlType t, const LONGID_TYPE &s);
ID(hlType t, const LONG_STKID_TYPE &s);
ID(hlType t, const LONGGLB_TYPE &s);
bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);}
bool isSigned() const { return (type==TYPE_BYTE_SIGN) or (type==TYPE_WORD_SIGN) or (type==TYPE_LONG_SIGN);}
uint16_t typeBitsize() const
{
return TypeContainer::typeSize(type)*8;
}
bool isLong() const { return (type==TYPE_LONG_UNSIGN)||(type==TYPE_LONG_SIGN); }
bool isLong() const { return (type==TYPE_LONG_UNSIGN) or (type==TYPE_LONG_SIGN); }
void setLocalName(int i)
{
char buf[32];
sprintf (buf, "loc%d", i);
name=buf;
}
bool isLongRegisterPair() const { return (loc == REG_FRAME) and isLong();}
eReg getPairedRegister(eReg first) const;
};
struct LOCAL_ID
@ -164,12 +169,13 @@ public:
int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
void newIdent(hlType t, frameType f);
void flagByteWordId(int off);
void propLongId(uint8_t regL, uint8_t regH, const char *name);
void propLongId(uint8_t regL, uint8_t regH, const QString & name);
size_t csym() const {return id_arr.size();}
void newRegArg(iICODE picode, iICODE ticode) const;
void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong) const;
void forwardSubs(Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
void newRegArg(ICODE & picode, ICODE & ticode) const;
void processTargetIcode(ICODE & picode, int &numHlIcodes, ICODE & ticode, bool isLong) const;
void forwardSubs(Expr *lhs, Expr *rhs, ICODE & picode, ICODE & ticode, int &numHlIcodes) const;
AstIdent *createId(const ID *retVal, iICODE ix_);
eReg getPairedRegisterAt(int idx,eReg first) const;
};

View File

@ -1,9 +1,11 @@
#pragma once
#include <QtCore/QString>
#include <stdint.h>
#include <string>
#include <sstream>
#include <bitset>
class QTextStream;
struct LivenessSet;
/* Machine registers */
enum eReg
@ -60,13 +62,13 @@ class Machine_X86 : public SourceMachine
public:
Machine_X86();
virtual ~Machine_X86() {}
static const std::string &regName(eReg r);
static const std::string &opcodeName(unsigned r);
static const std::string &floatOpName(unsigned r);
static const QString & regName(eReg r);
static const QString & opcodeName(unsigned r);
static const QString & floatOpName(unsigned r);
bool physicalReg(eReg r);
/* Writes the registers that are set in the bitvector */
//TODO: move this into Machine_X86 ?
static void writeRegVector (std::ostream &ostr,const LivenessSet &regi);
static void writeRegVector (QTextStream & ostr, const LivenessSet &regi);
static eReg subRegH(eReg reg);
static eReg subRegL(eReg reg);
static bool isMemOff(eReg r);

3
include/msvc_fixes.h Normal file
View File

@ -0,0 +1,3 @@
#ifdef _MSC_VER
#include <iso646.h>
#endif

View File

@ -1,38 +0,0 @@
#pragma once
/* Perfect hashing function library. Contains functions to generate perfect
hashing functions
* (C) Mike van Emmerik
*/
#include <stdint.h>
/* Prototypes */
void hashCleanup(void); /* Frees memory allocated by hashParams() */
void map(void); /* Part 1 of creating the tables */
/* The application must provide these functions: */
void getKey(int i, uint8_t **pKeys);/* Set *keys to point to the i+1th key */
void dispKey(int i); /* Display the key */
class PatternHasher
{
uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
int NumEntry; /* Number of entries in the hash table (# keys) */
int EntryLen; /* Size (bytes) of each entry (size of keys) */
int SetSize; /* Size of the char set */
char SetMin; /* First char in the set */
int NumVert; /* c times NumEntry */
int *graphNode; /* The array of edges */
int *graphNext; /* Linked list of edges */
int *graphFirst;/* First edge at a vertex */
public:
uint16_t *readT1(void); /* Returns a pointer to the T1 table */
uint16_t *readT2(void); /* Returns a pointer to the T2 table */
uint16_t *readG(void); /* Returns a pointer to the g table */
void init(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,int _NumVert); /* Set the parameters for the hash table */
void cleanup();
int hash(unsigned char *string); //!< Hash the string to an int 0 .. NUMENTRY-1
};
extern PatternHasher g_pattern_hasher;
/* Macro reads a LH uint16_t from the image regardless of host convention */
#ifndef LH
#define LH(p) ((int)((uint8_t *)(p))[0] + ((int)((uint8_t *)(p))[1] << 8))
#endif

View File

@ -3,65 +3,72 @@
#include <stdint.h>
#include <cassert>
#include <list>
#include <llvm/ADT/ilist.h>
#include <boost/icl/interval.hpp>
#include <boost/icl/interval_map.hpp>
#include <boost/icl/split_interval_map.hpp>
#include <unordered_set>
#include <QtCore/QString>
#include "symtab.h"
#include "BinaryImage.h"
#include "Procedure.h"
class QString;
class SourceMachine;
struct CALL_GRAPH;
class IProject
{
virtual PROG *binary()=0;
virtual const std::string & project_name() const =0;
virtual const std::string & binary_path() const =0;
virtual const QString & project_name() const =0;
virtual const QString & binary_path() const =0;
};
class Project : public IProject
{
static Project *s_instance;
std::string m_fname;
std::string m_project_name;
static Project *s_instance;
QString m_fname;
QString m_project_name;
QString m_output_path;
public:
typedef llvm::iplist<Function> FunctionListType;
typedef FunctionListType lFunction;
typedef std::list<Function> FunctionListType;
typedef FunctionListType lFunction;
typedef FunctionListType::iterator ilFunction;
SYMTAB symtab; /* Global symbol table */
FunctionListType pProcList;
CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
PROG prog; /* Loaded program image parameters */
// no copies
Project(const Project&) = delete;
const Project &operator=(const Project & l) =delete;
// only moves
Project(); // default constructor,
SYMTAB symtab; /* Global symbol table */
FunctionListType pProcList;
CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
PROG prog; /* Loaded program image parameters */
// no copies
Project(const Project&) = delete;
const Project & operator=(const Project & l) =delete;
// only moves
Project(); // default constructor,
public:
void create(const std::string & a);
const std::string &project_name() const {return m_project_name;}
const std::string &binary_path() const {return m_fname;}
ilFunction funcIter(Function *to_find);
ilFunction findByEntry(uint32_t entry);
ilFunction createFunction(FunctionType *f,const std::string &name);
bool valid(ilFunction iter);
void create(const QString &a);
bool load();
const QString & output_path() const {return m_output_path;}
const QString & project_name() const {return m_project_name;}
const QString & binary_path() const {return m_fname;}
QString output_name(const char *ext);
ilFunction funcIter(Function *to_find);
ilFunction findByEntry(uint32_t entry);
ilFunction createFunction(FunctionType *f, const QString & name);
bool valid(ilFunction iter);
int getSymIdxByAdd(uint32_t adr);
bool validSymIdx(size_t idx);
size_t symbolSize(size_t idx);
hlType symbolType(size_t idx);
const std::string &symbolName(size_t idx);
const SYM &getSymByIdx(size_t idx) const;
int getSymIdxByAddr(uint32_t adr);
bool validSymIdx(size_t idx);
size_t symbolSize(size_t idx);
hlType symbolType(size_t idx);
const QString & symbolName(size_t idx);
const SYM & getSymByIdx(size_t idx) const;
static Project *get();
PROG * binary() {return &prog;}
SourceMachine *machine();
static Project * get();
PROG * binary() {return &prog;}
SourceMachine *machine();
const FunctionListType &functions() const { return pProcList; }
FunctionListType &functions() { return pProcList; }
protected:
void initialize();
void writeGlobSymTable();
void initialize();
void writeGlobSymTable();
};
//extern Project g_proj;

View File

@ -11,7 +11,7 @@
struct STATE
{
uint32_t IP; /* Offset into Image */
int16_t r[INDEX_BX_SI]; /* Value of segs and AX */
uint16_t r[INDEX_BX_SI]; /* Value of segs and AX */
bool f[INDEX_BX_SI]; /* True if r[.] has a value */
struct
{ /* For case stmt indexed reg */

View File

@ -3,10 +3,13 @@
* (C) Mike van Emmerik
*/
#pragma once
#include <string>
#include <stdint.h>
#include "Enums.h"
#include "types.h"
#include "msvc_fixes.h"
#include <QtCore/QString>
#include <string>
#include <stdint.h>
struct Expr;
struct AstIdent;
struct TypeContainer;
@ -15,7 +18,7 @@ struct TypeContainer;
/* * * * * * * * * * * * * * * * * */
struct SymbolCommon
{
std::string name; /* New name for this variable/symbol/argument */
QString name; /* New name for this variable/symbol/argument */
int size; /* Size/maximum size */
hlType type; /* probable type */
eDuVal duVal; /* DEF, USE, VAL */
@ -36,21 +39,13 @@ struct SYM : public SymbolCommon
struct STKSYM : public SymbolCommon
{
typedef int16_t tLabel;
Expr *actual; /* Expression tree of actual parameter */
AstIdent *regs; /* For register arguments only */
tLabel label; /* Immediate off from BP (+:args, -:params) */
uint8_t regOff; /* Offset is a register (e.g. SI, DI) */
bool hasMacro; /* This type needs a macro */
std::string macro; /* Macro name */
bool invalid; /* Boolean: invalid entry in formal arg list*/
STKSYM()
{
actual=0;
regs=0;
label=0;
regOff=0;
invalid=hasMacro = false;
}
Expr * actual=0; /* Expression tree of actual parameter */
AstIdent * regs=0; /* For register arguments only */
tLabel label=0; /* Immediate off from BP (+:args, -:params) */
uint8_t regOff=0; /* Offset is a register (e.g. SI, DI) */
bool hasMacro=false; /* This type needs a macro */
QString macro; /* Macro name */
bool invalid=false; /* Boolean: invalid entry in formal arg list*/
void setArgName(int i)
{
char buf[32];
@ -99,7 +94,7 @@ struct SYMTABLE
{
// does not yse pSymName, to ease finding by symOff/symProc combo
// in map<SYMTABLE,X>
return (symOff==other.symOff) && symProc==(other.symProc);
return (symOff==other.symOff) and symProc==(other.symProc);
}
};
@ -112,6 +107,6 @@ constexpr int NUM_TABLE_TYPES = int(Comment)+1; /* Number of entries: must be la
void createSymTables(void);
void destroySymTables(void);
bool readVal (std::ostringstream &symName, uint32_t symOff, Function *symProc);
bool readVal (QTextStream & symName, uint32_t symOff, Function *symProc);
void selectTable(tableType); /* Select a particular table */

View File

@ -5,9 +5,13 @@
***************************************************************************
*/
#pragma once
#include "Enums.h"
#include "msvc_fixes.h"
#include <cassert>
#include <stdint.h>
#include "Enums.h"
#include <stdlib.h>
/**** Common definitions and macros ****/
#define MAX 0x7FFFFFFF
@ -25,7 +29,7 @@
// Macro reads a LH word from the image regardless of host convention
// Returns a 16 bit quantity, e.g. C000 is read into an Int as C000
//#define LH(p) ((int16)((byte *)(p))[0] + ((int16)((byte *)(p))[1] << 8))
#define LH(p) ((uint16_t)((uint8_t *)(p))[0] + ((uint16_t)((uint8_t *)(p))[1] << 8))
#define LH(p) ((uint16_t)((uint8_t *)(p))[0] + ((uint16_t)((uint8_t *)(p))[1] << 8))
/* Macro reads a LH word from the image regardless of host convention */
@ -64,12 +68,22 @@ struct eDuVal
use = x&USE;
val = x&VAL;
}
bool isUSE_VAL() {return use&&val;} //Use and Val
bool isUSE_VAL() {return use and val;} //Use and Val
};
static constexpr const char * hlTypes[13] = {
"", "char", "unsigned char", "int", "unsigned int",
"long", "unsigned long", "record", "int *", "char *",
"", "float", "double"
"",
"char",
"unsigned char",
"int",
"unsigned int",
"long",
"unsigned long",
"record",
"int *",
"char *",
"",
"float",
"double"
};
struct TypeContainer

BIN
prototypes/dcclibs.dat Normal file

Binary file not shown.

View File

@ -14,9 +14,9 @@ def perform_test(exepath,filepath,outname,args)
filepath=path_local(filepath)
joined_args = args.join(' ')
printf("calling:" + "#{exepath} -a1 #{joined_args} -o#{output_path}.a1 #{filepath}\n")
STDERR << "Errors for : #{filepath}"
result = `#{exepath} -a1 -o#{output_path}.a1 #{filepath}`
result = `#{exepath} -a2 #{joined_args} -o#{output_path}.a2 #{filepath}`
STDERR << "Errors for : #{filepath}\n"
result = `#{exepath} -a 1 -o#{output_path}.a1 #{filepath}`
result = `#{exepath} -a 2 #{joined_args} -o#{output_path}.a2 #{filepath}`
result = `#{exepath} #{joined_args} -o#{output_path} #{filepath}`
puts result
p $?

BIN
sigs/dccb2s.sig Normal file

Binary file not shown.

View File

@ -1,11 +1,16 @@
#include "BasicBlock.h"
#include "msvc_fixes.h"
#include "Procedure.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include <QtCore/QTextStream>
#include <cassert>
#include <string>
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/adaptors.hpp>
#include "BasicBlock.h"
#include "Procedure.h"
#include "dcc.h"
using namespace std;
using namespace boost;
@ -28,11 +33,11 @@ BB *BB::Create(const rCODE &r,eBBKind _nodeType, Function *parent)
pnewBB->loopHead = pnewBB->caseHead = pnewBB->caseTail =
pnewBB->latchNode= pnewBB->loopFollow = NO_NODE;
pnewBB->instructions = r;
int addr = pnewBB->begin()->loc_ip;
/* Mark the basic block to which the icodes belong to, but only for
* real code basic blocks (ie. not interval bbs) */
if(parent)
{
int addr = pnewBB->begin()->loc_ip;
//setInBB should automatically handle if our range is empty
parent->Icode.SetInBB(pnewBB->instructions, pnewBB);
@ -40,10 +45,10 @@ BB *BB::Create(const rCODE &r,eBBKind _nodeType, Function *parent)
parent->m_ip_to_bb[addr] = pnewBB;
parent->m_actual_cfg.push_back(pnewBB);
pnewBB->Parent = parent;
}
if ( r.begin() != parent->Icode.end() ) /* Only for code BB's */
stats.numBBbef++;
}
return pnewBB;
}
@ -90,7 +95,7 @@ void BB::displayDfs()
dfsFirstNum, dfsLastNum,
immedDom == MAX ? -1 : immedDom);
printf("loopType = %s, loopHead = %d, latchNode = %d, follow = %d\n",
s_loopType[loopType],
s_loopType[(int)loopType],
loopHead == MAX ? -1 : loopHead,
latchNode == MAX ? -1 : latchNode,
loopFollow == MAX ? -1 : loopFollow);
@ -136,12 +141,15 @@ void BB::displayDfs()
*/
ICODE* BB::writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&latch, bool &repCond)
{
if(loopType == eNodeHeaderType::NO_TYPE)
return nullptr;
latch = pProc->m_dfsLast[this->latchNode];
std::ostringstream ostr;
QString ostr_contents;
QTextStream ostr(&ostr_contents);
ICODE* picode;
switch (loopType)
{
case WHILE_TYPE:
case eNodeHeaderType::WHILE_TYPE:
picode = &this->back();
/* Check for error in while condition */
@ -163,43 +171,45 @@ ICODE* BB::writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&lat
picode->hlU()->replaceExpr(picode->hl()->expr()->inverse());
}
{
string e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
QString e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
ostr << "\n"<<indentStr(indLevel)<<"while ("<<e<<") {\n";
}
picode->invalidate();
break;
case REPEAT_TYPE:
case eNodeHeaderType::REPEAT_TYPE:
ostr << "\n"<<indentStr(indLevel)<<"do {\n";
picode = &latch->back();
picode->invalidate();
break;
case ENDLESS_TYPE:
case eNodeHeaderType::ENDLESS_TYPE:
ostr << "\n"<<indentStr(indLevel)<<"for (;;) {\n";
picode = &latch->back();
break;
}
cCode.appendCode(ostr.str());
ostr.flush();
cCode.appendCode(ostr_contents);
stats.numHLIcode += 1;
indLevel++;
return picode;
}
bool BB::isEndOfPath(int latch_node_idx) const
{
return nodeType == RETURN_NODE || nodeType == TERMINATE_NODE ||
nodeType == NOWHERE_NODE || (dfsLastNum == latch_node_idx);
return nodeType == RETURN_NODE or nodeType == TERMINATE_NODE or
nodeType == NOWHERE_NODE or dfsLastNum == latch_node_idx;
}
void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode, int _ifFollow)
{
int follow; /* ifFollow */
BB * succ, *latch; /* Successor and latching node */
BB * succ, *latch; /* Successor and latching node */
ICODE * picode; /* Pointer to HLI_JCOND instruction */
std::string l; /* Pointer to HLI_JCOND expression */
QString l; /* Pointer to HLI_JCOND expression */
bool emptyThen, /* THEN clause is empty */
repCond; /* Repeat condition for while() */
repCond; /* Repeat condition for while() */
/* Check if this basic block should be analysed */
if ((_ifFollow != UN_INIT) && (this == pProc->m_dfsLast[_ifFollow]))
if ((_ifFollow != UN_INIT) and (this == pProc->m_dfsLast[_ifFollow]))
return;
if (wasTraversedAtLevel(DFS_ALPHA))
@ -209,17 +219,16 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
/* Check for start of loop */
repCond = false;
latch = nullptr;
if (loopType)
{
picode=writeLoopHeader(indLevel, pProc, numLoc, latch, repCond);
}
/* Write the code for this basic block */
if (repCond == false)
{
std::ostringstream ostr;
QString ostr_contents;
QTextStream ostr(&ostr_contents);
writeBB(ostr,indLevel, pProc, numLoc);
cCode.appendCode(ostr.str());
ostr.flush();
cCode.appendCode(ostr_contents);
}
/* Check for end of path */
@ -227,12 +236,12 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
return;
/* Check type of loop/node and process code */
if ( loopType ) /* there is a loop */
if ( loopType!=eNodeHeaderType::NO_TYPE ) /* there is a loop */
{
assert(latch);
if (this != latch) /* loop is over several bbs */
{
if (loopType == WHILE_TYPE)
if (loopType == eNodeHeaderType::WHILE_TYPE)
{
succ = edges[THEN].BBptr;
if (succ->dfsLastNum == loopFollow)
@ -248,9 +257,10 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
/* Loop epilogue: generate the loop trailer */
indLevel--;
if (loopType == WHILE_TYPE)
if (loopType == eNodeHeaderType::WHILE_TYPE)
{
std::ostringstream ostr;
QString ostr_contents;
QTextStream ostr(&ostr_contents);
/* Check if there is need to repeat other statements involved
* in while condition, then, emit the loop trailer */
if (repCond)
@ -258,13 +268,14 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
writeBB(ostr,indLevel+1, pProc, numLoc);
}
ostr <<indentStr(indLevel)<< "} /* end of while */\n";
cCode.appendCode(ostr.str());
ostr.flush();
cCode.appendCode(ostr_contents);
}
else if (loopType == ENDLESS_TYPE)
else if (loopType == eNodeHeaderType::ENDLESS_TYPE)
cCode.appendCode( "%s} /* end of loop */\n",indentStr(indLevel));
else if (loopType == REPEAT_TYPE)
else if (loopType == eNodeHeaderType::REPEAT_TYPE)
{
string e = "//*failed*//";
QString e = "//*failed*//";
if (picode->hl()->opcode != HLI_JCOND)
{
reportError (REPEAT_FAIL);
@ -273,7 +284,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
{
e=picode->hl()->expr()->walkCondExpr (pProc, numLoc);
}
cCode.appendCode( "%s} while (%s);\n", indentStr(indLevel),e.c_str());
cCode.appendCode( "%s} while (%s);\n", indentStr(indLevel),qPrintable(e));
}
/* Recurse on the loop follow */
@ -305,13 +316,13 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
if (succ->dfsLastNum != follow) /* THEN part */
{
l = writeJcond ( *back().hl(), pProc, numLoc);
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l.c_str());
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), qPrintable(l));
succ->writeCode (indLevel, pProc, numLoc, _latchNode,follow);
}
else /* empty THEN part => negate ELSE part */
{
l = writeJcondInv ( *back().hl(), pProc, numLoc);
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l.c_str());
cCode.appendCode( "\n%s%s", indentStr(indLevel-1), qPrintable(l));
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, follow);
emptyThen = true;
}
@ -331,7 +342,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
}
/* else (empty ELSE part) */
}
else if (! emptyThen) /* already visited => emit label */
else if (not emptyThen) /* already visited => emit label */
{
cCode.appendCode( "%s}\n%selse {\n",
indentStr(indLevel-1), indentStr(indLevel - 1));
@ -347,7 +358,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
else /* no follow => if..then..else */
{
l = writeJcond ( *back().hl(), pProc, numLoc);
cCode.appendCode( "%s%s", indentStr(indLevel-1), l.c_str());
cCode.appendCode( "%s%s", indentStr(indLevel-1), qPrintable(l));
edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
cCode.appendCode( "%s}\n%selse {\n", indentStr(indLevel-1), indentStr(indLevel - 1));
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
@ -370,7 +381,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode,
* Args: pBB: pointer to the current basic block.
* Icode: pointer to the array of icodes for current procedure.
* lev: indentation level - used for formatting. */
void BB::writeBB(std::ostream &ostr,int lev, Function * pProc, int *numLoc)
void BB::writeBB(QTextStream &ostr,int lev, Function * pProc, int *numLoc)
{
/* Save the index into the code table in case there is a later goto
* into this instruction (first instruction of the BB) */
@ -380,10 +391,10 @@ void BB::writeBB(std::ostream &ostr,int lev, Function * pProc, int *numLoc)
for(ICODE &pHli : instructions)
{
if ((pHli.type == HIGH_LEVEL) && ( pHli.valid() )) //TODO: use filtering range here.
if ((pHli.type == HIGH_LEVEL_ICODE) and ( pHli.valid() )) //TODO: use filtering range here.
{
std::string line = pHli.hl()->write1HlIcode(pProc, numLoc);
if (!line.empty())
QString line = pHli.hl()->write1HlIcode(pProc, numLoc);
if (not line.isEmpty())
{
ostr<<indentStr(lev)<<line;
stats.numHLIcode++;

View File

@ -1,36 +1,37 @@
#include <ostream>
#include <cassert>
#include "CallConvention.h"
#include <QtCore/QTextStream>
CConv *CConv::create(Type v)
{
static C_CallingConvention *c_call = nullptr;
static Pascal_CallingConvention *p_call = nullptr;
static Unknown_CallingConvention *u_call= nullptr;
if(!c_call)
if(nullptr==c_call)
c_call = new C_CallingConvention;
if(!p_call)
if(nullptr==p_call)
p_call = new Pascal_CallingConvention;
if(!u_call)
if(nullptr==u_call)
u_call = new Unknown_CallingConvention;
switch(v) {
case UNKNOWN: return u_call;
case C: return c_call;
case PASCAL: return p_call;
case eUnknown: return u_call;
case eCdecl: return c_call;
case ePascal: return p_call;
}
assert(false);
return nullptr;
}
void C_CallingConvention::writeComments(std::ostream &ostr)
void C_CallingConvention::writeComments(QTextStream & ostr)
{
ostr << " * C calling convention.\n";
}
void Pascal_CallingConvention::writeComments(std::ostream &ostr)
void Pascal_CallingConvention::writeComments(QTextStream & ostr)
{
ostr << " * Pascal calling convention.\n";
}
void Unknown_CallingConvention::writeComments(std::ostream &ostr)
void Unknown_CallingConvention::writeComments(QTextStream & ostr)
{
ostr << " * Unknown calling convention.\n";
}

535
src/DccFrontend.cpp Normal file
View File

@ -0,0 +1,535 @@
#include "DccFrontend.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include "project.h"
#include "disassem.h"
#include "CallGraph.h"
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <cstdio>
class Loader
{
bool loadIntoProject(IProject *);
};
struct PSP { /* PSP structure */
uint16_t int20h; /* interrupt 20h */
uint16_t eof; /* segment, end of allocation block */
uint8_t res1; /* reserved */
uint8_t dosDisp[5]; /* far call to DOS function dispatcher */
uint8_t int22h[4]; /* vector for terminate routine */
uint8_t int23h[4]; /* vector for ctrl+break routine */
uint8_t int24h[4]; /* vector for error routine */
uint8_t res2[22]; /* reserved */
uint16_t segEnv; /* segment address of environment block */
uint8_t res3[34]; /* reserved */
uint8_t int21h[6]; /* opcode for int21h and far return */
uint8_t res4[6]; /* reserved */
uint8_t fcb1[16]; /* default file control block 1 */
uint8_t fcb2[16]; /* default file control block 2 */
uint8_t res5[4]; /* reserved */
uint8_t cmdTail[0x80]; /* command tail and disk transfer area */
};
static struct MZHeader { /* EXE file header */
uint8_t sigLo; /* .EXE signature: 0x4D 0x5A */
uint8_t sigHi;
uint16_t lastPageSize; /* Size of the last page */
uint16_t numPages; /* Number of pages in the file */
uint16_t numReloc; /* Number of relocation items */
uint16_t numParaHeader; /* # of paragraphs in the header */
uint16_t minAlloc; /* Minimum number of paragraphs */
uint16_t maxAlloc; /* Maximum number of paragraphs */
uint16_t initSS; /* Segment displacement of stack */
uint16_t initSP; /* Contents of SP at entry */
uint16_t checkSum; /* Complemented checksum */
uint16_t initIP; /* Contents of IP at entry */
uint16_t initCS; /* Segment displacement of code */
uint16_t relocTabOffset; /* Relocation table offset */
uint16_t overlayNum; /* Overlay number */
} header;
#define EXE_RELOCATION 0x10 /* EXE images rellocated to above PSP */
//static void LoadImage(char *filename);
static void displayMemMap(void);
/****************************************************************************
* displayLoadInfo - Displays low level loader type info.
***************************************************************************/
void PROG::displayLoadInfo(void)
{
int i;
printf("File type is %s\n", (fCOM)?"COM":"EXE");
if (not fCOM) {
printf("Signature = %02X%02X\n", header.sigLo, header.sigHi);
printf("File size %% 512 = %04X\n", LH(&header.lastPageSize));
printf("File size / 512 = %04X pages\n", LH(&header.numPages));
printf("# relocation items = %04X\n", LH(&header.numReloc));
printf("Offset to load image = %04X paras\n", LH(&header.numParaHeader));
printf("Minimum allocation = %04X paras\n", LH(&header.minAlloc));
printf("Maximum allocation = %04X paras\n", LH(&header.maxAlloc));
}
printf("Load image size = %08lX\n", cbImage - sizeof(PSP));
printf("Initial SS:SP = %04X:%04X\n", initSS, initSP);
printf("Initial CS:IP = %04X:%04X\n", initCS, initIP);
if (option.VeryVerbose and cReloc)
{
printf("\nRelocation Table\n");
for (i = 0; i < cReloc; i++)
{
printf("%06X -> [%04X]\n", relocTable[i],LH(image() + relocTable[i]));
}
}
printf("\n");
}
/*****************************************************************************
* fill - Fills line for displayMemMap()
****************************************************************************/
static void fill(int ip, char *bf)
{
PROG &prog(Project::get()->prog);
static uint8_t type[4] = {'.', 'd', 'c', 'x'};
uint8_t i;
for (i = 0; i < 16; i++, ip++)
{
*bf++ = ' ';
*bf++ = (ip < prog.cbImage)? type[(prog.map[ip >> 2] >> ((ip & 3) * 2)) & 3]: ' ';
}
*bf = '\0';
}
/*****************************************************************************
* displayMemMap - Displays the memory bitmap
****************************************************************************/
static void displayMemMap(void)
{
PROG &prog(Project::get()->prog);
char c, b1[33], b2[33], b3[33];
uint8_t i;
int ip = 0;
printf("\nMemory Map\n");
while (ip < prog.cbImage)
{
fill(ip, b1);
printf("%06X %s\n", ip, b1);
ip += 16;
for (i = 3, c = b1[1]; i < 32 and c == b1[i]; i += 2)
; /* Check if all same */
if (i > 32)
{
fill(ip, b2); /* Skip until next two are not same */
fill(ip+16, b3);
if (not (strcmp(b1, b2) || strcmp(b1, b3)))
{
printf(" :\n");
do
{
ip += 16;
fill(ip+16, b1);
} while (0==strcmp(b1, b2));
}
}
}
printf("\n");
}
DccFrontend::DccFrontend(QObject *parent) :
QObject(parent)
{
}
/*****************************************************************************
* FrontEnd - invokes the loader, parser, disassembler (if asm1), icode
* rewritter, and displays any useful information.
****************************************************************************/
bool DccFrontend::FrontEnd ()
{
/* Do depth first flow analysis building call graph and procedure list,
* and attaching the I-code to each procedure */
parse (*Project::get());
if (option.asm1)
{
qWarning() << "dcc: writing assembler file "<<asm1_name<<'\n';
}
/* Search through code looking for impure references and flag them */
Disassembler ds(1);
for(Function &f : Project::get()->pProcList)
{
f.markImpure();
if (option.asm1)
{
ds.disassem(&f);
}
}
if (option.Interact)
{
interactDis(&Project::get()->pProcList.front(), 0); /* Interactive disassembler */
}
/* Converts jump target addresses to icode offsets */
for(Function &f : Project::get()->pProcList)
{
f.bindIcodeOff();
}
/* Print memory bitmap */
if (option.Map)
displayMemMap();
return(true); // we no longer own proj !
}
struct DosLoader {
protected:
void prepareImage(PROG &prog,size_t sz,QFile &fp) {
/* Allocate a block of memory for the program. */
prog.cbImage = sz + sizeof(PSP);
prog.Imagez = new uint8_t [prog.cbImage];
prog.Imagez[0] = 0xCD; /* Fill in PSP int 20h location */
prog.Imagez[1] = 0x20; /* for termination checking */
/* Read in the image past where a PSP would go */
if (sz != fp.read((char *)prog.Imagez + sizeof(PSP),sz))
fatalError(CANNOT_READ, fp.fileName().toLocal8Bit().data());
}
};
struct ComLoader : public DosLoader {
bool canLoad(QFile &fp) {
fp.seek(0);
char sig[2];
if(2==fp.read(sig,2)) {
return not (sig[0] == 0x4D and sig[1] == 0x5A);
}
return false;
}
bool load(PROG &prog,QFile &fp) {
fp.seek(0);
/* COM file
* In this case the load module size is just the file length
*/
auto cb = fp.size();
/* COM programs start off with an ORG 100H (to leave room for a PSP)
* This is also the implied start address so if we load the image
* at offset 100H addresses should all line up properly again.
*/
prog.initCS = 0;
prog.initIP = 0x100;
prog.initSS = 0;
prog.initSP = 0xFFFE;
prog.cReloc = 0;
prepareImage(prog, cb, fp);
/* Set up memory map */
cb = (prog.cbImage + 3) / 4;
prog.map = (uint8_t *)malloc(cb);
memset(prog.map, BM_UNKNOWN, (size_t)cb);
return true;
}
};
#if 0
struct RomLoader {
bool canLoad(QFile &fp) {
fp.seek(0xFFF0);
uint8_t sig[1];
if(fp.read((char *)sig,1) == 1)
{
return (sig[0] == 0xEA);
}
return false;
}
bool load(PROG &prog,QFile &fp) {
printf("Loading ROM...\n");
fp.seek(0);
/* ROM file
* In this case the load module size is just the file length
*/
auto cb = fp.size();
fp.seek(cb - 0x10);
uint8_t buf[5];
printf("Going to get CS/IP...\n");
if(fp.read((char *)buf, 5) != 5)
{
return false;
}
fp.seek(0);
/* ROM File, Hard to say where it is suppose to start, so try to trust the
*/
prog.initIP = (buf[2] << 8) | buf[1];
//prog.initCS = 0;
prog.initCS = (buf[4] << 8) | buf[3];
prog.initSS = 0;
prog.initSP = 0xFFFE;
prog.cReloc = 0;
prepareImage(prog, cb, fp);
/* Set up memory map */
cb = (prog.cbImage + 3) / 4;
prog.map = (uint8_t *)malloc(cb);
memset(prog.map, BM_UNKNOWN, (size_t)cb);
return true;
}
protected:
void prepareImage(PROG &prog, size_t sz, QFile &fp)
{
int32_t start = 0x100000 - sz;
/* Allocate a block of memory for the program. */
prog.cbImage = 1 * 1024 * 1024; /* Allocate the whole 1MB memory */
//prog.cbImage = 64 * 1024; /* Allocate the whole 1MB memory */
prog.Imagez = new uint8_t [prog.cbImage];
if (fp.read((char *)prog.Imagez + start, sz) != sz)
//if (fp.read((char *)prog.Imagez, sz) != sz)
{
fatalError(CANNOT_READ, fp.fileName().toLocal8Bit().data());
}
}
};
#else
struct RomLoader {
bool canLoad(QFile &fp) {
fp.seek(0xFFF0);
uint8_t sig[1];
if(fp.read((char *)sig,1) == 1)
{
return (sig[0] == 0xEA);
}
return false;
}
bool load(PROG &prog,QFile &fp) {
fp.seek(0);
/* COM file
* In this case the load module size is just the file length
*/
auto cb = fp.size();
/* COM programs start off with an ORG 100H (to leave room for a PSP)
* This is also the implied start address so if we load the image
* at offset 100H addresses should all line up properly again.
*/
prog.initCS = 0;
prog.initIP = 0x000;
prog.initSS = 0;
prog.initSP = 0xFFFE;
prog.cReloc = 0;
prepareImage(prog, cb, fp);
/* Set up memory map */
cb = (prog.cbImage + 3) / 4;
prog.map = (uint8_t *)malloc(cb);
memset(prog.map, BM_UNKNOWN, (size_t)cb);
return true;
}
protected:
void prepareImage(PROG &prog, size_t sz, QFile &fp)
{
/* Allocate a block of memory for the program. */
prog.cbImage = sz;
prog.Imagez = new uint8_t[prog.cbImage];
if (sz != fp.read((char *)prog.Imagez, sz))
fatalError(CANNOT_READ, fp.fileName().toLocal8Bit().data());
}
};
#endif
struct ExeLoader : public DosLoader {
bool canLoad(QFile &fp) {
if(fp.size()<sizeof(header))
return false;
MZHeader tmp_header;
fp.seek(0);
fp.read((char *)&tmp_header, sizeof(header));
if(not (tmp_header.sigLo == 0x4D and tmp_header.sigHi == 0x5A))
return false;
/* This is a typical DOS kludge! */
if (LH(&header.relocTabOffset) == 0x40)
{
qDebug() << "Don't understand new EXE format";
return false;
}
return true;
}
bool load(PROG &prog,QFile &fp) {
/* Read rest of header */
fp.seek(0);
if (fp.read((char *)&header, sizeof(header)) != sizeof(header))
return false;
/* Calculate the load module size.
* This is the number of pages in the file
* less the length of the header and reloc table
* less the number of bytes unused on last page
*/
uint32_t cb = (uint32_t)LH(&header.numPages) * 512 - (uint32_t)LH(&header.numParaHeader) * 16;
if (header.lastPageSize)
{
cb -= 512 - LH(&header.lastPageSize);
}
/* We quietly ignore minAlloc and maxAlloc since for our
* purposes it doesn't really matter where in real memory
* the program would end up. EXE programs can't really rely on
* their load location so setting the PSP segment to 0 is fine.
* Certainly programs that prod around in DOS or BIOS are going
* to have to load DS from a constant so it'll be pretty
* obvious.
*/
prog.initCS = (int16_t)LH(&header.initCS) + EXE_RELOCATION;
prog.initIP = (int16_t)LH(&header.initIP);
prog.initSS = (int16_t)LH(&header.initSS) + EXE_RELOCATION;
prog.initSP = (int16_t)LH(&header.initSP);
prog.cReloc = (int16_t)LH(&header.numReloc);
/* Allocate the relocation table */
if (prog.cReloc)
{
prog.relocTable.resize(prog.cReloc);
fp.seek(LH(&header.relocTabOffset));
/* Read in seg:offset pairs and convert to Image ptrs */
uint8_t buf[4];
for (int i = 0; i < prog.cReloc; i++)
{
fp.read((char *)buf,4);
prog.relocTable[i] = LH(buf) + (((int)LH(buf+2) + EXE_RELOCATION)<<4);
}
}
/* Seek to start of image */
uint32_t start_of_image= LH(&header.numParaHeader) * 16;
fp.seek(start_of_image);
/* Allocate a block of memory for the program. */
prepareImage(prog,cb,fp);
/* Set up memory map */
cb = (prog.cbImage + 3) / 4;
prog.map = (uint8_t *)malloc(cb);
memset(prog.map, BM_UNKNOWN, (size_t)cb);
/* Relocate segment constants */
for(uint32_t v : prog.relocTable) {
uint8_t *p = &prog.Imagez[v];
uint16_t w = (uint16_t)LH(p) + EXE_RELOCATION;
*p++ = (uint8_t)(w & 0x00FF);
*p = (uint8_t)((w & 0xFF00) >> 8);
}
return true;
}
};
/*****************************************************************************
* LoadImage
****************************************************************************/
bool Project::load()
{
// addTask(loaderSelection,PreCond(BinaryImage))
// addTask(applyLoader,PreCond(Loader))
const char *fname = binary_path().toLocal8Bit().data();
QFile finfo(binary_path());
/* Open the input file */
if(not finfo.open(QFile::ReadOnly)) {
fatalError(CANNOT_OPEN, fname);
}
/* Read in first 2 bytes to check EXE signature */
if (finfo.size()<=2)
{
fatalError(CANNOT_READ, fname);
}
RomLoader rom_loader;
ComLoader com_loader;
ExeLoader exe_loader;
if(rom_loader.canLoad(finfo)) {
/* We have no relacation and code should be on 64K only,
* So let's consider it as a COM file
*/
prog.fCOM = true;
return rom_loader.load(prog,finfo);
}
if(exe_loader.canLoad(finfo)) {
prog.fCOM = false;
return exe_loader.load(prog,finfo);
}
if(com_loader.canLoad(finfo)) {
prog.fCOM = true;
return com_loader.load(prog,finfo);
}
return false;
}
uint32_t SynthLab;
/* Parses the program, builds the call graph, and returns the list of
* procedures found */
void DccFrontend::parse(Project &proj)
{
PROG &prog(proj.prog);
STATE state;
/* Set initial state */
state.setState(rES, 0); /* PSP segment */
state.setState(rDS, 0);
state.setState(rCS, prog.initCS);
state.setState(rSS, prog.initSS);
state.setState(rSP, prog.initSP);
state.IP = ((uint32_t)prog.initCS << 4) + prog.initIP;
SynthLab = SYNTHESIZED_MIN;
/* Check for special settings of initial state, based on idioms of the
startup code */
state.checkStartup();
ilFunction start_proc;
/* Make a struct for the initial procedure */
if (prog.offMain != -1)
{
start_proc = proj.createFunction(0,"main");
start_proc->retVal.loc = REG_FRAME;
start_proc->retVal.type = TYPE_WORD_SIGN;
start_proc->retVal.id.regi = rAX;
/* We know where main() is. Start the flow of control from there */
start_proc->procEntry = prog.offMain;
/* In medium and large models, the segment of main may (will?) not be
the same as the initial CS segment (of the startup code) */
state.setState(rCS, prog.segMain);
state.IP = prog.offMain;
}
else
{
start_proc = proj.createFunction(0,"start");
/* Create initial procedure at program start address */
start_proc->procEntry = (uint32_t)state.IP;
}
/* The state info is for the first procedure */
start_proc->state = state;
/* Set up call graph initial node */
proj.callGraph = new CALL_GRAPH;
proj.callGraph->proc = start_proc;
/* This proc needs to be called to set things up for LibCheck(), which
checks a proc to see if it is a know C (etc) library */
prog.bSigs = SetupLibCheck();
//BUG: proj and g_proj are 'live' at this point !
/* Recursively build entire procedure list */
start_proc->FollowCtrl(proj.callGraph, &state);
/* This proc needs to be called to clean things up from SetupLibCheck() */
CleanupLibCheck();
}

View File

@ -1,6 +1,9 @@
#include "Procedure.h"
#include "msvc_fixes.h"
#include "project.h"
#include "scanner.h"
//FunctionType *Function::getFunctionType() const
//{
// return &m_type;
@ -15,7 +18,7 @@ void JumpTable::pruneEntries(uint16_t cs)
for (uint32_t i = start; i < finish; i += 2)
{
uint32_t target = cs + LH(&prg->image()[i]);
if (target < finish && target >= start)
if (target < finish and target >= start)
finish = target;
else if (target >= (uint32_t)prg->cbImage)
finish = i;
@ -25,7 +28,7 @@ void JumpTable::pruneEntries(uint16_t cs)
{
uint32_t target = cs + LH(&prg->image()[i]);
/* Be wary of 00 00 as code - it's probably data */
if (! (prg->image()[target] || prg->image()[target+1]) || scan(target, _Icode))
if (not (prg->image()[target] or prg->image()[target+1]) or scan(target, _Icode))
finish = i;
}

View File

@ -1,19 +1,20 @@
#include "types.h"
#include "msvc_fixes.h"
#include "ast.h"
#include "bundle.h"
#include "machine_x86.h"
#include "project.h"
#include <stdint.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
//#include <boost/range/algorithm.hpp>
//#include <boost/assign.hpp>
#include "types.h"
#include "ast.h"
#include "bundle.h"
#include "machine_x86.h"
#include "project.h"
using namespace std;
using namespace boost::adaptors;
RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
@ -41,7 +42,7 @@ RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
// ident.type(REGISTER);
// hlType type_sel;
// regType reg_type;
// if ((icodeFlg & B) || (icodeFlg & SRC_B))
// if ((icodeFlg & B) or (icodeFlg & SRC_B))
// {
// type_sel = TYPE_BYTE_SIGN;
// reg_type = BYTE_REG;
@ -55,42 +56,42 @@ RegisterNode::RegisterNode(const LLOperand &op, LOCAL_ID *locsym)
// regiType = reg_type;
//}
string RegisterNode::walkCondExpr(Function *pProc, int *numLoc) const
QString RegisterNode::walkCondExpr(Function *pProc, int *numLoc) const
{
std::ostringstream codeOut;
QString codeOut;
std::ostringstream o;
QString o;
assert(&pProc->localId==m_syms);
ID *id = &pProc->localId.id_arr[regiIdx];
if (id->name[0] == '\0') /* no name */
{
id->setLocalName(++(*numLoc));
codeOut <<TypeContainer::typeName(id->type)<< " "<<id->name<<"; ";
codeOut <<"/* "<<Machine_X86::regName(id->id.regi)<<" */\n";
codeOut += QString("%1 %2; ").arg(TypeContainer::typeName(id->type)).arg(id->name);
codeOut += QString("/* %1 */\n").arg(Machine_X86::regName(id->id.regi));
}
if (id->hasMacro)
o << id->macro << "("<<id->name<<")";
o += QString("%1(%2)").arg(id->macro).arg(id->name);
else
o << id->name;
o += id->name;
cCode.appendDecl(codeOut.str());
return o.str();
cCode.appendDecl(codeOut);
return o;
}
int RegisterNode::hlTypeSize(Function *) const
{
if (regiType == BYTE_REG)
return (1);
return 1;
else
return (2);
return 2;
}
hlType RegisterNode::expType(Function *pproc) const
{
if (regiType == BYTE_REG)
return (TYPE_BYTE_SIGN);
return TYPE_BYTE_SIGN;
else
return (TYPE_WORD_SIGN);
return TYPE_WORD_SIGN;
}
Expr *RegisterNode::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)

View File

@ -4,34 +4,41 @@
* Date: September 1993
* (C) Cristina Cifuentes
*/
#include "ast.h"
#include "msvc_fixes.h"
#include "types.h"
#include "bundle.h"
#include "machine_x86.h"
#include "project.h"
#include <QtCore/QTextStream>
#include <QtCore/QDebug>
#include <boost/range.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/assign.hpp>
#include <stdint.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/assign.hpp>
#include "types.h"
#include "ast.h"
#include "bundle.h"
#include "machine_x86.h"
#include "project.h"
using namespace std;
using namespace boost;
using namespace boost::adaptors;
extern int strSize (const uint8_t *, char);
extern char *cChar(uint8_t c);
namespace
{
// Conditional operator symbols in C. Index by condOp enumeration type
static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
" & ", " | ", " ^ ", " ~ ",
" + ", " - ", " * ", " / ",
" >> ", " << ", " % ", " && ", " || " };
constexpr const char * condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
" & ", " | ", " ^ ", " ~ ",
" + ", " - ", " * ", " / ",
" >> ", " << ", " % ", " && ", " || " };
/* Size of hl types */
constexpr const int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
/* Local expression stack */
//typedef struct _EXP_STK {
@ -40,13 +47,13 @@ static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ",
//} EXP_STK; - for local expression stack
/* Returns the integer i in C hexadecimal format */
static const char *hexStr (uint16_t i)
const char *hexStr (uint16_t i)
{
static char buf[10];
sprintf (buf, "%s%x", (i > 9) ? "0x" : "", i);
return (buf);
return buf;
}
}
/* Sets the du record for registers according to the du flag */
void ICODE::setRegDU (eReg regi, operDu du_in)
@ -121,7 +128,7 @@ GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
valid = true;
ident.idType = GLOB_VAR;
adr = opAdr(segValue, off);
auto i=Project::get()->getSymIdxByAdd(adr);
auto i=Project::get()->getSymIdxByAddr(adr);
if ( not Project::get()->validSymIdx(i) )
{
printf ("Error, glob var not found in symtab\n");
@ -130,10 +137,10 @@ GlobalVariable::GlobalVariable(int16_t segValue, int16_t off)
globIdx = i;
}
string GlobalVariable::walkCondExpr(Function *, int *) const
QString GlobalVariable::walkCondExpr(Function *, int *) const
{
if(valid)
return Project::get()->symtab[globIdx].name;
return Project::get()->symbolName(globIdx);
return "INVALID GlobalVariable";
}
@ -146,7 +153,7 @@ AstIdent *AstIdent::Loc(int off, LOCAL_ID *localId)
for (i = 0; i < localId->csym(); i++)
{
const ID &lID(localId->id_arr[i]);
if ((lID.id.bwId.off == off) && (lID.id.bwId.regOff == 0))
if ((lID.id.bwId.off == off) and (lID.id.bwId.regOff == 0))
break;
}
if (i == localId->csym())
@ -161,7 +168,7 @@ AstIdent *AstIdent::Loc(int off, LOCAL_ID *localId)
AstIdent *AstIdent::Param(int off, const STKFRAME * argSymtab)
{
AstIdent *newExp;
newExp = new AstIdent();
newExp->ident.idType = PARAM;
auto iter=argSymtab->findByLabel(off);
@ -181,19 +188,17 @@ GlobalVariableIdx::GlobalVariableIdx (int16_t segValue, int16_t off, uint8_t reg
for (i = 0; i < locSym->csym(); i++)
{
const BWGLB_TYPE &lID(locSym->id_arr[i].id.bwGlb);
if ((lID.seg == segValue) && (lID.off == off) && (lID.regi == regi))
if ((lID.seg == segValue) and (lID.off == off) and (lID.regi == regi))
break;
}
if (i == locSym->csym())
printf ("Error, indexed-glob var not found in local id table\n");
idxGlbIdx = i;
}
string GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
QString GlobalVariableIdx::walkCondExpr(Function *pProc, int *) const
{
ostringstream o;
auto bwGlb = &pProc->localId.id_arr[idxGlbIdx].id.bwGlb;
o << (bwGlb->seg << 4) + bwGlb->off << "["<<Machine_X86::regName(bwGlb->regi)<<"]";
return o.str();
return QString("%1[%2]").arg((bwGlb->seg << 4) + bwGlb->off).arg(Machine_X86::regName(bwGlb->regi));
}
@ -221,7 +226,7 @@ AstIdent *AstIdent::Long(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f,
{
AstIdent *newExp;
/* Check for long constant and save it as a constant expression */
if ((sd == SRC) && pIcode->ll()->testFlags(I)) /* constant */
if ((sd == SRC) and pIcode->ll()->testFlags(I)) /* constant */
{
int value;
if (f == HIGH_FIRST)
@ -258,18 +263,18 @@ AstIdent *AstIdent::Other(eReg seg, eReg regi, int16_t off)
* TYPE_WORD_SIGN */
AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
{
int idx;
AstIdent *newExp=nullptr;
switch(retVal->type)
{
case TYPE_LONG_SIGN:
{
newExp = new AstIdent();
idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), ix_);
int idx = locsym->newLongReg (TYPE_LONG_SIGN, retVal->longId(), ix_);
newExp->ident.idType = LONG_VAR;
newExp->ident.idNode.longIdx = idx;
break;
}
case TYPE_WORD_UNSIGN:
case TYPE_WORD_SIGN:
newExp = new RegisterNode(locsym->newByteWordReg(retVal->type, retVal->id.regi),WORD_REG,locsym);
break;
@ -291,13 +296,13 @@ AstIdent *AstIdent::idID (const ID *retVal, LOCAL_ID *locsym, iICODE ix_)
Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_,ICODE &duIcode, operDu du)
{
Expr *newExp;
int idx; /* idx into pIcode->localId table */
const LLOperand &pm(*ll_insn.get(sd));
if ( ((sd == DST) && ll_insn.testFlags(IM_DST)) or
((sd == SRC) && ll_insn.testFlags(IM_SRC)) or
if ( ((sd == DST) and ll_insn.testFlags(IM_DST)) or
((sd == SRC) and ll_insn.testFlags(IM_SRC)) or
(sd == LHS_OP)) /* for MUL lhs */
{ /* implicit dx:ax */
idx = pProc->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(rDX, rAX), ix_);
@ -305,14 +310,14 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
duIcode.setRegDU (rDX, du);
duIcode.setRegDU (rAX, du);
}
else if ((sd == DST) && ll_insn.testFlags(IM_TMP_DST))
else if ((sd == DST) and ll_insn.testFlags(IM_TMP_DST))
{ /* implicit tmp */
newExp = new RegisterNode(LLOperand(rTMP,2), &pProc->localId);
duIcode.setRegDU(rTMP, (operDu)eUSE);
}
else if ((sd == SRC) && ll_insn.testFlags(I)) /* constant */
else if ((sd == SRC) and ll_insn.testFlags(I)) /* constant */
newExp = new Constant(ll_insn.src().getImm2(), 2);
else if (pm.regi == rUNDEF) /* global variable */
newExp = new GlobalVariable(pm.segValue, pm.off);
@ -322,17 +327,17 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
newExp = new RegisterNode(pm, &pProc->localId);
duIcode.setRegDU( pm.regi, du);
}
else if (pm.off) /* offset */
{ // TODO: this is ABI specific, should be actually based on Function calling conv
if ((pm.seg == rSS) && (pm.regi == INDEX_BP)) /* idx on bp */
if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) /* idx on bp */
{
if (pm.off >= 0) /* argument */
newExp = AstIdent::Param (pm.off, &pProc->args);
else /* local variable */
newExp = AstIdent::Loc (pm.off, &pProc->localId);
}
else if ((pm.seg == rDS) && (pm.regi == INDEX_BX)) /* bx */
else if ((pm.seg == rDS) and (pm.regi == INDEX_BX)) /* bx */
{
if (pm.off > 0) /* global variable */
newExp = new GlobalVariableIdx(pm.segValue, pm.off, rBX,&pProc->localId);
@ -344,10 +349,9 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
newExp = AstIdent::Other (pm.seg, pm.regi, pm.off);
/**** check long ops, indexed global var *****/
}
else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
{
if ((pm.seg == rDS) && (pm.regi > INDEX_BP_DI)) /* dereference */
if ((pm.seg == rDS) and (pm.regi > INDEX_BP_DI)) /* dereference */
{
eReg selected;
switch (pm.regi) {
@ -367,7 +371,7 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
else
newExp = AstIdent::Other (pm.seg, pm.regi, 0);
}
return (newExp);
return newExp;
}
@ -375,33 +379,30 @@ Expr *AstIdent::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICODE ix_
condId LLInst::idType(opLoc sd) const
{
const LLOperand &pm((sd == SRC) ? src() : m_dst);
if ((sd == SRC) && testFlags(I))
return (CONSTANT);
if ((sd == SRC) and testFlags(I))
return CONSTANT;
else if (pm.regi == 0)
return (GLOB_VAR);
return GLOB_VAR;
else if ( pm.isReg() )
return (REGISTER);
else if ((pm.seg == rSS) && (pm.regi == INDEX_BP))
return REGISTER;
else if ((pm.seg == rSS) and (pm.regi == INDEX_BP)) // TODO: this assumes BP-based function frames !
{
//TODO: which pm.seg/pm.regi pairs should produce PARAM/LOCAL_VAR ?
if (pm.off >= 0)
return (PARAM);
else
return (LOCAL_VAR);
return PARAM;
return LOCAL_VAR;
}
else
return (OTHER);
return OTHER;
}
/* Size of hl types */
int hlSize[] = {2, 1, 1, 2, 2, 4, 4, 4, 2, 2, 1, 4, 4};
int Expr::hlTypeSize(Function * pproc) const
{
if (this == nullptr)
return (2); /* for TYPE_UNKNOWN */
return 2; /* for TYPE_UNKNOWN */
fprintf(stderr,"hlTypeSize queried for Unkown type %d \n",m_type);
return 2; // CC: is this correct?
}
@ -436,11 +437,11 @@ int AstIdent::hlTypeSize(Function *pproc) const
case PARAM:
return (hlSize[pproc->args[ident.idNode.paramIdx].type]);
case STRING:
return (2);
return 2;
case LONG_VAR:
return (4);
return 4;
case OTHER:
return (2);
return 2;
default:
assert(false);
return -1;
@ -523,48 +524,46 @@ Expr * HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, Expr *tree
}
/* Returns the string located in image, formatted in C format. */
static std::string getString (int offset)
static QString getString (int offset)
{
PROG &prog(Project::get()->prog);
ostringstream o;
QString o;
int strLen, i;
strLen = strSize (&prog.image()[offset], '\0');
o << '"';
o += '"';
for (i = 0; i < strLen; i++)
o<<cChar(prog.image()[offset+i]);
o << "\"\0";
return (o.str());
o += cChar(prog.image()[offset+i]);
o += "\"\0";
return o;
}
string BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
QString BinaryOperator::walkCondExpr(Function * pProc, int* numLoc) const
{
std::ostringstream outStr;
outStr << "(";
if(m_op!=NOT)
{
outStr << lhs()->walkCondExpr(pProc, numLoc);
}
assert(rhs());
outStr << condOpSym[m_op];
outStr << rhs()->walkCondExpr(pProc, numLoc);
outStr << ")";
return outStr.str();
return QString("(%1%2%3)")
.arg((m_op!=NOT) ? lhs()->walkCondExpr(pProc, numLoc) : "")
.arg(condOpSym[m_op])
.arg(rhs()->walkCondExpr(pProc, numLoc));
}
string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
QString AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
{
int16_t off; /* temporal - for OTHER */
ID* id; /* Pointer to local identifier table */
BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */
STKSYM * psym; /* Pointer to argument in the stack */
std::ostringstream outStr,codeOut;
std::ostringstream o;
QString codeContents;
QString collectedContents;
QTextStream codeOut(&codeContents);
QTextStream o(&collectedContents);
switch (ident.idType)
{
case LOCAL_VAR:
o << pProc->localId.id_arr[ident.idNode.localIdx].name;
break;
case PARAM:
psym = &pProc->args[ident.idNode.paramIdx];
if (psym->hasMacro)
@ -575,7 +574,7 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
case STRING:
o << getString (ident.idNode.strIdx);
break;
case LONG_VAR:
id = &pProc->localId.id_arr[ident.idNode.longIdx];
if (id->name[0] != '\0') /* STK_FRAME & REG w/name*/
@ -587,7 +586,7 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
codeOut <<"/* "<<Machine_X86::regName(id->longId().h()) << ":" <<
Machine_X86::regName(id->longId().l()) << " */\n";
o << id->name;
pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name.c_str());
pProc->localId.propLongId (id->longId().l(),id->longId().h(), id->name);
}
else /* GLB_FRAME */
{
@ -595,6 +594,10 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"]";
else if (id->id.longGlb.regi == rBX)
o << "[" << (id->id.longGlb.seg<<4) + id->id.longGlb.offH <<"][bx]";
else {
qCritical() << "AstIdent::walkCondExpr unhandled LONG_VAR in GLB_FRAME";
assert(false);
}
}
break;
case OTHER:
@ -610,82 +613,63 @@ string AstIdent::walkCondExpr(Function *pProc, int *numLoc) const
default:
assert(false);
return "";
} /* eos */
outStr << o.str();
cCode.appendDecl(codeOut.str());
return outStr.str();
cCode.appendDecl(codeContents);
return collectedContents;
}
string UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
QString UnaryOperator::wrapUnary(Function *pProc, int *numLoc,QChar op) const
{
std::ostringstream outStr;
bool needBracket=true;
QString outStr = op;
QString inner = unaryExp->walkCondExpr (pProc, numLoc);
if (unaryExp->m_type == IDENTIFIER)
outStr += inner;
else
outStr += "("+inner+')';
return outStr;
}
QString UnaryOperator::walkCondExpr(Function *pProc, int *numLoc) const
{
QString outStr;
switch(m_type)
{
case NEGATION:
if (unaryExp->m_type == IDENTIFIER)
{
needBracket = false;
outStr << "!";
}
else
outStr << "! (";
outStr << unaryExp->walkCondExpr (pProc, numLoc);
if (needBracket == true)
outStr << ")";
outStr+=wrapUnary(pProc,numLoc,'!');
break;
case ADDRESSOF:
if (unaryExp->m_type == IDENTIFIER)
{
needBracket = false;
outStr << "&";
}
else
outStr << "&(";
outStr << unaryExp->walkCondExpr (pProc, numLoc);
if (needBracket == true)
outStr << ")";
outStr+=wrapUnary(pProc,numLoc,'&');
break;
case DEREFERENCE:
outStr << "*";
if (unaryExp->m_type == IDENTIFIER)
needBracket = false;
else
outStr << "(";
outStr << unaryExp->walkCondExpr (pProc, numLoc);
if (needBracket == true)
outStr << ")";
outStr+=wrapUnary(pProc,numLoc,'*');
break;
case POST_INC:
outStr << unaryExp->walkCondExpr (pProc, numLoc) << "++";
outStr += unaryExp->walkCondExpr (pProc, numLoc) + "++";
break;
case POST_DEC:
outStr << unaryExp->walkCondExpr (pProc, numLoc) << "--";
outStr += unaryExp->walkCondExpr (pProc, numLoc) + "--";
break;
case PRE_INC:
outStr << "++"<< unaryExp->walkCondExpr (pProc, numLoc);
outStr += "++" + unaryExp->walkCondExpr (pProc, numLoc);
break;
case PRE_DEC:
outStr << "--"<< unaryExp->walkCondExpr (pProc, numLoc);
outStr += "--" + unaryExp->walkCondExpr (pProc, numLoc);
break;
}
return outStr.str();
return outStr;
}
/* Walks the conditional expression tree and returns the result on a string */
/* Changes the boolean conditional operator at the root of this expression */
void BinaryOperator::changeBoolOp (condOp newOp)
{
@ -707,7 +691,7 @@ bool Expr::insertSubTreeReg (AstIdent *&tree, Expr *_expr, eReg regi,const LOCAL
* register regi */
bool Expr::insertSubTreeReg (Expr *&tree, Expr *_expr, eReg regi,const LOCAL_ID *locsym)
{
if (tree == nullptr)
return false;
Expr *temp=tree->insertSubTreeReg(_expr,regi,locsym);
@ -722,7 +706,7 @@ bool Expr::insertSubTreeReg (Expr *&tree, Expr *_expr, eReg regi,const LOCAL_ID
Expr *UnaryOperator::insertSubTreeReg(Expr *_expr, eReg regi, const LOCAL_ID *locsym)
{
Expr *temp;
switch (m_type) {
case NEGATION:
case ADDRESSOF:
@ -816,9 +800,8 @@ Expr *BinaryOperator::insertSubTreeLongReg(Expr *_expr, int longIdx)
Expr *AstIdent::insertSubTreeLongReg(Expr *_expr, int longIdx)
{
if (ident.idNode.longIdx == longIdx)
{
return _expr;
}
return nullptr;
}
@ -834,10 +817,10 @@ Expr *BinaryOperator::clone() const
Expr *BinaryOperator::inverse() const
{
static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
DUMMY, DBL_OR, DBL_AND};
constexpr static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
DUMMY, DUMMY, DUMMY, DUMMY, DUMMY, DUMMY,
DUMMY, DBL_OR, DBL_AND};
BinaryOperator *res=reinterpret_cast<BinaryOperator *>(this->clone());
switch (m_op)
{
@ -845,11 +828,11 @@ Expr *BinaryOperator::inverse() const
case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
res->m_op = invCondOp[m_op];
return res;
case AND: case OR: case XOR: case NOT: case ADD:
case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
return UnaryOperator::Create(NEGATION, res);
case DBL_AND: case DBL_OR:
res->m_op = invCondOp[m_op];
res->m_lhs=m_lhs->inverse ();
@ -860,43 +843,25 @@ Expr *BinaryOperator::inverse() const
} /* eos */
assert(false);
return res;
}
Expr *AstIdent::performLongRemoval(eReg regi, LOCAL_ID *locId)
{
eReg otherRegi; /* high or low part of long register */
if (ident.idType == LONG_VAR)
{
otherRegi = otherLongRegi (regi, ident.idNode.longIdx, locId);
delete this;
return new RegisterNode(locId->newByteWordReg(TYPE_WORD_SIGN,otherRegi),WORD_REG,locId);
}
return this;
}
eReg AstIdent::otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
{
ID *id = &locTbl->id_arr[idx];
if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
(id->type == TYPE_LONG_UNSIGN)))
{
if (id->longId().h() == regi)
return (id->longId().l());
else if (id->longId().l() == regi)
return (id->longId().h());
}
return rUNDEF; // Cristina: please check this!
if (ident.idType != LONG_VAR)
return this;
otherRegi = locId->getPairedRegisterAt(ident.idNode.longIdx,regi);
bool long_was_signed = locId->id_arr[ident.idNode.longIdx].isSigned();
delete this;
return new RegisterNode(locId->newByteWordReg(long_was_signed ? TYPE_WORD_SIGN : TYPE_WORD_UNSIGN,otherRegi),WORD_REG,locId);
}
string Constant::walkCondExpr(Function *, int *) const
QString Constant::walkCondExpr(Function *, int *) const
{
ostringstream o;
if (kte.kte < 1000)
o << kte.kte;
else
o << "0x"<<std::hex << kte.kte;
return o.str();
return QString::number(kte.kte);
return "0x" + QString::number(kte.kte,16);
}
int Constant::hlTypeSize(Function *) const
@ -904,12 +869,7 @@ int Constant::hlTypeSize(Function *) const
return kte.size;
}
hlType Constant::expType(Function *pproc) const
{
return TYPE_CONST;
}
string FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
QString FuncNode::walkCondExpr(Function *pProc, int *numLoc) const
{
return pProc->writeCall(call.proc,*call.args, numLoc);
}

View File

@ -4,10 +4,20 @@
* Purpose: Back-end module. Generates C code for each procedure.
* (C) Cristina Cifuentes
****************************************************************************/
#include "dcc.h"
#include "msvc_fixes.h"
#include "disassem.h"
#include "project.h"
#include "CallGraph.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QStringList>
#include <QtCore/QDebug>
#include <cassert>
#include <string>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm.hpp>
#include <fstream>
@ -15,15 +25,13 @@
#include <sstream>
#include <string.h>
#include <stdio.h>
#include "dcc.h"
#include "disassem.h"
#include "project.h"
#include "CallGraph.h"
using namespace boost;
using namespace boost::adaptors;
bundle cCode; /* Procedure declaration and code */
using namespace std;
bundle cCode; /* Procedure declaration and code */
/* Returns a unique index to the next label */
int getNextLabel()
{
@ -35,14 +43,14 @@ int getNextLabel()
/* displays statistics on the subroutine */
void Function::displayStats ()
{
printf("\nStatistics - Subroutine %s\n", name.c_str());
printf ("Number of Icode instructions:\n");
printf (" Low-level : %4d\n", stats.numLLIcode);
if (! (flg & PROC_ASM))
qDebug() << "\nStatistics - Subroutine" << name;
qDebug() << "Number of Icode instructions:";
qDebug() << " Low-level :" << stats.numLLIcode;
if (not (flg & PROC_ASM))
{
printf (" High-level: %4d\n", stats.numHLIcode);
printf (" Percentage reduction: %2.2f%%\n", 100.0 - (stats.numHLIcode *
100.0) / stats.numLLIcode);
qDebug() << " High-level:"<<stats.numHLIcode;
qDebug() << QString(" Percentage reduction: %1%%").arg(100.0 - (stats.numHLIcode *
100.0) / stats.numLLIcode,4,'f',2,QChar('0'));
}
}
@ -100,7 +108,7 @@ char *cChar (uint8_t c)
* Note: to get to the value of the variable:
* com file: prog.Image[operand]
* exe file: prog.Image[operand+0x100] */
static void printGlobVar (std::ostream &ostr,SYM * psym)
static void printGlobVar (QTextStream &ostr,SYM * psym)
{
int j;
PROG &prog(Project::get()->prog);
@ -123,10 +131,10 @@ static void printGlobVar (std::ostream &ostr,SYM * psym)
break;
default:
{
ostringstream strContents;
QString strContents;
for (j=0; j < psym->size; j++)
strContents << cChar(prog.image()[relocOp + j]);
ostr << "char\t*"<<psym->name<<" = \""<<strContents.str()<<"\";\n";
strContents += cChar(prog.image()[relocOp + j]);
ostr << "char\t*"<<psym->name<<" = \""<<strContents<<"\";\n";
}
}
}
@ -137,7 +145,8 @@ static void printGlobVar (std::ostream &ostr,SYM * psym)
* initialization. */
void Project::writeGlobSymTable()
{
std::ostringstream ostr;
QString contents;
QTextStream ostr(&contents);
if (symtab.empty())
return;
@ -145,13 +154,13 @@ void Project::writeGlobSymTable()
for (SYM &sym : symtab)
{
if (sym.duVal.isUSE_VAL()) /* first used */
printGlobVar (ostr,&sym);
printGlobVar (ostr,&sym);
else { /* first defined */
switch (sym.size) {
case 1: ostr<<"uint8_t\t"; break;
case 2: ostr<<"int\t"; break;
case 2: ostr<<"int16_t\t"; break;
case 4: if (sym.type == TYPE_PTR)
ostr<<"int\t*";
ostr<<"int32_t\t*";
else
ostr<<"char\t*";
break;
@ -161,19 +170,20 @@ void Project::writeGlobSymTable()
}
}
ostr<< "\n";
cCode.appendDecl( ostr.str() );
ostr.flush();
cCode.appendDecl( contents );
}
/* Writes the header information and global variables to the output C file
* fp. */
static void writeHeader (std::ostream &_ios, char *fileName)
static void writeHeader (QIODevice &_ios, const std::string &fileName)
{
PROG &prog(Project::get()->prog);
/* Write header information */
cCode.init();
cCode.appendDecl( "/*\n");
cCode.appendDecl( " * Input file\t: %s\n", fileName);
cCode.appendDecl( " * Input file\t: %s\n", fileName.c_str());
cCode.appendDecl( " * File type\t: %s\n", (prog.fCOM)?"COM":"EXE");
cCode.appendDecl( " */\n\n#include \"dcc.h\"\n\n");
@ -204,10 +214,11 @@ static void emitFwdGotoLabel (ICODE * pt, int indLevel)
/* Writes the procedure's declaration (including arguments), local variables,
* and invokes the procedure that writes the code of the given record *hli */
void Function::codeGen (std::ostream &fs)
void Function::codeGen (QIODevice &fs)
{
int numLoc;
ostringstream ostr;
QString ostr_contents;
QTextStream ostr(&ostr_contents);
//STKFRAME * args; /* Procedure arguments */
//char buf[200], /* Procedure's definition */
// arg[30]; /* One argument */
@ -216,30 +227,27 @@ void Function::codeGen (std::ostream &fs)
/* Write procedure/function header */
cCode.init();
if (flg & PROC_IS_FUNC) /* Function */
ostr<< "\n"<<TypeContainer::typeName(retVal.type)<<" "<<name<<" (";
ostr << QString("\n%1 %2 (").arg(TypeContainer::typeName(retVal.type)).arg(name);
else /* Procedure */
ostr<< "\nvoid "<<name<<" (";
ostr << "\nvoid "+name+" (";
/* Write arguments */
struct validArg
{
bool operator()(STKSYM &s) { return s.invalid==false;}
};
auto valid_args = args | filtered(validArg());
int count_valid = std::distance(valid_args.begin(),valid_args.end());
for (STKSYM &arg : valid_args)
QStringList parts;
for (STKSYM &arg : (args | filtered(validArg())))
{
ostr<<hlTypes[arg.type]<<" "<<arg.name;
if(--count_valid!=0)
ostr<<", ";
parts << QString("%1 %2").arg(hlTypes[arg.type]).arg(arg.name);
}
ostr<<")\n";
ostr << parts.join(", ")+")\n";
/* Write comments */
writeProcComments( ostr );
/* Write local variables */
if (! (flg & PROC_ASM))
if (not (flg & PROC_ASM))
{
numLoc = 0;
for (ID &refId : localId )
@ -250,8 +258,8 @@ void Function::codeGen (std::ostream &fs)
if (refId.loc == REG_FRAME)
{
/* Register variables are assigned to a local variable */
if (((flg & SI_REGVAR) && (refId.id.regi == rSI)) ||
((flg & DI_REGVAR) && (refId.id.regi == rDI)))
if (((flg & SI_REGVAR) and (refId.id.regi == rSI)) or
((flg & DI_REGVAR) and (refId.id.regi == rDI)))
{
refId.setLocalName(++numLoc);
ostr << "int "<<refId.name<<";\n";
@ -267,7 +275,9 @@ void Function::codeGen (std::ostream &fs)
}
}
}
fs<<ostr.str();
ostr.flush();
fs.write(ostr_contents.toLatin1());
/* Write procedure's code */
if (flg & PROC_ASM) /* generate assembler */
{
@ -284,36 +294,41 @@ void Function::codeGen (std::ostream &fs)
freeBundle (&cCode);
/* Write Live register analysis information */
if (option.verbose)
if (option.verbose) {
QString debug_contents;
QTextStream debug_stream(&debug_contents);
for (size_t i = 0; i < numBBs; i++)
{
pBB = m_dfsLast[i];
if (pBB->flg & INVALID_BB) continue; /* skip invalid BBs */
cout << "BB "<<i<<"\n";
cout << " Start = "<<pBB->begin()->loc_ip;
cout << ", end = "<<pBB->begin()->loc_ip+pBB->size()<<"\n";
cout << " LiveUse = ";
Machine_X86::writeRegVector(cout,pBB->liveUse);
cout << "\n Def = ";
Machine_X86::writeRegVector(cout,pBB->def);
cout << "\n LiveOut = ";
Machine_X86::writeRegVector(cout,pBB->liveOut);
cout << "\n LiveIn = ";
Machine_X86::writeRegVector(cout,pBB->liveIn);
cout <<"\n\n";
debug_stream << "BB "<<i<<"\n";
debug_stream << " Start = "<<pBB->begin()->loc_ip;
debug_stream << ", end = "<<pBB->begin()->loc_ip+pBB->size()<<"\n";
debug_stream << " LiveUse = ";
Machine_X86::writeRegVector(debug_stream,pBB->liveUse);
debug_stream << "\n Def = ";
Machine_X86::writeRegVector(debug_stream,pBB->def);
debug_stream << "\n LiveOut = ";
Machine_X86::writeRegVector(debug_stream,pBB->liveOut);
debug_stream << "\n LiveIn = ";
Machine_X86::writeRegVector(debug_stream,pBB->liveIn);
debug_stream <<"\n\n";
}
debug_stream.flush();
qDebug() << debug_contents.toLatin1();
}
}
/* Recursive procedure. Displays the procedure's code in depth-first order
* of the call graph. */
static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
static void backBackEnd (CALL_GRAPH * pcallGraph, QIODevice &_ios)
{
// IFace.Yield(); /* This is a good place to yield to other apps */
/* Check if this procedure has been processed already */
if ((pcallGraph->proc->flg & PROC_OUTPUT) ||
if ((pcallGraph->proc->flg & PROC_OUTPUT) or
(pcallGraph->proc->flg & PROC_ISLIB))
return;
pcallGraph->proc->flg |= PROC_OUTPUT;
@ -332,7 +347,7 @@ static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
/* Generate statistics */
if (option.Stats)
pcallGraph->proc->displayStats ();
if (! (pcallGraph->proc->flg & PROC_ASM))
if (not (pcallGraph->proc->flg & PROC_ASM))
{
stats.totalLL += stats.numLLIcode;
stats.totalHL += stats.numHLIcode;
@ -341,22 +356,20 @@ static void backBackEnd (CALL_GRAPH * pcallGraph, std::ostream &_ios)
/* Invokes the necessary routines to produce code one procedure at a time. */
void BackEnd (char *fileName, CALL_GRAPH * pcallGraph)
void BackEnd(CALL_GRAPH * pcallGraph)
{
std::ofstream fs; /* Output C file */
/* Get output file name */
std::string outNam(fileName);
outNam = outNam.substr(0,outNam.rfind("."))+".b"; /* b for beta */
QString outNam(Project::get()->output_name("b")); /* b for beta */
QFile fs(outNam); /* Output C file */
/* Open output file */
fs.open(outNam);
if(!fs.is_open())
fatalError (CANNOT_OPEN, outNam.c_str());
printf ("dcc: Writing C beta file %s\n", outNam.c_str());
if(not fs.open(QFile::WriteOnly|QFile::Text))
fatalError (CANNOT_OPEN, outNam.toStdString().c_str());
qDebug()<<"dcc: Writing C beta file"<<outNam;
/* Header information */
writeHeader (fs, option.filename);
writeHeader (fs, option.filename.toStdString());
/* Initialize total Icode instructions statistics */
stats.totalLL = 0;
@ -367,7 +380,7 @@ void BackEnd (char *fileName, CALL_GRAPH * pcallGraph)
/* Close output file */
fs.close();
printf ("dcc: Finished writing C beta file\n");
qDebug() << "dcc: Finished writing C beta file";
}

View File

@ -10,7 +10,7 @@
#include <memory.h>
#include <stdlib.h>
#include <string.h>
#include <QtCore/QIODevice>
#define deltaProcLines 20
using namespace std;
@ -21,26 +21,26 @@ using namespace std;
* tab is removed and replaced by this label */
void strTable::addLabelBundle (int idx, int label)
{
char s[16];
sprintf (s, "l%d: ", label);
if(at(idx).size()<4)
at(idx)=s;
QString &processedLine(at(idx));
QString s = QString("l%1: ").arg(label);
if(processedLine.size()<4)
processedLine = s;
else
at(idx) = string(s)+at(idx).substr(4);
processedLine = s+processedLine.mid(4);
}
/* Writes the contents of the string table on the file fp. */
static void writeStrTab (std::ostream &ios, strTable &strTab)
static void writeStrTab (QIODevice &ios, strTable &strTab)
{
for (size_t i = 0; i < strTab.size(); i++)
ios << strTab[i];
ios.write(strTab[i].toLatin1());
}
/* Writes the contents of the bundle (procedure code and declaration) to
* a file. */
void writeBundle (std::ostream &ios, bundle procCode)
void writeBundle (QIODevice &ios, bundle procCode)
{
writeStrTab (ios, procCode.decl);
writeStrTab (ios, procCode.code);
@ -70,7 +70,7 @@ void bundle::appendCode(const char *format,...)
code.push_back(buf);
va_end (args);
}
void bundle::appendCode(const std::string &s)
void bundle::appendCode(const QString & s)
{
code.push_back(s);
}
@ -85,7 +85,7 @@ void bundle::appendDecl(const char *format,...)
va_end (args);
}
void bundle::appendDecl(const std::string &v)
void bundle::appendDecl(const QString &v)
{
decl.push_back(v);
}

View File

@ -5,18 +5,21 @@
* (C) Mike van Emmerik
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef __BORLAND__
#include <mem.h>
#else
#include <memory.h>
#endif
#include <string.h>
#include "dcc.h"
#include "msvc_fixes.h"
#include "project.h"
#include "perfhlib.h"
#include "dcc_interface.h"
#include <QtCore/QDir>
#include <QtCore/QString>
#include <QtCore/QDebug>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
PerfectHash g_pattern_hasher;
#define NIL -1 /* Used like NULL, but 0 is valid */
/* Hash table structure */
@ -50,7 +53,7 @@ int numVert; /* Number of vertices in the graph (also size of g[]
unsigned PatLen; /* Size of the keys (pattern length) */
unsigned SymLen; /* Max size of the symbols, including null */
static FILE *g_file; /* File being read */
static char sSigName[100]; /* Full path name of .sig file */
static QString sSigName; /* Full path name of .sig file */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *g; /* g[] */
@ -68,7 +71,6 @@ void readFileSection(uint16_t* p, int len, FILE *_file);
void cleanup(void);
void checkStartup(STATE *state);
void readProtoFile(void);
void fixNewline(char *s);
int searchPList(char *name);
void checkHeap(char *msg); /* For debugging */
@ -296,22 +298,21 @@ static uint8_t pattMsChkstk[] =
/* This procedure is called to initialise the library check code */
void SetupLibCheck(void)
bool SetupLibCheck(void)
{
PROG &prog(Project::get()->prog);
uint16_t w, len;
int i;
if ((g_file = fopen(sSigName, "rb")) == nullptr)
IDcc *dcc = IDcc::get();
QString fpath = dcc->dataDir("sigs").absoluteFilePath(sSigName);
if ((g_file = fopen(qPrintable(fpath), "rb")) == nullptr)
{
printf("Warning: cannot open signature file %s\n", sSigName);
return;
printf("Warning: cannot open signature file %s\n", qPrintable(fpath));
return false;
}
readProtoFile();
prog.bSigs = false; /* False unless everything goes right */
/* Read the parameters */
grab(4, g_file);
if (memcmp("dccs", buf, 4) != 0)
@ -323,16 +324,15 @@ void SetupLibCheck(void)
numVert = readFileShort(g_file);
PatLen = readFileShort(g_file);
SymLen = readFileShort(g_file);
if ((PatLen != PATLEN) || (SymLen != SYMLEN))
if ((PatLen != PATLEN) or (SymLen != SYMLEN))
{
printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n",
SYMLEN, PATLEN);
exit(1);
printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n", SYMLEN, PATLEN);
return false;
}
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
/* Set the parameters for the hash table */
g_pattern_hasher.init(
g_pattern_hasher.setHashParams(
numKeys, /* The number of symbols */
PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
@ -354,7 +354,7 @@ void SetupLibCheck(void)
if (w != len)
{
printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4);
return false;
}
readFileSection(T1base, len, g_file);
@ -362,13 +362,13 @@ void SetupLibCheck(void)
if (memcmp("T2", buf, 2) != 0)
{
printf("Expected 'T2'\n");
exit(3);
return false;
}
w = readFileShort(g_file);
if (w != len)
{
printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4);
return false;
}
readFileSection(T2base, len, g_file);
@ -377,14 +377,14 @@ void SetupLibCheck(void)
if (memcmp("gg", buf, 2) != 0)
{
printf("Expected 'gg'\n");
exit(3);
return false;
}
len = (uint16_t)(numVert * sizeof(uint16_t));
w = readFileShort(g_file);
if (w != len)
{
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4);
return false;
}
readFileSection(g, len, g_file);
@ -395,19 +395,19 @@ void SetupLibCheck(void)
if ( nullptr == ht)
{
printf("Could not allocate hash table\n");
exit(1);
return false;
}
grab(2, g_file);
if (memcmp("ht", buf, 2) != 0)
{
printf("Expected 'ht'\n");
exit(3);
return false;
}
w = readFileShort(g_file);
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
{
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6);
return false;
}
@ -416,11 +416,11 @@ void SetupLibCheck(void)
if (fread(&ht[i], 1, SymLen + PatLen, g_file) != SymLen + PatLen)
{
printf("Could not read signature\n");
exit(11);
return false;
}
}
fclose(g_file);
prog.bSigs = true;
return true;
}
@ -467,17 +467,17 @@ bool LibCheck(Function & pProc)
if (memcmp(ht[h].htPat, pat, PATLEN) == 0)
{
/* We have a match. Save the name, if not already set */
if (pProc.name.empty() ) /* Don't overwrite existing name */
if (pProc.name.isEmpty() ) /* Don't overwrite existing name */
{
/* Give proc the new name */
pProc.name = ht[h].htSym;
}
/* But is it a real library function? */
i = NIL;
if ((numFunc == 0) || (i=searchPList(ht[h].htSym)) != NIL)
if ((numFunc == 0) or (i=searchPList(ht[h].htSym)) != NIL)
{
pProc.flg |= PROC_ISLIB; /* It's a lib function */
pProc.callingConv(CConv::C);
pProc.callingConv(CConv::eCdecl);
if (i != NIL)
{
/* Allocate space for the arg struct, and copy the hlType to
@ -503,12 +503,14 @@ bool LibCheck(Function & pProc)
case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
pProc.liveOut.setReg(rAL);
break;
case TYPE_STR:
case TYPE_PTR:
fprintf(stderr,"Warning assuming Large memory model\n");
pProc.liveOut.setReg(rAX).addReg(rDS);
break;
default:
fprintf(stderr,"Unknown retval type %d for %s in LibCheck\n",pProc.retVal.type,pProc.name.c_str());
qCritical() << QString("Unknown retval type %1 for %2 in LibCheck")
.arg(pProc.retVal.type).arg(pProc.name);
/*** other types are not considered yet ***/
}
}
@ -532,7 +534,7 @@ bool LibCheck(Function & pProc)
pProc.args.numArgs = 0; /* With no args */
}
return (bool)((pProc.flg & PROC_ISLIB) != 0);
return pProc.isLibrary();
}
@ -546,8 +548,7 @@ void grab(int n, FILE *_file)
}
}
uint16_t
readFileShort(FILE *f)
uint16_t readFileShort(FILE *f)
{
uint8_t b1, b2;
@ -604,7 +605,7 @@ static bool locatePattern(const uint8_t *source, int iMin, int iMax, uint8_t *pa
for (j=0; j < iPatLen; j++)
{
/* j is the index of the uint8_t being considered in the pattern. */
if ((*pSrc != pattern[j]) && (pattern[j] != WILD))
if ((*pSrc != pattern[j]) and (pattern[j] != WILD))
{
/* A definite mismatch */
break; /* Break to outer loop */
@ -639,7 +640,6 @@ void STATE::checkStartup()
char chModel = 'x';
char chVendor = 'x';
char chVersion = 'x';
char *pPath;
char temp[4];
startOff = ((uint32_t)prog.initCS << 4) + prog.initIP;
@ -830,31 +830,12 @@ void STATE::checkStartup()
gotVendor:
/* Use the DCC environment variable to set where the .sig files will
be found. Otherwise, assume current directory */
pPath = getenv("DCC");
if (pPath)
{
strcpy(sSigName, pPath); /* Use path given */
if (sSigName[strlen(sSigName)-1] != '/')
{
strcat(sSigName, "/"); /* Append a slash if necessary */
}
}
else
{
strcpy(sSigName, "./"); /* Current directory */
}
strcat(sSigName, "dcc");
temp[1] = '\0';
temp[0] = chVendor;
strcat(sSigName, temp); /* Add vendor */
temp[0] = chVersion;
strcat(sSigName, temp); /* Add version */
temp[0] = chModel;
strcat(sSigName, temp); /* Add model */
strcat(sSigName, ".sig"); /* Add extension */
printf("Signature file: %s\n", sSigName);
sSigName = QString("dcc%1%2%3.sig")
.arg(QChar(chVendor)) /* Add vendor */
.arg(QChar(chVersion)) /* Add version */
.arg(QChar(chModel)) /* Add model */
;
printf("Signature file: %s\n", qPrintable(sSigName));
}
@ -867,45 +848,29 @@ gotVendor:
*/
void readProtoFile(void)
{
IDcc *dcc = IDcc::get();
QString szProFName = dcc->dataDir("prototypes").absoluteFilePath(DCCLIBS); /* Full name of dclibs.lst */
FILE *fProto;
char *pPath; /* Point to the environment string */
char szProFName[81]; /* Full name of dclibs.lst */
int i;
/* Use the DCC environment variable to set where the dcclibs.lst file will
be found. Otherwise, assume current directory */
pPath = getenv("DCC");
if (pPath)
if ((fProto = fopen(qPrintable(szProFName), "rb")) == nullptr)
{
strcpy(szProFName, pPath); /* Use path given */
if (szProFName[strlen(szProFName)-1] != '/')
{
strcat(szProFName, "/"); /* Append a slash if necessary */
}
}
else
{
strcpy(szProFName, "./"); /* Current directory */
}
strcat(szProFName, DCCLIBS);
if ((fProto = fopen(szProFName, "rb")) == nullptr)
{
printf("Warning: cannot open library prototype data file %s\n", szProFName);
printf("Warning: cannot open library prototype data file %s\n", qPrintable(szProFName));
return;
}
grab(4, fProto);
if (strncmp(buf, "dccp", 4) != 0)
{
printf("%s is not a dcc prototype file\n", szProFName);
printf("%s is not a dcc prototype file\n", qPrintable(szProFName));
exit(1);
}
grab(2, fProto);
if (strncmp(buf, "FN", 2) != 0)
{
printf("FN (Function Name) subsection expected in %s\n", szProFName);
printf("FN (Function Name) subsection expected in %s\n", qPrintable(szProFName));
exit(2);
}
@ -932,7 +897,7 @@ void readProtoFile(void)
grab(2, fProto);
if (strncmp(buf, "PM", 2) != 0)
{
printf("PM (Parameter) subsection expected in %s\n", szProFName);
printf("PM (Parameter) subsection expected in %s\n", qPrintable(szProFName));
exit(2);
}

View File

@ -7,9 +7,12 @@
****************************************************************************/
#include "dcc.h"
#include "msvc_fixes.h"
#include "machine_x86.h"
#include <string.h>
#include <sstream>
#include <QTextStream>
using namespace std;
#define intSize 40
@ -148,7 +151,7 @@ static const char *intOthers[] = {
/* Writes the description of the current interrupt. Appends it to the
* string s. */
void LLInst::writeIntComment (std::ostringstream &s)
void LLInst::writeIntComment (QTextStream &s)
{
uint32_t src_immed=src().getImm2();
s<<"\t/* ";
@ -156,7 +159,7 @@ void LLInst::writeIntComment (std::ostringstream &s)
{
s <<int21h[m_dst.off];
}
else if (src_immed > 0x1F && src_immed < 0x2F)
else if (src_immed > 0x1F and src_immed < 0x2F)
{
s <<intOthers[src_immed - 0x20];
}
@ -186,12 +189,15 @@ void LLInst::writeIntComment (std::ostringstream &s)
//, &cCode.decl
void Function::writeProcComments()
{
std::ostringstream ostr;
writeProcComments(ostr);
cCode.appendDecl(ostr.str());
QString dest_str;
{
QTextStream ostr(&dest_str);
writeProcComments(ostr);
}
cCode.appendDecl(dest_str);
}
void Function::writeProcComments(std::ostream &ostr)
void Function::writeProcComments(QTextStream &ostr)
{
int i;
ID *id; /* Pointer to register argument identifier */

View File

@ -2,33 +2,34 @@
* Description : Performs control flow analysis on the CFG
* (C) Cristina Cifuentes
********************************************************************/
#include <boost/range/algorithm.hpp>
#include <algorithm>
#include <list>
#include <cassert>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "dcc.h"
#include "msvc_fixes.h"
//typedef struct list {
// int nodeIdx;
// struct list *next;
//} nodeList;
#include <boost/range/algorithm.hpp>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <list>
namespace {
typedef std::list<int> nodeList; /* dfsLast index to the node */
#define ancestor(a,b) ((a->dfsLastNum < b->dfsLastNum) && (a->dfsFirstNum < b->dfsFirstNum))
/* there is a path on the DFST from a to b if the a was first visited in a
* dfs, and a was later visited than b when doing the last visit of each
* node. */
bool inline ancestor(BB *a,BB *b)
{
return (a->dfsLastNum < b->dfsLastNum) and (a->dfsFirstNum < b->dfsFirstNum);
}
/* Checks if the edge (p,s) is a back edge. If node s was visited first
/** Checks if the edge (p,s) is a back edge. If node s was visited first
* during the dfs traversal (ie. s has a smaller dfsFirst number) or s == p,
* then it is a backedge.
* Also incrementes the number of backedges entries to the header node. */
static bool isBackEdge (BB * p,BB * s)
bool isBackEdge (BB * p,BB * s)
{
if (p->dfsFirstNum >= s->dfsFirstNum)
{
@ -39,16 +40,16 @@ static bool isBackEdge (BB * p,BB * s)
}
/* Finds the common dominator of the current immediate dominator
/** Finds the common dominator of the current immediate dominator
* currImmDom and its predecessor's immediate dominator predImmDom */
static int commonDom (int currImmDom, int predImmDom, Function * pProc)
int commonDom (int currImmDom, int predImmDom, Function * pProc)
{
if (currImmDom == NO_DOM)
return (predImmDom);
if (predImmDom == NO_DOM) /* predecessor is the root */
return (currImmDom);
while ((currImmDom != NO_DOM) && (predImmDom != NO_DOM) &&
while ((currImmDom != NO_DOM) and (predImmDom != NO_DOM) and
(currImmDom != predImmDom))
{
if (currImmDom < predImmDom)
@ -58,9 +59,214 @@ static int commonDom (int currImmDom, int predImmDom, Function * pProc)
}
return (currImmDom);
}
/* Returns whether or not the node n (dfsLast numbering of a basic block)
* is on the list l. */
bool inList (const nodeList &l, int n)
{
return std::find(l.begin(),l.end(),n)!=l.end();
}
/* Returns whether the node n belongs to the queue list q. */
bool inInt(BB * n, queue &q)
{
return std::find(q.begin(),q.end(),n)!=q.end();
}
/** Recursive procedure to find nodes that belong to the interval (ie. nodes
* from G1). */
void findNodesInInt (queue &intNodes, int level, interval *Ii)
{
if (level == 1)
{
for(BB *en : Ii->nodes)
{
appendQueue(intNodes,en);
}
}
else
{
for(BB *en : Ii->nodes)
{
findNodesInInt(intNodes,level-1,en->correspInt);
}
}
}
/* Finds the follow of the endless loop headed at node head (if any).
* The follow node is the closest node to the loop. */
void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
{
head->loopFollow = MAX;
for( int loop_node : loopNodes)
{
for (const TYPEADR_TYPE &typeaddr: pProc->m_dfsLast[loop_node]->edges)
{
int succ = typeaddr.BBptr->dfsLastNum;
if ((not inList(loopNodes, succ)) and (succ < head->loopFollow))
head->loopFollow = succ;
}
}
}
/* Finds the immediate dominator of each node in the graph pProc->cfg.
//static void findNodesInLoop(BB * latchNode,BB * head,PPROC pProc,queue *intNodes)
/* Flags nodes that belong to the loop determined by (latchNode, head) and
* determines the type of loop. */
void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &intNodes)
{
int i, headDfsNum, intNodeType;
nodeList loopNodes;
int immedDom, /* dfsLast index to immediate dominator */
thenDfs, elseDfs; /* dsfLast index for THEN and ELSE nodes */
BB * pbb;
/* Flag nodes in loop headed by head (except header node) */
headDfsNum = head->dfsLastNum;
head->loopHead = headDfsNum;
loopNodes.push_back(headDfsNum);
for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++)
{
if (pProc->m_dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */
continue;
immedDom = pProc->m_dfsLast[i]->immedDom;
if (inList (loopNodes, immedDom) and inInt(pProc->m_dfsLast[i], intNodes))
{
loopNodes.push_back(i);
if (pProc->m_dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/
pProc->m_dfsLast[i]->loopHead = headDfsNum;
}
}
latchNode->loopHead = headDfsNum;
if (latchNode != head)
loopNodes.push_back(latchNode->dfsLastNum);
/* Determine type of loop and follow node */
intNodeType = head->nodeType;
if (latchNode->nodeType == TWO_BRANCH)
if ((intNodeType == TWO_BRANCH) or (latchNode == head))
if ((latchNode == head) or
(inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum) and
inList (loopNodes, head->edges[ELSE].BBptr->dfsLastNum)))
{
head->loopType = eNodeHeaderType::REPEAT_TYPE;
if (latchNode->edges[0].BBptr == head)
head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
latchNode->back().ll()->setFlags(JX_LOOP);
}
else
{
head->loopType = eNodeHeaderType::WHILE_TYPE;
if (inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum))
head->loopFollow = head->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = head->edges[THEN].BBptr->dfsLastNum;
head->back().ll()->setFlags(JX_LOOP);
}
else /* head = anything besides 2-way, latch = 2-way */
{
head->loopType = eNodeHeaderType::REPEAT_TYPE;
if (latchNode->edges[THEN].BBptr == head)
head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
latchNode->back().ll()->setFlags(JX_LOOP);
}
else /* latch = 1-way */
if (latchNode->nodeType == LOOP_NODE)
{
head->loopType = eNodeHeaderType::REPEAT_TYPE;
head->loopFollow = latchNode->edges[0].BBptr->dfsLastNum;
}
else if (intNodeType == TWO_BRANCH)
{
head->loopType = eNodeHeaderType::WHILE_TYPE;
pbb = latchNode;
thenDfs = head->edges[THEN].BBptr->dfsLastNum;
elseDfs = head->edges[ELSE].BBptr->dfsLastNum;
while (1)
{
if (pbb->dfsLastNum == thenDfs)
{
head->loopFollow = elseDfs;
break;
}
else if (pbb->dfsLastNum == elseDfs)
{
head->loopFollow = thenDfs;
break;
}
/* Check if couldn't find it, then it is a strangely formed
* loop, so it is safer to consider it an endless loop */
if (pbb->dfsLastNum <= head->dfsLastNum)
{
head->loopType = eNodeHeaderType::ENDLESS_TYPE;
findEndlessFollow (pProc, loopNodes, head);
break;
}
pbb = pProc->m_dfsLast[pbb->immedDom];
}
if (pbb->dfsLastNum > head->dfsLastNum)
pProc->m_dfsLast[head->loopFollow]->loopHead = NO_NODE; /*****/
head->back().ll()->setFlags(JX_LOOP);
}
else
{
head->loopType = eNodeHeaderType::ENDLESS_TYPE;
findEndlessFollow (pProc, loopNodes, head);
}
loopNodes.clear();
}
/** \returns whether the BB indexed by s is a successor of the BB indexed by \arg h
* \note that h is a case node.
*/
bool successor (int s, int h, Function * pProc)
{
BB * header = pProc->m_dfsLast[h];
auto iter = std::find_if(header->edges.begin(),
header->edges.end(),
[s](const TYPEADR_TYPE &te)->bool{ return te.BBptr->dfsLastNum == s;});
return iter!=header->edges.end();
}
/** Recursive procedure to tag nodes that belong to the case described by
* the list l, head and tail (dfsLast index to first and exit node of the
* case). */
void tagNodesInCase (BB * pBB, nodeList &l, int head, int tail)
{
int current; /* index to current node */
pBB->traversed = DFS_CASE;
current = pBB->dfsLastNum;
if ((current != tail) and (pBB->nodeType != MULTI_BRANCH) and (inList (l, pBB->immedDom)))
{
l.push_back(current);
pBB->caseHead = head;
for(TYPEADR_TYPE &edge : pBB->edges)
{
if (edge.BBptr->traversed != DFS_CASE)
tagNodesInCase (edge.BBptr, l, head, tail);
}
}
}
/** Flags all nodes in the list l as having follow node f, and deletes all
* nodes from the list. */
void flagNodes (nodeList &l, int f, Function * pProc)
{
for(int idx : l)
{
pProc->m_dfsLast[idx]->ifFollow = f;
}
l.clear();
}
} // end of anonymouse namespace
/** Finds the immediate dominator of each node in the graph pProc->cfg.
* Adapted version of the dominators algorithm by Hecht and Ullman; finds
* immediate dominators only.
* Note: graph should be reducible */
@ -82,188 +288,7 @@ void Function::findImmedDom ()
}
/* Inserts the node n to the list l. */
static void insertList (nodeList &l, int n)
{
l.push_back(n);
}
/* Returns whether or not the node n (dfsLast numbering of a basic block)
* is on the list l. */
static bool inList (const nodeList &l, int n)
{
return std::find(l.begin(),l.end(),n)!=l.end();
}
/* Frees space allocated by the list l. */
static void freeList (nodeList &l)
{
l.clear();
}
/* Returns whether the node n belongs to the queue list q. */
static bool inInt(BB * n, queue &q)
{
return std::find(q.begin(),q.end(),n)!=q.end();
}
/* Finds the follow of the endless loop headed at node head (if any).
* The follow node is the closest node to the loop. */
static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
{
head->loopFollow = MAX;
for( int loop_node : loopNodes)
{
for (TYPEADR_TYPE &typeaddr: pProc->m_dfsLast[loop_node]->edges)
{
int succ = typeaddr.BBptr->dfsLastNum;
if ((! inList(loopNodes, succ)) && (succ < head->loopFollow))
head->loopFollow = succ;
}
}
}
//static void findNodesInLoop(BB * latchNode,BB * head,PPROC pProc,queue *intNodes)
/* Flags nodes that belong to the loop determined by (latchNode, head) and
* determines the type of loop. */
static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &intNodes)
{
int i, headDfsNum, intNodeType;
nodeList loopNodes;
int immedDom, /* dfsLast index to immediate dominator */
thenDfs, elseDfs; /* dsfLast index for THEN and ELSE nodes */
BB * pbb;
/* Flag nodes in loop headed by head (except header node) */
headDfsNum = head->dfsLastNum;
head->loopHead = headDfsNum;
insertList (loopNodes, headDfsNum);
for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++)
{
if (pProc->m_dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */
continue;
immedDom = pProc->m_dfsLast[i]->immedDom;
if (inList (loopNodes, immedDom) && inInt(pProc->m_dfsLast[i], intNodes))
{
insertList (loopNodes, i);
if (pProc->m_dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/
pProc->m_dfsLast[i]->loopHead = headDfsNum;
}
}
latchNode->loopHead = headDfsNum;
if (latchNode != head)
insertList (loopNodes, latchNode->dfsLastNum);
/* Determine type of loop and follow node */
intNodeType = head->nodeType;
if (latchNode->nodeType == TWO_BRANCH)
if ((intNodeType == TWO_BRANCH) || (latchNode == head))
if ((latchNode == head) ||
(inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum) &&
inList (loopNodes, head->edges[ELSE].BBptr->dfsLastNum)))
{
head->loopType = REPEAT_TYPE;
if (latchNode->edges[0].BBptr == head)
head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
latchNode->back().ll()->setFlags(JX_LOOP);
}
else
{
head->loopType = WHILE_TYPE;
if (inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum))
head->loopFollow = head->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = head->edges[THEN].BBptr->dfsLastNum;
head->back().ll()->setFlags(JX_LOOP);
}
else /* head = anything besides 2-way, latch = 2-way */
{
head->loopType = REPEAT_TYPE;
if (latchNode->edges[THEN].BBptr == head)
head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
else
head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
latchNode->back().ll()->setFlags(JX_LOOP);
}
else /* latch = 1-way */
if (latchNode->nodeType == LOOP_NODE)
{
head->loopType = REPEAT_TYPE;
head->loopFollow = latchNode->edges[0].BBptr->dfsLastNum;
}
else if (intNodeType == TWO_BRANCH)
{
head->loopType = WHILE_TYPE;
pbb = latchNode;
thenDfs = head->edges[THEN].BBptr->dfsLastNum;
elseDfs = head->edges[ELSE].BBptr->dfsLastNum;
while (1)
{
if (pbb->dfsLastNum == thenDfs)
{
head->loopFollow = elseDfs;
break;
}
else if (pbb->dfsLastNum == elseDfs)
{
head->loopFollow = thenDfs;
break;
}
/* Check if couldn't find it, then it is a strangely formed
* loop, so it is safer to consider it an endless loop */
if (pbb->dfsLastNum <= head->dfsLastNum)
{
head->loopType = ENDLESS_TYPE;
findEndlessFollow (pProc, loopNodes, head);
break;
}
pbb = pProc->m_dfsLast[pbb->immedDom];
}
if (pbb->dfsLastNum > head->dfsLastNum)
pProc->m_dfsLast[head->loopFollow]->loopHead = NO_NODE; /*****/
head->back().ll()->setFlags(JX_LOOP);
}
else
{
head->loopType = ENDLESS_TYPE;
findEndlessFollow (pProc, loopNodes, head);
}
freeList(loopNodes);
}
//static void findNodesInInt (queue **intNodes, int level, interval *Ii)
/* Recursive procedure to find nodes that belong to the interval (ie. nodes
* from G1). */
static void findNodesInInt (queue &intNodes, int level, interval *Ii)
{
if (level == 1)
{
for(BB *en : Ii->nodes)
{
appendQueue(intNodes,en);
}
}
else
{
for(BB *en : Ii->nodes)
{
findNodesInInt(intNodes,level-1,en->correspInt);
}
}
}
/* Algorithm for structuring loops */
/** Algorithm for structuring loops */
void Function::structLoops(derSeq *derivedG)
{
interval *Ii;
@ -279,8 +304,7 @@ void Function::structLoops(derSeq *derivedG)
for(auto & elem : *derivedG)
{
level++;
Ii = elem.Ii;
while (Ii) /* for all intervals Ii of Gi */
for (Ii = elem.Ii; Ii!=nullptr; Ii = Ii->next) /* for all intervals Ii of Gi */
{
latchNode = nullptr;
intNodes.clear();
@ -299,9 +323,9 @@ void Function::structLoops(derSeq *derivedG)
for (size_t i = 0; i < intHead->inEdges.size(); i++)
{
pred = intHead->inEdges[i];
if (inInt(pred, intNodes) && isBackEdge(pred, intHead))
if (inInt(pred, intNodes) and isBackEdge(pred, intHead))
{
if (! latchNode)
if (nullptr == latchNode)
latchNode = pred;
else if (pred->dfsLastNum > latchNode->dfsLastNum)
latchNode = pred;
@ -314,7 +338,7 @@ void Function::structLoops(derSeq *derivedG)
/* Check latching node is at the same nesting level of case
* statements (if any) and that the node doesn't belong to
* another loop. */
if ((latchNode->caseHead == intHead->caseHead) &&
if ((latchNode->caseHead == intHead->caseHead) and
(latchNode->loopHead == NO_NODE))
{
intHead->latchNode = latchNode->dfsLastNum;
@ -322,45 +346,6 @@ void Function::structLoops(derSeq *derivedG)
latchNode->flg |= IS_LATCH_NODE;
}
}
/* Next interval */
Ii = Ii->next;
}
/* Next derived sequence */
}
}
/* Returns whether the BB indexed by s is a successor of the BB indexed by
* h. Note that h is a case node. */
static bool successor (int s, int h, Function * pProc)
{
BB * header = pProc->m_dfsLast[h];
auto iter = std::find_if(header->edges.begin(),
header->edges.end(),
[s](const TYPEADR_TYPE &te)->bool{ return te.BBptr->dfsLastNum == s;});
return iter!=header->edges.end();
}
/* Recursive procedure to tag nodes that belong to the case described by
* the list l, head and tail (dfsLast index to first and exit node of the
* case). */
static void tagNodesInCase (BB * pBB, nodeList &l, int head, int tail)
{
int current; /* index to current node */
pBB->traversed = DFS_CASE;
current = pBB->dfsLastNum;
if ((current != tail) && (pBB->nodeType != MULTI_BRANCH) && (inList (l, pBB->immedDom)))
{
insertList (l, current);
pBB->caseHead = head;
for(TYPEADR_TYPE &edge : pBB->edges)
{
if (edge.BBptr->traversed != DFS_CASE)
tagNodesInCase (edge.BBptr, l, head, tail);
}
}
}
@ -385,12 +370,10 @@ void Function::structCases()
* the current header node, and is not a successor. */
for (size_t j = i + 2; j < numBBs; j++)
{
if ((!successor(j, i, this)) && (m_dfsLast[j]->immedDom == i))
if ((not successor(j, i, this)) and (m_dfsLast[j]->immedDom == i))
{
if (exitNode == NO_NODE)
{
exitNode = j;
}
else if (m_dfsLast[exitNode]->inEdges.size() < m_dfsLast[j]->inEdges.size())
exitNode = j;
}
@ -399,7 +382,7 @@ void Function::structCases()
/* Tag nodes that belong to the case by recording the
* header field with caseHeader. */
insertList (caseNodes, i);
caseNodes.push_back(i);
m_dfsLast[i]->caseHead = i;
for(TYPEADR_TYPE &pb : caseHeader->edges)
{
@ -412,20 +395,6 @@ void Function::structCases()
}
}
/* Flags all nodes in the list l as having follow node f, and deletes all
* nodes from the list. */
static void flagNodes (nodeList &l, int f, Function * pProc)
{
nodeList::iterator p;
for(int idx : l)
{
pProc->m_dfsLast[idx]->ifFollow = f;
}
l.clear();
}
/* Structures if statements */
void Function::structIfs ()
{
@ -437,7 +406,7 @@ void Function::structIfs ()
unresolved /* List of unresolved if nodes */
;
BB * currNode, /* Pointer to current node */
* pbb;
* pbb;
/* Linear scan of nodes in reverse dfsLast order */
for (curr = numBBs - 1; curr >= 0; curr--)
@ -446,7 +415,7 @@ void Function::structIfs ()
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
continue;
if ((currNode->nodeType == TWO_BRANCH) && (!currNode->back().ll()->testFlags(JX_LOOP)))
if ((currNode->nodeType == TWO_BRANCH) and (not currNode->back().ll()->testFlags(JX_LOOP)))
{
followInEdges = 0;
follow = 0;
@ -456,7 +425,7 @@ void Function::structIfs ()
{
if (m_dfsLast[desc]->immedDom == curr)
{
insertList (domDesc, desc);
domDesc.push_back(desc);
pbb = m_dfsLast[desc];
if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges)
{
@ -468,16 +437,16 @@ void Function::structIfs ()
/* Determine follow according to number of descendants
* immediately dominated by this node */
if ((follow != 0) && (followInEdges > 1))
if ((follow != 0) and (followInEdges > 1))
{
currNode->ifFollow = follow;
if (!unresolved.empty())
if (not unresolved.empty())
flagNodes (unresolved, follow, this);
}
else
insertList (unresolved, curr);
unresolved.push_back(curr);
}
freeList (domDesc);
domDesc.clear();
}
}
bool Function::removeInEdge_Flag_and_ProcessLatch(BB *pbb,BB *a,BB *b)
@ -617,33 +586,33 @@ void Function::compoundCond()
change = true; //assume change
/* Check (X || Y) case */
if ((thenBB->nodeType == TWO_BRANCH) && (thenBB->numHlIcodes == 1) &&
(thenBB->inEdges.size() == 1) && (thenBB->edges[ELSE].BBptr == elseBB))
/* Check (X or Y) case */
if ((thenBB->nodeType == TWO_BRANCH) and (thenBB->numHlIcodes == 1) and
(thenBB->inEdges.size() == 1) and (thenBB->edges[ELSE].BBptr == elseBB))
{
if(Case_X_or_Y(pbb, thenBB, elseBB))
--i;
}
/* Check (!X && Y) case */
else if ((thenBB->nodeType == TWO_BRANCH) && (thenBB->numHlIcodes == 1) &&
(thenBB->inEdges.size() == 1) && (thenBB->edges[THEN].BBptr == elseBB))
/* Check (not X and Y) case */
else if ((thenBB->nodeType == TWO_BRANCH) and (thenBB->numHlIcodes == 1) and
(thenBB->inEdges.size() == 1) and (thenBB->edges[THEN].BBptr == elseBB))
{
if(Case_notX_and_Y(pbb, thenBB, elseBB))
--i;
}
/* Check (X && Y) case */
else if ((elseBB->nodeType == TWO_BRANCH) && (elseBB->numHlIcodes == 1) &&
(elseBB->inEdges.size()==1) && (elseBB->edges[THEN].BBptr == thenBB))
/* Check (X and Y) case */
else if ((elseBB->nodeType == TWO_BRANCH) and (elseBB->numHlIcodes == 1) and
(elseBB->inEdges.size()==1) and (elseBB->edges[THEN].BBptr == thenBB))
{
if(Case_X_and_Y(pbb, thenBB, elseBB ))
--i;
}
/* Check (!X || Y) case */
else if ((elseBB->nodeType == TWO_BRANCH) && (elseBB->numHlIcodes == 1) &&
(elseBB->inEdges.size() == 1) && (elseBB->edges[ELSE].BBptr == thenBB))
/* Check (not X or Y) case */
else if ((elseBB->nodeType == TWO_BRANCH) and (elseBB->numHlIcodes == 1) and
(elseBB->inEdges.size() == 1) and (elseBB->edges[ELSE].BBptr == thenBB))
{
if(Case_notX_or_Y(pbb, thenBB, elseBB ))
--i;
@ -654,8 +623,7 @@ void Function::compoundCond()
}
}
/* Structuring algorithm to find the structures of the graph pProc->cfg */
/** Structuring algorithm to find the structures of the graph pProc->cfg */
void Function::structure(derSeq *derivedG)
{
/* Find immediate dominators of the graph */

View File

@ -4,20 +4,27 @@
* Purpose: Data flow analysis module.
* (C) Cristina Cifuentes
****************************************************************************/
#include "dcc.h"
#include "project.h"
#include "msvc_fixes.h"
#include <boost/range.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/assign.hpp>
#include <stdint.h>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/assign.hpp>
#include "dcc.h"
#include "project.h"
using namespace boost;
using namespace boost::adaptors;
using namespace std;
namespace
{
struct ExpStack
{
Function *func;
@ -28,7 +35,7 @@ struct ExpStack
void push(Expr *);
Expr * pop();
Expr * top() const {
if(!expStk.empty())
if(not expStk.empty())
return expStk.back();
return nullptr;
}
@ -45,6 +52,57 @@ struct ExpStack
}
};
ExpStack g_exp_stk;
/** Returns a string with the source operand of Icode */
Expr *srcIdent (const LLInst &ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
{
const LLOperand * src_op = ll_insn.get(SRC);
if (src_op->isImmediate()) /* immediate operand ll_insn.testFlags(I)*/
{
//if (ll_insn.testFlags(B))
return new Constant(src_op->getImm2(), src_op->byteWidth());
}
// otherwise
return AstIdent::id (ll_insn, SRC, pProc, i, duIcode, du);
}
/** Returns the destination operand */
Expr *dstIdent (const LLInst & ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
{
Expr *n;
n = AstIdent::id (ll_insn, DST, pProc, i, duIcode, du);
/** Is it needed? (pIcode->ll()->flg) & NO_SRC_B **/
return (n);
}
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the expression exp given */
void forwardSubsLong (int longIdx, Expr *_exp, ICODE &picode, ICODE &ticode, int *numHlIcodes)
{
bool res;
if (_exp == nullptr) /* In case expression popped is NULL */
return;
/* Insert on rhs of ticode, if possible */
res = Expr::insertSubTreeLongReg (_exp, ticode.hlU()->asgn.m_rhs, longIdx);
if (res)
{
picode.invalidate();
(*numHlIcodes)--;
}
else
{
/* Try to insert it on lhs of ticode*/
res = Expr::insertSubTreeLongReg (_exp, ticode.hlU()->asgn.m_lhs, longIdx);
if (res)
{
picode.invalidate();
(*numHlIcodes)--;
}
}
}
} // end of anonymous namespace
/***************************************************************************
* Expression stack functions
**************************************************************************/
@ -88,8 +146,6 @@ bool ExpStack::empty()
return expStk.empty();
}
using namespace std;
ExpStack g_exp_stk;
/* Returns the index of the local variable or parameter at offset off, if it
* is in the stack frame provided. */
@ -100,28 +156,6 @@ size_t STKFRAME::getLocVar(int off)
}
/* Returns a string with the source operand of Icode */
static Expr *srcIdent (const LLInst &ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
{
const LLOperand * src_op = ll_insn.get(SRC);
if (src_op->isImmediate()) /* immediate operand ll_insn.testFlags(I)*/
{
//if (ll_insn.testFlags(B))
return new Constant(src_op->getImm2(), src_op->byteWidth());
}
// otherwise
return AstIdent::id (ll_insn, SRC, pProc, i, duIcode, du);
}
/* Returns the destination operand */
static Expr *dstIdent (const LLInst & ll_insn, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
{
Expr *n;
n = AstIdent::id (ll_insn, DST, pProc, i, duIcode, du);
/** Is it needed? (pIcode->ll()->flg) & NO_SRC_B **/
return (n);
}
/* Eliminates all condition codes and generates new hlIcode instructions */
void Function::elimCondCodes ()
{
@ -143,10 +177,9 @@ void Function::elimCondCodes ()
//auto reversed_instructions = pBB->range() | reversed;
for (useAt = pBB->rbegin(); useAt != pBB->rend(); useAt++)
{
ICODE &useIcode(*useAt);
llIcode useAtOp = llIcode(useAt->ll()->getOpcode());
use = useAt->ll()->flagDU.u;
if ((useAt->type != LOW_LEVEL) || ( ! useAt->valid() ) || ( 0 == use ))
if ((useAt->type != LOW_LEVEL_ICODE) or ( not useAt->valid() ) or ( 0 == use ))
continue;
/* Find definition within the same basic block */
defAt=useAt;
@ -159,8 +192,7 @@ void Function::elimCondCodes ()
continue;
notSup = false;
LLOperand *dest_ll = defIcode.ll()->get(DST);
LLOperand *src_ll = defIcode.ll()->get(SRC);
if ((useAtOp >= iJB) && (useAtOp <= iJNS))
if ((useAtOp >= iJB) and (useAtOp <= iJNS))
{
iICODE befDefAt = (++riICODE(defAt)).base();
switch (defIcode.ll()->getOpcode())
@ -196,7 +228,7 @@ void Function::elimCondCodes ()
reportError (JX_NOT_DEF, defIcode.ll()->getOpcode());
flg |= PROC_ASM; /* generate asm */
}
if (! notSup)
if (not notSup)
{
assert(lhs);
assert(rhs);
@ -221,14 +253,14 @@ void Function::elimCondCodes ()
{
ICODE &a(*defAt);
ICODE &b(*useAt);
reportError (NOT_DEF_USE,a.ll()->getOpcode(),b.ll()->getOpcode());
reportError (NOT_DEF_USE,a.ll()->label,a.ll()->getOpcode(),b.ll()->getOpcode());
flg |= PROC_ASM; /* generate asm */
}
break;
}
/* Check for extended basic block */
if ((pBB->size() == 1) &&(useAtOp >= iJB) && (useAtOp <= iJNS))
if ((pBB->size() == 1) and(useAtOp >= iJB) and (useAtOp <= iJNS))
{
ICODE & _prev(pBB->back()); /* For extended basic blocks - previous icode inst */
if (_prev.hl()->opcode == HLI_JCOND)
@ -270,7 +302,7 @@ void Function::genLiveKtes ()
continue; // skip invalid BBs
for(ICODE &insn : *pbb)
{
if ((insn.type == HIGH_LEVEL) && ( insn.valid() ))
if ((insn.type == HIGH_LEVEL_ICODE) and ( insn.valid() ))
{
liveUse |= (insn.du.use - def);
def |= insn.du.def;
@ -344,7 +376,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
pcallee = ticode.hl()->call.proc;
/* user/runtime routine */
if (! (pcallee->flg & PROC_ISLIB))
if (not (pcallee->flg & PROC_ISLIB))
{
if (pcallee->liveAnal == false) /* hasn't been processed */
pcallee->dataFlow (pbb->liveOut);
@ -352,7 +384,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
}
else /* library routine */
{
if ( (pcallee->flg & PROC_IS_FUNC) && /* returns a value */
if ( (pcallee->flg & PROC_IS_FUNC) and /* returns a value */
(pcallee->liveOut & pbb->edges[0].BBptr->liveIn).any()
)
pbb->liveOut = pcallee->liveOut;
@ -360,10 +392,11 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
pbb->liveOut.reset();
}
if ((! (pcallee->flg & PROC_ISLIB)) || ( pbb->liveOut.any() ))
if ((not (pcallee->flg & PROC_ISLIB)) or ( pbb->liveOut.any() ))
{
switch (pcallee->retVal.type) {
case TYPE_LONG_SIGN: case TYPE_LONG_UNSIGN:
case TYPE_LONG_SIGN:
case TYPE_LONG_UNSIGN:
ticode.du1.setDef(rAX).addDef(rDX);
//TODO: use Calling convention to properly set regs here
break;
@ -387,7 +420,7 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
pbb->liveIn = LivenessSet(pbb->liveUse + (pbb->liveOut - pbb->def));
/* Check if live sets have been modified */
if ((prevLiveIn != pbb->liveIn) || (prevLiveOut != pbb->liveOut))
if ((prevLiveIn != pbb->liveIn) or (prevLiveOut != pbb->liveOut))
change = true;
}
}
@ -414,9 +447,9 @@ void Function::liveRegAnalysis (LivenessSet &in_liveOut)
* register */
bool BB::FindUseBeforeDef(eReg regi, int defRegIdx, iICODE start_at)
{
if ((regi == rDI) && (flg & DI_REGVAR))
if ((regi == rDI) and (flg & DI_REGVAR))
return true;
if ((regi == rSI) && (flg & SI_REGVAR))
if ((regi == rSI) and (flg & SI_REGVAR))
return true;
if (distance(start_at,end())>1) /* several instructions */
{
@ -459,7 +492,7 @@ bool BB::FindUseBeforeDef(eReg regi, int defRegIdx, iICODE start_at)
* on optimized code. */
void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
{
if (!((picode.hl()->opcode == HLI_CALL) && (picode.hl()->call.proc->flg & PROC_IS_FUNC)))
if (not ((picode.hl()->opcode == HLI_CALL) and (picode.hl()->call.proc->flg & PROC_IS_FUNC)))
return;
BB *tbb = this->edges[0].BBptr;
@ -477,7 +510,7 @@ void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
/* if not used in this basic block, check if the
* register is live out, if so, make it the last
* definition of this register */
if ( picode.du1.used(defRegIdx) && tbb->liveOut.testRegAndSubregs(regi))
if ( picode.du1.used(defRegIdx) and tbb->liveOut.testRegAndSubregs(regi))
picode.du.lastDefRegi.addReg(regi);
}
@ -490,11 +523,11 @@ void BB::ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode)
void BB::RemoveUnusedDefs(eReg regi, int defRegIdx, iICODE picode)
{
if (picode->valid() and not picode->du1.used(defRegIdx) and
(not picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
(not ((picode->hl()->opcode == HLI_CALL) &&
(not picode->du.lastDefRegi.testRegAndSubregs(regi)) and
(not ((picode->hl()->opcode == HLI_CALL) and
(picode->hl()->call.proc->flg & PROC_ISLIB))))
{
if (! (this->liveOut.testRegAndSubregs(regi))) /* not liveOut */
if (not (this->liveOut.testRegAndSubregs(regi))) /* not liveOut */
{
bool res = picode->removeDefRegi (regi, defRegIdx+1,&Parent->localId);
if (res == true)
@ -542,7 +575,7 @@ void BB::genDU1()
defRegIdx++;
/* Check if all defined registers have been processed */
if ((defRegIdx >= picode->du1.getNumRegsDef()) || (defRegIdx == MAX_REGS_DEF))
if ((defRegIdx >= picode->du1.getNumRegsDef()) or (defRegIdx == MAX_REGS_DEF))
break;
}
}
@ -560,7 +593,7 @@ void Function::genDU1 ()
}
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs of picode. */
void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const
void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, ICODE &picode, ICODE &ticode, int &numHlIcodes) const
{
bool res;
UnaryOperator *lhs_unary;
@ -576,64 +609,37 @@ void LOCAL_ID::forwardSubs (Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode,
return;
/* Insert on rhs of ticode, if possible */
res = Expr::insertSubTreeReg (ticode->hlU()->asgn.rhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
res = Expr::insertSubTreeReg (ticode.hlU()->asgn.m_rhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
if (res)
{
picode->invalidate();
picode.invalidate();
numHlIcodes--;
}
else
{
/* Try to insert it on lhs of ticode*/
RegisterNode *op = dynamic_cast<RegisterNode *>(ticode->hlU()->asgn.m_lhs);
RegisterNode *op = dynamic_cast<RegisterNode *>(ticode.hlU()->asgn.m_lhs);
if(op)
{
eReg inserted = id_arr[lhs_reg->regiIdx].id.regi;
eReg lhsReg = id_arr[op->regiIdx].id.regi;
if((lhsReg==inserted)||Machine_X86::isSubRegisterOf(lhsReg,inserted))
if((lhsReg==inserted) or Machine_X86::isSubRegisterOf(lhsReg,inserted))
{
// Do not replace ax = XYZ; given ax = H << P; with H << P =
return;
}
}
res = Expr::insertSubTreeReg (ticode->hlU()->asgn.m_lhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
res = Expr::insertSubTreeReg (ticode.hlU()->asgn.m_lhs,rhs, id_arr[lhs_reg->regiIdx].id.regi, this);
if (res)
{
picode->invalidate();
picode.invalidate();
numHlIcodes--;
}
}
}
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the expression exp given */
static void forwardSubsLong (int longIdx, Expr *_exp, iICODE picode, iICODE ticode, int *numHlIcodes)
{
bool res;
if (_exp == nullptr) /* In case expression popped is NULL */
return;
/* Insert on rhs of ticode, if possible */
res = Expr::insertSubTreeLongReg (_exp, ticode->hlU()->asgn.rhs, longIdx);
if (res)
{
picode->invalidate();
(*numHlIcodes)--;
}
else
{
/* Try to insert it on lhs of ticode*/
res = Expr::insertSubTreeLongReg (_exp, ticode->hlU()->asgn.m_lhs, longIdx);
if (res)
{
picode->invalidate();
(*numHlIcodes)--;
}
}
}
/* Returns whether the elements of the expression rhs are all x-clear from
/** Returns whether the elements of the expression rhs are all x-clear from
* instruction f up to instruction t. */
bool UnaryOperator::xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs)
{
@ -646,7 +652,7 @@ bool BinaryOperator::xClear(rICODE range_to_check, iICODE lastBBinst, const LOCA
{
if(nullptr==m_rhs)
return false;
if ( ! m_rhs->xClear (range_to_check, lastBBinst, locs) )
if ( not m_rhs->xClear (range_to_check, lastBBinst, locs) )
return false;
if(nullptr==m_lhs)
return false;
@ -676,7 +682,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
return; */
assert(pProc==g_exp_stk.func);
_exp = g_exp_stk.pop();
if (callee->flg & PROC_ISLIB) /* library function */
if (callee->isLibrary() ) /* library function */
{
if (callee->args.numArgs > 0)
{
@ -692,7 +698,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
else {
if(numArgs<callee->args.size()) {
if(prog.addressingMode=='l') {
if((callee->args[numArgs].type==TYPE_STR)||(callee->args[numArgs].type==TYPE_PTR)) {
if((callee->args[numArgs].type==TYPE_STR) or (callee->args[numArgs].type==TYPE_PTR)) {
RegisterNode *rn = dynamic_cast<RegisterNode *>(g_exp_stk.top());
AstIdent *idn = dynamic_cast<AstIdent *>(g_exp_stk.top());
if(rn) {
@ -729,7 +735,7 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
callee->args.adjustForArgType (numArgs, _exp->expType (pProc));
}
}
res = picode->newStkArg (_exp, (llIcode)picode->ll()->getOpcode(), pProc);
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), pProc);
/* Do not update the size of k if the expression was a segment register
* in a near call */
if (res == false)
@ -744,11 +750,11 @@ int C_CallingConvention::processCArg (Function * callee, Function * pProc, ICODE
/** Eliminates extraneous intermediate icode instructions when finding
* expressions. Generates new hlIcodes in the form of expression trees.
* For HLI_CALL hlIcodes, places the arguments in the argument list. */
void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode,bool isLong) const
void LOCAL_ID::processTargetIcode(ICODE &picode, int &numHlIcodes, ICODE &ticode,bool isLong) const
{
bool res;
HLTYPE &p_hl(*picode->hlU());
HLTYPE &t_hl(*ticode->hlU());
HLTYPE &p_hl(*picode.hlU());
HLTYPE &t_hl(*ticode.hlU());
AstIdent *lhs_ident = dynamic_cast<AstIdent *>(p_hl.asgn.lhs());
switch (t_hl.opcode)
@ -758,11 +764,11 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
if(isLong)
{
forwardSubsLong (lhs_ident->ident.idNode.longIdx,
p_hl.asgn.rhs, picode,ticode,
p_hl.asgn.m_rhs, picode,ticode,
&numHlIcodes);
}
else
this->forwardSubs (lhs_ident, p_hl.asgn.rhs, picode, ticode, numHlIcodes);
this->forwardSubs (lhs_ident, p_hl.asgn.m_rhs, picode, ticode, numHlIcodes);
break;
case HLI_JCOND: case HLI_PUSH: case HLI_RET:
@ -770,7 +776,7 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
{
assert(lhs_ident);
res = Expr::insertSubTreeLongReg (
p_hl.asgn.rhs,
p_hl.asgn.m_rhs,
t_hl.exp.v,
lhs_ident->ident.idNode.longIdx);
}
@ -780,20 +786,20 @@ void LOCAL_ID::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
assert(lhs_reg);
res = Expr::insertSubTreeReg (
t_hl.exp.v,
p_hl.asgn.rhs,
p_hl.asgn.m_rhs,
id_arr[lhs_reg->regiIdx].id.regi,
this);
}
if (res)
{
picode->invalidate();
picode.invalidate();
numHlIcodes--;
}
break;
case HLI_CALL: /* register arguments */
newRegArg ( picode, ticode);
picode->invalidate();
picode.invalidate();
numHlIcodes--;
break;
default:
@ -817,9 +823,9 @@ void C_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE picode) {
numArgs++;
}
}
else if ((cb == 0) && picode->ll()->testFlags(REST_STK))
else if ((cb == 0) and picode->ll()->testFlags(REST_STK))
{
while (! g_exp_stk.empty())
while (not g_exp_stk.empty())
{
k+=processCArg (pp, func, &(*picode), numArgs);
numArgs++;
@ -840,11 +846,11 @@ void Pascal_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE pico
while(k<cb)
{
_exp = g_exp_stk.pop();
if (pp->flg & PROC_ISLIB) /* library function */
if (pp->isLibrary() ) /* library function */
{
if (pp->args.numArgs > 0)
_exp = func->adjustActArgType(_exp, pp->args[numArgs].type);
res = picode->newStkArg (_exp, (llIcode)picode->ll()->getOpcode(), func);
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), func);
}
else /* user function */
{
@ -854,9 +860,10 @@ void Pascal_CallingConvention::processHLI(Function *func,Expr *_exp, iICODE pico
{
fprintf(stderr,"Would try to adjustForArgType with null _exp\n");
}
pp->args.adjustForArgType (numArgs,_exp->expType (func));
else
pp->args.adjustForArgType (numArgs,_exp->expType (func));
}
res = picode->newStkArg (_exp,(llIcode)picode->ll()->getOpcode(), func);
res = picode->newStkArg (_exp, picode->ll()->getOpcode(), func);
}
if (res == false)
k += _exp->hlTypeSize (func);
@ -877,28 +884,28 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
ID *_retVal; // function return value
Expr *_exp; // expression pointer - for HLI_POP and HLI_CALL */
//Expr *lhs; // exp ptr for return value of a HLI_CALL */
iICODE ticode; // Target icode */
HLTYPE *ti_hl=nullptr;
uint8_t regi;
numHlIcodes = 0;
assert(&fnc->localId==&locals);
// register(s) to be forward substituted */
auto valid_and_highlevel = instructions | filtered(ICODE::TypeAndValidFilter<HIGH_LEVEL>());
auto valid_and_highlevel = instructions | filtered(ICODE::TypeAndValidFilter<HIGH_LEVEL_ICODE>());
for (auto picode = valid_and_highlevel.begin(); picode != valid_and_highlevel.end(); picode++)
{
HLTYPE &_icHl(*picode->hlU());
ICODE &_ic(*picode);
HLTYPE &_icHl(*_ic.hlU());
numHlIcodes++;
if (picode->du1.getNumRegsDef() == 1) /* uint8_t/uint16_t regs */
if (_ic.du1.getNumRegsDef() == 1) /* uint8_t/uint16_t regs */
{
/* Check for only one use of this register. If this is
* the last definition of the register in this BB, check
* that it is not liveOut from this basic block */
if (picode->du1.numUses(0)==1)
if (_ic.du1.numUses(0)==1)
{
/* Check that this register is not liveOut, if it
* is the last definition of the register */
regi = picode->du1.regi[0];
regi = _ic.du1.regi[0];
/* Check if we can forward substitute this register */
switch (_icHl.opcode)
@ -907,16 +914,16 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
/* Replace rhs of current icode into target
* icode expression */
ticode = picode->du1.idx[0].uses.front();
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
((ticode->hl()->opcode != HLI_CALL) &&
ticode = _ic.du1.idx[0].uses.front();
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
((ticode->hl()->opcode != HLI_CALL) and
(ticode->hl()->opcode != HLI_RET)))
continue;
if (_icHl.asgn.rhs->xClear (make_iterator_range(picode.base(),picode->du1.idx[0].uses[0]),
if (_icHl.asgn.m_rhs->xClear (make_iterator_range(picode.base(),_ic.du1.idx[0].uses[0]),
end(), locals))
{
locals.processTargetIcode(picode.base(), numHlIcodes, ticode,false);
locals.processTargetIcode(_ic, numHlIcodes, *ticode,false);
}
break;
@ -925,17 +932,17 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
// pop X
// lab1:
// call F() <- somehow this is marked as user of POP ?
ticode = picode->du1.idx[0].uses.front();
ticode = _ic.du1.idx[0].uses.front();
ti_hl = ticode->hlU();
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
((ti_hl->opcode != HLI_CALL) &&
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
((ti_hl->opcode != HLI_CALL) and
(ti_hl->opcode != HLI_RET)))
continue;
_exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->hl()->opcode) {
case HLI_ASSIGN:
locals.forwardSubs(_icHl.expr(), _exp, picode.base(), ticode, numHlIcodes);
locals.forwardSubs(_icHl.expr(), _exp, _ic, *ticode, numHlIcodes);
break;
case HLI_JCOND: case HLI_PUSH: case HLI_RET:
@ -948,7 +955,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
&locals);
if (res)
{
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
}
}
@ -966,25 +973,25 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
break;
case HLI_CALL:
ticode = picode->du1.idx[0].uses.front();
ticode = _ic.du1.idx[0].uses.front();
ti_hl = ticode->hlU();
_retVal = &_icHl.call.proc->retVal;
switch (ti_hl->opcode)
{
case HLI_ASSIGN:
assert(ti_hl->asgn.rhs);
assert(ti_hl->asgn.m_rhs);
_exp = _icHl.call.toAst();
res = Expr::insertSubTreeReg (ti_hl->asgn.rhs,_exp, _retVal->id.regi, &locals);
if (! res)
res = Expr::insertSubTreeReg (ti_hl->asgn.m_rhs,_exp, _retVal->id.regi, &locals);
if (not res)
Expr::insertSubTreeReg (ti_hl->asgn.m_lhs, _exp,_retVal->id.regi, &locals);
//TODO: HERE missing: 2 regs
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
break;
case HLI_PUSH: case HLI_RET:
ti_hl->expr( _icHl.call.toAst() );
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
break;
@ -993,13 +1000,13 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
res = Expr::insertSubTreeReg (ti_hl->exp.v, _exp, _retVal->id.regi, &locals);
if (res) /* was substituted */
{
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
}
else /* cannot substitute function */
{
auto lhs = AstIdent::idID(_retVal,&locals,picode.base());
picode->setAsgn(lhs, _exp);
_ic.setAsgn(lhs, _exp);
}
break;
default:
@ -1012,35 +1019,35 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
}
}
else if (picode->du1.getNumRegsDef() == 2) /* long regs */
else if (_ic.du1.getNumRegsDef() == 2) /* long regs */
{
/* Check for only one use of these registers */
if ((picode->du1.numUses(0) == 1) and (picode->du1.numUses(1) == 1))
if ((_ic.du1.numUses(0) == 1) and (_ic.du1.numUses(1) == 1))
{
regi = picode->du1.regi[0]; //TODO: verify that regi actually should be assigned this
regi = _ic.du1.regi[0]; //TODO: verify that regi actually should be assigned this
switch (_icHl.opcode)
{
case HLI_ASSIGN:
/* Replace rhs of current icode into target
* icode expression */
if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
if (_ic.du1.idx[0].uses[0] == _ic.du1.idx[1].uses[0])
{
ticode = picode->du1.idx[0].uses.front();
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
((ticode->hl()->opcode != HLI_CALL) &&
ticode = _ic.du1.idx[0].uses.front();
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
((ticode->hl()->opcode != HLI_CALL) and
(ticode->hl()->opcode != HLI_RET)))
continue;
locals.processTargetIcode(picode.base(), numHlIcodes, ticode,true);
locals.processTargetIcode(_ic, numHlIcodes, *ticode,true);
}
break;
case HLI_POP:
if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
if (_ic.du1.idx[0].uses[0] == _ic.du1.idx[1].uses[0])
{
ticode = picode->du1.idx[0].uses.front();
if ((picode->du.lastDefRegi.testRegAndSubregs(regi)) &&
((ticode->hl()->opcode != HLI_CALL) &&
ticode = _ic.du1.idx[0].uses.front();
if ((_ic.du.lastDefRegi.testRegAndSubregs(regi)) and
((ticode->hl()->opcode != HLI_CALL) and
(ticode->hl()->opcode != HLI_RET)))
continue;
@ -1048,7 +1055,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
switch (ticode->hl()->opcode) {
case HLI_ASSIGN:
forwardSubsLong (dynamic_cast<AstIdent *>(_icHl.expr())->ident.idNode.longIdx,
_exp, picode.base(), ticode, &numHlIcodes);
_exp, _ic, *ticode, &numHlIcodes);
break;
case HLI_JCOND: case HLI_PUSH:
res = Expr::insertSubTreeLongReg (_exp,
@ -1056,7 +1063,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
dynamic_cast<AstIdent *>(_icHl.asgn.lhs())->ident.idNode.longIdx);
if (res)
{
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
}
break;
@ -1069,7 +1076,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
break;
case HLI_CALL: /* check for function return */
ticode = picode->du1.idx[0].uses.front();
ticode = _ic.du1.idx[0].uses.front();
switch (ticode->hl()->opcode)
{
case HLI_ASSIGN:
@ -1078,33 +1085,33 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
AstIdent::Long(&locals, DST,
ticode,HIGH_FIRST, picode.base(),
eDEF, *(++iICODE(ticode))->ll()));
ticode->hlU()->asgn.rhs = _exp;
picode->invalidate();
ticode->hlU()->asgn.m_rhs = _exp;
_ic.invalidate();
numHlIcodes--;
break;
case HLI_PUSH:
case HLI_RET:
ticode->hlU()->expr( _icHl.call.toAst() );
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
break;
case HLI_JCOND:
_exp = _icHl.call.toAst();
_retVal = &picode->hl()->call.proc->retVal;
_retVal = &_ic.hl()->call.proc->retVal;
res = Expr::insertSubTreeLongReg (_exp,
ticode->hlU()->exp.v,
locals.newLongReg ( _retVal->type, _retVal->longId(), picode.base()));
if (res) /* was substituted */
{
picode->invalidate();
_ic.invalidate();
numHlIcodes--;
}
else /* cannot substitute function */
{
auto lhs = locals.createId(_retVal,picode.base());
picode->setAsgn(lhs, _exp);
_ic.setAsgn(lhs, _exp);
}
break;
default:
@ -1117,6 +1124,7 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
} /* eos */
}
}
/* HLI_PUSH doesn't define any registers, only uses registers.
* Push the associated expression to the register on the local
* expression stack */
@ -1124,8 +1132,8 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
{
g_exp_stk.processExpPush(numHlIcodes, *picode);
}
else if(picode->du1.getNumRegsDef()!=0)
printf("Num def %d\n",picode->du1.getNumRegsDef());
else if(_ic.du1.getNumRegsDef()!=0)
printf("Num def %d\n",_ic.du1.getNumRegsDef());
/* For HLI_CALL instructions that use arguments from the stack,
* pop them from the expression stack and place them on the
@ -1139,11 +1147,11 @@ void BB::findBBExps(LOCAL_ID &locals,Function *fnc)
/* If we could not substitute the result of a function,
* assign it to the corresponding registers */
if ( not _icHl.call.proc->isLibrary() and (not picode->du1.used(0)) and (picode->du1.getNumRegsDef() > 0))
if ( not _icHl.call.proc->isLibrary() and (not _ic.du1.used(0)) and (_ic.du1.getNumRegsDef() > 0))
{
_exp = new FuncNode(_icHl.call.proc, _icHl.call.args);
auto lhs = AstIdent::idID (&_icHl.call.proc->retVal, &locals, picode.base());
picode->setAsgn(lhs, _exp);
_ic.setAsgn(lhs, _exp);
}
}
}
@ -1184,44 +1192,44 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
isBx = _liveOut.testReg(rBX);
isCx = _liveOut.testReg(rCX);
isDx = _liveOut.testReg(rDX);
bool isAL = !isAx && _liveOut.testReg(rAL);
bool isAH = !isAx && _liveOut.testReg(rAH);
bool isBL = !isBx && _liveOut.testReg(rBL);
bool isBH = !isBx && _liveOut.testReg(rBH);
bool isCL = !isCx && _liveOut.testReg(rCL);
bool isCH = !isCx && _liveOut.testReg(rCH);
bool isDL = !isDx && _liveOut.testReg(rDL);
bool isDH = !isDx && _liveOut.testReg(rDH);
if(isAL && isAH)
bool isAL = not isAx and _liveOut.testReg(rAL);
bool isAH = not isAx and _liveOut.testReg(rAH);
bool isBL = not isBx and _liveOut.testReg(rBL);
bool isBH = not isBx and _liveOut.testReg(rBH);
bool isCL = not isCx and _liveOut.testReg(rCL);
bool isCH = not isCx and _liveOut.testReg(rCH);
bool isDL = not isDx and _liveOut.testReg(rDL);
bool isDH = not isDx and _liveOut.testReg(rDH);
if(isAL and isAH)
{
isAx = true;
isAH=isAL=false;
}
if(isDL && isDH)
if(isDL and isDH)
{
isDx = true;
isDH=isDL=false;
}
if(isBL && isBH)
if(isBL and isBH)
{
isBx = true;
isBH=isBL=false;
}
if(isCL && isCH)
if(isCL and isCH)
{
isCx = true;
isCH=isCL=false;
}
if (isAx && isDx) /* long or pointer */
if (isAx and isDx) /* long or pointer */
{
retVal.type = TYPE_LONG_SIGN;
retVal.loc = REG_FRAME;
retVal.longId() = LONGID_TYPE(rDX,rAX);
/*idx = */localId.newLongReg(TYPE_LONG_SIGN, LONGID_TYPE(rDX,rAX), Icode.begin());
localId.propLongId (rAX, rDX, "\0");
localId.propLongId (rAX, rDX, "");
}
else if (isAx || isBx || isCx || isDx) /* uint16_t */
else if (isAx or isBx or isCx or isDx) /* uint16_t */
{
retVal.type = TYPE_WORD_SIGN;
retVal.loc = REG_FRAME;
@ -1235,7 +1243,7 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
retVal.id.regi = rDX;
/*idx = */localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi);
}
else if(isAL||isBL||isCL||isDL)
else if(isAL or isBL or isCL or isDL)
{
retVal.type = TYPE_BYTE_SIGN;
retVal.loc = REG_FRAME;
@ -1249,7 +1257,7 @@ void Function::preprocessReturnDU(LivenessSet &_liveOut)
retVal.id.regi = rDL;
/*idx = */localId.newByteWordReg(TYPE_BYTE_SIGN,retVal.id.regi);
}
else if(isAH||isBH||isCH||isDH)
else if(isAH or isBH or isCH or isDH)
{
retVal.type = TYPE_BYTE_SIGN;
retVal.loc = REG_FRAME;
@ -1285,7 +1293,7 @@ void Function::dataFlow(LivenessSet &_liveOut)
elimCondCodes();
genLiveKtes();
liveRegAnalysis (_liveOut); /* calls dataFlow() recursively */
if (! (flg & PROC_ASM)) /* can generate C for pProc */
if (not (flg & PROC_ASM)) /* can generate C for pProc */
{
genDU1 (); /* generate def/use level 1 chain */
findExps (); /* forward substitution algorithm */

View File

@ -4,124 +4,109 @@
* (C) Cristina Cifuentes
****************************************************************************/
#include <cstring>
#include "dcc.h"
#include "project.h"
#include "msvc_fixes.h"
#include "project.h"
#include "CallGraph.h"
#include "DccFrontend.h"
#include <cstring>
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QCommandLineParser>
#include <QtCore/QFile>
/* Global variables - extern to other modules */
extern char *asm1_name, *asm2_name; /* Assembler output filenames */
extern QString asm1_name, asm2_name; /* Assembler output filenames */
extern SYMTAB symtab; /* Global symbol table */
extern STATS stats; /* cfg statistics */
//PROG prog; /* programs fields */
extern OPTION option; /* Command line options */
//Function * pProcList; /* List of procedures, topologically sort */
//Function * pLastProc; /* Pointer to last node in procedure list */
//FunctionListType pProcList;
//CALL_GRAPH *callGraph; /* Call graph of the program */
static char *initargs(int argc, char *argv[]);
static void displayTotalStats(void);
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/PrettyStackTrace.h>
#include <llvm/Support/Signals.h>
#include <llvm/Support/Host.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetInstrInfo.h>
#include <llvm/MC/MCAsmInfo.h>
#include <llvm/CodeGen/MachineInstrBuilder.h>
#include <llvm/TableGen/Main.h>
#include <llvm/TableGen/TableGenBackend.h>
#include <llvm/TableGen/Record.h>
static void displayTotalStats();
/****************************************************************************
* main
***************************************************************************/
#include <iostream>
using namespace llvm;
bool TVisitor(raw_ostream &OS, RecordKeeper &Records)
{
Record *rec = Records.getDef("ADD8i8");
if(rec)
{
if(not rec->getTemplateArgs().empty())
std::cout << "Has template args\n";
auto classes(rec->getSuperClasses());
for(auto val : rec->getSuperClasses())
std::cout << "Super "<<val->getName()<<"\n";
// DagInit * in = rec->getValueAsDag(val.getName());
// in->dump();
for(const RecordVal &val : rec->getValues())
{
// val.dump();
}
rec->dump();
void setupOptions(const QCoreApplication &app) {
//[-a1a2cmsi]
QCommandLineParser parser;
parser.setApplicationDescription("dcc");
parser.addHelpOption();
//parser.addVersionOption();
//QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
QCommandLineOption boolOpts[] {
QCommandLineOption {"v", QCoreApplication::translate("main", "verbose")},
QCommandLineOption {"V", QCoreApplication::translate("main", "very verbose")},
QCommandLineOption {"c", QCoreApplication::translate("main", "Follow register indirect calls")},
QCommandLineOption {"m", QCoreApplication::translate("main", "Print memory maps of program")},
QCommandLineOption {"s", QCoreApplication::translate("main", "Print stats")}
};
for(QCommandLineOption &o : boolOpts) {
parser.addOption(o);
}
// rec = Records.getDef("CCR");
// if(rec)
// rec->dump();
for(auto val : Records.getDefs())
{
//std::cout<< "Def "<<val.first<<"\n";
}
return false;
}
int testTblGen(int argc, char **argv)
{
using namespace llvm;
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram(argc,argv);
cl::ParseCommandLineOptions(argc,argv);
return llvm::TableGenMain(argv[0],TVisitor);
InitializeNativeTarget();
Triple TheTriple;
std::string def = sys::getDefaultTargetTriple();
std::string MCPU="i386";
std::string MARCH="x86";
InitializeAllTargetInfos();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();
InitializeAllDisassemblers();
std::string TargetTriple("i386-pc-linux-gnu");
TheTriple = Triple(Triple::normalize(TargetTriple));
MCOperand op=llvm::MCOperand::CreateImm(11);
MCAsmInfo info;
raw_os_ostream wrap(std::cerr);
op.print(wrap,&info);
wrap.flush();
std::cerr<<"\n";
std::string lookuperr;
TargetRegistry::printRegisteredTargetsForVersion();
const Target *t = TargetRegistry::lookupTarget(MARCH,TheTriple,lookuperr);
TargetOptions opts;
std::string Features;
opts.PrintMachineCode=1;
TargetMachine *tm = t->createTargetMachine(TheTriple.getTriple(),MCPU,Features,opts);
std::cerr<<tm->getInstrInfo()->getName(97)<<"\n";
const MCInstrDesc &ds(tm->getInstrInfo()->get(97));
const MCOperandInfo *op1=ds.OpInfo;
uint16_t impl_def = ds.getImplicitDefs()[0];
std::cerr<<lookuperr<<"\n";
QCommandLineOption assembly("a", QCoreApplication::translate("main", "Produce assembly"),"assembly_level");
QCommandLineOption targetFileOption(QStringList() << "o" << "output",
QCoreApplication::translate("main", "Place output into <file>."),
QCoreApplication::translate("main", "file"));
QCommandLineOption entryPointOption(QStringList() << "E",
QCoreApplication::translate("main", "Custom entry point as hex"),
QCoreApplication::translate("main", "offset"),
"0"
);
parser.addOption(targetFileOption);
parser.addOption(assembly);
parser.addOption(entryPointOption);
//parser.addOption(forceOption);
// Process the actual command line arguments given by the user
parser.addPositionalArgument("source", QCoreApplication::translate("main", "Dos Executable file to decompile."));
parser.process(app);
exit(0);
const QStringList args = parser.positionalArguments();
if(args.empty()) {
parser.showHelp();
}
// source is args.at(0), destination is args.at(1)
option.verbose = parser.isSet(boolOpts[0]);
option.VeryVerbose = parser.isSet(boolOpts[1]);
if(parser.isSet(assembly)) {
option.asm1 = parser.value(assembly).toInt()==1;
option.asm2 = parser.value(assembly).toInt()==2;
}
option.Map = parser.isSet(boolOpts[3]);
option.Stats = parser.isSet(boolOpts[4]);
option.Interact = false;
option.Calls = parser.isSet(boolOpts[2]);
option.filename = args.first();
option.CustomEntryPoint = parser.value(entryPointOption).toUInt(nullptr,16);
if(parser.isSet(targetFileOption))
asm1_name = asm2_name = parser.value(targetFileOption);
else if(option.asm1 or option.asm2) {
asm1_name = option.filename+".a1";
asm2_name = option.filename+".a2";
}
}
int main(int argc, char **argv)
{
/* Extract switches and filename */
strcpy(option.filename, initargs(argc, argv));
QCoreApplication app(argc,argv);
QCoreApplication::setApplicationVersion("0.1");
setupOptions(app);
/* Front end reads in EXE or COM file, parses it into I-code while
* building the call graph and attaching appropriate bits of code for
* each procedure.
*/
DccFrontend fe(option.filename);
Project::get()->create(option.filename);
DccFrontend fe(&app);
if(not Project::get()->load()) {
return -1;
}
if (option.verbose)
Project::get()->prog.displayLoadInfo();
if(false==fe.FrontEnd ())
return -1;
if(option.asm1)
@ -138,98 +123,16 @@ int main(int argc, char **argv)
* analysis, data flow etc. and outputs it to output file ready for
* re-compilation.
*/
BackEnd(asm1_name ? asm1_name:option.filename, Project::get()->callGraph);
BackEnd(Project::get()->callGraph);
Project::get()->callGraph->write();
if (option.Stats)
displayTotalStats();
/*
freeDataStructures(pProcList);
*/
return 0;
}
/****************************************************************************
* initargs - Extract command line arguments
***************************************************************************/
static char *initargs(int argc, char *argv[])
{
char *pc;
while (--argc > 0 && (*++argv)[0] == '-')
{
for (pc = argv[0]+1; *pc; pc++)
switch (*pc)
{
case 'a': /* Print assembler listing */
if (*(pc+1) == '2')
option.asm2 = true;
else
option.asm1 = true;
if (*(pc+1) == '1' || *(pc+1) == '2')
pc++;
break;
case 'c':
option.Calls = true;
break;
case 'i':
option.Interact = true;
break;
case 'm': /* Print memory map */
option.Map = true;
break;
case 's': /* Print Stats */
option.Stats = true;
break;
case 'V': /* Very verbose => verbose */
option.VeryVerbose = true;
case 'v':
option.verbose = true; /* Make everything verbose */
break;
case 'o': /* assembler output file */
if (*(pc+1)) {
asm1_name = asm2_name = pc+1;
goto NextArg;
}
else if (--argc > 0) {
asm1_name = asm2_name = *++argv;
goto NextArg;
}
default:
fatalError(INVALID_ARG, *pc);
return *argv;
}
NextArg:;
}
if (argc == 1)
{
if (option.asm1 || option.asm2)
{
if (! asm1_name)
{
asm1_name = strcpy((char*)malloc(strlen(*argv)+4), *argv);
pc = strrchr(asm1_name, '.');
if (pc > strrchr(asm1_name, '/'))
{
*pc = '\0';
}
asm2_name = (char*)malloc(strlen(asm1_name)+4) ;
strcat(strcpy(asm2_name, asm1_name), ".a2");
unlink(asm2_name);
strcat(asm1_name, ".a1");
}
unlink(asm1_name); /* Remove asm output files */
}
return *argv; /* filename of the program to decompile */
}
fatalError(USAGE);
return *argv; // does not reach this.
}
static void
displayTotalStats ()
/* Displays final statistics for the complete program */

70
src/dcc_interface.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "dcc_interface.h"
#include "dcc.h"
#include "project.h"
struct DccImpl : public IDcc {
ilFunction m_current_func;
// IDcc interface
public:
void BaseInit()
{
m_current_func = Project::get()->functions().end();
}
void Init(QObject *tgt)
{
}
ilFunction GetFirstFuncHandle()
{
return Project::get()->functions().begin();
}
ilFunction GetCurFuncHandle()
{
return m_current_func;
}
void analysis_Once()
{
}
void load(QString name)
{
option.filename = name;
Project::get()->create(name);
}
void prtout_asm(IXmlTarget *, int level)
{
}
void prtout_cpp(IXmlTarget *, int level)
{
}
size_t getFuncCount()
{
return Project::get()->functions().size();
}
const lFunction &validFunctions() const
{
return Project::get()->functions();
}
void SetCurFunc_by_Name(QString v)
{
lFunction & funcs(Project::get()->functions());
for(auto iter=funcs.begin(),fin=funcs.end(); iter!=fin; ++iter) {
if(iter->name==v) {
m_current_func = iter;
return;
}
}
}
QDir installDir() {
return QDir(".");
}
QDir dataDir(QString kind) { // return directory containing decompilation helper data -> signatures/includes/etc.
QDir res(installDir());
res.cd(kind);
return res;
}
};
IDcc* IDcc::get() {
static IDcc *v=0;
if(nullptr == v)
v = new DccImpl;
return v;
}

View File

@ -2,6 +2,15 @@
* dcc project disassembler
* (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
****************************************************************************/
#include "disassem.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include "symtab.h"
#include "project.h"
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <stdint.h>
#include <vector>
#include <map>
@ -10,10 +19,6 @@
#include <stdio.h>
#include <string.h>
#include "dcc.h"
#include "symtab.h"
#include "disassem.h"
#include "project.h"
// Note: for the time being, there is no interactive disassembler
// for unix
@ -70,8 +75,8 @@ static const char *szFlops3C[] =
static const char *szPtr[2] = { "word ptr ", "byte ptr " };
static void formatRM(ostringstream &p, const LLOperand &pm);
static ostringstream &strDst(ostringstream &os, uint32_t flg, const LLOperand &pm);
static void formatRM(QTextStream & p, const LLOperand &pm);
static QTextStream & strDst(QTextStream & os, uint32_t flg, const LLOperand &pm);
static char *strHex(uint32_t d);
//static int checkScanned(uint32_t pcCur);
@ -107,7 +112,7 @@ static vector<POSSTACK_ENTRY> posStack; /* position stack */
void LLInst::findJumpTargets(CIcodeRec &_pc)
{
if (testFlags(I) && ! testFlags(JMP_ICODE) && isJmpInst())
if (testFlags(I) and not testFlags(JMP_ICODE) and isJmpInst())
{
/* Replace the immediate operand with an icode index */
iICODE labTgt=_pc.labelSrch(src().getImm2());
@ -150,11 +155,11 @@ void Disassembler::disassem(Function * ppProc)
if (pass != 3)
{
auto p = (pass == 1)? asm1_name: asm2_name;
m_fp.open(p,ios_base::app);
if (!m_fp.is_open())
{
fatalError(CANNOT_OPEN, p);
m_disassembly_target = new QFile(p);
if(!m_disassembly_target->open(QFile::WriteOnly|QFile::Text|QFile::Append)) {
fatalError(CANNOT_OPEN, p.toStdString().c_str());
}
m_fp.setDevice(m_disassembly_target);
}
/* Create temporary code array */
// Mike: needs objectising!
@ -177,7 +182,7 @@ void Disassembler::disassem(Function * ppProc)
/* Write procedure header */
if (pass != 3)
{
std::string near_far=(pProc->flg & PROC_FAR)? "FAR": "NEAR";
const char * near_far=(pProc->flg & PROC_FAR)? "FAR": "NEAR";
m_fp << "\t\t"<<pProc->name<<" PROC "<< near_far<<"\n";
}
@ -192,7 +197,10 @@ void Disassembler::disassem(Function * ppProc)
if (pass != 3)
{
m_fp << "\n\t\t"<<pProc->name<<" ENDP\n\n";
m_fp.close();
m_fp.setDevice(nullptr);
m_disassembly_target->close();
delete m_disassembly_target;
}
pc.clear();
@ -206,19 +214,23 @@ void Disassembler::disassem(Function * ppProc)
void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
{
PROG &prog(Project::get()->prog);
ostringstream oper_stream;
ostringstream hex_bytes;
ostringstream result_stream;
ostringstream opcode_with_mods;
ostringstream operands_s;
oper_stream << uppercase;
hex_bytes << uppercase;
QString oper_contents;
QTextStream oper_stream(&oper_contents);
QString hex_bytes;
QString result_contents;
QTextStream result_stream(&result_contents);
QString opcode_with_mods;
QString operands_contents;
QTextStream operands_s(&operands_contents);
oper_stream.setNumberFlags(QTextStream::UppercaseBase|QTextStream::UppercaseDigits);
/* Disassembly stage 1 --
* Do not try to display NO_CODE entries or synthetic instructions,
* other than JMPs, that have been introduced for def/use analysis. */
if ((option.asm1) &&
( inst.testFlags(NO_CODE) ||
(inst.testFlags(SYNTHETIC) && (inst.getOpcode() != iJMP))))
* Do not try to display NO_CODE entries or synthetic instructions,
* other than JMPs, that have been introduced for def/use analysis. */
if ((option.asm1) and
( inst.testFlags(NO_CODE) or
(inst.testFlags(SYNTHETIC) and (inst.getOpcode() != iJMP))))
{
return;
}
@ -242,25 +254,28 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
cb = (uint32_t) inst.numBytes;
nextInst = inst.label + cb;
/* Output hexa code in program image */
/* Output hex code in program image */
if (pass != 3)
{
for (j = 0; j < cb; j++)
{
hex_bytes << hex << setw(2) << setfill('0') << uint16_t(prog.image()[inst.label + j]);
hex_bytes += QString("%1").arg(uint16_t(prog.image()[inst.label + j]),2,16,QChar('0')).toUpper();
}
hex_bytes << ' ';
hex_bytes += ' ';
}
}
oper_stream << setw(POS_LAB) << left<< hex_bytes.str();
oper_stream.setFieldWidth(POS_LAB);
oper_stream.setFieldAlignment(QTextStream::AlignLeft);
oper_stream << hex_bytes;
/* Check if there is a symbol here */
selectTable(Label);
oper_stream << setw(5)<<left; // align for the labels
oper_stream.setFieldWidth(5); // align for the labels
{
ostringstream lab_contents;
if (readVal(lab_contents, inst.label, nullptr))
QString lab_contents;
QTextStream lab_stream(&lab_contents);
if (readVal(lab_stream, inst.label, nullptr))
{
lab_contents << ':'; /* Also removes the null */
lab_stream << ':'; /* Also removes the null */
}
else if (inst.testFlags(TARGET)) /* Symbols override Lnn labels */
{
@ -269,15 +284,17 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
{
pl[loc_ip] = ++g_lab;
}
lab_contents<< "L"<<pl[loc_ip]<<':';
lab_stream<< "L"<<pl[loc_ip]<<':';
}
oper_stream<< lab_contents.str();
lab_stream.flush();
oper_stream << lab_contents;
oper_stream.setFieldWidth(0);
}
if ((inst.getOpcode()==iSIGNEX )&& inst.testFlags(B))
if ((inst.getOpcode()==iSIGNEX )and inst.testFlags(B))
{
inst.setOpcode(iCBW);
}
opcode_with_mods<<Machine_X86::opcodeName(inst.getOpcode());
opcode_with_mods += Machine_X86::opcodeName(inst.getOpcode());
switch ( inst.getOpcode() )
{
@ -342,7 +359,7 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
{
ICODE *lab=pc.GetIcode(inst.src().getImm2());
selectTable(Label);
if ((inst.src().getImm2() < (uint32_t)numIcode) && /* Ensure in range */
if ((inst.src().getImm2() < (uint32_t)numIcode) and /* Ensure in range */
readVal(operands_s, lab->ll()->label, nullptr))
{
break; /* Symbolic label. Done */
@ -381,11 +398,10 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
case iCALL: case iCALLF:
if (inst.testFlags(I))
{
if((inst.getOpcode() == iCALL))
operands_s<< "near";
else
operands_s<< " far";
operands_s<<" ptr "<<(inst.src().proc.proc)->name;
QString oper = QString("%1 ptr %2")
.arg((inst.getOpcode() == iCALL) ? "near" : "far")
.arg((inst.src().proc.proc)->name);
operands_s<< qPrintable(oper);
}
else if (inst.getOpcode() == iCALLF)
{
@ -416,14 +432,14 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
case iOUTS: case iREP_OUTS:
if (inst.src().segOver)
{
bool is_dx_src=(inst.getOpcode() == iOUTS || inst.getOpcode() == iREP_OUTS);
bool is_dx_src=(inst.getOpcode() == iOUTS or inst.getOpcode() == iREP_OUTS);
if(is_dx_src)
operands_s<<"dx, "<<szPtr[inst.getFlag() & B];
else
operands_s<<szPtr[inst.getFlag() & B];
if (inst.getOpcode() == iLODS ||
inst.getOpcode() == iREP_LODS ||
inst.getOpcode() == iOUTS ||
if (inst.getOpcode() == iLODS or
inst.getOpcode() == iREP_LODS or
inst.getOpcode() == iOUTS or
inst.getOpcode() == iREP_OUTS)
{
operands_s<<Machine_X86::regName(inst.src().segOver); // szWreg[src.segOver-rAX]
@ -436,7 +452,10 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
}
else
{
(inst.getFlag() & B)? opcode_with_mods<< "B": opcode_with_mods<< "W";
if(inst.getFlag() & B)
opcode_with_mods+='B';
else
opcode_with_mods+='W';
}
break;
@ -455,8 +474,8 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
case iOUT:
{
std::string d1=((inst.testFlags(I))? strHex(inst.src().getImm2()): "dx");
std::string d2=((inst.getFlag() & B) ? ", al": ", ax");
QString d1=((inst.testFlags(I))? strHex(inst.src().getImm2()): "dx");
QString d2=((inst.getFlag() & B) ? ", al": ", ax");
operands_s<<d1 << d2;
}
break;
@ -464,8 +483,9 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
default:
break;
}
oper_stream << setw(15) << left <<opcode_with_mods.str();
oper_stream << operands_s.str();
oper_stream.setFieldWidth(15);
operands_s.flush();
oper_stream << qSetFieldWidth(15) << opcode_with_mods << qSetFieldWidth(0) << operands_contents;
/* Comments */
if (inst.testFlags(SYNTHETIC))
{
@ -473,21 +493,26 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
}
else
{
for (j = inst.label, fImpure = 0; j > 0 && j < (int)nextInst; j++)
for (j = inst.label, fImpure = 0; j > 0 and j < (int)nextInst; j++)
{
fImpure |= BITMAP(j, BM_DATA);
}
}
result_stream << setw(54) << left << oper_stream.str();
result_stream.setFieldWidth(54);
result_stream.setFieldAlignment(QTextStream::AlignLeft);
oper_stream.flush();
result_stream << oper_contents;
result_stream.setFieldWidth(0);
/* Check for user supplied comment */
selectTable(Comment);
ostringstream cbuf;
QString cbuf_contents;
QTextStream cbuf(&cbuf_contents);
if (readVal(cbuf, inst.label, nullptr))
{
result_stream <<"; "<<cbuf.str();
cbuf.flush();
result_stream <<"; "<<*cbuf.string();
}
else if (fImpure || (inst.testFlags(SWITCH | CASE | SEG_IMMED | IMPURE | SYNTHETIC | TERMINATES)))
else if (fImpure or (inst.testFlags(SWITCH | CASE | SEG_IMMED | IMPURE | SYNTHETIC | TERMINATES)))
{
if (inst.testFlags(CASE))
{
@ -525,8 +550,9 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
/* output to .b code buffer */
if (inst.testFlags(SYNTHETIC))
result_stream<<";Synthetic inst";
if (pass == 3) /* output to .b code buffer */
cCode.appendCode("%s\n", result_stream.str().c_str());
if (pass == 3) { /* output to .b code buffer */
cCode.appendCode("%s\n", qPrintable(result_contents));
}
}
else
@ -542,7 +568,8 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
sprintf(buf,"%03d ",loc_ip);
result_stream<<";Synthetic inst";
}
m_fp<<buf<< " " << result_stream.str() << "\n";
result_stream.flush();
m_fp<<buf<< " " << result_contents << "\n";
}
}
@ -551,7 +578,7 @@ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass)
/****************************************************************************
* formatRM
***************************************************************************/
static void formatRM(std::ostringstream &p, const LLOperand &pm)
static void formatRM(QTextStream &p, const LLOperand &pm)
{
//char seg[4];
@ -588,7 +615,7 @@ static void formatRM(std::ostringstream &p, const LLOperand &pm)
/*****************************************************************************
* strDst
****************************************************************************/
static ostringstream & strDst(ostringstream &os,uint32_t flg, const LLOperand &pm)
static QTextStream & strDst(QTextStream &os,uint32_t flg, const LLOperand &pm)
{
/* Immediates to memory require size descriptor */
//os << setw(WID_PTR);
@ -602,7 +629,7 @@ static ostringstream & strDst(ostringstream &os,uint32_t flg, const LLOperand &p
/****************************************************************************
* strSrc *
****************************************************************************/
ostringstream &LLInst::strSrc(ostringstream &os,bool skip_comma)
QTextStream &LLInst::strSrc(QTextStream &os,bool skip_comma)
{
if(false==skip_comma)
os<<", ";
@ -633,14 +660,18 @@ static char *strHex(uint32_t d)
/****************************************************************************
* interactDis - interactive disassembler *
****************************************************************************/
void interactDis(Function * /*initProc*/, int /*initIC*/)
void interactDis(Function * initProc, int initIC)
{
printf("Sorry - interactive disasassembler option not available for Unix\n");
QString procname = "UNKNOWN";
if(initProc)
procname = initProc->name;
qDebug() << "Wanted to start interactive disasassembler for "<<procname<<":"<<initIC;
return;
}
/* Handle the floating point opcodes (icode iESC) */
void LLInst::flops(std::ostringstream &out)
void LLInst::flops(QTextStream &out)
{
//char bf[30];
uint8_t op = (uint8_t)src().getImm2();
@ -652,8 +683,8 @@ void LLInst::flops(std::ostringstream &out)
{
/* The mod/rm mod bits are not set to 11 (i.e. register). This is the normal floating point opcode */
out<<Machine_X86::floatOpName(op)<<' ';
out <<setw(10);
if ((op == 0x29) || (op == 0x1F))
out.setFieldWidth(10);
if ((op == 0x29) or (op == 0x1F))
{
out << "tbyte ptr ";
}
@ -683,7 +714,7 @@ void LLInst::flops(std::ostringstream &out)
break;
}
}
out.setFieldWidth(0);
formatRM(out, m_dst);
}
else
@ -722,7 +753,7 @@ void LLInst::flops(std::ostringstream &out)
break;
default:
out << Machine_X86::floatOpName(0x40+op);
if ((op >= 0x20) && (op <= 0x27))
if ((op >= 0x20) and (op <= 0x27))
{
/* This is the ST(i), ST form. */
out << "ST("<<destRegIdx - rAX<<"),ST";

View File

@ -11,36 +11,37 @@
#include "dcc.h"
static std::map<eErrorId,std::string> errorMessage =
{
{INVALID_ARG ,"Invalid option -%c\n"},
{INVALID_OPCODE ,"Invalid instruction %02X at location %06lX\n"},
{INVALID_386OP ,"Don't understand 80386 instruction %02X at location %06lX\n"},
{FUNNY_SEGOVR ,"Segment override with no memory operand at location %06lX\n"},
{FUNNY_REP ,"REP prefix without a string instruction at location %06lX\n"},
{CANNOT_OPEN ,"Cannot open %s\n"},
{CANNOT_READ ,"Error while reading %s\n"},
{MALLOC_FAILED ,"malloc of %ld bytes failed\n"},
{NEWEXE_FORMAT ,"Don't understand new EXE format\n"},
{NO_BB ,"Failed to find a BB for jump to %ld in proc %s\n"},
{INVALID_SYNTHETIC_BB,"Basic Block is a synthetic jump\n"},
{INVALID_INT_BB ,"Failed to find a BB for interval\n"},
{IP_OUT_OF_RANGE ,"Instruction at location %06lX goes beyond loaded image\n"},
{DEF_NOT_FOUND ,"Definition not found for condition code usage at opcode %d\n"},
{JX_NOT_DEF ,"JX use, definition not supported at opcode #%d\n"},
{NOT_DEF_USE ,"Def - use not supported. Def op = %d, use op = %d.\n"},
{REPEAT_FAIL ,"Failed to construct repeat..until() condition.\n"},
{WHILE_FAIL ,"Failed to construct while() condition.\n"},
};
static const std::map<eErrorId,std::string> errorMessage =
{
{INVALID_ARG ,"Invalid option -%c\n"},
{INVALID_OPCODE ,"Invalid instruction %02X at location %06lX\n"},
{INVALID_386OP ,"Don't understand 80386 instruction %02X at location %06lX\n"},
{FUNNY_SEGOVR ,"Segment override with no memory operand at location %06lX\n"},
{FUNNY_REP ,"REP prefix without a string instruction at location %06lX\n"},
{CANNOT_OPEN ,"Cannot open %s\n"},
{CANNOT_READ ,"Error while reading %s\n"},
{MALLOC_FAILED ,"malloc of %ld bytes failed\n"},
{NEWEXE_FORMAT ,"Don't understand new EXE format\n"},
{NO_BB ,"Failed to find a BB for jump to %ld in proc %s\n"},
{INVALID_SYNTHETIC_BB,"Basic Block is a synthetic jump\n"},
{INVALID_INT_BB ,"Failed to find a BB for interval\n"},
{IP_OUT_OF_RANGE ,"Instruction at location %06lX goes beyond loaded image\n"},
{DEF_NOT_FOUND ,"Definition not found for condition code usage at opcode %d\n"},
{JX_NOT_DEF ,"JX use, definition not supported at opcode #%d\n"},
{NOT_DEF_USE ,"%x: Def - use not supported. Def op = %d, use op = %d.\n"},
{REPEAT_FAIL ,"Failed to construct repeat..until() condition.\n"},
{WHILE_FAIL ,"Failed to construct while() condition.\n"},
};
/****************************************************************************
fatalError: displays error message and exits the program.
****************************************************************************/
void fatalError(eErrorId errId, ...)
{ va_list args;
//#ifdef __UNIX__ /* ultrix */
{
va_list args;
//#ifdef __UNIX__ /* ultrix */
#if 0
int errId;
int errId;
va_start(args);
errId = va_arg(args, int);
@ -49,10 +50,12 @@ void fatalError(eErrorId errId, ...)
#endif
if (errId == USAGE)
fprintf(stderr,"Usage: dcc [-a1a2cmpsvVi][-o asmfile] DOS_executable\n");
fprintf(stderr,"Usage: dcc [-a1a2cmpsvVi][-o asmfile] DOS_executable\n");
else {
auto msg_iter = errorMessage.find(errId);
assert(msg_iter!=errorMessage.end());
fprintf(stderr, "dcc: ");
vfprintf(stderr, errorMessage[errId].c_str(), args);
vfprintf(stderr, msg_iter->second.c_str(), args);
}
va_end(args);
exit((int)errId);
@ -63,10 +66,11 @@ void fatalError(eErrorId errId, ...)
reportError: reports the warning/error and continues with the program.
****************************************************************************/
void reportError(eErrorId errId, ...)
{ va_list args;
//#ifdef __UNIX__ /* ultrix */
{
va_list args;
//#ifdef __UNIX__ /* ultrix */
#if 0
int errId;
int errId;
va_start(args);
errId = va_arg(args, int);
@ -74,6 +78,8 @@ void reportError(eErrorId errId, ...)
va_start(args, errId);
#endif
fprintf(stderr, "dcc: ");
vfprintf(stderr, errorMessage[errId].c_str(), args);
auto msg_iter = errorMessage.find(errId);
assert(msg_iter!=errorMessage.end());
vfprintf(stderr, msg_iter->second.c_str(), args);
va_end(args);
}

View File

@ -9,18 +9,15 @@
* *
\* * * * * * * * * * * * */
#include "msvc_fixes.h"
#include <memory.h>
#include <stdint.h>
#ifndef PATLEN
#define PATLEN 23
#define WILD 0xF4
#endif
#ifndef bool
#define bool unsigned char
#define uint8_t unsigned char
#endif
static int pc; /* Indexes into pat[] */
/* prototypes */
@ -410,7 +407,7 @@ void fixWildCards(uint8_t pat[])
case 0xCD: /* int nn */
intArg = pat[pc++];
if ((intArg >= 0x34) && (intArg <= 0x3B))
if ((intArg >= 0x34) and (intArg <= 0x3B))
{
/* Borland/Microsoft FP emulations */
if (ModRM(pat)) return;

View File

@ -82,7 +82,7 @@ bool DccFrontend::FrontEnd ()
if (option.asm1)
{
printf("dcc: writing assembler file %s\n", asm1_name);
printf("dcc: writing assembler file %s\n", asm1_name.c_str());
}
/* Search through code looking for impure references and flag them */

View File

@ -3,14 +3,15 @@
* (C) Cristina Cifuentes
****************************************************************************/
#include <string.h>
#include "graph.h"
#include "msvc_fixes.h"
#include "dcc.h"
#include "project.h"
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/adaptors.hpp>
#include "dcc.h"
#include "graph.h"
#include "project.h"
#include <string.h>
using namespace std;
using namespace boost;
@ -19,6 +20,14 @@ extern Project g_proj;
//static void mergeFallThrough(Function * pProc, BB * pBB);
//static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, int *first, int *last);
void Function::addOutEdgesForConditionalJump(BB * pBB,int next_ip, LLInst *ll)
{
pBB->addOutEdge(next_ip);
/* This is checking for jumps off into nowhere */
if ( not ll->testFlags(NO_LABEL) )
pBB->addOutEdge(ll->src().getImm2());
}
/*****************************************************************************
* createCFG - Create the basic control flow graph
****************************************************************************/
@ -68,16 +77,13 @@ void Function::createCFG()
case iJO: case iJNO: case iJP: case iJNP:
case iJCXZ:
pBB = BB::Create(current_range, TWO_BRANCH, this);
CondJumps:
pBB->addOutEdge(nextIcode->loc_ip);
/* This is checking for jumps off into nowhere */
if ( not ll->testFlags(NO_LABEL) )
pBB->addOutEdge(ll->src().getImm2());
addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
break;
case iLOOP: case iLOOPE: case iLOOPNE:
pBB = BB::Create(current_range, LOOP_NODE, this);
goto CondJumps;
addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
break;
case iJMPF: case iJMP:
if (ll->testFlags(SWITCH))
@ -100,7 +106,7 @@ CondJumps:
{
Function * p = ll->src().proc.proc;
pBB = BB::Create(current_range, CALL_NODE, this);
if (p && not ((p->flg) & TERMINATES) )
if (p and not ((p->flg) & TERMINATES) )
pBB->addOutEdge(nextIcode->loc_ip);
break;
}
@ -132,6 +138,8 @@ CondJumps:
// end iterator will be updated by expression in for statement
current_range=make_iterator_range(nextIcode,nextIcode);
}
if (nextIcode == Icode.end())
break;
}
for (auto pr : m_ip_to_bb)
{
@ -146,7 +154,7 @@ CondJumps:
}
auto iter2=m_ip_to_bb.find(ip);
if(iter2==m_ip_to_bb.end())
fatalError(NO_BB, ip, name.c_str());
fatalError(NO_BB, ip, qPrintable(name));
psBB = iter2->second;
elem.BBptr = psBB;
psBB->inEdges.push_back((BB *)nullptr);
@ -203,7 +211,7 @@ void Function::compressCFG()
* (Un)Conditional -> Unconditional jump */
for (BB *pBB : m_actual_cfg) //m_cfg
{
if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH))
if(pBB->inEdges.empty() or (pBB->nodeType != ONE_BRANCH and pBB->nodeType != TWO_BRANCH))
continue;
for (TYPEADR_TYPE &edgeRef : pBB->edges)
{
@ -265,7 +273,7 @@ BB *BB::rmJMP(int marker, BB * pBB)
{
marker += (int)DFS_JMP;
while (pBB->nodeType == ONE_BRANCH && pBB->size() == 1)
while (pBB->nodeType == ONE_BRANCH and pBB->size() == 1)
{
if (pBB->traversed != marker)
{
@ -292,7 +300,7 @@ BB *BB::rmJMP(int marker, BB * pBB)
do {
pBB = pBB->edges[0].BBptr;
pBB->inEdges.pop_back(); // was --numInedges
if (! pBB->inEdges.empty())
if (not pBB->inEdges.empty())
{
pBB->front().ll()->setFlags(NO_CODE);
pBB->front().invalidate();
@ -314,11 +322,11 @@ BB *BB::rmJMP(int marker, BB * pBB)
void BB::mergeFallThrough( CIcodeRec &Icode)
{
BB * pChild;
if (!this)
if (nullptr==this)
{
printf("mergeFallThrough on empty BB!\n");
}
while (nodeType == FALL_NODE || nodeType == ONE_BRANCH)
while (nodeType == FALL_NODE or nodeType == ONE_BRANCH)
{
pChild = edges[0].BBptr;
/* Jump to next instruction can always be removed */

View File

@ -4,17 +4,17 @@
* Date: September-October 1993
* (C) Cristina Cifuentes
*/
#include "dcc.h"
#include <QtCore/QDebug>
#include <QtCore/QString>
#include <cassert>
#include <string.h>
#include <string>
#include <sstream>
#include "dcc.h"
using namespace std;
static char buf[lineSize]; /* Line buffer for hl icode output */
/* Places the new HLI_ASSIGN high-level operand in the high-level icode array */
void HLTYPE::setAsgn(Expr *lhs, Expr *rhs)
{
@ -24,12 +24,12 @@ void HLTYPE::setAsgn(Expr *lhs, Expr *rhs)
}
void ICODE::checkHlCall()
{
//assert((ll()->immed.proc.cb != 0)||ll()->immed.proc.proc!=0);
//assert((ll()->immed.proc.cb != 0) or ll()->immed.proc.proc!=0);
}
/* Places the new HLI_CALL high-level operand in the high-level icode array */
void ICODE::newCallHl()
{
type = HIGH_LEVEL;
type = HIGH_LEVEL_ICODE;
hlU()->setCall(ll()->src().proc.proc);
if (ll()->src().proc.cb != 0)
@ -48,7 +48,7 @@ void ICODE::newCallHl()
* array */
void ICODE::setUnary(hlIcode op, Expr *_exp)
{
type = HIGH_LEVEL;
type = HIGH_LEVEL_ICODE;
hlU()->set(op,_exp);
}
@ -56,7 +56,7 @@ void ICODE::setUnary(hlIcode op, Expr *_exp)
/* Places the new HLI_JCOND high-level operand in the high-level icode array */
void ICODE::setJCond(Expr *cexp)
{
type = HIGH_LEVEL;
type = HIGH_LEVEL_ICODE;
hlU()->set(HLI_JCOND,cexp);
}
@ -83,7 +83,7 @@ bool ICODE::removeDefRegi (eReg regi, int thisDefIdx, LOCAL_ID *locId)
for ( ; numDefs > 0; numDefs--)
{
if (du1.used(numDefs-1)||(du.lastDefRegi.testReg(regi)))
if (du1.used(numDefs-1) or (du.lastDefRegi.testReg(regi)))
break;
}
}
@ -320,7 +320,7 @@ void Function::highLevelGen()
LLOperand *src_ll = ll->get(SRC);
if ( ll->testFlags(NOT_HLL) )
pIcode->invalidate();
if ((pIcode->type != LOW_LEVEL) or not pIcode->valid() )
if ((pIcode->type != LOW_LEVEL_ICODE) or not pIcode->valid() )
continue;
_flg = ll->getFlag();
if (not ll->testFlags(IM_OPS)) /* not processing IM_OPS yet */
@ -328,7 +328,7 @@ void Function::highLevelGen()
{
if ( not ll->testFlags(NO_SRC) ) /* if there is src op */
rhs = AstIdent::id (*pIcode->ll(), SRC, this, i, *pIcode, NONE);
if(ll->m_dst.isSet() || (ll->getOpcode()==iMOD))
if(ll->m_dst.isSet() or (ll->getOpcode()==iMOD))
lhs = AstIdent::id (*pIcode->ll(), DST, this, i, *pIcode, NONE);
}
if(ll->getOpcode()==iPUSH) {
@ -353,7 +353,7 @@ void Function::highLevelGen()
case iCALL:
case iCALLF:
pIcode->type = HIGH_LEVEL;
pIcode->type = HIGH_LEVEL_ICODE;
pIcode->hl( ll->createCall() );
break;
@ -477,82 +477,68 @@ void Function::highLevelGen()
/* Returns the string that represents the procedure call of tproc (ie. with
* actual parameters) */
std::string Function::writeCall (Function * tproc, STKFRAME & args, int *numLoc)
QString Function::writeCall (Function * tproc, STKFRAME & args, int *numLoc)
{
//string condExp;
ostringstream ostr;
ostr<<tproc->name<<" (";
QString ostr;
ostr+=tproc->name+" (";
for(const STKSYM &sym : args)
{
if(sym.actual)
ostr << sym.actual->walkCondExpr (this, numLoc);
ostr += sym.actual->walkCondExpr(this, numLoc);
else
ostr << "";
ostr += "";
if((&sym)!=&(args.back()))
ostr << ", ";
ostr += ", ";
}
ostr << ")";
return ostr.str();
ostr += ")";
return ostr;
}
/* Displays the output of a HLI_JCOND icode. */
const char *writeJcond (const HLTYPE &h, Function * pProc, int *numLoc)
QString writeJcond (const HLTYPE &h, Function * pProc, int *numLoc)
{
memset (buf, ' ', sizeof(buf));
buf[0] = '\0';
strcat (buf, "if ");
if(h.opcode==HLI_INVALID)
{
return "if (*HLI_INVALID*) {\n";
}
assert(h.expr());
Expr *inverted=h.expr()->inverse();
//inverseCondOp (&h.exp);
std::string e = inverted->walkCondExpr (pProc, numLoc);
QString inverted_form = inverted->walkCondExpr (pProc, numLoc);
delete inverted;
strcat (buf, e.c_str());
strcat (buf, " {\n");
return (buf);
return QString("if %1 {\n").arg(inverted_form);
}
/* Displays the inverse output of a HLI_JCOND icode. This is used in the case
* when the THEN clause of an if..then..else is empty. The clause is
* negated and the ELSE clause is used instead. */
const char *writeJcondInv(HLTYPE h, Function * pProc, int *numLoc)
QString writeJcondInv(HLTYPE h, Function * pProc, int *numLoc)
{
memset (buf, ' ', sizeof(buf));
buf[0] = '\0';
strcat (buf, "if ");
std::string e;
QString _form;
if(h.expr()==nullptr)
e = "( *failed condition recovery* )";
_form = "( *failed condition recovery* )";
else
e = h.expr()->walkCondExpr (pProc, numLoc);
strcat (buf, e.c_str());
strcat (buf, " {\n");
return (buf);
_form = h.expr()->walkCondExpr (pProc, numLoc);
return QString("if %1 {\n").arg(_form);
}
string AssignType::writeOut(Function *pProc, int *numLoc) const
QString AssignType::writeOut(Function *pProc, int *numLoc) const
{
ostringstream ostr;
ostr << m_lhs->walkCondExpr (pProc, numLoc);
ostr << " = ";
ostr << rhs->walkCondExpr (pProc, numLoc);
ostr << ";\n";
return ostr.str();
return QString("%1 = %2;\n")
.arg(m_lhs->walkCondExpr (pProc, numLoc))
.arg(m_rhs->walkCondExpr (pProc, numLoc));
}
string CallType::writeOut(Function *pProc, int *numLoc) const
QString CallType::writeOut(Function *pProc, int *numLoc) const
{
ostringstream ostr;
ostr << pProc->writeCall (proc, *args, numLoc);
ostr << ";\n";
return ostr.str();
return pProc->writeCall (proc, *args, numLoc) + ";\n";
}
string ExpType::writeOut(Function *pProc, int *numLoc) const
QString ExpType::writeOut(Function *pProc, int *numLoc) const
{
if(v==nullptr)
return "";
@ -567,44 +553,39 @@ void HLTYPE::set(Expr *l, Expr *r)
//assert((asgn.lhs==0) and (asgn.rhs==0)); //prevent memory leaks
assert(dynamic_cast<UnaryOperator *>(l));
asgn.m_lhs=l;
asgn.rhs=r;
asgn.m_rhs=r;
}
/* Returns a string with the contents of the current high-level icode.
* Note: this routine does not output the contens of HLI_JCOND icodes. This is
* done in a separate routine to be able to support the removal of
* empty THEN clauses on an if..then..else. */
string HLTYPE::write1HlIcode (Function * pProc, int *numLoc) const
QString HLTYPE::write1HlIcode (Function * pProc, int *numLoc) const
{
string e;
ostringstream ostr;
const HlTypeSupport *p = get();
switch (opcode)
{
case HLI_ASSIGN:
return p->writeOut(pProc,numLoc);
case HLI_CALL:
return p->writeOut(pProc,numLoc);
case HLI_RET:
e = p->writeOut(pProc,numLoc);
if (! e.empty())
ostr << "return (" << e << ");\n";
break;
case HLI_POP:
ostr << "HLI_POP ";
ostr << p->writeOut(pProc,numLoc);
ostr << "\n";
break;
case HLI_PUSH:
ostr << "HLI_PUSH ";
ostr << p->writeOut(pProc,numLoc);
ostr << "\n";
break;
case HLI_JCOND: //Handled elsewhere
break;
default:
fprintf(stderr," HLTYPE::write1HlIcode - Unhandled opcode %d\n",opcode);
case HLI_ASSIGN:
return p->writeOut(pProc,numLoc);
case HLI_CALL:
return p->writeOut(pProc,numLoc);
case HLI_RET:
{
QString e;
e = p->writeOut(pProc,numLoc);
if (not e.isEmpty())
return QString("return (%1);\n").arg(e);
break;
}
return ostr.str();
case HLI_POP:
return QString("HLI_POP %1\n").arg(p->writeOut(pProc,numLoc));
case HLI_PUSH:
return QString("HLI_PUSH %1\n").arg(p->writeOut(pProc,numLoc));
case HLI_JCOND: //Handled elsewhere
break;
default:
qCritical() << " HLTYPE::write1HlIcode - Unhandled opcode" << opcode;
}
return "";
}
@ -619,16 +600,22 @@ void ICODE::writeDU()
{
int my_idx = loc_ip;
{
ostringstream ostr;
Machine_X86::writeRegVector(ostr,du.def);
if (!ostr.str().empty())
printf ("Def (reg) = %s\n", ostr.str().c_str());
QString ostr_contents;
{
QTextStream ostr(&ostr_contents);
Machine_X86::writeRegVector(ostr,du.def);
}
if (not ostr_contents.isEmpty())
qDebug() << QString("Def (reg) = %1\n").arg(ostr_contents);
}
{
ostringstream ostr;
Machine_X86::writeRegVector(ostr,du.use);
if (!ostr.str().empty())
printf ("Use (reg) = %s\n", ostr.str().c_str());
QString ostr_contents;
{
QTextStream ostr(&ostr_contents);
Machine_X86::writeRegVector(ostr,du.use);
}
if (not ostr_contents.isEmpty())
qDebug() << QString("Use (reg) = %1\n").arg(ostr_contents);
}
/* Print du1 chain */

View File

@ -1,16 +1,16 @@
// Object oriented icode code for dcc
// (C) 1997 Mike Van Emmerik
#include "icode.h"
#include <stdlib.h>
#include "msvc_fixes.h"
#include "dcc.h"
#include "types.h" // Common types like uint8_t, etc
#include "ast.h" // Some icode types depend on these
#include "icode.h"
#include <stdlib.h>
ICODE::TypeFilter<HIGH_LEVEL> ICODE::select_high_level;
ICODE::TypeAndValidFilter<HIGH_LEVEL> ICODE::select_valid_high_level;
ICODE::TypeFilter<HIGH_LEVEL_ICODE> ICODE::select_high_level;
ICODE::TypeAndValidFilter<HIGH_LEVEL_ICODE> ICODE::select_valid_high_level;
CIcodeRec::CIcodeRec()
{
}
@ -87,7 +87,7 @@ void LLInst::emitGotoLabel (int indLevel)
bool LLOperand::isReg() const
{
return (regi>=rAX) && (regi<=rTMP);
return (regi>=rAX) and (regi<=rTMP);
}
void LLOperand::addProcInformation(int param_count, CConv::Type call_conv)
{

View File

@ -3,10 +3,12 @@
* (C) Cristina Cifuentes
****************************************************************************/
#include <llvm/Support/PatternMatch.h>
#include <boost/iterator/filter_iterator.hpp>
#include <cstring>
#include <deque>
//#include <llvm/Config/llvm-config.h>
//#if( (LLVM_VERSION_MAJOR==3 ) and (LLVM_VERSION_MINOR>3) )
//#include <llvm/IR/PatternMatch.h>
//#else
//#include <llvm/Support/PatternMatch.h>
//#endif
#include "idiom.h"
#include "idiom1.h"
#include "epilogue_idioms.h"
@ -17,6 +19,11 @@
#include "shift_idioms.h"
#include "arith_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include <boost/iterator/filter_iterator.hpp>
#include <cstring>
#include <deque>
/*****************************************************************************
* JmpInst - Returns true if opcode is a conditional or unconditional jump
****************************************************************************/
@ -117,11 +124,11 @@ void Function::findIdioms()
/* Check for library functions that return a long register.
* Propagate this result */
if (pIcode->ll()->src().proc.proc != nullptr)
if ((pIcode->ll()->src().proc.proc->flg & PROC_ISLIB) &&
if ((pIcode->ll()->src().proc.proc->flg & PROC_ISLIB) and
(pIcode->ll()->src().proc.proc->flg & PROC_IS_FUNC))
{
if ((pIcode->ll()->src().proc.proc->retVal.type==TYPE_LONG_SIGN)
|| (pIcode->ll()->src().proc.proc->retVal.type == TYPE_LONG_UNSIGN))
or (pIcode->ll()->src().proc.proc->retVal.type == TYPE_LONG_UNSIGN))
localId.newLongReg(TYPE_LONG_SIGN, LONGID_TYPE(rDX,rAX), pIcode/*ip*/);
}
@ -204,14 +211,14 @@ void Function::findIdioms()
}
/* Check if number of parameter bytes match their calling convention */
if ((flg & PROC_HLL) && (!args.empty()))
if ((flg & PROC_HLL) and (not args.empty()))
{
args.m_minOff += (flg & PROC_FAR ? 4 : 2);
args.m_minOff += ((flg & PROC_FAR)!=0 ? 4 : 2);
delta = args.maxOff - args.m_minOff;
if (cbParam != delta)
{
cbParam = delta;
callingConv(CConv::UNKNOWN);
callingConv(CConv::eUnknown);
}
}
}
@ -231,7 +238,7 @@ void Function::bindIcodeOff()
for(ICODE &c : Icode) // TODO: use filtered here
{
LLInst *ll=c.ll();
if (ll->testFlags(I) && ll->isJmpInst())
if (ll->testFlags(I) and ll->isJmpInst())
{
iICODE loc=Icode.labelSrch(ll->src().getImm2());
if (loc!=Icode.end())
@ -251,7 +258,7 @@ void Function::bindIcodeOff()
if (ll->testFlags(I) )
{
uint32_t found;
if (! Icode.labelSrch(ll->src().getImm2(), found))
if (not Icode.labelSrch(ll->src().getImm2(), found))
ll->setFlags( NO_LABEL );
else
ll->replaceSrc(LLOperand::CreateImm2(found));

View File

@ -1,5 +1,10 @@
#include "dcc.h"
#include "arith_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include <QtCore/QDebug>
using namespace std;
/*****************************************************************************
@ -105,7 +110,7 @@ bool Idiom18::match(iICODE picode)
if(not m_icodes[0]->ll()->matchWithRegDst(iMOV) )
return false;
regi = m_icodes[0]->ll()->m_dst.regi;
if( not ( m_icodes[2]->ll()->match(iCMP,regi) &&
if( not ( m_icodes[2]->ll()->match(iCMP,regi) and
m_icodes[3]->ll()->conditionalJump() ) )
return false;
// Simple matching finished, select apropriate matcher based on dst type
@ -118,9 +123,9 @@ bool Idiom18::match(iICODE picode)
else if ( m_icodes[1]->ll()->m_dst.isReg() ) /* register */
{
m_idiom_type = 1;
// if ((m_icodes[1]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR))
// if ((m_icodes[1]->ll()->dst.regi == rSI) and (m_func->flg & SI_REGVAR))
// m_idiom_type = 1;
// else if ((m_icodes[1]->ll()->dst.regi == rDI) && (m_func->flg & DI_REGVAR))
// else if ((m_icodes[1]->ll()->dst.regi == rDI) and (m_func->flg & DI_REGVAR))
// m_idiom_type = 1;
}
else if (m_icodes[1]->ll()->m_dst.off) /* local variable */
@ -131,7 +136,8 @@ bool Idiom18::match(iICODE picode)
/* not supported yet */
ICODE &ic(*picode);
const Function *my_proc(ic.getParent()->getParent());
printf("Unsupported idiom18 type at %x in %s:%x : indexed\n",ic.loc_ip,my_proc->name.c_str(),my_proc->procEntry);
qWarning() << "Unsupported idiom18 type at"<< QString::number(ic.loc_ip,16)
<< "in"<< my_proc->name <<':'<< QString::number(my_proc->procEntry,16) << "- indexed";
}
switch(m_idiom_type)
@ -141,13 +147,13 @@ bool Idiom18::match(iICODE picode)
break;
case 1: /* register variable */
/* Check previous instruction for a MOV */
if ( (m_icodes[0]->ll()->src().regi == m_icodes[1]->ll()->m_dst.regi))
if ( m_icodes[0]->ll()->src().regi == m_icodes[1]->ll()->m_dst.regi)
{
return true;
}
break;
case 2: /* local */
if ((m_icodes[0]->ll()->src().off == m_icodes[1]->ll()->m_dst.off))
if (m_icodes[0]->ll()->src().off == m_icodes[1]->ll()->m_dst.off)
{
return true;
}
@ -204,8 +210,8 @@ bool Idiom19::match(iICODE picode)
/* not supported yet */ ;
else if ( m_icodes[0]->ll()->m_dst.isReg() ) /* register */
{
// if (((picode->ll()->dst.regi == rSI) && (pproc->flg & SI_REGVAR)) ||
// ((picode->ll()->dst.regi == rDI) && (pproc->flg & DI_REGVAR)))
// if (((picode->ll()->dst.regi == rSI) and (pproc->flg & SI_REGVAR)) or
// ((picode->ll()->dst.regi == rDI) and (pproc->flg & DI_REGVAR)))
return true;
}
else if (m_icodes[0]->ll()->m_dst.off) /* stack variable */
@ -271,9 +277,9 @@ bool Idiom20::match(iICODE picode)
else if ( ll_dest.isReg() ) /* register */
{
type = 1;
// if ((ll_dest.regi == rSI) && (m_func->flg & SI_REGVAR))
// if ((ll_dest.regi == rSI) and (m_func->flg & SI_REGVAR))
// type = 1;
// else if ((ll_dest.regi == rDI) && (m_func->flg & DI_REGVAR))
// else if ((ll_dest.regi == rDI) and (m_func->flg & DI_REGVAR))
// type = 1;
}
else if (ll_dest.off) /* local variable */
@ -286,7 +292,7 @@ bool Idiom20::match(iICODE picode)
}
regi = m_icodes[1]->ll()->m_dst.regi;
const LLOperand &mov_src(m_icodes[1]->ll()->src());
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) && m_icodes[3]->ll()->conditionalJump())
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) and m_icodes[3]->ll()->conditionalJump())
{
switch(type)
{

View File

@ -1,5 +1,8 @@
#include "dcc.h"
#include "call_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
using namespace std;
/*****************************************************************************
* idiom3 - C calling convention.
@ -22,7 +25,7 @@ bool Idiom3::match(iICODE picode)
/* Match ADD SP, immed */
for(int i=0; i<2; ++i)
m_icodes[i] = picode++;
if ( m_icodes[1]->ll()->testFlags(I) && m_icodes[1]->ll()->match(iADD,rSP))
if ( m_icodes[1]->ll()->testFlags(I) and m_icodes[1]->ll()->match(iADD,rSP))
{
m_param_count = m_icodes[1]->ll()->src().getImm2();
return true;
@ -38,7 +41,7 @@ int Idiom3::action()
{
if (m_icodes[0]->ll()->testFlags(I) )
{
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::C);
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::eCdecl);
}
else
{
@ -78,10 +81,10 @@ bool Idiom17::match(iICODE picode)
{
int i=0;
regi = m_icodes[1]->ll()->m_dst.regi;
if ((regi >= rAX) && (regi <= rBX))
if ((regi >= rAX) and (regi <= rBX))
i++;
while (picode != m_end && picode->ll()->match(iPOP))
while (picode != m_end and picode->ll()->match(iPOP))
{
if (picode->ll()->m_dst.regi != regi)
break;
@ -96,7 +99,7 @@ int Idiom17::action()
{
if (m_icodes[0]->ll()->testFlags(I))
{
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::C);
m_icodes[0]->ll()->src().addProcInformation(m_param_count,CConv::eCdecl);
for(size_t idx=1; idx<m_icodes.size(); ++idx)
{
m_icodes[idx]->invalidate();

View File

@ -1,6 +1,8 @@
#include "dcc.h"
#include "epilogue_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
/*****************************************************************************
* popStkVars - checks for
* [POP DI]
@ -14,9 +16,9 @@ void EpilogIdiom::popStkVars(iICODE pIcode)
/* Match [POP DI] */
if (pIcode->ll()->match(iPOP))
{
if ((m_func->flg & DI_REGVAR) && pIcode->ll()->match(rDI))
if ((m_func->flg & DI_REGVAR) and pIcode->ll()->match(rDI))
m_icodes.push_front(pIcode);
else if ((m_func->flg & SI_REGVAR) && pIcode->ll()->match(rSI))
else if ((m_func->flg & SI_REGVAR) and pIcode->ll()->match(rSI))
m_icodes.push_front(pIcode);
}
++pIcode;
@ -25,9 +27,9 @@ void EpilogIdiom::popStkVars(iICODE pIcode)
/* Match [POP SI] */
if (pIcode->ll()->match(iPOP))
{
if ((m_func->flg & SI_REGVAR) && pIcode->ll()->match(rSI))
if ((m_func->flg & SI_REGVAR) and pIcode->ll()->match(rSI))
m_icodes.push_front(pIcode);
else if ((m_func->flg & DI_REGVAR) && pIcode->ll()->match(rDI))
else if ((m_func->flg & DI_REGVAR) and pIcode->ll()->match(rDI))
m_icodes.push_front(pIcode);
}
}
@ -46,7 +48,7 @@ bool Idiom2::match(iICODE pIcode)
iICODE nicode;
if(pIcode==m_func->Icode.begin()) // pIcode->loc_ip == 0
return false;
if ( pIcode->ll()->testFlags(I) || (not pIcode->ll()->match(rSP,rBP)) )
if ( pIcode->ll()->testFlags(I) or (not pIcode->ll()->match(rSP,rBP)) )
return false;
if(distance(pIcode,m_end)<3)
return false;
@ -55,21 +57,21 @@ bool Idiom2::match(iICODE pIcode)
m_icodes.push_back(pIcode);
/* Get next icode, skip over holes in the icode array */
nicode = ++iICODE(pIcode);
while (nicode->ll()->testFlags(NO_CODE) && (nicode != m_end))
while (nicode->ll()->testFlags(NO_CODE) and (nicode != m_end))
{
nicode++;
}
if(nicode == m_end)
return false;
if (nicode->ll()->match(iPOP,rBP) && ! (nicode->ll()->testFlags(I | TARGET | CASE)) )
if (nicode->ll()->match(iPOP,rBP) and not (nicode->ll()->testFlags(I | TARGET | CASE)) )
{
m_icodes.push_back(nicode++); // Matched POP BP
/* Match RET(F) */
if ( nicode != m_end &&
!(nicode->ll()->testFlags(I | TARGET | CASE)) &&
(nicode->ll()->match(iRET) || nicode->ll()->match(iRETF))
if ( nicode != m_end and
not (nicode->ll()->testFlags(I | TARGET | CASE)) and
(nicode->ll()->match(iRET) or nicode->ll()->match(iRETF))
)
{
m_icodes.push_back(nicode); // Matched RET
@ -118,7 +120,7 @@ bool Idiom4::match(iICODE pIcode)
{
iICODE prev1 = --iICODE(pIcode);
/* Check for POP BP */
if (prev1->ll()->match(iPOP,rBP) && not prev1->ll()->testFlags(I) )
if (prev1->ll()->match(iPOP,rBP) and not prev1->ll()->testFlags(I) )
m_icodes.push_back(prev1);
else if(prev1!=m_func->Icode.begin())
{
@ -138,7 +140,7 @@ bool Idiom4::match(iICODE pIcode)
}
int Idiom4::action()
{
if( ! m_icodes.empty()) // if not an empty RET[F] N
if( not m_icodes.empty()) // if not an empty RET[F] N
{
for(size_t idx=0; idx<m_icodes.size()-1; ++idx) // don't invalidate last entry
m_icodes[idx]->invalidate();
@ -146,7 +148,7 @@ int Idiom4::action()
if(m_param_count)
{
m_func->cbParam = (int16_t)m_param_count;
m_func->callingConv(CConv::PASCAL);
m_func->callingConv(CConv::ePascal);
}
return 1;
}

View File

@ -1,5 +1,8 @@
#include "idiom1.h"
#include "dcc.h"
#include "msvc_fixes.h"
/*****************************************************************************
* checkStkVars - Checks for PUSH SI
@ -19,14 +22,14 @@ int Idiom1::checkStkVars (iICODE pIcode)
{
si_matched = 1;
++pIcode;
if ((pIcode != m_end) && pIcode->ll()->match(iPUSH,rDI)) // Look for PUSH DI
if ((pIcode != m_end) and pIcode->ll()->match(iPUSH,rDI)) // Look for PUSH DI
di_matched = 1;
}
else if (pIcode->ll()->match(iPUSH,rDI))
{
di_matched = 1;
++pIcode;
if ((pIcode != m_end) && pIcode->ll()->match(iPUSH,rSI)) // Look for PUSH SI
if ((pIcode != m_end) and pIcode->ll()->match(iPUSH,rSI)) // Look for PUSH SI
si_matched = 1;
}
m_func->flg |= (si_matched ? SI_REGVAR : 0) | (di_matched ? DI_REGVAR : 0);
@ -60,13 +63,13 @@ bool Idiom1::match(iICODE picode)
m_icodes.clear();
m_min_off = 0;
/* PUSH BP as first instruction of procedure */
if ( (not picode->ll()->testFlags(I)) && picode->ll()->src().regi == rBP)
if ( (not picode->ll()->testFlags(I)) and picode->ll()->src().regi == rBP)
{
m_icodes.push_back( picode++ ); // insert iPUSH
if(picode==m_end)
return false;
/* MOV BP, SP as next instruction */
if ( !picode->ll()->testFlags(I | TARGET | CASE) && picode->ll()->match(iMOV ,rBP,rSP) )
if ( not picode->ll()->testFlags(I | TARGET | CASE) and picode->ll()->match(iMOV ,rBP,rSP) )
{
m_icodes.push_back( picode++ ); // insert iMOV
if(picode==m_end)
@ -75,7 +78,7 @@ bool Idiom1::match(iICODE picode)
/* Look for SUB SP, immed */
if (
picode->ll()->testFlags(I | TARGET | CASE) && picode->ll()->match(iSUB,rSP)
picode->ll()->testFlags(I | TARGET | CASE) and picode->ll()->match(iSUB,rSP)
)
{
m_icodes.push_back( picode++ ); // insert iSUB
@ -98,8 +101,8 @@ bool Idiom1::match(iICODE picode)
if(picode == m_end)
return false;
/* Look for MOV BP, SP */
if ( picode != m_end &&
!picode->ll()->testFlags(I | TARGET | CASE) &&
if ( picode != m_end and
not picode->ll()->testFlags(I | TARGET | CASE) and
picode->ll()->match(iMOV,rBP,rSP))
{
m_icodes.push_back(picode);
@ -119,7 +122,7 @@ bool Idiom1::match(iICODE picode)
m_icodes.push_back(picode++);
}
return !m_icodes.empty();
return not m_icodes.empty();
}
int Idiom1::action()
{

View File

@ -1,5 +1,8 @@
#include "dcc.h"
#include "mov_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
using namespace std;
/*****************************************************************************
@ -27,20 +30,20 @@ bool Idiom14::match(iICODE pIcode)
return false;
m_icodes[0]=pIcode++;
m_icodes[1]=pIcode++;
LLInst * matched [] = {m_icodes[0]->ll(),m_icodes[1]->ll()};
LLInst * matched [] {m_icodes[0]->ll(),m_icodes[1]->ll()};
/* Check for regL */
m_regL = m_icodes[0]->ll()->m_dst.regi;
if (not m_icodes[0]->ll()->testFlags(I) && ((m_regL == rAX) || (m_regL ==rBX)))
m_regL = matched[0]->m_dst.regi;
if (not matched[0]->testFlags(I) and ((m_regL == rAX) or (m_regL ==rBX)))
{
/* Check for XOR regH, regH */
if (m_icodes[1]->ll()->match(iXOR) && not m_icodes[1]->ll()->testFlags(I))
if (matched[1]->match(iXOR) and not matched[1]->testFlags(I))
{
m_regH = m_icodes[1]->ll()->m_dst.regi;
if (m_regH == m_icodes[1]->ll()->src().getReg2())
m_regH = matched[1]->m_dst.regi;
if (m_regH == matched[1]->src().getReg2())
{
if ((m_regL == rAX) && (m_regH == rDX))
if ((m_regL == rAX) and (m_regH == rDX))
return true;
if ((m_regL == rBX) && (m_regH == rCX))
if ((m_regL == rBX) and (m_regH == rCX))
return true;
}
}
@ -49,14 +52,11 @@ bool Idiom14::match(iICODE pIcode)
}
int Idiom14::action()
{
int idx;
AstIdent *lhs;
Expr *rhs;
idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(m_regH,m_regL), m_icodes[0]);
lhs = AstIdent::LongIdx (idx);
int idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(m_regH,m_regL), m_icodes[0]);
AstIdent *lhs = AstIdent::LongIdx (idx);
m_icodes[0]->setRegDU( m_regH, eDEF);
rhs = AstIdent::id (*m_icodes[0]->ll(), SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
Expr *rhs = AstIdent::id (*m_icodes[0]->ll(), SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
m_icodes[0]->setAsgn(lhs, rhs);
m_icodes[1]->invalidate();
return 2;
@ -84,10 +84,10 @@ bool Idiom13::match(iICODE pIcode)
/* Check for regL */
regi = m_icodes[0]->ll()->m_dst.regi;
if (not m_icodes[0]->ll()->testFlags(I) && (regi >= rAL) && (regi <= rBH))
if (not m_icodes[0]->ll()->testFlags(I) and (regi >= rAL) and (regi <= rBH))
{
/* Check for MOV regH, 0 */
if (m_icodes[1]->ll()->match(iMOV,I) && (m_icodes[1]->ll()->src().getImm2() == 0))
if (m_icodes[1]->ll()->match(iMOV,I) and (m_icodes[1]->ll()->src().getImm2() == 0))
{
if (m_icodes[1]->ll()->m_dst.regi == (regi + 4)) //WARNING: based on distance between AH-AL,BH-BL etc.
{

View File

@ -1,5 +1,8 @@
#include "dcc.h"
#include "neg_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
using namespace std;
@ -23,7 +26,7 @@ bool Idiom11::match (iICODE picode)
for(int i=0; i<3; ++i)
m_icodes[i]=picode++;
type = m_icodes[0]->ll()->idType(DST);
if(type==CONSTANT || type == OTHER)
if(type==CONSTANT or type == OTHER)
return false;
/* Check NEG reg/mem
* SBB reg/mem, 0*/
@ -32,7 +35,7 @@ bool Idiom11::match (iICODE picode)
switch (type)
{
case GLOB_VAR:
if ((m_icodes[2]->ll()->m_dst.segValue == m_icodes[0]->ll()->m_dst.segValue) &&
if ((m_icodes[2]->ll()->m_dst.segValue == m_icodes[0]->ll()->m_dst.segValue) and
(m_icodes[2]->ll()->m_dst.off == m_icodes[0]->ll()->m_dst.off))
return true;
break;
@ -83,11 +86,11 @@ bool Idiom16::match (iICODE picode)
m_icodes[i]=picode++;
uint8_t regi = m_icodes[0]->ll()->m_dst.regi;
if ((regi >= rAX) && (regi < INDEX_BX_SI))
if ((regi >= rAX) and (regi < INDEX_BX_SI))
{
if (m_icodes[1]->ll()->match(iSBB) && m_icodes[2]->ll()->match(iINC))
if ((m_icodes[1]->ll()->m_dst.regi == (m_icodes[1]->ll()->src().getReg2())) &&
m_icodes[1]->ll()->match((eReg)regi) &&
if (m_icodes[1]->ll()->match(iSBB) and m_icodes[2]->ll()->match(iINC))
if ((m_icodes[1]->ll()->m_dst.regi == (m_icodes[1]->ll()->src().getReg2())) and
m_icodes[1]->ll()->match((eReg)regi) and
m_icodes[2]->ll()->match((eReg)regi))
return true;
}

View File

@ -1,5 +1,8 @@
#include "dcc.h"
#include "shift_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
using namespace std;
@ -18,8 +21,8 @@ bool Idiom8::match(iICODE pIcode)
return false;
m_icodes[0]=pIcode++;
m_icodes[1]=pIcode++;
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
if ( m_icodes[1]->ll()->match(iRCR,I) &&
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
if ( m_icodes[1]->ll()->match(iRCR,I) and
(m_icodes[1]->ll()->src().getImm2() == 1))
return true;
return false;
@ -107,8 +110,8 @@ bool Idiom12::match(iICODE pIcode)
return false;
m_icodes[0]=pIcode++;
m_icodes[1]=pIcode++;
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
if (m_icodes[1]->ll()->match(iRCL,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
if (m_icodes[1]->ll()->match(iRCL,I) and (m_icodes[1]->ll()->src().getImm2() == 1))
return true;
return false;
}
@ -147,8 +150,8 @@ bool Idiom9::match(iICODE pIcode)
return false;
m_icodes[0]=pIcode++;
m_icodes[1]=pIcode++;
if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
if (m_icodes[1]->ll()->match(iRCR,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
if (m_icodes[0]->ll()->testFlags(I) and (m_icodes[0]->ll()->src().getImm2() == 1))
if (m_icodes[1]->ll()->match(iRCR,I) and (m_icodes[1]->ll()->src().getImm2() == 1))
return true;
return false;
}

View File

@ -1,5 +1,8 @@
#include "dcc.h"
#include "xor_idioms.h"
#include "dcc.h"
#include "msvc_fixes.h"
using namespace std;
/*****************************************************************************
@ -28,11 +31,11 @@ bool Idiom21::match (iICODE picode)
dst = &m_icodes[0]->ll()->m_dst;
src = &m_icodes[0]->ll()->src();
if ((dst->regi == src->getReg2()) && (dst->getReg2() > 0) && (dst->getReg2() < INDEX_BX_SI))
if ((dst->regi == src->getReg2()) and (dst->getReg2() > 0) and (dst->getReg2() < INDEX_BX_SI))
{
if ((dst->getReg2() == rDX) && m_icodes[1]->ll()->match(rAX))
if ((dst->getReg2() == rDX) and m_icodes[1]->ll()->match(rAX))
return true;
if ((dst->getReg2() == rCX) && m_icodes[1]->ll()->match(rBX))
if ((dst->getReg2() == rCX) and m_icodes[1]->ll()->match(rBX))
return true;
}
return false;
@ -67,7 +70,7 @@ bool Idiom7::match(iICODE picode)
src = &picode->ll()->src();
if (dst->regi == 0) /* global variable */
{
if ((dst->segValue == src->segValue) && (dst->off == src->off))
if ((dst->segValue == src->segValue) and (dst->off == src->off))
return true;
}
else if (dst->regi < INDEX_BX_SI) /* register */
@ -75,9 +78,9 @@ bool Idiom7::match(iICODE picode)
if (dst->regi == src->regi)
return true;
}
else if ((dst->off) && (dst->seg == rSS) && (dst->regi == INDEX_BP)) /* offset from BP */
else if ((dst->off) and (dst->seg == rSS) and (dst->regi == INDEX_BP)) /* offset from BP */
{
if ((dst->off == src->off) && (dst->seg == src->seg) && (dst->regi == src->regi))
if ((dst->off == src->off) and (dst->seg == src->seg) and (dst->regi == src->regi))
return true;
}
return false;
@ -114,8 +117,8 @@ bool Idiom10::match(iICODE pIcode)
m_icodes[0]=pIcode++;
m_icodes[1]=pIcode++;
/* Check OR reg, reg */
if (not m_icodes[0]->ll()->testFlags(I) &&
m_icodes[0]->ll()->src().isReg() &&
if (not m_icodes[0]->ll()->testFlags(I) and
m_icodes[0]->ll()->src().isReg() and
(m_icodes[0]->ll()->src().getReg2() == m_icodes[0]->ll()->m_dst.getReg2()))
if (m_icodes[1]->ll()->match(iJNE)) //.conditionalJump()
{

View File

@ -4,10 +4,18 @@
* Date: October 1993
* (C) Cristina Cifuentes
*/
#include "locident.h"
#include "dcc.h"
#include "msvc_fixes.h"
#include <cstring>
#include "locident.h"
#include "dcc.h"
#include <QtCore/QDebug>
static const int LOCAL_ID_DELTA = 25;
static const int IDX_ARRAY_DELTA = 5;
bool LONGID_TYPE::srcDstRegMatch(iICODE a, iICODE b) const
{
return (a->ll()->src().getReg2()==m_l) and (b->ll()->m_dst.getReg2()==m_h);
@ -23,7 +31,7 @@ ID::ID(hlType t, frameType f) : type(t),illegal(false),hasMacro(false)
macro[0]=0;
memset(&id,0,sizeof(id));
loc=f;
assert(not ((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN)));
assert(not ((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN)));
}
ID::ID(hlType t,const LONGID_TYPE &s) : type(t),illegal(false),hasMacro(false)
{
@ -31,7 +39,7 @@ ID::ID(hlType t,const LONGID_TYPE &s) : type(t),illegal(false),hasMacro(false)
memset(&id,0,sizeof(id));
loc=REG_FRAME;
m_longId = s;
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
}
ID::ID(hlType t,const LONG_STKID_TYPE &s) : type(t),illegal(false),hasMacro(false)
{
@ -39,7 +47,7 @@ ID::ID(hlType t,const LONG_STKID_TYPE &s) : type(t),illegal(false),hasMacro(fals
memset(&id,0,sizeof(id));
loc=STK_FRAME;
id.longStkId = s;
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
}
ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
@ -48,12 +56,17 @@ ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
memset(&id,0,sizeof(id));
loc=GLB_FRAME;
id.longGlb = s;
assert((t==TYPE_LONG_SIGN)||(t==TYPE_LONG_UNSIGN));
assert((t==TYPE_LONG_SIGN) or (t==TYPE_LONG_UNSIGN));
}
eReg ID::getPairedRegister(eReg first) const {
if (longId().h() == first)
return (longId().l());
else if (longId().l() == first)
return (longId().h());
return rUNDEF;
}
#define LOCAL_ID_DELTA 25
#define IDX_ARRAY_DELTA 5
/* Creates a new identifier node of type t and returns it.
* Arguments: locSym : local long symbol table
@ -62,8 +75,7 @@ ID::ID(hlType t, const LONGGLB_TYPE &s) : type(t),illegal(false)
* ix : index into icode array where this var is used */
void LOCAL_ID::newIdent(hlType t, frameType f)
{
ID newid(t,f);
id_arr.push_back(newid);
id_arr.emplace_back(t,f);
}
@ -71,19 +83,16 @@ void LOCAL_ID::newIdent(hlType t, frameType f)
* TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
int LOCAL_ID::newByteWordReg(hlType t, eReg regi)
{
int idx;
/* Check for entry in the table */
auto found=std::find_if(id_arr.begin(),id_arr.end(),[t,regi](ID &el)->bool {
return ((el.type == t) && (el.id.regi == regi));
});
return ((el.type == t) and (el.id.regi == regi));
});
if(found!=id_arr.end())
return found-id_arr.begin();
/* Not in table, create new identifier */
newIdent (t, REG_FRAME);
idx = id_arr.size() - 1;
id_arr[idx].id.regi = regi;
return (idx);
id_arr.back().id.regi = regi;
return id_arr.size() - 1;
}
@ -96,11 +105,8 @@ int LOCAL_ID::newByteWordReg(hlType t, eReg regi)
void LOCAL_ID::flagByteWordId (int off)
{
auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool {
//if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) &&
if ((en.typeBitsize()<=16) &&
(en.id.bwId.off == off) && (en.id.bwId.regOff == 0))
return true;
return false;
//if (((en.type == TYPE_WORD_SIGN) or (en.type == TYPE_BYTE_SIGN)) and
return ((en.typeBitsize()<=16) and (en.id.bwId.off == off) and (en.id.bwId.regOff == 0));
});
if(found==id_arr.end())
{
@ -114,23 +120,21 @@ void LOCAL_ID::flagByteWordId (int off)
* TYPE_WORD_(UN)SIGN type. Returns the index to this new entry. */
int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
{
int idx;
/* Check for entry in the table */
auto found=std::find_if(id_arr.begin(),id_arr.end(),[off,regOff](ID &el)->bool {
if ((el.id.bwId.off == off) && (el.id.bwId.regOff == regOff))
return true;
return false;
});
if ((el.id.bwId.off == off) and (el.id.bwId.regOff == regOff))
return true;
return false;
});
if(found!=id_arr.end())
return found-id_arr.begin(); //return Index to found element
/* Not in table, create new identifier */
newIdent (t, STK_FRAME);
idx = id_arr.size() - 1;
id_arr[idx].id.bwId.regOff = regOff;
id_arr[idx].id.bwId.off = off;
return (idx);
ID &last_id(id_arr.back());
last_id.id.bwId.regOff = regOff;
last_id.id.bwId.off = off;
return id_arr.size()-1;
}
@ -148,10 +152,10 @@ int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, eReg regi, hlType t)
/* Check for entry in the table */
for (size_t idx = 0; idx < id_arr.size(); idx++)
{
if (/*(locSym->id[idx].type == t) && Not checking type */
(id_arr[idx].id.bwGlb.seg == seg) &&
(id_arr[idx].id.bwGlb.off == off) &&
(id_arr[idx].id.bwGlb.regi == regi))
if (/*(locSym->id[idx].type == t) and Not checking type */
(id_arr[idx].id.bwGlb.seg == seg) and
(id_arr[idx].id.bwGlb.off == off) and
(id_arr[idx].id.bwGlb.regi == regi))
return (idx);
}
@ -178,31 +182,30 @@ int LOCAL_ID::newLongReg(hlType t, const LONGID_TYPE &longT, iICODE ix_)
for (idx = 0; idx < id_arr.size(); idx++)
{
ID &entry(id_arr[idx]);
if(!entry.isLong() || (entry.loc != REG_FRAME))
if(not entry.isLongRegisterPair())
continue;
if (/*(locSym->id[idx].type == t) && Not checking type */
(entry.longId().h() == regH) &&
if (/*(locSym->id[idx].type == t) and Not checking type */
(entry.longId().h() == regH) and
(entry.longId().l() == regL))
{
/* Check for occurrence in the list */
if (entry.idx.inList(ix_))
return (idx);
return idx;
else
{
/* Insert icode index in list */
entry.idx.push_back(ix_);
return (idx);
return idx;
}
}
}
/* Not in the table, create new identifier */
id_arr.push_back(ID(t, LONGID_TYPE(regH,regL)));
id_arr.emplace_back(t, LONGID_TYPE(regH,regL));
id_arr.back().idx.push_back(ix_);
return (id_arr.size() - 1);
}
/* Returns an identifier conditional expression node of type TYPE_LONG or
* TYPE_WORD_SIGN */
/** \returns an identifier conditional expression node of type TYPE_LONG or TYPE_WORD_SIGN */
AstIdent * LOCAL_ID::createId(const ID *retVal, iICODE ix_)
{
return AstIdent::idID(retVal,this,ix_);
@ -218,16 +221,16 @@ int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
/* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++)
{
if (/*(locSym->id[idx].type == t) && Not checking type */
(id_arr[idx].id.longGlb.seg == seg) &&
(id_arr[idx].id.longGlb.offH == offH) &&
(id_arr[idx].id.longGlb.offL == offL))
if (/*(locSym->id[idx].type == t) and Not checking type */
(id_arr[idx].id.longGlb.seg == seg) and
(id_arr[idx].id.longGlb.offH == offH) and
(id_arr[idx].id.longGlb.offL == offL))
return (idx);
}
printf("%d",t);
/* Not in the table, create new identifier */
id_arr.push_back(ID(t, LONGGLB_TYPE(seg,offH,offL)));
return (id_arr.size() - 1);
id_arr.emplace_back(t, LONGGLB_TYPE(seg,offH,offL));
return id_arr.size() - 1;
}
@ -237,27 +240,20 @@ int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
* TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
{
size_t idx;
/* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++)
for (size_t idx = 0; idx < id_arr.size(); idx++)
{
if (/*(locSym->id[idx].type == t) && Not checking type */
(id_arr[idx].id.longGlb.seg == seg) &&
(id_arr[idx].id.longGlb.offH == offH) &&
(id_arr[idx].id.longGlb.offL == offL) &&
(id_arr[idx].id.longGlb.regi == regi))
if (/*(locSym->id[idx].type == t) and Not checking type */
(id_arr[idx].id.longGlb.seg == seg) and
(id_arr[idx].id.longGlb.offH == offH) and
(id_arr[idx].id.longGlb.offL == offL) and
(id_arr[idx].id.longGlb.regi == regi))
return (idx);
}
/* Not in the table, create new identifier */
newIdent (t, GLB_FRAME);
idx = id_arr.size() - 1;
id_arr[idx].id.longGlb.seg = seg;
id_arr[idx].id.longGlb.offH = offH;
id_arr[idx].id.longGlb.offL = offL;
id_arr[idx].id.longGlb.regi = regi;
return (idx);
id_arr.emplace_back(t,LONGGLB_TYPE(seg,offH,offL,regi));
return id_arr.size() - 1;
}
@ -272,9 +268,9 @@ int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
{
if(id_arr[idx].loc!=STK_FRAME)
continue;
if ((id_arr[idx].type == t) &&
(id_arr[idx].longStkId().offH == offH) &&
(id_arr[idx].longStkId().offL == offL))
if ((id_arr[idx].type == t) and
(id_arr[idx].longStkId().offH == offH) and
(id_arr[idx].longStkId().offL == offL))
return (idx);
}
@ -283,7 +279,7 @@ int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
flagByteWordId (offL);
/* Create new identifier */
id_arr.push_back(ID(t,LONG_STKID_TYPE(offH,offL)));
id_arr.emplace_back(t,LONG_STKID_TYPE(offH,offL));
return (id_arr.size() - 1);
}
@ -295,7 +291,7 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, L
{
size_t idx = ~0; //WARNING: clients of this method might propagate this bogus value!
const LLOperand *pmH, *pmL;
LLInst &p_ll(*pIcode->ll());
LLInst &p_ll(*pIcode->ll());
if (f == LOW_FIRST)
{
pmL = p_ll.get(sd);
@ -320,9 +316,9 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, L
}
else if (pmL->off) { /* offset */
if ((pmL->seg == rSS) && (pmL->regi == INDEX_BP)) /* idx on bp */
if ((pmL->seg == rSS) and (pmL->regi == INDEX_BP)) /* idx on bp */
idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
else if ((pmL->seg == rDS) && (pmL->regi == INDEX_BX)) /* bx */
else if ((pmL->seg == rDS) and (pmL->regi == INDEX_BX)) /* bx */
{ /* glb var indexed on bx */
printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);
@ -332,10 +328,10 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, L
printf ("long not supported, idx <> bp\n");
}
else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
printf ("long not supported, idx && no off\n");
else /* (pm->regi >= INDEXBASE and pm->off = 0) => indexed and no off */
printf ("long not supported, idx and no off\n");
return (idx);
return idx;
}
@ -357,24 +353,24 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
pmLdst = &atOffset.m_dst;
pmHsrc = &pIcode->ll()->src();
pmLsrc = &atOffset.src();
// if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
// {
// asgn.lhs = AstIdent::LongIdx (i);
// if ((longId.offH == pmHsrc->off) and (longId.offL == pmLsrc->off))
// {
// asgn.lhs = AstIdent::LongIdx (i);
// if ( not pIcode->ll()->testFlags(NO_SRC) )
// {
// asgn.rhs = AstIdent::Long (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
// }
// return true;
// }
// else if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
// {
// asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
// asgn.rhs = AstIdent::LongIdx (i);
// return true;
// }
// if ( not pIcode->ll()->testFlags(NO_SRC) )
// {
// asgn.rhs = AstIdent::Long (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, atOffset);
// }
// return true;
// }
// else if ((longId.offH == pmHdst->off) and (longId.offL == pmLdst->off))
// {
// asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
// asgn.rhs = AstIdent::LongIdx (i);
// return true;
// }
if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
if ((longId.offH == pmHdst->off) and (longId.offL == pmLdst->off))
{
asgn.lhs = AstIdent::LongIdx (i);
@ -384,7 +380,7 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
}
return true;
}
else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
else if ((longId.offH == pmHsrc->off) and (longId.offL == pmLsrc->off))
{
asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, atOffset);
asgn.rhs = AstIdent::LongIdx (i);
@ -404,7 +400,7 @@ bool checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, int i, Function * pProc
* pProc : ptr to current procedure record
* rhs, lhs : return expressions if successful. */
bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
Function * pProc, Assignment &asgn, LLInst &atOffset)
Function * pProc, Assignment &asgn, LLInst &atOffset)
{
/* pointers to LOW_LEVEL icodes */
const LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc;
@ -414,7 +410,7 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
pmHsrc = &pIcode->ll()->src();
pmLsrc = &atOffset.src();
if ((longId.h() == pmHdst->regi) && (longId.l() == pmLdst->regi))
if ((longId.h() == pmHdst->regi) and (longId.l() == pmLdst->regi))
{
asgn.lhs = AstIdent::LongIdx (i);
if ( not pIcode->ll()->testFlags(NO_SRC) )
@ -423,7 +419,7 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
}
return true;
}
else if ((longId.h() == pmHsrc->regi) && (longId.l() == pmLsrc->regi))
else if ((longId.h() == pmHsrc->regi) and (longId.l() == pmLsrc->regi))
{
asgn.lhs = AstIdent::Long (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode, eDEF, atOffset);
asgn.rhs = AstIdent::LongIdx (i);
@ -432,25 +428,19 @@ bool checkLongRegEq (LONGID_TYPE longId, iICODE pIcode, int i,
return false;
}
/* Given an index into the local identifier table for a long register
* variable, determines whether regi is the high or low part, and returns
* the other part */
eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
eReg LOCAL_ID::getPairedRegisterAt(int idx,eReg regi) const
{
ID *id;
id = &locTbl->id_arr[idx];
if ((id->loc == REG_FRAME) && ((id->type == TYPE_LONG_SIGN) ||
(id->type == TYPE_LONG_UNSIGN)))
eReg res=rUNDEF; // Cristina: please check this!
const ID *id = &id_arr[idx];
if (id->isLongRegisterPair())
{
if (id->longId().h() == regi)
return (id->longId().l());
else if (id->longId().l() == regi)
return (id->longId().h());
res = id->getPairedRegister(regi);
}
return rUNDEF; // Cristina: please check this!
qWarning() << "Cannot find paired register";
return res;
}
@ -458,7 +448,7 @@ eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl)
* the local identifier table. If so, macros for these registers are
* placed in the local identifier table, as these registers belong to a
* long register identifier. */
void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const QString &name)
{
for (ID &rid : id_arr)
{
@ -473,9 +463,9 @@ void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name)
if (rid.id.regi == regL)
{
strcpy (rid.macro, "LO");
}
}
else // if (rid.id.regi == regH)
{
{
strcpy (rid.macro, "HI");
}
}

View File

@ -1,8 +1,13 @@
#include <cassert>
#include "machine_x86.h"
#include "msvc_fixes.h"
#include "icode.h"
#include <QtCore/QTextStream>
#include <cassert>
// Index registers **** temp solution
static const std::string regNames[] = {
static const QString regNames[] = {
"undef",
"ax", "cx", "dx", "bx",
"sp", "bp", "si", "di",
@ -17,17 +22,17 @@ static const std::string regNames[] = {
/* uint8_t and uint16_t registers */
Machine_X86::Machine_X86()
{
static_assert((sizeof(regNames)/sizeof(std::string))==LAST_REG,
static_assert((sizeof(regNames)/sizeof(QString))==LAST_REG,
"Reg count not equal number of strings");
}
const std::string &Machine_X86::regName(eReg r)
const QString &Machine_X86::regName(eReg r)
{
assert(r<(sizeof(regNames)/sizeof(std::string)));
assert(r<(sizeof(regNames)/sizeof(QString)));
return regNames[r];
}
static const std::string szOps[] =
static const QString szOps[] =
{
"CBW", "AAA", "AAD", "AAM", "AAS", "ADC", "ADD", "AND",
"BOUND","CALL", "CALL", "CLC", "CLD", "CLI", "CMC", "CMP",
@ -45,7 +50,7 @@ static const std::string szOps[] =
"XLAT", "XOR", "INTO", "NOP", "REPNE", "REPE", "MOD"
};
/* The following opcodes are for mod != 3 */
static std::string szFlops1[] =
static const QString szFlops1[] =
{
/* 0 1 2 3 4 5 6 7 */
"FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
@ -58,7 +63,7 @@ static std::string szFlops1[] =
"FILD", "???", "FIST", "FISTP", "FBLD", "???", "FBSTP", "FISTP" /* 38 */
};
/* The following opcodes are for mod == 3 */
static std::string szFlops2[] =
static const QString szFlops2[] =
{
/* 0 1 2 3 4 5 6 7 */
"FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
@ -71,17 +76,17 @@ static std::string szFlops2[] =
"FILD", "???", "FIST", "FISTP", "", "???", "FBSTP", "FISTP" /* 38 */
};
const std::string &Machine_X86::opcodeName(unsigned r)
const QString &Machine_X86::opcodeName(unsigned r)
{
assert(r<(sizeof(szOps)/sizeof(std::string)));
assert(r<(sizeof(szOps)/sizeof(QString)));
return szOps[r];
}
const std::string &Machine_X86::floatOpName(unsigned r)
const QString &Machine_X86::floatOpName(unsigned r)
{
if(r>=(sizeof(szFlops1)/sizeof(std::string)))
if(r>=(sizeof(szFlops1)/sizeof(QString)))
{
r-= (sizeof(szFlops1)/sizeof(std::string));
assert(r<(sizeof(szFlops2)/sizeof(std::string)));
r-= (sizeof(szFlops1)/sizeof(QString));
assert(r<(sizeof(szFlops2)/sizeof(QString)));
return szFlops2[r];
}
return szFlops1[r];
@ -89,11 +94,11 @@ const std::string &Machine_X86::floatOpName(unsigned r)
bool Machine_X86::physicalReg(eReg r)
{
return (r>=rAX) && (r<rTMP);
return (r>=rAX) and (r<rTMP);
}
bool Machine_X86::isMemOff(eReg r)
{
return r == 0 || r >= INDEX_BX_SI;
return r == 0 or r >= INDEX_BX_SI;
}
//TODO: Move these to Machine_X86
eReg Machine_X86::subRegH(eReg reg)
@ -106,18 +111,18 @@ eReg Machine_X86::subRegL(eReg reg)
}
bool Machine_X86::isSubRegisterOf(eReg reg,eReg parent)
{
if ((parent < rAX) || (parent > rBX))
if ((parent < rAX) or (parent > rBX))
return false; // only AX -> BX are coverede by subregisters
return ((reg==subRegH(parent)) || (reg == subRegL(parent)));
return ((reg==subRegH(parent)) or (reg == subRegL(parent)));
}
bool Machine_X86::hasSubregisters(eReg reg)
{
return ((reg >= rAX) && (reg <= rBX));
return ((reg >= rAX) and (reg <= rBX));
}
bool Machine_X86::isPartOfComposite(eReg reg)
{
return ((reg >= rAL) && (reg <= rBH));
return ((reg >= rAL) and (reg <= rBH));
}
eReg Machine_X86::compositeParent(eReg reg)
@ -133,7 +138,7 @@ eReg Machine_X86::compositeParent(eReg reg)
}
return rUNDEF;
}
void Machine_X86::writeRegVector (std::ostream &ostr,const LivenessSet &regi)
void Machine_X86::writeRegVector (QTextStream &ostr,const LivenessSet &regi)
{
int j;
for (j = rAX; j < INDEX_BX_SI; j++)

View File

@ -2,17 +2,21 @@
* dcc project procedure list builder
* (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
****************************************************************************/
#include <inttypes.h>
#include <string.h>
#include <stdlib.h> /* For exit() */
#include <sstream>
#include <stdio.h>
#include <algorithm>
#include "dcc.h"
#include "project.h"
#include "CallGraph.h"
#include "msvc_fixes.h"
#include <QMap>
#include <QtCore/QDebug>
#include <inttypes.h>
#include <cstring>
#include <cstdlib> /* For exit() */
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <deque>
using namespace std;
//static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate);
@ -20,70 +24,8 @@ static void setBits(int16_t type, uint32_t start, uint32_t len);
static void process_MOV(LLInst &ll, STATE * pstate);
static SYM * lookupAddr (LLOperand *pm, STATE * pstate, int size, uint16_t duFlag);
void interactDis(Function * initProc, int ic);
static uint32_t SynthLab;
extern uint32_t SynthLab;
/* Parses the program, builds the call graph, and returns the list of
* procedures found */
void DccFrontend::parse(Project &proj)
{
PROG &prog(proj.prog);
STATE state;
/* Set initial state */
state.setState(rES, 0); /* PSP segment */
state.setState(rDS, 0);
state.setState(rCS, prog.initCS);
state.setState(rSS, prog.initSS);
state.setState(rSP, prog.initSP);
state.IP = ((uint32_t)prog.initCS << 4) + prog.initIP;
SynthLab = SYNTHESIZED_MIN;
// default-construct a Function object !
/*auto func = */;
/* Check for special settings of initial state, based on idioms of the
startup code */
state.checkStartup();
Function *start_proc;
/* Make a struct for the initial procedure */
if (prog.offMain != -1)
{
start_proc = proj.createFunction(0,"main");
start_proc->retVal.loc = REG_FRAME;
start_proc->retVal.type = TYPE_WORD_SIGN;
start_proc->retVal.id.regi = rAX;
/* We know where main() is. Start the flow of control from there */
start_proc->procEntry = prog.offMain;
/* In medium and large models, the segment of main may (will?) not be
the same as the initial CS segment (of the startup code) */
state.setState(rCS, prog.segMain);
state.IP = prog.offMain;
}
else
{
start_proc = proj.createFunction(0,"start");
/* Create initial procedure at program start address */
start_proc->procEntry = (uint32_t)state.IP;
}
/* The state info is for the first procedure */
start_proc->state = state;
/* Set up call graph initial node */
proj.callGraph = new CALL_GRAPH;
proj.callGraph->proc = start_proc;
/* This proc needs to be called to set things up for LibCheck(), which
checks a proc to see if it is a know C (etc) library */
SetupLibCheck();
//BUG: proj and g_proj are 'live' at this point !
/* Recursively build entire procedure list */
start_proc->FollowCtrl(proj.callGraph, &state);
/* This proc needs to be called to clean things up from SetupLibCheck() */
CleanupLibCheck();
}
/* Returns the size of the string pointed by sym and delimited by delim.
* Size includes delimiter. */
@ -100,7 +42,7 @@ ICODE * Function::translate_DIV(LLInst *ll, ICODE &_Icode)
ICODE eIcode = ICODE();
eIcode.type = LOW_LEVEL;
eIcode.type = LOW_LEVEL_ICODE;
eIcode.ll()->set(iMOV,0,rTMP);
if (ll->testFlags(B) )
{
@ -124,7 +66,7 @@ ICODE * Function::translate_DIV(LLInst *ll, ICODE &_Icode)
/* iMOD */
eIcode = ICODE();
eIcode.type = LOW_LEVEL;
eIcode.type = LOW_LEVEL_ICODE;
eIcode.ll()->set(iMOD,ll->getFlag() | SYNTHETIC | IM_TMP_DST);
eIcode.ll()->replaceSrc(_Icode.ll()->src());
eIcode.du = _Icode.du;
@ -135,14 +77,14 @@ ICODE *Function::translate_XCHG(LLInst *ll,ICODE &_Icode)
{
/* MOV rTMP, regDst */
ICODE eIcode;
eIcode.type = LOW_LEVEL;
eIcode.type = LOW_LEVEL_ICODE;
eIcode.ll()->set(iMOV,SYNTHETIC,rTMP,ll->m_dst);
eIcode.setRegDU( rTMP, eDEF);
if(eIcode.ll()->src().getReg2())
{
eReg srcreg=eIcode.ll()->src().getReg2();
eIcode.setRegDU( srcreg, eUSE);
if((srcreg>=rAL) && (srcreg<=rBH))
if((srcreg>=rAL) and (srcreg<=rBH))
eIcode.ll()->setFlags( B );
}
eIcode.ll()->label = ll->label;
@ -155,12 +97,12 @@ ICODE *Function::translate_XCHG(LLInst *ll,ICODE &_Icode)
/* MOV regSrc, rTMP */
eIcode = ICODE();
eIcode.type = LOW_LEVEL;
eIcode.type = LOW_LEVEL_ICODE;
eIcode.ll()->set(iMOV,SYNTHETIC);
eIcode.ll()->replaceDst(ll->src());
if(eIcode.ll()->m_dst.regi)
{
if((eIcode.ll()->m_dst.regi>=rAL) && (eIcode.ll()->m_dst.regi<=rBH))
if((eIcode.ll()->m_dst.regi>=rAL) and (eIcode.ll()->m_dst.regi<=rBH))
eIcode.ll()->setFlags( B );
eIcode.setRegDU( eIcode.ll()->m_dst.regi, eDEF);
}
@ -182,28 +124,28 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
eErrorId err;
bool done = false;
SYMTAB &global_symbol_table(Project::get()->symtab);
if (name.find("chkstk") != string::npos)
if (name.contains("chkstk"))
{
// Danger! Dcc will likely fall over in this code.
// So we act as though we have done with this proc
// pProc->flg &= ~TERMINATES; // Not sure about this
done = true;
// And mark it as a library function, so structure() won't choke on it
flg |= PROC_ISLIB;
return;
}
if (option.VeryVerbose)
{
printf("Parsing proc %s at %X\n", name.c_str(), pstate->IP);
qDebug() << "Parsing proc" << name << "at" << QString::number(pstate->IP,16).toUpper();
}
while (! done )
while (not done )
{
err = scan(pstate->IP, _Icode);
if(err)
break;
LLInst *ll = _Icode.ll();
pstate->IP += (uint32_t)ll->numBytes;
setBits(BM_CODE, ll->label, (uint32_t)ll->numBytes);
process_operands(_Icode,pstate);
@ -215,14 +157,14 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
iICODE labLoc = Icode.labelSrch(ll->label);
if (Icode.end()!=labLoc)
{ /* Synthetic jump */
_Icode.type = LOW_LEVEL;
_Icode.type = LOW_LEVEL_ICODE;
ll->set(iJMP,I | SYNTHETIC | NO_OPS);
ll->replaceSrc(LLOperand::CreateImm2(labLoc->ll()->GetLlLabel()));
ll->label = SynthLab++;
}
/* Copy Icode to Proc */
if ((ll->getOpcode() == iDIV) || (ll->getOpcode() == iIDIV))
if ((ll->getOpcode() == iDIV) or (ll->getOpcode() == iIDIV))
pIcode = translate_DIV(ll, _Icode);
else if (_Icode.ll()->getOpcode() == iXCHG)
pIcode = translate_XCHG(ll, _Icode);
@ -239,6 +181,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
case iJCXZ:
{
STATE StCopy;
uint32_t lastIp = pstate->IP - 2;
int ip = Icode.size()-1; /* Index of this jump */
ICODE &prev(*(++Icode.rbegin())); /* Previous icode */
bool fBranch = false;
@ -248,18 +191,18 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
/* This sets up range check for indexed JMPs hopefully
* Handles JA/JAE for fall through and JB/JBE on branch
*/
if (ip > 0 && prev.ll()->getOpcode() == iCMP && (prev.ll()->testFlags(I)))
if (ip > 0 and prev.ll()->getOpcode() == iCMP and (prev.ll()->testFlags(I)))
{
pstate->JCond.immed = (int16_t)prev.ll()->src().getImm2();
if (ll->match(iJA) || ll->match(iJBE) )
if (ll->match(iJA) or ll->match(iJBE) )
pstate->JCond.immed++;
if (ll->getOpcode() == iJAE || ll->getOpcode() == iJA)
if (ll->getOpcode() == iJAE or ll->getOpcode() == iJA)
pstate->JCond.regi = prev.ll()->m_dst.regi;
fBranch = (bool)
(ll->getOpcode() == iJB || ll->getOpcode() == iJBE);
fBranch = (bool) (ll->getOpcode() == iJB or ll->getOpcode() == iJBE);
}
StCopy = *pstate;
//memcpy(&StCopy, pstate, sizeof(STATE));
//printf("From %X condJump to %X\n", lastIp, pstate->IP);
/* Straight line code */
this->FollowCtrl (pcallGraph, &StCopy); // recurrent ?
@ -298,7 +241,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
break;
case iINT:
if (ll->src().getImm2() == 0x21 && pstate->f[rAH])
if (ll->src().getImm2() == 0x21 and pstate->f[rAH])
{
int funcNum = pstate->r[rAH];
int operand;
@ -309,7 +252,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
//Icode.GetIcode(Icode.GetNumIcodes() - 1)->
/* Program termination: int21h, fn 00h, 31h, 4Ch */
done = (bool)(funcNum == 0x00 || funcNum == 0x31 ||
done = (bool)(funcNum == 0x00 or funcNum == 0x31 or
funcNum == 0x4C);
/* String functions: int21h, fn 09h */
@ -324,12 +267,12 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
global_symbol_table.updateSymType (operand, TypeContainer(TYPE_STR, size));
}
}
else if ((ll->src().getImm2() == 0x2F) && (pstate->f[rAH]))
else if ((ll->src().getImm2() == 0x2F) and (pstate->f[rAH]))
{
Icode.back().ll()->m_dst.off = pstate->r[rAH];
}
else /* Program termination: int20h, int27h */
done = (ll->src().getImm2() == 0x20 || ll->src().getImm2() == 0x27);
done = (ll->src().getImm2() == 0x20 or ll->src().getImm2() == 0x27);
if (done)
pIcode->ll()->setFlags(TERMINATES);
break;
@ -346,7 +289,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
case iSHL:
if (pstate->JCond.regi == ll->m_dst.regi)
{
if ((ll->testFlags(I)) && ll->src().getImm2() == 1)
if ((ll->testFlags(I)) and ll->src().getImm2() == 1)
pstate->JCond.immed *= 2;
else
pstate->JCond.regi = 0;
@ -360,7 +303,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
case iLDS: case iLES:
if ((psym = lookupAddr(&ll->src(), pstate, 4, eDuVal::USE))
/* && (Icode.ll()->flg & SEG_IMMED) */ )
/* and (Icode.ll()->flg & SEG_IMMED) */ )
{
offset = LH(&prog.image()[psym->label]);
pstate->setState( (ll->getOpcode() == iLDS)? rDS: rES,
@ -375,7 +318,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
if (err) {
this->flg &= ~TERMINATES;
if (err == INVALID_386OP || err == INVALID_OPCODE)
if (err == INVALID_386OP or err == INVALID_OPCODE)
{
fatalError(err, prog.image()[_Icode.ll()->label], _Icode.ll()->label);
this->flg |= PROC_BADINST;
@ -429,12 +372,185 @@ bool Function::followAllTableEntries(JumpTable &table, uint32_t cs, ICODE& pIcod
}
return true;
}
bool Function::decodeIndirectJMP(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
{
PROG &prog(Project::get()->prog);
// mov cx,NUM_CASES
// mov bx,JUMP_TABLE
// LAB1:
// mov ax, [bx]
// cmp ax,VAL
// jz LAB2
// add bx,2
// loop LAB1
// jmp DEFAULT_CASE
// LAB2
// jmp word ptr [bx+2*NUM_CASES]
static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
if(Icode.size()<8)
return false;
if(&Icode.back()!=&pIcode) // not the last insn in the list skip it
return false;
if(pIcode.ll()->src().regi != INDEX_BX) {
return false;
}
// find address-wise predecessors of the icode
std::deque<ICODE *> matched;
QMap<uint32_t,ICODE *> addrmap;
for(ICODE & ic : Icode) {
addrmap[ic.ll()->GetLlLabel()] = &ic;
}
auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
while(matched.size()<9) {
matched.push_front(*iter);
--iter;
if(iter==addrmap.end())
return false;
}
// pattern starts at the last jmp
ICODE *load_num_cases = matched[0];
ICODE *load_jump_table_addr = matched[1];
ICODE *read_case_entry_insn = matched[2];
ICODE *cmp_case_val_insn = matched[3];
ICODE *exit_loop_insn = matched[4];
ICODE *add_bx_insn = matched[5];
ICODE *loop_insn = matched[6];
ICODE *default_jmp = matched[7];
ICODE *last_jmp = matched[8];
for(int i=0; i<8; ++i) {
if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
return false;
}
}
for(int i=0; i<9; ++i) {
if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
return false;
}
}
// verify that bx+offset == 2* case count
uint32_t num_cases = load_num_cases->ll()->src().getImm2();
if(last_jmp->ll()->src().off != 2*num_cases)
return false;
const LLOperand &op = last_jmp->ll()->src();
if(op.regi != INDEX_BX)
return false;
if(not load_jump_table_addr->ll()->src().isImmediate())
return false;
uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
uint32_t default_label = cs + default_jmp->ll()->src().getImm2();
setBits(BM_DATA, table_addr, num_cases*2 + num_cases*2); // num_cases of short values + num cases short ptrs
pIcode.ll()->setFlags(SWITCH);
for(int i=0; i<num_cases; ++i) {
STATE StCopy = *pstate;
uint32_t jump_target_location = table_addr + num_cases*2 + i*2;
StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
iICODE last_current_insn = (++Icode.rbegin()).base();
FollowCtrl (pcallGraph, &StCopy);
++last_current_insn;
last_current_insn->ll()->caseEntry = i;
last_current_insn->ll()->setFlags(CASE);
pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
}
return true;
}
bool Function::decodeIndirectJMP2(ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
{
PROG &prog(Project::get()->prog);
// mov cx,NUM_CASES
// mov bx,JUMP_TABLE
// LAB1:
// mov ax, [bx]
// cmp ax, LOW_WORD_OF_VAL
// jnz LAB2
// mov ax, [bx + 2 * NUM_CASES]
// cmp ax, HIGH_WORD_OF_VAL
// jz LAB3
// LAB2
// add bx,2
// loop LAB1
// jmp DEFAULT_CASE
// LAB3
// jmp word ptr [bx+2*NUM_CASES]
static const llIcode match_seq[] = {iMOV,iMOV,iMOV,iCMP,iJNE,iMOV,iCMP,iJE,iADD,iLOOP,iJMP,iJMP};
if(Icode.size()<12)
return false;
if(&Icode.back() != &pIcode) // not the last insn in the list skip it
return false;
if(pIcode.ll()->src().regi != INDEX_BX) {
return false;
}
// find address-wise predecessors of the icode
std::deque<ICODE *> matched;
QMap<uint32_t,ICODE *> addrmap;
for(ICODE & ic : Icode) {
addrmap[ic.ll()->GetLlLabel()] = &ic;
}
auto iter = addrmap.find(pIcode.ll()->GetLlLabel());
while(matched.size()<12) {
matched.push_front(*iter);
--iter;
if(iter==addrmap.end())
return false;
}
// pattern starts at the last jmp
ICODE *load_num_cases = matched[0];
ICODE *load_jump_table_addr = matched[1];
ICODE *default_jmp = matched[10];
ICODE *last_jmp = matched[11];
for(int i=0; i<11; ++i) {
if(matched[i+1]->ll()->GetLlLabel()!=matched[i]->ll()->GetLlLabel()+matched[i]->ll()->numBytes) {
qDebug() << "Matching jump pattern impossible - undecoded instructions in pattern area ";
return false;
}
}
for(int i=0; i<12; ++i) {
if(matched[i]->ll()->getOpcode()!=match_seq[i]) {
return false;
}
}
// verify that bx+offset == 2* case count
uint32_t num_cases = load_num_cases->ll()->src().getImm2();
if(last_jmp->ll()->src().off != 4*num_cases)
return false;
const LLOperand &op = last_jmp->ll()->src();
if(op.regi != INDEX_BX)
return false;
if(not load_jump_table_addr->ll()->src().isImmediate())
return false;
uint32_t cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
uint32_t table_addr = cs + load_jump_table_addr->ll()->src().getImm2();
int default_label = cs + default_jmp->ll()->src().getImm2();
setBits(BM_DATA, table_addr, num_cases*4 + num_cases*2); // num_cases of long values + num cases short ptrs
pIcode.ll()->setFlags(SWITCH);
for(int i=0; i<num_cases; ++i) {
STATE StCopy = *pstate;
uint32_t jump_target_location = table_addr + num_cases*4 + i*2;
StCopy.IP = cs + *(uint16_t *)(prog.image()+jump_target_location);
iICODE last_current_insn = (++Icode.rbegin()).base();
FollowCtrl (pcallGraph, &StCopy);
++last_current_insn;
last_current_insn->ll()->caseEntry = i;
last_current_insn->ll()->setFlags(CASE);
pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
}
return true;
}
bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
{
PROG &prog(Project::get()->prog);
static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
ICODE _Icode;
uint32_t lastIp = pstate->IP - 1;
uint32_t cs, offTable, endTable;
uint32_t i, k, seg, target;
@ -442,7 +558,19 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
{
if (pIcode.ll()->getOpcode() == iJMPF)
pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
pstate->IP = pIcode.ll()->src().getImm2();
pstate->IP = pIcode.ll()->src().getImm2();
//printf("From seg:%04X JMP(F) to %X\n", lastIp, pstate->IP);
if (pstate->IP == 0xFFFF0)
{
/* Nasty (wrong) trick use to reset, consider it as terminating */
pIcode.ll()->setFlags(TERMINATES);
pstate->setState( rCS, 0);
pstate->IP = 0;
}
int64_t i = pIcode.ll()->src().getImm2();
if (i < 0)
{
@ -452,16 +580,15 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
/* Return true if jump target is already parsed */
return Icode.alreadyDecoded(i);
}
/* We've got an indirect JMP - look for switch() stmt. idiom of the form
* JMP uint16_t ptr word_offset[rBX | rSI | rDI] */
seg = (pIcode.ll()->src().seg)? pIcode.ll()->src().seg: rDS;
/* Ensure we have a uint16_t offset & valid seg */
if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) &&
pstate->f[seg] &&
(pIcode.ll()->src().regi == INDEX_SI ||
pIcode.ll()->src().regi == INDEX_DI || /* Idx reg. BX, SI, DI */
if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) and
pstate->f[seg] and
(pIcode.ll()->src().regi == INDEX_SI or
pIcode.ll()->src().regi == INDEX_DI or /* Idx reg. BX, SI, DI */
pIcode.ll()->src().regi == INDEX_BX))
{
@ -491,7 +618,7 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
for (i = offTable; i < endTable; i += 2)
{
target = cs + LH(&prog.image()[i]);
if (target < endTable && target >= offTable)
if (target < endTable and target >= offTable)
endTable = target;
else if (target >= (uint32_t)prog.cbImage)
endTable = i;
@ -501,7 +628,7 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
{
target = cs + LH(&prog.image()[i]);
/* Be wary of 00 00 as code - it's probably data */
if (! (prog.image()[target] || prog.image()[target+1]) ||
if (not (prog.image()[target] or prog.image()[target+1]) or
scan(target, _Icode))
endTable = i;
}
@ -537,6 +664,12 @@ bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGra
return true;
}
}
if(decodeIndirectJMP(pIcode,pstate,pcallGraph)) {
return true;
}
if(decodeIndirectJMP2(pIcode,pstate,pcallGraph)) {
return true;
}
/* Can't do anything with this jump */
@ -561,6 +694,7 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
PROG &prog(Project::get()->prog);
ICODE &last_insn(Icode.back());
STATE localState; /* Local copy of the machine state */
uint32_t lastIp = pstate->IP - 2;
uint32_t off;
/* For Indirect Calls, find the function address */
bool indirect = false;
@ -569,13 +703,12 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
{
/* Not immediate, i.e. indirect call */
if (pIcode.ll()->m_dst.regi && (!option.Calls))
if (pIcode.ll()->m_dst.regi and (not option.Calls))
{
/* We have not set the brave option to attempt to follow
the execution path through register indirect calls.
So we just exit this function, and ignore the call.
We probably should not have parsed this deep, anyway.
*/
We probably should not have parsed this deep, anyway. */
return false;
}
@ -643,11 +776,9 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
if (indirect)
x.flg |= PROC_ICALL;
if (x.name.empty()) /* Don't overwrite existing name */
if (x.name.isEmpty()) /* Don't overwrite existing name */
{
ostringstream os;
os<<"proc_"<< ++prog.cProcs;
x.name = os.str();
x.name = QString("proc_%1_%2").arg(x.procEntry ,6,16,QChar('0')).arg(++prog.cProcs);
}
x.depth = x.depth + 1;
x.flg |= TERMINATES;
@ -657,10 +788,13 @@ bool Function::process_CALL(ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *psta
pstate->IP = pIcode.ll()->src().getImm2();
if (pIcode.ll()->getOpcode() == iCALLF)
pstate->setState( rCS, LH(prog.image() + pIcode.ll()->label + 3));
x.state = *pstate;
/* Insert new procedure in call graph */
pcallGraph->insertCallGraph (this, iter);
- pcallGraph->insertCallGraph (this, iter);
//printf("From %X CALL to %X\n", lastIp, pstate->IP);
/* Process new procedure */
x.FollowCtrl (pcallGraph, pstate);
@ -691,17 +825,17 @@ static void process_MOV(LLInst & ll, STATE * pstate)
SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
uint8_t dstReg = ll.m_dst.regi;
uint8_t srcReg = ll.src().regi;
if (dstReg > 0 && dstReg < INDEX_BX_SI)
if (dstReg > 0 and dstReg < INDEX_BX_SI)
{
if (ll.testFlags(I))
pstate->setState( dstReg, (int16_t)ll.src().getImm2());
else if (srcReg == 0) /* direct memory offset */
{
psym = lookupAddr(&ll.src(), pstate, 2, eDuVal::USE);
if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val))
if (psym and ((psym->flg & SEG_IMMED) or psym->duVal.val))
pstate->setState( dstReg, LH(&prog.image()[psym->label]));
}
else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
{
pstate->setState( dstReg, pstate->r[srcReg]);
@ -712,10 +846,10 @@ static void process_MOV(LLInst & ll, STATE * pstate)
}
else if (dstReg == 0) { /* direct memory offset */
int size=2;
if((ll.src().regi>=rAL)&&(ll.src().regi<=rBH))
if((ll.src().regi>=rAL)and(ll.src().regi<=rBH))
size=1;
psym = lookupAddr (&ll.m_dst, pstate, size, eDEF);
if (psym && ! (psym->duVal.val)) /* no initial value yet */
if (psym and not (psym->duVal.val)) /* no initial value yet */
{
if (ll.testFlags(I)) /* immediate */
{
@ -731,7 +865,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
else if (srcReg == 0) /* direct mem offset */
{
psym2 = lookupAddr (&ll.src(), pstate, 2, eDuVal::USE);
if (psym2 && ((psym->flg & SEG_IMMED) || (psym->duVal.val)))
if (psym2 and ((psym->flg & SEG_IMMED) or (psym->duVal.val)))
{
//prog.image()[psym->label] = (uint8_t)prog.image()[psym2->label];
pstate->setMemoryByte(psym->label,(uint8_t)prog.image()[psym2->label]);
@ -744,7 +878,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
psym2->duVal.setFlags(eDuVal::USE);
}
}
else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
else if (srcReg < INDEX_BX_SI and pstate->f[srcReg]) /* reg */
{
//prog.image()[psym->label] = (uint8_t)pstate->r[srcReg];
pstate->setMemoryByte(psym->label,(uint8_t)pstate->r[srcReg]);
@ -926,13 +1060,13 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
}
else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
(uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
{
if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
{
if (pm->off > 0) /* global indexed variable */
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
@ -954,7 +1088,7 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
}
/* Use of register */
else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
pIcode.du.use.addReg(pm->regi);
}
@ -986,15 +1120,15 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
}
else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
else if (pm->regi == INDEX_BP_SI or pm->regi == INDEX_BP_DI)
{
pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
(uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
}
else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
else if ((pm->regi >= INDEX_SI) and (pm->regi <= INDEX_BX))
{
if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
if ((pm->seg == rDS) and (pm->regi == INDEX_BX)) /* bx */
{
if (pm->off > 0) /* global var */
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,TYPE_WORD_SIGN);
@ -1003,7 +1137,7 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
}
}
/* Definition of register */
else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
else if ((d == DST) or ((d == SRC) and (not pIcode.ll()->testFlags(I))))
{
assert(not pIcode.ll()->match(iPUSH));
pIcode.du1.addDef(pm->regi);
@ -1047,7 +1181,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iSAR: case iSHL: case iSHR:
case iRCL: case iRCR: case iROL: case iROR:
case iADD: case iADC: case iSUB: case iSBB:
if (! Imm) {
if (not Imm) {
use(SRC, pIcode, this, pstate, cb);
}
case iINC: case iDEC: case iNEG: case iNOT:
@ -1065,7 +1199,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
break;
case iTEST: case iCMP:
if (! Imm)
if (not Imm)
use(SRC, pIcode, this, pstate, cb);
use(DST, pIcode, this, pstate, cb);
break;
@ -1078,7 +1212,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iMUL: case iIMUL:
use(SRC, pIcode, this, pstate, cb);
if (! Imm)
if (not Imm)
{
use (DST, pIcode, this, pstate, cb);
if (cb == 1)
@ -1115,7 +1249,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iCALLF: /* Ignore def's on CS for now */
cb = 4;
case iCALL: case iPUSH: case iPOP:
if (! Imm) {
if (not Imm) {
if (pIcode.ll()->getOpcode() == iPOP)
def(DST, pIcode, this, pstate, cb);
else
@ -1153,7 +1287,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iJMPF:
cb = 4;
case iJMP:
if (! Imm)
if (not Imm)
use(SRC, pIcode, this, pstate, cb);
break;
@ -1180,7 +1314,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iSCAS: case iSTOS: case iINS:
pIcode.du.def.addReg(rDI);
pIcode.du1.addDef(rDI);
if (pIcode.ll()->getOpcode() == iREP_INS || pIcode.ll()->getOpcode()== iINS)
if (pIcode.ll()->getOpcode() == iREP_INS or pIcode.ll()->getOpcode()== iINS)
{
pIcode.du.use.addReg(rDI).addReg(rES).addReg(rDX);
}
@ -1214,7 +1348,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
case iIN: case iOUT:
def(DST, pIcode, this, pstate, cb);
if (! Imm)
if (not Imm)
{
pIcode.du.use.addReg(rDX);
}

View File

@ -1,101 +0,0 @@
/*
* Perfect hashing function library. Contains functions to generate perfect
* hashing functions
* (C) Mike van Emmerik
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "perfhlib.h"
/* Private data structures */
static uint16_t *T1, *T2; /* Pointers to T1[i], T2[i] */
static short *g; /* g[] */
//static int numEdges; /* An edge counter */
//static bool *visited; /* Array of bools: whether visited */
/* Private prototypes */
//static void initGraph(void);
//static void addToGraph(int e, int v1, int v2);
//static bool isCycle(void);
//static void duplicateKeys(int v1, int v2);
PatternHasher g_pattern_hasher;
void PatternHasher::init(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin, int _NumVert)
{
/* These parameters are stored in statics so as to obviate the need for
passing all these (or defererencing pointers) for every call to hash()
*/
NumEntry = _NumEntry;
EntryLen = _EntryLen;
SetSize = _SetSize;
SetMin = _SetMin;
NumVert = _NumVert;
/* Allocate the variable sized tables etc */
T1base = new uint16_t [EntryLen * SetSize];
T2base = new uint16_t [EntryLen * SetSize];
graphNode = new int [NumEntry*2 + 1];
graphNext = new int [NumEntry*2 + 1];
graphFirst = new int [NumVert + 1];
g = new short [NumVert + 1];
// visited = new bool [NumVert + 1];
return;
}
void PatternHasher::cleanup(void)
{
/* Free the storage for variable sized tables etc */
delete [] T1base;
delete [] T2base;
delete [] graphNode;
delete [] graphNext;
delete [] graphFirst;
delete [] g;
// delete [] visited;
}
int PatternHasher::hash(uint8_t *string)
{
uint16_t u, v;
int j;
u = 0;
for (j=0; j < EntryLen; j++)
{
T1 = T1base + j * SetSize;
u += T1[string[j] - SetMin];
}
u %= NumVert;
v = 0;
for (j=0; j < EntryLen; j++)
{
T2 = T2base + j * SetSize;
v += T2[string[j] - SetMin];
}
v %= NumVert;
return (g[u] + g[v]) % NumEntry;
}
uint16_t * PatternHasher::readT1(void)
{
return T1base;
}
uint16_t *PatternHasher::readT2(void)
{
return T2base;
}
uint16_t * PatternHasher::readG(void)
{
return (uint16_t *)g;
}

View File

@ -5,12 +5,15 @@
* (C) Cristina Cifuentes
*/
#include <cstring>
#include <cassert>
#include "dcc.h"
#include "msvc_fixes.h"
#include "project.h"
#include "CallGraph.h"
#include <QtCore/QDebug>
#include <cstring>
#include <cassert>
extern Project g_proj;
/* Static indentation buffer */
static constexpr int indSize=81; /* size of indentation buffer; max 20 */
@ -27,7 +30,6 @@ const char *indentStr(int indLevel) // Indentation according to the depth of the
* not exist. */
void CALL_GRAPH::insertArc (ilFunction newProc)
{
CALL_GRAPH *pcg;
/* Check if procedure already exists */
@ -35,7 +37,7 @@ void CALL_GRAPH::insertArc (ilFunction newProc)
if(res!=outEdges.end())
return;
/* Include new arc */
pcg = new CALL_GRAPH;
CALL_GRAPH *pcg = new CALL_GRAPH;
pcg->proc = newProc;
outEdges.push_back(pcg);
}
@ -49,13 +51,10 @@ bool CALL_GRAPH::insertCallGraph(ilFunction caller, ilFunction callee)
insertArc (callee);
return true;
}
else
{
for (CALL_GRAPH *edg : outEdges)
if (edg->insertCallGraph (caller, callee))
return true;
return (false);
}
return false;
}
bool CALL_GRAPH::insertCallGraph(Function *caller, ilFunction callee)
@ -68,7 +67,7 @@ bool CALL_GRAPH::insertCallGraph(Function *caller, ilFunction callee)
* the nodes the procedure invokes. */
void CALL_GRAPH::writeNodeCallGraph(int indIdx)
{
printf ("%s%s\n", indentStr(indIdx), proc->name.c_str());
qDebug() << indentStr(indIdx)+proc->name;
for (CALL_GRAPH *cg : outEdges)
cg->writeNodeCallGraph (indIdx + 1);
}
@ -89,7 +88,7 @@ void CALL_GRAPH::write()
/* Updates the argument table by including the register(s) (ie. lhs of
* picode) and the actual expression (ie. rhs of picode).
* Note: register(s) are only included once in the table. */
void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
void LOCAL_ID::newRegArg(ICODE &picode, ICODE &ticode) const
{
AstIdent *lhs;
STKFRAME * call_args_stackframe, *target_stackframe;
@ -102,20 +101,20 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
eReg regH; /* Registers involved in arguments */
/* Flag ticode as having register arguments */
tproc = ticode->hl()->call.proc;
tproc = ticode.hl()->call.proc;
tproc->flg |= REG_ARGS;
/* Get registers and index into target procedure's local list */
call_args_stackframe = ticode->hl()->call.args;
call_args_stackframe = ticode.hl()->call.args;
target_stackframe = &tproc->args;
lhs = dynamic_cast<AstIdent *>(picode->hl()->asgn.lhs());
lhs = dynamic_cast<AstIdent *>(picode.hl()->asgn.lhs());
RegisterNode *lhs_reg = dynamic_cast<RegisterNode *>(lhs);
assert(lhs);
type = lhs->ident.type();
if(type==REGISTER)
assert(lhs_reg);
if(type==LONG_VAR)
assert(!lhs_reg);
assert(nullptr==lhs_reg);
if (lhs_reg)
{
regL = id_arr[lhs_reg->regiIdx].id.regi;
@ -180,7 +179,7 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
newsym.type = TYPE_LONG_SIGN;
assert(regL!=rUNDEF);
tproc->localId.id_arr[tidx].name = newsym.name;
tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name.c_str());
tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name);
}
target_stackframe->push_back(newsym);
target_stackframe->numArgs++;
@ -189,13 +188,13 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
/* Do ps (actual arguments) */
STKSYM newsym;
newsym.setArgName(call_args_stackframe->size());
newsym.actual = picode->hl()->asgn.rhs;
newsym.actual = picode.hl()->asgn.m_rhs;
newsym.regs = lhs;
/* Mask off high and low register(s) in picode */
switch (type) {
case REGISTER:
id = &id_arr[lhs_reg->regiIdx];
picode->du.def.clrReg(id->id.regi);
picode.du.def.clrReg(id->id.regi);
if (id->id.regi < rAL)
newsym.type = TYPE_WORD_SIGN;
else
@ -203,8 +202,8 @@ void LOCAL_ID::newRegArg(iICODE picode, iICODE ticode) const
break;
case LONG_VAR:
id = &id_arr[lhs->ident.idNode.longIdx];
picode->du.def.clrReg(id->longId().h());
picode->du.def.clrReg(id->longId().l());
picode.du.def.clrReg(id->longId().h());
picode.du.def.clrReg(id->longId().l());
newsym.type = TYPE_LONG_SIGN;
break;
default:
@ -231,7 +230,7 @@ bool CallType::newStkArg(Expr *exp, llIcode opcode, Function * pproc)
if (expr)
{
regi = pproc->localId.id_arr[expr->regiIdx].id.regi;
if ((regi >= rES) && (regi <= rDS))
if ((regi >= rES) and (regi <= rDS))
{
return (opcode == iCALLF) ? false : true;
}
@ -333,7 +332,6 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
{
hlType forType;
STKSYM * psym, * nsym;
int off;
/* If formal argument does not exist, do not create new ones, just
* ignore actual argument
*/
@ -341,7 +339,7 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
return;
/* Find stack offset for this argument */
off = m_minOff;
int off = m_minOff;
size_t i=0;
for(STKSYM &s : *this) // walk formal arguments upto numArg_
{
@ -353,7 +351,6 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
/* Find formal argument */
//psym = &at(numArg_);
//i = numArg_;
//auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;});
auto iter=std::find_if(begin()+numArg_,end(),[off](STKSYM &s)->bool {return s.label==off;});
if(iter==end()) // symbol not found
@ -361,17 +358,18 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
psym = &(*iter);
forType = psym->type;
if (forType != actType_)
{
if (forType == actType_)
return;
switch (actType_) {
case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
case TYPE_WORD_UNSIGN: case TYPE_RECORD:
break;
case TYPE_LONG_UNSIGN: case TYPE_LONG_SIGN:
if ((forType == TYPE_WORD_UNSIGN) ||
(forType == TYPE_WORD_SIGN) ||
case TYPE_LONG_UNSIGN:
case TYPE_LONG_SIGN:
if ((forType == TYPE_WORD_UNSIGN) or
(forType == TYPE_WORD_SIGN) or
(forType == TYPE_UNKNOWN))
{
/* Merge low and high */
@ -395,6 +393,5 @@ void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
default:
fprintf(stderr,"STKFRAME::adjustForArgType unhandled actType_ %d \n",actType_);
} /* eos */
}
}

View File

@ -1,37 +1,36 @@
#include <QtCore/QString>
#include <QtCore/QDir>
#include <utility>
#include "dcc.h"
#include "CallGraph.h"
#include "project.h"
#include "Procedure.h"
using namespace std;
//Project g_proj;
char *asm1_name, *asm2_name; /* Assembler output filenames */
SYMTAB symtab; /* Global symbol table */
STATS stats; /* cfg statistics */
//PROG prog; /* programs fields */
OPTION option; /* Command line options */
QString asm1_name, asm2_name; /* Assembler output filenames */
STATS stats; /* cfg statistics */
OPTION option; /* Command line options */
Project *Project::s_instance = nullptr;
Project::Project() : callGraph(nullptr)
{
memset(&prog,0,sizeof(prog));
}
void Project::initialize()
{
delete callGraph;
callGraph = nullptr;
}
void Project::create(const string &a)
void Project::create(const QString &a)
{
initialize();
QFileInfo fi(a);
m_fname=a;
string::size_type ext_loc=a.find_last_of('.');
string::size_type slash_loc=a.find_last_of('/',ext_loc);
if(slash_loc==string::npos)
slash_loc=0;
else
slash_loc++;
if(ext_loc!=string::npos)
m_project_name = a.substr(slash_loc,(ext_loc-slash_loc));
else
m_project_name = a.substr(slash_loc);
m_project_name = fi.completeBaseName();
m_output_path = fi.path();
}
QString Project::output_name(const char *ext) {
return m_output_path+QDir::separator()+m_project_name+"."+ext;
}
bool Project::valid(ilFunction iter)
{
@ -40,7 +39,7 @@ bool Project::valid(ilFunction iter)
ilFunction Project::funcIter(Function *to_find)
{
auto iter=std::find_if(pProcList.begin(),pProcList.end(),
[to_find](const Function &f)->bool {return to_find==&f;});
[to_find](const Function &f)->bool {return to_find==&f;});
assert(iter!=pProcList.end());
return iter;
}
@ -49,17 +48,17 @@ ilFunction Project::findByEntry(uint32_t entry)
{
/* Search procedure list for one with appropriate entry point */
ilFunction iter= std::find_if(pProcList.begin(),pProcList.end(),
[entry](const Function &f) { return f.procEntry==entry; });
return iter;
[entry](const Function &f) { return f.procEntry==entry; });
return iter;
}
ilFunction Project::createFunction(FunctionType *f,const std::string &name)
ilFunction Project::createFunction(FunctionType *f,const QString &name)
{
pProcList.push_back(Function::Create(f,0,name,0));
pProcList.push_back(*Function::Create(f,0,name,0));
return (++pProcList.rbegin()).base();
}
int Project::getSymIdxByAdd(uint32_t adr)
int Project::getSymIdxByAddr(uint32_t adr)
{
size_t i;
for (i = 0; i < symtab.size(); i++)
@ -86,7 +85,7 @@ hlType Project::symbolType(size_t idx)
return symtab[idx].type;
}
const std::string &Project::symbolName(size_t idx)
const QString &Project::symbolName(size_t idx)
{
assert(validSymIdx(idx));
return symtab[idx].name;
@ -98,8 +97,6 @@ Project *Project::get()
s_instance=new Project;
return s_instance;
}
SourceMachine *Project::machine()
{
return nullptr;

View File

@ -4,18 +4,20 @@
* registers) along the graph. Structure the graph in this way.
* (C) Cristina Cifuentes
**************************************************************************/
#include "dcc.h"
#include "msvc_fixes.h"
#include <string.h>
#include <memory.h>
#include <cassert>
#include <algorithm>
#include "dcc.h"
/* Returns whether the given icode opcode is within the range of valid
* high-level conditional jump icodes (iJB..iJG) */
static bool isJCond (llIcode opcode)
{
if ((opcode >= iJB) && (opcode <= iJG))
if ((opcode >= iJB) and (opcode <= iJG))
return true;
return false;
}
@ -32,10 +34,10 @@ static bool isLong23 (BB * pbb, iICODE &off, int *arc)
e = pbb->edges[ELSE].BBptr;
/* Check along the THEN path */
if ((t->size() == 1) && (t->nodeType == TWO_BRANCH) && (t->inEdges.size() == 1))
if ((t->size() == 1) and (t->nodeType == TWO_BRANCH) and (t->inEdges.size() == 1))
{
obb2 = t->edges[THEN].BBptr;
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
if ((obb2->size() == 2) and (obb2->nodeType == TWO_BRANCH) and (obb2->front().ll()->getOpcode() == iCMP))
{
off = obb2->begin();//std::distance(iter,obb2->begin2());
*arc = THEN;
@ -44,10 +46,10 @@ static bool isLong23 (BB * pbb, iICODE &off, int *arc)
}
/* Check along the ELSE path */
else if ((e->size() == 1) && (e->nodeType == TWO_BRANCH) && (e->inEdges.size() == 1))
else if ((e->size() == 1) and (e->nodeType == TWO_BRANCH) and (e->inEdges.size() == 1))
{
obb2 = e->edges[THEN].BBptr;
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
if ((obb2->size() == 2) and (obb2->nodeType == TWO_BRANCH) and (obb2->front().ll()->getOpcode() == iCMP))
{
off = obb2->begin();//std::distance(iter,obb2->begin2());//obb2->front().loc_ip - i;
*arc = ELSE;
@ -66,9 +68,9 @@ static bool isLong22 (iICODE pIcode, iICODE pEnd, iICODE &off)
return false;
// preincrement because pIcode is not checked here
iICODE icodes[] = { ++pIcode,++pIcode,++pIcode };
if ( icodes[1]->ll()->match(iCMP) &&
(isJCond ((llIcode)icodes[0]->ll()->getOpcode())) &&
(isJCond ((llIcode)icodes[2]->ll()->getOpcode())))
if ( icodes[1]->ll()->match(iCMP) and
(isJCond (icodes[0]->ll()->getOpcode())) and
(isJCond (icodes[2]->ll()->getOpcode())))
{
off = initial_icode;
advance(off,2);
@ -101,7 +103,7 @@ static int longJCond23 (Assignment &asgn, iICODE pIcode, int arc, iICODE atOffse
/* Modify in edges of target basic block */
auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
[obb1,obb2](BB *b) -> bool {
return (b==obb1) || (b==obb2); });
return (b==obb1) or (b==obb2); });
tbb->inEdges.erase(newlast,tbb->inEdges.end());
tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
@ -129,7 +131,7 @@ static int longJCond23 (Assignment &asgn, iICODE pIcode, int arc, iICODE atOffse
/* Modify in edges of the ELSE basic block */
tbb = obb2->edges[ELSE].BBptr;
auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
[obb1,obb2](BB *b) -> bool { return (b==obb1) || (b==obb2); });
[obb1,obb2](BB *b) -> bool { return (b==obb1) or (b==obb2); });
tbb->inEdges.erase(newlast,tbb->inEdges.end());
tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
@ -210,7 +212,7 @@ static int longJCond22 (Assignment &asgn, iICODE pIcode,iICODE pEnd)
iter=std::find(tbb->inEdges.begin(),tbb->inEdges.end(),obb1);
assert(iter!=tbb->inEdges.end());
tbb->inEdges.erase(iter);
if (icodes[3]->ll()->getOpcode() == iJE) /* replace */
if (icodes[3]->ll()->getOpcode() == iJE) /* replace */
tbb->inEdges.push_back(pbb);
/* Update statistics */
@ -246,7 +248,7 @@ void Function::propLongStk (int i, const ID &pLocId)
next1 = ++iICODE(pIcode);
if(next1==pEnd)
break;
if ((pIcode->type == HIGH_LEVEL) || ( not pIcode->valid() ))
if ((pIcode->type == HIGH_LEVEL_ICODE) or ( not pIcode->valid() ))
continue;
if (pIcode->ll()->getOpcode() == next1->ll()->getOpcode())
{
@ -264,9 +266,9 @@ void Function::propLongStk (int i, const ID &pLocId)
condOp oper = DUMMY;
switch (pIcode->ll()->getOpcode())
{
case iAND: oper=AND; break;
case iOR: oper=OR; break;
case iXOR: oper=XOR; break;
case iAND: oper=AND; break;
case iOR: oper=OR; break;
case iXOR: oper=XOR; break;
}
if(DUMMY!=oper)
{
@ -288,7 +290,7 @@ void Function::propLongStk (int i, const ID &pLocId)
}
//TODO: Simplify this!
/* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), l23, &arc)))
else if ((pIcode->ll()->getOpcode() == iCMP) and (isLong23 (pIcode->getParent(), l23, &arc)))
{
if ( checkLongEq (pLocId.longStkId(), pIcode, i, this, asgn, *l23->ll()) )
{
@ -298,7 +300,7 @@ void Function::propLongStk (int i, const ID &pLocId)
/* Check for long conditional equality or inequality. This requires
* 2 CMPs and 2 branches */
else if ((pIcode->ll()->getOpcode() == iCMP) && isLong22 (pIcode, pEnd, l23))
else if ((pIcode->ll()->getOpcode() == iCMP) and isLong22 (pIcode, pEnd, l23))
{
if ( checkLongEq (pLocId.longStkId(), pIcode, i, this,asgn, *l23->ll()) )
{
@ -321,7 +323,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
ICODE &icode(*pIcode);
if ((icode.type == HIGH_LEVEL) || ( not icode.valid() ))
if ((icode.type == HIGH_LEVEL_ICODE) or ( not icode.valid() ))
continue;
if (icode.ll()->getOpcode() != next1->ll()->getOpcode())
continue;
@ -331,7 +333,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
case iMOV:
pmH = &icode.ll()->m_dst;
pmL = &next1->ll()->m_dst;
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
{
localId.id_arr[loc_ident_idx].idx.push_back(pIcode);//idx-1//insert
icode.setRegDU( pmL->regi, eDEF);
@ -346,7 +348,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
case iPOP:
pmH = &next1->ll()->m_dst;
pmL = &icode.ll()->m_dst;
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
{
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
icode.setRegDU( pmH->regi, eDEF);
@ -362,7 +364,7 @@ int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE be
case iAND: case iOR: case iXOR:
pmL = &icode.ll()->m_dst;
pmH = &next1->ll()->m_dst;
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
{
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
asgn.rhs = AstIdent::Long (&this->localId, SRC, pIcode, LOW_FIRST, pIcode, eUSE, *next1->ll());
@ -399,7 +401,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
LLOperand * pmH,* pmL; /* Pointers to dst LOW_LEVEL icodes */
int arc;
if ((pIcode->type == HIGH_LEVEL) || ( not pIcode->valid() ))
if ((pIcode->type == HIGH_LEVEL_ICODE) or ( not pIcode->valid() ))
continue;
if (pIcode->ll()->getOpcode() == next1->ll()->getOpcode())
@ -412,7 +414,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
const LLOperand &src_op2(next1->ll()->src());
eReg srcReg1=src_op1.getReg2();
eReg nextReg2=src_op2.getReg2();
if ((ref_long.h() == srcReg1) && (ref_long.l() == nextReg2))
if ((ref_long.h() == srcReg1) and (ref_long.l() == nextReg2))
{
pIcode->setRegDU( nextReg2, eUSE);
@ -431,7 +433,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
const LONGID_TYPE &ref_long(pLocId.longId());
const LLOperand &src_op1(pIcode->ll()->src());
const LLOperand &src_op2(next1->ll()->src());
if ((ref_long.h() == src_op1.getReg2()) && (ref_long.l() == src_op2.getReg2()))
if ((ref_long.h() == src_op1.getReg2()) and (ref_long.l() == src_op2.getReg2()))
{
asgn.rhs = AstIdent::LongIdx (loc_ident_idx);
pIcode->setRegDU( next1->ll()->src().getReg2(), eUSE);
@ -447,7 +449,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
case iAND: case iOR: case iXOR:
pmL = &pIcode->ll()->m_dst;
pmH = &next1->ll()->m_dst;
if ((pLocId.longId().h() == pmH->regi) && (pLocId.longId().l() == pmL->regi))
if ((pLocId.longId().h() == pmH->regi) and (pLocId.longId().l() == pmL->regi))
{
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
pIcode->setRegDU( pmH->regi, USE_DEF);
@ -474,7 +476,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
} /* eos */
/* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), long_loc, &arc)))
else if ((pIcode->ll()->getOpcode() == iCMP) and (isLong23 (pIcode->getParent(), long_loc, &arc)))
{
if (checkLongRegEq (pLocId.longId(), pIcode, loc_ident_idx, this, asgn, *long_loc->ll()))
{
@ -485,7 +487,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
/* Check for long conditional equality or inequality. This requires
* 2 CMPs and 2 branches */
else if (pIcode->ll()->match(iCMP) && (isLong22 (pIcode, pEnd, long_loc)))
else if (pIcode->ll()->match(iCMP) and (isLong22 (pIcode, pEnd, long_loc)))
{
if (checkLongRegEq (pLocId.longId(), pIcode, loc_ident_idx, this, asgn, *long_loc->ll()) )
{
@ -498,12 +500,12 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
* JX lab
* => HLI_JCOND (regH:regL X 0) lab
* This is better code than HLI_JCOND (HI(regH:regL) | LO(regH:regL)) */
else if (pIcode->ll()->match(iOR) && (next1 != pEnd) && (isJCond ((llIcode)next1->ll()->getOpcode())))
else if (pIcode->ll()->match(iOR) and (next1 != pEnd) and (isJCond (next1->ll()->getOpcode())))
{
if (pLocId.longId().srcDstRegMatch(pIcode,pIcode))
{
asgn.lhs = AstIdent::LongIdx (loc_ident_idx);
asgn.rhs = new Constant(0, 4); /* long 0 */
asgn.rhs = new Constant(0, 4); /* long 0 */
asgn.lhs = new BinaryOperator(condOpJCond[next1->ll()->getOpcode() - iJB],asgn.lhs, asgn.rhs);
next1->setJCond(asgn.lhs);
next1->copyDU(*pIcode, eUSE, eUSE);
@ -546,7 +548,7 @@ void Function::propLongReg (int loc_ident_idx, const ID &pLocId)
* Transforms some LOW_LEVEL icodes into HIGH_LEVEL */
void Function::propLongGlb (int /*i*/, const ID &/*pLocId*/)
{
printf("WARN: Function::propLongGlb not implemented");
printf("WARN: Function::propLongGlb not implemented\n");
}

View File

@ -3,12 +3,14 @@
* constructs an equivalent reducible graph if one is not found.
* (C) Cristina Cifuentes
********************************************************************/
#include "dcc.h"
#include "msvc_fixes.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <stdint.h>
#include "dcc.h"
static int numInt; /* Number of intervals */
@ -26,7 +28,7 @@ bool trivialGraph(BB *G)
* from the list. Q is not an empty queue. */
static BB *firstOfQueue (queue &Q)
{
assert(!Q.empty());
assert(not Q.empty());
BB *res=Q.front();
Q.pop_front();
return res;
@ -79,7 +81,7 @@ void interval::appendNodeInt(queue &pqH, BB *node)
/* Check header list for occurrence of node, if found, remove it
* and decrement number of out-edges from this interval. */
if (node->beenOnH && !pqH.empty())
if (node->beenOnH and not pqH.empty())
{
auto found_iter=std::find(pqH.begin(),pqH.end(),node);
if(found_iter!=pqH.end())
@ -111,7 +113,7 @@ void derSeq_Entry::findIntervals (Function *c)
Gi->reachingInt = BB::Create(nullptr,"",c); /* ^ empty BB */
/* Process header nodes list H */
while (!H.empty())
while (not H.empty())
{
header = firstOfQueue (H);
pI = new interval;
@ -137,7 +139,7 @@ void derSeq_Entry::findIntervals (Function *c)
succ->reachingInt = header;
if (succ->inEdgeCount == 0)
pI->appendNodeInt (H, succ);
else if (! succ->beenOnH) /* out edge */
else if (not succ->beenOnH) /* out edge */
{
appendQueue (H, succ);
succ->beenOnH = true;
@ -147,7 +149,7 @@ void derSeq_Entry::findIntervals (Function *c)
else /* node has been visited before */
if (succ->inEdgeCount == 0)
{
if (succ->reachingInt == header || succ->inInterval == pI) /* same interval */
if (succ->reachingInt == header or succ->inInterval == pI) /* same interval */
{
if (succ != header)
pI->appendNodeInt (H, succ);
@ -155,13 +157,13 @@ void derSeq_Entry::findIntervals (Function *c)
else /* out edge */
pI->numOutEdges++;
}
else if (succ != header && succ->beenOnH)
else if (succ != header and succ->beenOnH)
pI->numOutEdges++;
}
}
/* Link interval I to list of intervals */
if (! first)
if (not first)
{
m_intervals.push_back(pI);
J->next = pI;
@ -230,7 +232,7 @@ void freeDerivedSeq(derSeq &derivedG)
derSeq_Entry::~derSeq_Entry()
{
freeInterval (&Ii);
// if(Gi && Gi->nodeType == INTERVAL_NODE)
// if(Gi and Gi->nodeType == INTERVAL_NODE)
// freeCFG (Gi);
}
@ -259,7 +261,7 @@ bool Function::nextOrderGraph (derSeq &derivedGi)
const queue &listIi(Ii->nodes);
/* Check for more than 1 interval */
if (sameGraph && (listIi.size()>1))
if (sameGraph and (listIi.size()>1))
sameGraph = false;
/* Find out edges */
@ -309,20 +311,20 @@ bool Function::findDerivedSeq (derSeq &derivedGi)
derSeq::iterator iter=derivedGi.begin();
assert(iter!=derivedGi.end());
BB *Gi = iter->Gi; /* Current derived sequence graph */
while (! trivialGraph (Gi))
while (not trivialGraph (Gi))
{
/* Find the intervals of Gi and place them in derivedGi->Ii */
iter->findIntervals(this);
/* Create Gi+1 and check if it is equivalent to Gi */
if (! nextOrderGraph (derivedGi))
if (not nextOrderGraph (derivedGi))
break;
++iter;
Gi = iter->Gi;
stats.nOrder++;
}
if (! trivialGraph (Gi))
if (not trivialGraph (Gi))
{
++iter;
derivedGi.erase(iter,derivedGi.end()); /* remove Gi+1 */
@ -366,7 +368,7 @@ derSeq * Function::checkReducibility()
der_seq->back().Gi = *m_actual_cfg.begin(); /*m_cfg.front()*/;
reducible = findDerivedSeq(*der_seq);
if (! reducible)
if (not reducible)
{
flg |= GRAPH_IRRED;
m_actual_cfg.nodeSplitting();

View File

@ -1,22 +1,24 @@
/*****************************************************************************
* dcc project scanner module
* dcc project scanner module
* Implements a simple state driven scanner to convert 8086 machine code into
* I-code
* (C) Cristina Cifuentes, Jeff Ledermann
****************************************************************************/
#include "scanner.h"
#include "msvc_fixes.h"
#include "dcc.h"
#include "project.h"
#include <cstring>
#include <map>
#include <string>
#include "dcc.h"
#include "scanner.h"
#include "project.h"
/* Parser flags */
#define TO_REG 0x000100 /* rm is source */
#define S_EXT 0x000200 /* sign extend */
#define OP386 0x000400 /* 386 op-code */
#define NSP 0x000800 /* NOT_HLL if SP is src or dst */
// defined in Enums.h #define ICODEMASK 0xFF00FF /* Masks off parser flags */
static void rm(int i);
static void modrm(int i);
@ -48,276 +50,276 @@ static void none1(int i);
static void none2(int i);
static void checkInt(int i);
#define iZERO (llIcode)0 // For neatness
#define IC llIcode
#define IC llIcode
static struct {
struct StateTabelEntry {
void (*state1)(int);
void (*state2)(int);
uint32_t flg;
llIcode opcode;
} stateTable[] = {
{ modrm, none2, B , iADD }, /* 00 */
{ modrm, none2, 0 , iADD }, /* 01 */
{ modrm, none2, TO_REG | B , iADD }, /* 02 */
{ modrm, none2, TO_REG , iADD }, /* 03 */
{ data1, axImp, B , iADD }, /* 04 */
{ data2, axImp, 0 , iADD }, /* 05 */
{ segop, none2, NO_SRC , iPUSH }, /* 06 */
{ segop, none2, NO_SRC , iPOP }, /* 07 */
{ modrm, none2, B , iOR }, /* 08 */
{ modrm, none2, NSP , iOR }, /* 09 */
{ modrm, none2, TO_REG | B , iOR }, /* 0A */
{ modrm, none2, TO_REG | NSP , iOR }, /* 0B */
{ data1, axImp, B , iOR }, /* 0C */
{ data2, axImp, 0 , iOR }, /* 0D */
{ segop, none2, NO_SRC , iPUSH }, /* 0E */
{ none1, none2, OP386 , iZERO }, /* 0F */
{ modrm, none2, B , iADC }, /* 10 */
{ modrm, none2, NSP , iADC }, /* 11 */
{ modrm, none2, TO_REG | B , iADC }, /* 12 */
{ modrm, none2, TO_REG | NSP , iADC }, /* 13 */
{ data1, axImp, B , iADC }, /* 14 */
{ data2, axImp, 0 , iADC }, /* 15 */
{ segop, none2, NOT_HLL | NO_SRC , iPUSH }, /* 16 */
{ segop, none2, NOT_HLL | NO_SRC , iPOP }, /* 17 */
{ modrm, none2, B , iSBB }, /* 18 */
{ modrm, none2, NSP , iSBB }, /* 19 */
{ modrm, none2, TO_REG | B , iSBB }, /* 1A */
{ modrm, none2, TO_REG | NSP , iSBB }, /* 1B */
{ data1, axImp, B , iSBB }, /* 1C */
{ data2, axImp, 0 , iSBB }, /* 1D */
{ segop, none2, NO_SRC , iPUSH }, /* 1E */
{ segop, none2, NO_SRC , iPOP }, /* 1F */
{ modrm, none2, B , iAND }, /* 20 */
{ modrm, none2, NSP , iAND }, /* 21 */
{ modrm, none2, TO_REG | B , iAND }, /* 22 */
{ modrm, none2, TO_REG | NSP , iAND }, /* 23 */
{ data1, axImp, B , iAND }, /* 24 */
{ data2, axImp, 0 , iAND }, /* 25 */
{ prefix, none2, 0 , (IC)rES}, /* 26 */
{ none1, axImp, NOT_HLL | B|NO_SRC , iDAA }, /* 27 */
{ modrm, none2, B , iSUB }, /* 28 */
{ modrm, none2, 0 , iSUB }, /* 29 */
{ modrm, none2, TO_REG | B , iSUB }, /* 2A */
{ modrm, none2, TO_REG , iSUB }, /* 2B */
{ data1, axImp, B , iSUB }, /* 2C */
{ data2, axImp, 0 , iSUB }, /* 2D */
{ prefix, none2, 0 , (IC)rCS}, /* 2E */
{ none1, axImp, NOT_HLL | B|NO_SRC , iDAS }, /* 2F */
{ modrm, none2, B , iXOR }, /* 30 */
{ modrm, none2, NSP , iXOR }, /* 31 */
{ modrm, none2, TO_REG | B , iXOR }, /* 32 */
{ modrm, none2, TO_REG | NSP , iXOR }, /* 33 */
{ data1, axImp, B , iXOR }, /* 34 */
{ data2, axImp, 0 , iXOR }, /* 35 */
{ prefix, none2, 0 , (IC)rSS}, /* 36 */
{ none1, axImp, NOT_HLL | NO_SRC , iAAA }, /* 37 */
{ modrm, none2, B , iCMP }, /* 38 */
{ modrm, none2, NSP , iCMP }, /* 39 */
{ modrm, none2, TO_REG | B , iCMP }, /* 3A */
{ modrm, none2, TO_REG | NSP , iCMP }, /* 3B */
{ data1, axImp, B , iCMP }, /* 3C */
{ data2, axImp, 0 , iCMP }, /* 3D */
{ prefix, none2, 0 , (IC)rDS}, /* 3E */
{ none1, axImp, NOT_HLL | NO_SRC , iAAS }, /* 3F */
{ regop, none2, 0 , iINC }, /* 40 */
{ regop, none2, 0 , iINC }, /* 41 */
{ regop, none2, 0 , iINC }, /* 42 */
{ regop, none2, 0 , iINC }, /* 43 */
{ regop, none2, NOT_HLL , iINC }, /* 44 */
{ regop, none2, 0 , iINC }, /* 45 */
{ regop, none2, 0 , iINC }, /* 46 */
{ regop, none2, 0 , iINC }, /* 47 */
{ regop, none2, 0 , iDEC }, /* 48 */
{ regop, none2, 0 , iDEC }, /* 49 */
{ regop, none2, 0 , iDEC }, /* 4A */
{ regop, none2, 0 , iDEC }, /* 4B */
{ regop, none2, NOT_HLL , iDEC }, /* 4C */
{ regop, none2, 0 , iDEC }, /* 4D */
{ regop, none2, 0 , iDEC }, /* 4E */
{ regop, none2, 0 , iDEC }, /* 4F */
{ regop, none2, NO_SRC , iPUSH }, /* 50 */
{ regop, none2, NO_SRC , iPUSH }, /* 51 */
{ regop, none2, NO_SRC , iPUSH }, /* 52 */
{ regop, none2, NO_SRC , iPUSH }, /* 53 */
{ regop, none2, NOT_HLL | NO_SRC , iPUSH }, /* 54 */
{ regop, none2, NO_SRC , iPUSH }, /* 55 */
{ regop, none2, NO_SRC , iPUSH }, /* 56 */
{ regop, none2, NO_SRC , iPUSH }, /* 57 */
{ regop, none2, NO_SRC , iPOP }, /* 58 */
{ regop, none2, NO_SRC , iPOP }, /* 59 */
{ regop, none2, NO_SRC , iPOP }, /* 5A */
{ regop, none2, NO_SRC , iPOP }, /* 5B */
{ regop, none2, NOT_HLL | NO_SRC , iPOP }, /* 5C */
{ regop, none2, NO_SRC , iPOP }, /* 5D */
{ regop, none2, NO_SRC , iPOP }, /* 5E */
{ regop, none2, NO_SRC , iPOP }, /* 5F */
{ none1, none2, NOT_HLL | NO_OPS , iPUSHA}, /* 60 */
{ none1, none2, NOT_HLL | NO_OPS , iPOPA }, /* 61 */
{ memOnly, modrm, TO_REG | NSP , iBOUND}, /* 62 */
{ none1, none2, OP386 , iZERO }, /* 63 */
{ none1, none2, OP386 , iZERO }, /* 64 */
{ none1, none2, OP386 , iZERO }, /* 65 */
{ none1, none2, OP386 , iZERO }, /* 66 */
{ none1, none2, OP386 , iZERO }, /* 67 */
{ data2, none2, NO_SRC , iPUSH }, /* 68 */
{ modrm, data2, TO_REG | NSP , iIMUL }, /* 69 */
{ data1, none2, S_EXT | NO_SRC , iPUSH }, /* 6A */
{ modrm, data1, TO_REG | NSP | S_EXT , iIMUL }, /* 6B */
{ strop, memImp, NOT_HLL | B|IM_OPS , iINS }, /* 6C */
{ strop, memImp, NOT_HLL | IM_OPS , iINS }, /* 6D */
{ strop, memImp, NOT_HLL | B|IM_OPS , iOUTS }, /* 6E */
{ strop, memImp, NOT_HLL | IM_OPS , iOUTS }, /* 6F */
{ dispS, none2, NOT_HLL , iJO }, /* 70 */
{ dispS, none2, NOT_HLL , iJNO }, /* 71 */
{ dispS, none2, 0 , iJB }, /* 72 */
{ dispS, none2, 0 , iJAE }, /* 73 */
{ dispS, none2, 0 , iJE }, /* 74 */
{ dispS, none2, 0 , iJNE }, /* 75 */
{ dispS, none2, 0 , iJBE }, /* 76 */
{ dispS, none2, 0 , iJA }, /* 77 */
{ dispS, none2, 0 , iJS }, /* 78 */
{ dispS, none2, 0 , iJNS }, /* 79 */
{ dispS, none2, NOT_HLL , iJP }, /* 7A */
{ dispS, none2, NOT_HLL , iJNP }, /* 7B */
{ dispS, none2, 0 , iJL }, /* 7C */
{ dispS, none2, 0 , iJGE }, /* 7D */
{ dispS, none2, 0 , iJLE }, /* 7E */
{ dispS, none2, 0 , iJG }, /* 7F */
{ immed, data1, B , iZERO }, /* 80 */
{ immed, data2, NSP , iZERO }, /* 81 */
{ immed, data1, B , iZERO }, /* 82 */ /* ?? */
{ immed, data1, NSP | S_EXT , iZERO }, /* 83 */
{ modrm, none2, TO_REG | B , iTEST }, /* 84 */
{ modrm, none2, TO_REG | NSP , iTEST }, /* 85 */
{ modrm, none2, TO_REG | B , iXCHG }, /* 86 */
{ modrm, none2, TO_REG | NSP , iXCHG }, /* 87 */
{ modrm, none2, B , iMOV }, /* 88 */
{ modrm, none2, 0 , iMOV }, /* 89 */
{ modrm, none2, TO_REG | B , iMOV }, /* 8A */
{ modrm, none2, TO_REG , iMOV }, /* 8B */
{ segrm, none2, NSP , iMOV }, /* 8C */
{ memOnly, modrm, TO_REG | NSP , iLEA }, /* 8D */
{ segrm, none2, TO_REG | NSP , iMOV }, /* 8E */
{ memReg0, none2, NO_SRC , iPOP }, /* 8F */
{ none1, none2, NO_OPS , iNOP }, /* 90 */
{ regop, axImp, 0 , iXCHG }, /* 91 */
{ regop, axImp, 0 , iXCHG }, /* 92 */
{ regop, axImp, 0 , iXCHG }, /* 93 */
{ regop, axImp, NOT_HLL , iXCHG }, /* 94 */
{ regop, axImp, 0 , iXCHG }, /* 95 */
{ regop, axImp, 0 , iXCHG }, /* 96 */
{ regop, axImp, 0 , iXCHG }, /* 97 */
{ alImp, axImp, SRC_B | S_EXT , iSIGNEX}, /* 98 */
{axSrcIm, axImp, IM_DST | S_EXT , iSIGNEX}, /* 99 */
{ dispF, none2, 0 , iCALLF }, /* 9A */
{ none1, none2, FLOAT_OP| NO_OPS , iWAIT }, /* 9B */
{ none1, none2, NOT_HLL | NO_OPS , iPUSHF}, /* 9C */
{ none1, none2, NOT_HLL | NO_OPS , iPOPF }, /* 9D */
{ none1, none2, NOT_HLL | NO_OPS , iSAHF }, /* 9E */
{ none1, none2, NOT_HLL | NO_OPS , iLAHF }, /* 9F */
{ dispM, axImp, B , iMOV }, /* A0 */
{ dispM, axImp, 0 , iMOV }, /* A1 */
{ dispM, axImp, TO_REG | B , iMOV }, /* A2 */
{ dispM, axImp, TO_REG , iMOV }, /* A3 */
{ strop, memImp, B | IM_OPS , iMOVS }, /* A4 */
{ strop, memImp, IM_OPS , iMOVS }, /* A5 */
{ strop, memImp, B | IM_OPS , iCMPS }, /* A6 */
{ strop, memImp, IM_OPS , iCMPS }, /* A7 */
{ data1, axImp, B , iTEST }, /* A8 */
{ data2, axImp, 0 , iTEST }, /* A9 */
{ strop, memImp, B | IM_OPS , iSTOS }, /* AA */
{ strop, memImp, IM_OPS , iSTOS }, /* AB */
{ strop, memImp, B | IM_OPS , iLODS }, /* AC */
{ strop, memImp, IM_OPS , iLODS }, /* AD */
{ strop, memImp, B | IM_OPS , iSCAS }, /* AE */
{ strop, memImp, IM_OPS , iSCAS }, /* AF */
{ regop, data1, B , iMOV }, /* B0 */
{ regop, data1, B , iMOV }, /* B1 */
{ regop, data1, B , iMOV }, /* B2 */
{ regop, data1, B , iMOV }, /* B3 */
{ regop, data1, B , iMOV }, /* B4 */
{ regop, data1, B , iMOV }, /* B5 */
{ regop, data1, B , iMOV }, /* B6 */
{ regop, data1, B , iMOV }, /* B7 */
{ regop, data2, 0 , iMOV }, /* B8 */
{ regop, data2, 0 , iMOV }, /* B9 */
{ regop, data2, 0 , iMOV }, /* BA */
{ regop, data2, 0 , iMOV }, /* BB */
{ regop, data2, NOT_HLL , iMOV }, /* BC */
{ regop, data2, 0 , iMOV }, /* BD */
{ regop, data2, 0 , iMOV }, /* BE */
{ regop, data2, 0 , iMOV }, /* BF */
{ shift, data1, B , iZERO }, /* C0 */
{ shift, data1, NSP | SRC_B , iZERO }, /* C1 */
{ data2, none2, 0 , iRET }, /* C2 */
{ none1, none2, NO_OPS , iRET }, /* C3 */
{ memOnly, modrm, TO_REG | NSP , iLES }, /* C4 */
{ memOnly, modrm, TO_REG | NSP , iLDS }, /* C5 */
{ memReg0, data1, B , iMOV }, /* C6 */
{ memReg0, data2, 0 , iMOV }, /* C7 */
{ data2, data1, 0 , iENTER}, /* C8 */
{ none1, none2, NO_OPS , iLEAVE}, /* C9 */
{ data2, none2, 0 , iRETF }, /* CA */
{ none1, none2, NO_OPS , iRETF }, /* CB */
{ const3, none2, NOT_HLL , iINT }, /* CC */
{ data1,checkInt, NOT_HLL , iINT }, /* CD */
{ none1, none2, NOT_HLL | NO_OPS , iINTO }, /* CE */
{ none1, none2, NOT_HLL | NO_OPS , iIRET }, /* Cf */
{ shift, const1, B , iZERO }, /* D0 */
{ shift, const1, SRC_B , iZERO }, /* D1 */
{ shift, none1, B , iZERO }, /* D2 */
{ shift, none1, SRC_B , iZERO }, /* D3 */
{ data1, axImp, NOT_HLL , iAAM }, /* D4 */
{ data1, axImp, NOT_HLL , iAAD }, /* D5 */
{ none1, none2, 0 , iZERO }, /* D6 */
{ memImp, axImp, NOT_HLL | B| IM_OPS , iXLAT }, /* D7 */
{ escop, none2, FLOAT_OP , iESC }, /* D8 */
{ escop, none2, FLOAT_OP , iESC }, /* D9 */
{ escop, none2, FLOAT_OP , iESC }, /* DA */
{ escop, none2, FLOAT_OP , iESC }, /* DB */
{ escop, none2, FLOAT_OP , iESC }, /* DC */
{ escop, none2, FLOAT_OP , iESC }, /* DD */
{ escop, none2, FLOAT_OP , iESC }, /* DE */
{ escop, none2, FLOAT_OP , iESC }, /* Df */
{ dispS, none2, 0 , iLOOPNE}, /* E0 */
{ dispS, none2, 0 , iLOOPE}, /* E1 */
{ dispS, none2, 0 , iLOOP }, /* E2 */
{ dispS, none2, 0 , iJCXZ }, /* E3 */
{ data1, axImp, NOT_HLL | B|NO_SRC , iIN }, /* E4 */
{ data1, axImp, NOT_HLL | NO_SRC , iIN }, /* E5 */
{ data1, axImp, NOT_HLL | B|NO_SRC , iOUT }, /* E6 */
{ data1, axImp, NOT_HLL | NO_SRC , iOUT }, /* E7 */
{ dispN, none2, 0 , iCALL }, /* E8 */
{ dispN, none2, 0 , iJMP }, /* E9 */
{ dispF, none2, 0 , iJMPF }, /* EA */
{ dispS, none2, 0 , iJMP }, /* EB */
{ none1, axImp, NOT_HLL | B|NO_SRC , iIN }, /* EC */
{ none1, axImp, NOT_HLL | NO_SRC , iIN }, /* ED */
{ none1, axImp, NOT_HLL | B|NO_SRC , iOUT }, /* EE */
{ none1, axImp, NOT_HLL | NO_SRC , iOUT }, /* EF */
{ none1, none2, NOT_HLL | NO_OPS , iLOCK }, /* F0 */
{ none1, none2, 0 , iZERO }, /* F1 */
{ prefix, none2, 0 , iREPNE}, /* F2 */
{ prefix, none2, 0 , iREPE }, /* F3 */
{ none1, none2, NOT_HLL | NO_OPS , iHLT }, /* F4 */
{ none1, none2, NO_OPS , iCMC }, /* F5 */
{ arith, none1, B , iZERO }, /* F6 */
{ arith, none1, NSP , iZERO }, /* F7 */
{ none1, none2, NO_OPS , iCLC }, /* F8 */
{ none1, none2, NO_OPS , iSTC }, /* F9 */
{ none1, none2, NOT_HLL | NO_OPS , iCLI }, /* FA */
{ none1, none2, NOT_HLL | NO_OPS , iSTI }, /* FB */
{ none1, none2, NO_OPS , iCLD }, /* FC */
{ none1, none2, NO_OPS , iSTD }, /* FD */
{ trans, none1, B , iZERO }, /* FE */
{ trans, none1, NSP , iZERO } /* FF */
};
static const StateTabelEntry stateTable[] = {
{ modrm, none2, B , iADD }, /* 00 */
{ modrm, none2, 0 , iADD }, /* 01 */
{ modrm, none2, TO_REG | B , iADD }, /* 02 */
{ modrm, none2, TO_REG , iADD }, /* 03 */
{ data1, axImp, B , iADD }, /* 04 */
{ data2, axImp, 0 , iADD }, /* 05 */
{ segop, none2, NO_SRC , iPUSH }, /* 06 */
{ segop, none2, NO_SRC , iPOP }, /* 07 */
{ modrm, none2, B , iOR }, /* 08 */
{ modrm, none2, NSP , iOR }, /* 09 */
{ modrm, none2, TO_REG | B , iOR }, /* 0A */
{ modrm, none2, TO_REG | NSP , iOR }, /* 0B */
{ data1, axImp, B , iOR }, /* 0C */
{ data2, axImp, 0 , iOR }, /* 0D */
{ segop, none2, NO_SRC , iPUSH }, /* 0E */
{ none1, none2, OP386 , iINVALID }, /* 0F */
{ modrm, none2, B , iADC }, /* 10 */
{ modrm, none2, NSP , iADC }, /* 11 */
{ modrm, none2, TO_REG | B , iADC }, /* 12 */
{ modrm, none2, TO_REG | NSP , iADC }, /* 13 */
{ data1, axImp, B , iADC }, /* 14 */
{ data2, axImp, 0 , iADC }, /* 15 */
{ segop, none2, NOT_HLL | NO_SRC , iPUSH }, /* 16 */
{ segop, none2, NOT_HLL | NO_SRC , iPOP }, /* 17 */
{ modrm, none2, B , iSBB }, /* 18 */
{ modrm, none2, NSP , iSBB }, /* 19 */
{ modrm, none2, TO_REG | B , iSBB }, /* 1A */
{ modrm, none2, TO_REG | NSP , iSBB }, /* 1B */
{ data1, axImp, B , iSBB }, /* 1C */
{ data2, axImp, 0 , iSBB }, /* 1D */
{ segop, none2, NO_SRC , iPUSH }, /* 1E */
{ segop, none2, NO_SRC , iPOP }, /* 1F */
{ modrm, none2, B , iAND }, /* 20 */
{ modrm, none2, NSP , iAND }, /* 21 */
{ modrm, none2, TO_REG | B , iAND }, /* 22 */
{ modrm, none2, TO_REG | NSP , iAND }, /* 23 */
{ data1, axImp, B , iAND }, /* 24 */
{ data2, axImp, 0 , iAND }, /* 25 */
{ prefix, none2, 0 , (IC)rES}, /* 26 */
{ none1, axImp, NOT_HLL | B|NO_SRC , iDAA }, /* 27 */
{ modrm, none2, B , iSUB }, /* 28 */
{ modrm, none2, 0 , iSUB }, /* 29 */
{ modrm, none2, TO_REG | B , iSUB }, /* 2A */
{ modrm, none2, TO_REG , iSUB }, /* 2B */
{ data1, axImp, B , iSUB }, /* 2C */
{ data2, axImp, 0 , iSUB }, /* 2D */
{ prefix, none2, 0 , (IC)rCS}, /* 2E */
{ none1, axImp, NOT_HLL | B|NO_SRC , iDAS }, /* 2F */
{ modrm, none2, B , iXOR }, /* 30 */
{ modrm, none2, NSP , iXOR }, /* 31 */
{ modrm, none2, TO_REG | B , iXOR }, /* 32 */
{ modrm, none2, TO_REG | NSP , iXOR }, /* 33 */
{ data1, axImp, B , iXOR }, /* 34 */
{ data2, axImp, 0 , iXOR }, /* 35 */
{ prefix, none2, 0 , (IC)rSS}, /* 36 */
{ none1, axImp, NOT_HLL | NO_SRC , iAAA }, /* 37 */
{ modrm, none2, B , iCMP }, /* 38 */
{ modrm, none2, NSP , iCMP }, /* 39 */
{ modrm, none2, TO_REG | B , iCMP }, /* 3A */
{ modrm, none2, TO_REG | NSP , iCMP }, /* 3B */
{ data1, axImp, B , iCMP }, /* 3C */
{ data2, axImp, 0 , iCMP }, /* 3D */
{ prefix, none2, 0 , (IC)rDS}, /* 3E */
{ none1, axImp, NOT_HLL | NO_SRC , iAAS }, /* 3F */
{ regop, none2, 0 , iINC }, /* 40 */
{ regop, none2, 0 , iINC }, /* 41 */
{ regop, none2, 0 , iINC }, /* 42 */
{ regop, none2, 0 , iINC }, /* 43 */
{ regop, none2, NOT_HLL , iINC }, /* 44 */
{ regop, none2, 0 , iINC }, /* 45 */
{ regop, none2, 0 , iINC }, /* 46 */
{ regop, none2, 0 , iINC }, /* 47 */
{ regop, none2, 0 , iDEC }, /* 48 */
{ regop, none2, 0 , iDEC }, /* 49 */
{ regop, none2, 0 , iDEC }, /* 4A */
{ regop, none2, 0 , iDEC }, /* 4B */
{ regop, none2, NOT_HLL , iDEC }, /* 4C */
{ regop, none2, 0 , iDEC }, /* 4D */
{ regop, none2, 0 , iDEC }, /* 4E */
{ regop, none2, 0 , iDEC }, /* 4F */
{ regop, none2, NO_SRC , iPUSH }, /* 50 */
{ regop, none2, NO_SRC , iPUSH }, /* 51 */
{ regop, none2, NO_SRC , iPUSH }, /* 52 */
{ regop, none2, NO_SRC , iPUSH }, /* 53 */
{ regop, none2, NOT_HLL | NO_SRC , iPUSH }, /* 54 */
{ regop, none2, NO_SRC , iPUSH }, /* 55 */
{ regop, none2, NO_SRC , iPUSH }, /* 56 */
{ regop, none2, NO_SRC , iPUSH }, /* 57 */
{ regop, none2, NO_SRC , iPOP }, /* 58 */
{ regop, none2, NO_SRC , iPOP }, /* 59 */
{ regop, none2, NO_SRC , iPOP }, /* 5A */
{ regop, none2, NO_SRC , iPOP }, /* 5B */
{ regop, none2, NOT_HLL | NO_SRC , iPOP }, /* 5C */
{ regop, none2, NO_SRC , iPOP }, /* 5D */
{ regop, none2, NO_SRC , iPOP }, /* 5E */
{ regop, none2, NO_SRC , iPOP }, /* 5F */
{ none1, none2, NOT_HLL | NO_OPS , iPUSHA}, /* 60 */
{ none1, none2, NOT_HLL | NO_OPS , iPOPA }, /* 61 */
{ memOnly, modrm, TO_REG | NSP , iBOUND}, /* 62 */
{ none1, none2, OP386 , iINVALID }, /* 63 */
{ none1, none2, OP386 , iINVALID }, /* 64 */
{ none1, none2, OP386 , iINVALID }, /* 65 */
{ none1, none2, OP386 , iINVALID }, /* 66 */
{ none1, none2, OP386 , iINVALID }, /* 67 */
{ data2, none2, NO_SRC , iPUSH }, /* 68 */
{ modrm, data2, TO_REG | NSP , iIMUL }, /* 69 */
{ data1, none2, S_EXT | NO_SRC , iPUSH }, /* 6A */
{ modrm, data1, TO_REG | NSP | S_EXT , iIMUL }, /* 6B */
{ strop, memImp, NOT_HLL | B|IM_OPS , iINS }, /* 6C */
{ strop, memImp, NOT_HLL | IM_OPS , iINS }, /* 6D */
{ strop, memImp, NOT_HLL | B|IM_OPS , iOUTS }, /* 6E */
{ strop, memImp, NOT_HLL | IM_OPS , iOUTS }, /* 6F */
{ dispS, none2, NOT_HLL , iJO }, /* 70 */
{ dispS, none2, NOT_HLL , iJNO }, /* 71 */
{ dispS, none2, 0 , iJB }, /* 72 */
{ dispS, none2, 0 , iJAE }, /* 73 */
{ dispS, none2, 0 , iJE }, /* 74 */
{ dispS, none2, 0 , iJNE }, /* 75 */
{ dispS, none2, 0 , iJBE }, /* 76 */
{ dispS, none2, 0 , iJA }, /* 77 */
{ dispS, none2, 0 , iJS }, /* 78 */
{ dispS, none2, 0 , iJNS }, /* 79 */
{ dispS, none2, NOT_HLL , iJP }, /* 7A */
{ dispS, none2, NOT_HLL , iJNP }, /* 7B */
{ dispS, none2, 0 , iJL }, /* 7C */
{ dispS, none2, 0 , iJGE }, /* 7D */
{ dispS, none2, 0 , iJLE }, /* 7E */
{ dispS, none2, 0 , iJG }, /* 7F */
{ immed, data1, B , iINVALID }, /* 80 */
{ immed, data2, NSP , iINVALID }, /* 81 */
{ immed, data1, B , iINVALID }, /* 82 */ /* ?? */
{ immed, data1, NSP | S_EXT , iINVALID }, /* 83 */
{ modrm, none2, TO_REG | B , iTEST }, /* 84 */
{ modrm, none2, TO_REG | NSP , iTEST }, /* 85 */
{ modrm, none2, TO_REG | B , iXCHG }, /* 86 */
{ modrm, none2, TO_REG | NSP , iXCHG }, /* 87 */
{ modrm, none2, B , iMOV }, /* 88 */
{ modrm, none2, 0 , iMOV }, /* 89 */
{ modrm, none2, TO_REG | B , iMOV }, /* 8A */
{ modrm, none2, TO_REG , iMOV }, /* 8B */
{ segrm, none2, NSP , iMOV }, /* 8C */
{ memOnly, modrm, TO_REG | NSP , iLEA }, /* 8D */
{ segrm, none2, TO_REG | NSP , iMOV }, /* 8E */
{ memReg0, none2, NO_SRC , iPOP }, /* 8F */
{ none1, none2, NO_OPS , iNOP }, /* 90 */
{ regop, axImp, 0 , iXCHG }, /* 91 */
{ regop, axImp, 0 , iXCHG }, /* 92 */
{ regop, axImp, 0 , iXCHG }, /* 93 */
{ regop, axImp, NOT_HLL , iXCHG }, /* 94 */
{ regop, axImp, 0 , iXCHG }, /* 95 */
{ regop, axImp, 0 , iXCHG }, /* 96 */
{ regop, axImp, 0 , iXCHG }, /* 97 */
{ alImp, axImp, SRC_B | S_EXT , iSIGNEX}, /* 98 */
{axSrcIm, axImp, IM_DST | S_EXT , iSIGNEX}, /* 99 */
{ dispF, none2, TO_REG , iCALLF }, /* 9A */ // TO_REG set to use SRC when processing setAddress
{ none1, none2, FLOAT_OP| NO_OPS , iWAIT }, /* 9B */
{ none1, none2, NOT_HLL | NO_OPS , iPUSHF}, /* 9C */
{ none1, none2, NOT_HLL | NO_OPS , iPOPF }, /* 9D */
{ none1, none2, NOT_HLL | NO_OPS , iSAHF }, /* 9E */
{ none1, none2, NOT_HLL | NO_OPS , iLAHF }, /* 9F */
{ dispM, axImp, B , iMOV }, /* A0 */
{ dispM, axImp, 0 , iMOV }, /* A1 */
{ dispM, axImp, TO_REG | B , iMOV }, /* A2 */
{ dispM, axImp, TO_REG , iMOV }, /* A3 */
{ strop, memImp, B | IM_OPS , iMOVS }, /* A4 */
{ strop, memImp, IM_OPS , iMOVS }, /* A5 */
{ strop, memImp, B | IM_OPS , iCMPS }, /* A6 */
{ strop, memImp, IM_OPS , iCMPS }, /* A7 */
{ data1, axImp, B , iTEST }, /* A8 */
{ data2, axImp, 0 , iTEST }, /* A9 */
{ strop, memImp, B | IM_OPS , iSTOS }, /* AA */
{ strop, memImp, IM_OPS , iSTOS }, /* AB */
{ strop, memImp, B | IM_OPS , iLODS }, /* AC */
{ strop, memImp, IM_OPS , iLODS }, /* AD */
{ strop, memImp, B | IM_OPS , iSCAS }, /* AE */
{ strop, memImp, IM_OPS , iSCAS }, /* AF */
{ regop, data1, B , iMOV }, /* B0 */
{ regop, data1, B , iMOV }, /* B1 */
{ regop, data1, B , iMOV }, /* B2 */
{ regop, data1, B , iMOV }, /* B3 */
{ regop, data1, B , iMOV }, /* B4 */
{ regop, data1, B , iMOV }, /* B5 */
{ regop, data1, B , iMOV }, /* B6 */
{ regop, data1, B , iMOV }, /* B7 */
{ regop, data2, 0 , iMOV }, /* B8 */
{ regop, data2, 0 , iMOV }, /* B9 */
{ regop, data2, 0 , iMOV }, /* BA */
{ regop, data2, 0 , iMOV }, /* BB */
{ regop, data2, NOT_HLL , iMOV }, /* BC */
{ regop, data2, 0 , iMOV }, /* BD */
{ regop, data2, 0 , iMOV }, /* BE */
{ regop, data2, 0 , iMOV }, /* BF */
{ shift, data1, B , iINVALID }, /* C0 */
{ shift, data1, NSP | SRC_B , iINVALID }, /* C1 */
{ data2, none2, 0 , iRET }, /* C2 */
{ none1, none2, NO_OPS , iRET }, /* C3 */
{ memOnly, modrm, TO_REG | NSP , iLES }, /* C4 */
{ memOnly, modrm, TO_REG | NSP , iLDS }, /* C5 */
{ memReg0, data1, B , iMOV }, /* C6 */
{ memReg0, data2, 0 , iMOV }, /* C7 */
{ data2, data1, 0 , iENTER}, /* C8 */
{ none1, none2, NO_OPS , iLEAVE}, /* C9 */
{ data2, none2, 0 , iRETF }, /* CA */
{ none1, none2, NO_OPS , iRETF }, /* CB */
{ const3, none2, NOT_HLL , iINT }, /* CC */
{ data1,checkInt, NOT_HLL , iINT }, /* CD */
{ none1, none2, NOT_HLL | NO_OPS , iINTO }, /* CE */
{ none1, none2, NOT_HLL | NO_OPS , iIRET }, /* Cf */
{ shift, const1, B , iINVALID }, /* D0 */
{ shift, const1, SRC_B , iINVALID }, /* D1 */
{ shift, none1, B , iINVALID }, /* D2 */
{ shift, none1, SRC_B , iINVALID }, /* D3 */
{ data1, axImp, NOT_HLL , iAAM }, /* D4 */
{ data1, axImp, NOT_HLL , iAAD }, /* D5 */
{ none1, none2, 0 , iINVALID }, /* D6 */
{ memImp, axImp, NOT_HLL | B| IM_OPS , iXLAT }, /* D7 */
{ escop, none2, FLOAT_OP , iESC }, /* D8 */
{ escop, none2, FLOAT_OP , iESC }, /* D9 */
{ escop, none2, FLOAT_OP , iESC }, /* DA */
{ escop, none2, FLOAT_OP , iESC }, /* DB */
{ escop, none2, FLOAT_OP , iESC }, /* DC */
{ escop, none2, FLOAT_OP , iESC }, /* DD */
{ escop, none2, FLOAT_OP , iESC }, /* DE */
{ escop, none2, FLOAT_OP , iESC }, /* Df */
{ dispS, none2, 0 , iLOOPNE}, /* E0 */
{ dispS, none2, 0 , iLOOPE}, /* E1 */
{ dispS, none2, 0 , iLOOP }, /* E2 */
{ dispS, none2, 0 , iJCXZ }, /* E3 */
{ data1, axImp, NOT_HLL | B|NO_SRC , iIN }, /* E4 */
{ data1, axImp, NOT_HLL | NO_SRC , iIN }, /* E5 */
{ data1, axImp, NOT_HLL | B|NO_SRC , iOUT }, /* E6 */
{ data1, axImp, NOT_HLL | NO_SRC , iOUT }, /* E7 */
{ dispN, none2, 0 , iCALL }, /* E8 */
{ dispN, none2, 0 , iJMP }, /* E9 */
{ dispF, none2, 0 , iJMPF }, /* EA */
{ dispS, none2, 0 , iJMP }, /* EB */
{ none1, axImp, NOT_HLL | B|NO_SRC , iIN }, /* EC */
{ none1, axImp, NOT_HLL | NO_SRC , iIN }, /* ED */
{ none1, axImp, NOT_HLL | B|NO_SRC , iOUT }, /* EE */
{ none1, axImp, NOT_HLL | NO_SRC , iOUT }, /* EF */
{ none1, none2, NOT_HLL | NO_OPS , iLOCK }, /* F0 */
{ none1, none2, 0 , iINVALID }, /* F1 */
{ prefix, none2, 0 , iREPNE}, /* F2 */
{ prefix, none2, 0 , iREPE }, /* F3 */
{ none1, none2, NOT_HLL | NO_OPS , iHLT }, /* F4 */
{ none1, none2, NO_OPS , iCMC }, /* F5 */
{ arith, none1, B , iINVALID }, /* F6 */
{ arith, none1, NSP , iINVALID }, /* F7 */
{ none1, none2, NO_OPS , iCLC }, /* F8 */
{ none1, none2, NO_OPS , iSTC }, /* F9 */
{ none1, none2, NOT_HLL | NO_OPS , iCLI }, /* FA */
{ none1, none2, NOT_HLL | NO_OPS , iSTI }, /* FB */
{ none1, none2, NO_OPS , iCLD }, /* FC */
{ none1, none2, NO_OPS , iSTD }, /* FD */
{ trans, none1, B , iINVALID }, /* FE */
{ trans, none1, NSP , iINVALID } /* FF */
} ;
static uint16_t SegPrefix, RepPrefix;
static const uint8_t *pInst; /* Ptr. to current uint8_t of instruction */
static ICODE * pIcode; /* Ptr to Icode record filled in by scan() */
static const uint8_t *pInst; /* Ptr. to current uint8_t of instruction */
static ICODE * pIcode; /* Ptr to Icode record filled in by scan() */
static void decodeBranchTgt(x86_insn_t &insn)
@ -330,12 +332,12 @@ static void decodeBranchTgt(x86_insn_t &insn)
int32_t addr = tgt_op->getAddress();
if(tgt_op->is_relative())
{
addr += insn.addr+insn.size;
addr = (uint16_t)(addr + insn.addr + insn.size);
}
pIcode->ll()->replaceSrc((uint32_t)addr);
pIcode->ll()->setFlags(I);
// PROG &prog(Project::get()->prog);
// long off = (short)getWord(); /* Signed displacement */
// long off = (short)getWord(); /* Signed displacement */
// assert(addr==(uint32_t)(off + (unsigned)(pInst - prog.image())));
}
@ -380,7 +382,7 @@ static void fixFloatEmulation(x86_insn_t &insn)
return;
PROG &prog(Project::get()->prog);
uint16_t wOp=insn.x86_get_imm()->data.word;
if ((wOp < 0x34) || (wOp > 0x3B))
if ((wOp < 0x34) or (wOp > 0x3B))
return;
uint8_t buf[16];
/* This is a Borland/Microsoft floating point emulation instruction. Treat as if it is an ESC opcode */
@ -403,7 +405,7 @@ int disassembleOneLibDisasm(uint32_t ip,x86_insn_t &l)
PROG &prog(Project::get()->prog);
X86_Disasm ds(opt_16_bit);
int cnt=ds.x86_disasm(prog.image(),prog.cbImage,0,ip,&l);
if(cnt && l.is_valid())
if(cnt and l.is_valid())
{
fixFloatEmulation(l); //can change 'l'
}
@ -413,7 +415,7 @@ int disassembleOneLibDisasm(uint32_t ip,x86_insn_t &l)
}
eReg convertRegister(const x86_reg_t &reg)
{
if( (reg_pc==reg.type) || (0==reg.id))
if( (reg_pc==reg.type) or (0==reg.id))
return rUNDEF;
eReg regmap[]={ rUNDEF,
@ -505,7 +507,7 @@ LLOperand convertOperand(const x86_op_t &from)
case op_register:
res.regi = convertRegister(from.data.reg); break;
case op_immediate:
res.opz = from.data.sdword;
res.opz = from.data.sdword; break;
case op_expression:
res = convertExpression(from.data.expression); break;
case op_offset:
@ -518,7 +520,7 @@ LLOperand convertOperand(const x86_op_t &from)
default:
fprintf(stderr,"convertOperand does not know how to convert %d\n",from.type);
}
if(res.isSet() && (res.seg == rUNDEF))
if(res.isSet() and (res.seg == rUNDEF))
{
res.seg = rDS;
}
@ -534,8 +536,8 @@ eErrorId scan(uint32_t ip, ICODE &p)
PROG &prog(Project::get()->prog);
int op;
p = ICODE();
p.type = LOW_LEVEL;
p.ll()->label = ip; /* ip is absolute offset into image*/
p.type = LOW_LEVEL_ICODE;
p.ll()->label = ip; /* ip is absolute offset into image*/
if (ip >= (uint32_t)prog.cbImage)
{
return (IP_OUT_OF_RANGE);
@ -554,13 +556,13 @@ eErrorId scan(uint32_t ip, ICODE &p)
do
{
op = *pInst++; /* First state - trivial */
op = *pInst++; /* First state - trivial */
/* Convert to Icode.opcode */
p.ll()->set(stateTable[op].opcode,stateTable[op].flg & ICODEMASK);
(*stateTable[op].state1)(op); /* Second state */
(*stateTable[op].state2)(op); /* Third state */
(*stateTable[op].state1)(op); /* Second state */
(*stateTable[op].state2)(op); /* Third state */
} while (stateTable[op].state1 == prefix); /* Loop if prefix */
} while (stateTable[op].state1 == prefix); /* Loop if prefix */
if(p.insn.group == x86_insn_t::insn_controlflow)
{
if(p.insn.x86_get_branch_target())
@ -568,7 +570,7 @@ eErrorId scan(uint32_t ip, ICODE &p)
}
// LLOperand conv = convertOperand(*p.insn.get_dest());
// assert(conv==p.ll()->dst);
if (p.ll()->getOpcode())
if (p.ll()->getOpcode()!=iINVALID)
{
/* Save bytes of image used */
p.ll()->numBytes = (uint8_t)((pInst - prog.image()) - ip);
@ -588,8 +590,8 @@ eErrorId scan(uint32_t ip, ICODE &p)
static bool relocItem(const uint8_t *p)
{
PROG &prog(Project::get()->prog);
int i;
uint32_t off = p - prog.image();
int i;
uint32_t off = p - prog.image();
for (i = 0; i < prog.cReloc; i++)
if (prog.relocTable[i] == off)
@ -620,42 +622,42 @@ static int signex(uint8_t b)
/****************************************************************************
* setAddress - Updates the source or destination field for the current
* icode, based on fdst and the TO_REG flag.
* Note: fdst == true is for the r/m part of the field (dest, unless TO_REG)
* fdst == false is for reg part of the field
* icode, based on fdst and the TO_REG flag.
* Note: fdst == true is for the r/m part of the field (dest, unless TO_REG)
* fdst == false is for reg part of the field
***************************************************************************/
static void setAddress(int i, bool fdst, uint16_t seg, int16_t reg, uint16_t off)
{
/* If not to register (i.e. to r/m), and talking about r/m, then this is dest */
LLOperand *pm = (!(stateTable[i].flg & TO_REG) == fdst) ? &pIcode->ll()->m_dst : &pIcode->ll()->src();
LLOperand *pm = (! (stateTable[i].flg & TO_REG) == fdst) ? &pIcode->ll()->m_dst : &pIcode->ll()->src();
/* Set segment. A later procedure (lookupAddr in proclist.c) will
* provide the value of this segment in the field segValue.
*/
if (seg) /* segment override */
if (seg) /* segment override */
{
pm->seg = pm->segOver = (eReg)seg;
}
else
{ /* no override, check indexed register */
if ((reg >= INDEX_BX_SI) && (reg == INDEX_BP_SI || reg == INDEX_BP_DI || reg == INDEX_BP))
{ /* no override, check indexed register */
if ((reg >= INDEX_BX_SI) and (reg == INDEX_BP_SI or reg == INDEX_BP_DI or reg == INDEX_BP))
{
pm->seg = rSS; /* indexed on bp */
pm->seg = rSS; /* indexed on bp */
}
else
{
pm->seg = rDS; /* any other indexed reg */
pm->seg = rDS; /* any other indexed reg */
}
}
pm->regi = (eReg)reg;
pm->off = (int16_t)off;
if (reg && reg < INDEX_BX_SI && (stateTable[i].flg & B))
if (reg and (reg < INDEX_BX_SI) and (stateTable[i].flg & B))
{
pm->regi = Machine_X86::subRegL(pm->regi);
}
if (seg) /* So we can catch invalid use of segment overrides */
if (seg) /* So we can catch invalid use of segment overrides */
{
SegPrefix = 0;
}
@ -671,7 +673,7 @@ static void rm(int i)
uint8_t rm = *pInst++ & 7;
switch (mod) {
case 0: /* No disp unless rm == 6 */
case 0: /* No disp unless rm == 6 */
if (rm == 6) {
setAddress(i, true, SegPrefix, 0, getWord());
pIcode->ll()->setFlags(WORD_OFF);
@ -680,21 +682,21 @@ static void rm(int i)
setAddress(i, true, SegPrefix, rm + INDEX_BX_SI, 0);
break;
case 1: /* 1 uint8_t disp */
case 1: /* 1 uint8_t disp */
setAddress(i, true, SegPrefix, rm+INDEX_BX_SI, (uint16_t)signex(*pInst++));
break;
case 2: /* 2 uint8_t disp */
case 2: /* 2 uint8_t disp */
setAddress(i, true, SegPrefix, rm + INDEX_BX_SI, getWord());
pIcode->ll()->setFlags(WORD_OFF);
break;
case 3: /* reg */
case 3: /* reg */
setAddress(i, true, 0, rm + rAX, 0);
break;
}
//pIcode->insn.get_dest()->
if ((stateTable[i].flg & NSP) && (pIcode->ll()->src().getReg2()==rSP ||
if ((stateTable[i].flg & NSP) and (pIcode->ll()->src().getReg2()==rSP or
pIcode->ll()->m_dst.getReg2()==rSP))
pIcode->ll()->setFlags(NOT_HLL);
}
@ -715,9 +717,9 @@ static void modrm(int i)
****************************************************************************/
static void segrm(int i)
{
int reg = REG(*pInst) + rES;
int reg = REG(*pInst) + rES;
if (reg > rDS || (reg == rCS && (stateTable[i].flg & TO_REG)))
if (reg > rDS or (reg == rCS and (stateTable[i].flg & TO_REG)))
pIcode->ll()->setOpcode((llIcode)0); // setCBW because it has that index
else {
setAddress(i, false, 0, (int16_t)reg, 0);
@ -742,7 +744,7 @@ static void regop(int i)
static void segop(int i)
{
if(i==0x1E) {
printf("es");
// printf("es");
}
setAddress(i, true, 0, (((int16_t)i & 0x18) >> 3) + rES, 0);
}
@ -784,7 +786,7 @@ static void memImp(int i)
static void memOnly(int )
{
if ((*pInst & 0xC0) == 0xC0)
pIcode->ll()->setOpcode((llIcode)0);
pIcode->ll()->setOpcode(iINVALID);
}
@ -793,8 +795,8 @@ static void memOnly(int )
****************************************************************************/
static void memReg0(int i)
{
if (REG(*pInst) || (*pInst & 0xC0) == 0xC0)
pIcode->ll()->setOpcode((llIcode)0);
if (REG(*pInst) or (*pInst & 0xC0) == 0xC0)
pIcode->ll()->setOpcode(iINVALID);
else
rm(i);
}
@ -810,8 +812,8 @@ static void immed(int i)
pIcode->ll()->setOpcode(immedTable[REG(*pInst)]) ;
rm(i);
if (pIcode->ll()->getOpcode() == iADD || pIcode->ll()->getOpcode() == iSUB)
pIcode->ll()->clrFlags(NOT_HLL); /* Allow ADD/SUB SP, immed */
if (pIcode->ll()->getOpcode() == iADD or pIcode->ll()->getOpcode() == iSUB)
pIcode->ll()->clrFlags(NOT_HLL); /* Allow ADD/SUB SP, immed */
}
@ -823,7 +825,7 @@ static void shift(int i)
static llIcode shiftTable[8] =
{
(llIcode)iROL, (llIcode)iROR, (llIcode)iRCL, (llIcode)iRCR,
(llIcode)iSHL, (llIcode)iSHR, (llIcode)0, (llIcode)iSAR};
(llIcode)iSHL, (llIcode)iSHR, (llIcode)0, (llIcode)iSAR};
pIcode->ll()->setOpcode(shiftTable[REG(*pInst)]);
rm(i);
@ -838,20 +840,20 @@ static void trans(int i)
{
static llIcode transTable[8] =
{
(llIcode)iINC, iDEC, (llIcode)iCALL, (llIcode)iCALLF,
(llIcode)iJMP, (llIcode)iJMPF,(llIcode)iPUSH, (llIcode)0
iINC, iDEC, iCALL, iCALLF,
iJMP, iJMPF,iPUSH, (llIcode)0
};
LLInst *ll = pIcode->ll();
// if(transTable[REG(*pInst)]==iPUSH) {
// printf("es");
// }
if ((uint8_t)REG(*pInst) < 2 || !(stateTable[i].flg & B)) { /* INC & DEC */
if ((uint8_t)REG(*pInst) < 2 or not (stateTable[i].flg & B)) { /* INC & DEC */
ll->setOpcode(transTable[REG(*pInst)]); /* valid on bytes */
rm(i);
ll->replaceSrc( pIcode->ll()->m_dst );
if (ll->match(iJMP) || ll->match(iCALL) || ll->match(iCALLF))
if (ll->match(iJMP) or ll->match(iCALL) or ll->match(iCALLF))
ll->setFlags(NO_OPS);
else if (ll->match(iINC) || ll->match(iPUSH) || ll->match(iDEC))
else if (ll->match(iINC) or ll->match(iPUSH) or ll->match(iDEC))
ll->setFlags(NO_SRC);
}
}
@ -865,8 +867,8 @@ static void arith(int i)
uint8_t opcode;
static llIcode arithTable[8] =
{
iTEST, (llIcode)0, iNOT, iNEG,
iMUL , iIMUL, iDIV, iIDIV
iTEST, iINVALID, iNOT, iNEG,
iMUL , iIMUL, iDIV, iIDIV
};
opcode = arithTable[REG(*pInst)];
pIcode->ll()->setOpcode((llIcode)opcode);
@ -878,15 +880,15 @@ static void arith(int i)
else
data2(i);
}
else if (!(opcode == iNOT || opcode == iNEG))
else if (not (opcode == iNOT or opcode == iNEG))
{
pIcode->ll()->replaceSrc( pIcode->ll()->m_dst );
setAddress(i, true, 0, rAX, 0); /* dst = AX */
setAddress(i, true, 0, rAX, 0); /* dst = AX */
}
else if (opcode == iNEG || opcode == iNOT)
else if (opcode == iNEG or opcode == iNOT)
pIcode->ll()->setFlags(NO_SRC);
if ((opcode == iDIV) || (opcode == iIDIV))
if ((opcode == iDIV) or (opcode == iIDIV))
{
if ( not pIcode->ll()->testFlags(B) )
pIcode->ll()->setFlags(IM_TMP_DST);
@ -916,7 +918,7 @@ static void data2(int )
* but this field is being used as the number of bytes to allocate
* on the stack. The procedure level is stored in the immediate
* field. There is no source operand; therefore, the flag flg is
* set to NO_OPS. */
* set to NO_OPS. */
if (pIcode->ll()->getOpcode() == iENTER)
{
pIcode->ll()->m_dst.off = getWord();
@ -943,7 +945,7 @@ static void dispN(int )
{
//PROG &prog(Project::get()->prog);
/*long off = (short)*/getWord(); /* Signed displacement */
/*long off = (short)*/getWord(); /* Signed displacement */
/* Note: the result of the subtraction could be between 32k and 64k, and
still be positive; it is an offset from prog.Image. So this must be
@ -957,7 +959,7 @@ static void dispN(int )
***************************************************************************/
static void dispS(int )
{
/*long off =*/ signex(*pInst++); /* Signed displacement */
/*long off =*/ signex(*pInst++); /* Signed displacement */
// decodeBranchTgt();
}
@ -970,6 +972,7 @@ static void dispF(int i)
{
uint16_t off = (unsigned)getWord();
uint16_t seg = (unsigned)getWord();
// FIXME: this is wrong since seg here is seg value, but setAddress treats it as register id
setAddress(i, true, seg, 0, off);
// decodeBranchTgt();
}
@ -981,7 +984,7 @@ static void dispF(int i)
****************************************************************************/
static void prefix(int )
{
if (pIcode->ll()->getOpcode() == iREPE || pIcode->ll()->getOpcode() == iREPNE)
if ((pIcode->ll()->getOpcode() == iREPE) or (pIcode->ll()->getOpcode() == iREPNE))
RepPrefix = pIcode->ll()->getOpcode();
else
SegPrefix = pIcode->ll()->getOpcode();
@ -989,8 +992,8 @@ static void prefix(int )
inline void BumpOpcode(LLInst &ll)
{
llIcode ic((llIcode)ll.getOpcode());
ic = (llIcode)(((int)ic)+1); // Bump this icode via the int type
llIcode ic(ll.getOpcode());
ic = (llIcode)(((int)ic)+1); // Bump this icode via the int type
ll.setOpcode(ic);
}
@ -1001,7 +1004,7 @@ static void strop(int )
{
if (RepPrefix)
{
if ( pIcode->ll()->match(iCMPS) || pIcode->ll()->match(iSCAS) )
if ( pIcode->ll()->match(iCMPS) or pIcode->ll()->match(iSCAS) )
{
if(pIcode->insn.prefix & insn_rep_zero)
{
@ -1075,7 +1078,7 @@ static void none2(int )
static void checkInt(int )
{
uint16_t wOp = (uint16_t) pIcode->ll()->src().getImm2();
if ((wOp >= 0x34) && (wOp <= 0x3B))
if ((wOp >= 0x34) and (wOp <= 0x3B))
{
/* This is a Borland/Microsoft floating point emulation instruction.
Treat as if it is an ESC opcode */

View File

@ -133,7 +133,7 @@ void destroySymTables(void)
}
/* Using the value, read the symbolic name */
bool readVal(std::ostringstream &/*symName*/, uint32_t /*symOff*/, Function * /*symProc*/)
bool readVal(QTextStream &/*symName*/, uint32_t /*symOff*/, Function * /*symProc*/)
{
return false; // no symbolic names for now
}

View File

@ -4,13 +4,15 @@
* that just plays with abstract cfg's and intervals and such like.
* (C) Cristina Cifuentes
****************************************************************************/
#include <list>
#include <cassert>
#include <stdio.h>
#include "dcc.h"
#include "disassem.h"
#include "project.h"
#include <QtCore/QDebug>
#include <list>
#include <cassert>
#include <stdio.h>
#include <CallGraph.h>
extern Project g_proj;
//static void displayCFG(Function * pProc);
//static void displayDfs(BB * pBB);
@ -60,7 +62,7 @@ void Function::controlFlowAnalysis()
if (option.verbose)
{
printf("\nDepth first traversal - Proc %s\n", name.c_str());
qDebug() <<"\nDepth first traversal - Proc" <<name;
(*m_actual_cfg.begin())->displayDfs();
//m_cfg.front()->displayDfs();
}
@ -74,9 +76,16 @@ void udm(void)
/* Build the control flow graph, find idioms, and convert low-level
* icodes to high-level ones */
Project *proj = Project::get();
Disassembler ds(2);
for (auto iter = Project::get()->pProcList.rbegin(); iter!=Project::get()->pProcList.rend(); ++iter)
for (auto iter = proj->pProcList.rbegin(); iter!=proj->pProcList.rend(); ++iter)
{
Function &f(*iter);
if(option.CustomEntryPoint) {
if(f.procEntry!=option.CustomEntryPoint) {
continue;
}
}
iter->buildCFG(ds);
}
if (option.asm2)
@ -87,10 +96,23 @@ void udm(void)
* and intermediate instructions. Find expressions by forward
* substitution algorithm */
LivenessSet live_regs;
Project::get()->pProcList.front().dataFlow (live_regs);
if(option.CustomEntryPoint) {
ilFunction iter = proj->findByEntry(option.CustomEntryPoint);
if(iter==proj->pProcList.end()) {
qCritical()<< "No function found at entry point" << QString::number(option.CustomEntryPoint,16);
return;
}
iter->dataFlow(live_regs);
iter->controlFlowAnalysis();
delete proj->callGraph;
proj->callGraph = new CALL_GRAPH;
proj->callGraph->proc = iter;
return;
}
proj->pProcList.front().dataFlow (live_regs);
/* Control flow analysis - structuring algorithm */
for (auto iter = Project::get()->pProcList.rbegin(); iter!=Project::get()->pProcList.rend(); ++iter)
for (auto iter = proj->pProcList.rbegin(); iter!=proj->pProcList.rend(); ++iter)
{
iter->controlFlowAnalysis();
}
@ -101,7 +123,7 @@ void udm(void)
***************************************************************************/
void Function::displayCFG()
{
printf("\nBasic Block List - Proc %s", name.c_str());
qDebug() << "\nBasic Block List - Proc"<<name;
for (BB *pBB : /*m_cfg*/m_actual_cfg)
{
pBB->display();

4
tools/CMakeLists.txt Normal file
View File

@ -0,0 +1,4 @@
add_subdirectory(dispsrch)
add_subdirectory(makedsig)
add_subdirectory(readsig)
add_subdirectory(parsehdr)

View File

@ -0,0 +1,9 @@
add_executable(dispsig dispsig)
target_link_libraries(dispsig dcc_hash)
qt5_use_modules(dispsig Core)
add_executable(srchsig srchsig)
target_link_libraries(srchsig dcc_hash dcc_lib)
qt5_use_modules(srchsig Core)

201
tools/dispsrch/dispsig.cpp Normal file
View File

@ -0,0 +1,201 @@
/* Quick program to copy a named signature to a small file */
#include "perfhlib.h"
#include <QtCore/QString>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* statics */
uint8_t buf[100];
int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* File being read */
FILE *f2; /* File being written */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *g; /* g[] */
/* prototypes */
void grab(int n);
uint16_t readFileShort(void);
void cleanup(void);
#define SYMLEN 16
#define PATLEN 23
/* Hash table structure */
typedef struct HT_tag {
char htSym[SYMLEN];
uint8_t htPat[PATLEN];
} HT;
HT ht; /* One hash table entry */
PerfectHash g_pattern_hasher;
int main(int argc, char *argv[]) {
uint16_t w, len;
int i;
if (argc <= 3) {
printf("Usage: dispsig <SigFilename> <FunctionName> <BinFileName>\n");
printf("Example: dispsig dccm8s.sig printf printf.bin\n");
exit(1);
}
if ((f = fopen(argv[1], "rb")) == NULL) {
printf("Cannot open %s\n", argv[1]);
exit(2);
}
if ((f2 = fopen(argv[3], "wb")) == NULL) {
printf("Cannot write to %s\n", argv[3]);
exit(2);
}
/* Read the parameters */
grab(4);
if (memcmp("dccs", buf, 4) != 0) {
printf("Not a dccs file!\n");
exit(3);
}
numKeys = readFileShort();
numVert = readFileShort();
PatLen = readFileShort();
SymLen = readFileShort();
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
g_pattern_hasher.setHashParams(
numKeys, /* The number of symbols */
PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
T1base = g_pattern_hasher.readT1();
T2base = g_pattern_hasher.readT2();
g = g_pattern_hasher.readG();
/* Read T1 and T2 tables */
grab(2);
if (memcmp("T1", buf, 2) != 0) {
printf("Expected 'T1'\n");
exit(3);
}
len = PatLen * 256 * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T1base, 1, len, f) != len) {
printf("Could not read T1\n");
exit(5);
}
grab(2);
if (memcmp("T2", buf, 2) != 0) {
printf("Expected 'T2'\n");
exit(3);
}
w = readFileShort();
if (w != len) {
printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T2base, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
/* Now read the function g[] */
grab(2);
if (memcmp("gg", buf, 2) != 0) {
printf("Expected 'gg'\n");
exit(3);
}
len = numVert * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(g, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
/* This is now the hash table */
grab(2);
if (memcmp("ht", buf, 2) != 0) {
printf("Expected 'ht'\n");
exit(3);
}
w = readFileShort();
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6);
}
QString argv2(argv[2]);
for (i = 0; i < numKeys; i++) {
if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen)) {
printf("Could not read pattern %d from %s\n", i, argv[1]);
exit(7);
}
if (argv2.compare(ht.htSym, Qt::CaseInsensitive) == 0) {
/* Found it! */
break;
}
}
fclose(f);
if (i == numKeys) {
printf("Function %s not found!\n", argv[2]);
exit(2);
}
printf("Function %s index %d\n", ht.htSym, i);
for (i = 0; i < PatLen; i++) {
printf("%02X ", ht.htPat[i]);
}
fwrite(ht.htPat, 1, PatLen, f2);
fclose(f2);
printf("\n");
}
void cleanup(void) {
/* Free the storage for variable sized tables etc */
if (T1base)
free(T1base);
if (T2base)
free(T2base);
if (g)
free(g);
}
void grab(int n) {
if (fread(buf, 1, n, f) != (size_t)n) {
printf("Could not read\n");
exit(11);
}
}
uint16_t readFileShort(void) {
uint8_t b1, b2;
if (fread(&b1, 1, 1, f) != 1) {
printf("Could not read\n");
exit(11);
}
if (fread(&b2, 1, 1, f) != 1) {
printf("Could not read\n");
exit(11);
}
return (b2 << 8) + b1;
}

View File

@ -0,0 +1,11 @@
CFLAGS = -Zi -c -AL -W3 -D__MSDOS__
dispsig.exe: dispsig.obj perfhlib.obj
link /CO dispsig perfhlib;
dispsig.obj: dispsig.c dcc.h perfhlib.h
cl $(CFLAGS) $*.c
perfhlib.obj: perfhlib.c dcc.h perfhlib.h
cl $(CFLAGS) $*.c

221
tools/dispsrch/dispsrch.txt Normal file
View File

@ -0,0 +1,221 @@
DISPSIG and SRCHSIG
===================
1 What are DispSig and SrchSig?
2 How do I use DispSig?
3 How do I use SrchSig?
4 What can I do with the binary pattern file from DispSig?
5 How can I create a binary pattern file for SrchSig?
1 What are DispSig and SrchSig?
-------------------------------
SrchSig is a program to display the name of a function, given a
signature (pattern).
DispSig is a program to display a signature, given a function name.
Dispsig also writes the signature to a binary file, so you can
disassemble it, or use it in Srchsig to see if some other signature
file has the same pattern.
2 How do I use DispSig?
-----------------------
Just type
DispSig <SignatureFileName> <FunctionName> <BinaryFileName>
For example:
dispsig dccb2s.sig strcmp strcmp.bin
Function index 58
55 8B EC 56 57 8C D8 8E C0 FC 33 C0 8B D8 8B 7E 06 8B F7 32 C0 B9 F4
This tells us that the function was the 59th function in the
signature file (and that the signature above will hash to 58
(decimal)). We can see that it is a standard C function, since it
starts with "55 8B EC", which is the standard C function prologue.
The rest of it is a bit hard to follow, but fortunately we have also
written the pattern to a binary file, strcmp.bin. See section 4 on
how to disassemble this pattern.
If I type
dispsig dcct4p.sig writeln wl.bin
I get
Function writeln not found!
In fact, there is no one function that performs the writeln function;
there are functions like WriteString, WriteInt, CrLf (Carriage
return, linefeed), and so on. Dispsig is case insensitive, so:
dispsig dcct4p.sig writestring wl.bin
produces
Function WriteString index 53
55 8B EC C4 7E 0C E8 F4 F4 75 25 C5 76 08 8B 4E 06 FC AC F4 F4 2B C8
3 How do I use SrchSig?
-----------------------
Just type
srchsig <SignatureFileName> <BinaryFileName>
dispsig dcct4p.sig writeln wl.bin
where BinaryFileName contains a pattern. See section 5 for how to
create one of these. For now, we can use the pattern file from the
first example:
srchsig dccb2s.sig strcmp.bin
Pattern:
55 8B EC 56 57 8C D8 8E C0 FC 33 C0 8B D8 8B 7E 06 8B F7 32 C0 B9 F4
Pattern hashed to 58 (0x3A), symbol strcmp
Pattern matched
Note that the pattern reported above need not be exactly the same as
the one we provided in <BinaryFileName>. The pattern displayed is the
wildcarded and chopped version of the pattern provided; it will have
F4s (wildcards) and possibly zeroes at the end; see the file
makedstp.txt for a simple explanation of wildcarding and chopping.
If we type
srchsig dccb2s.sig ws.bin
we get
Pattern:
55 8B EC C4 7E 0C E8 F4 F4 75 25 C5 76 08 8B 4E 06 FC AC F4 F4 2B C8
Pattern hashed to 0 (0x0), symbol _IOERROR
Pattern mismatch: found following pattern
55 8B EC 56 8B 76 04 0B F6 7C 14 83 FE 58 76 03 BE F4 F4 89 36 F4 F4
300
The pattern often hashes to zero when the pattern is unknown, due to
the sparse nature of the tables used in the hash function. The first
pattern in dccb2s.sig happens to be _IOERROR, and its pattern is
completely different, apart from the first three bytes. The "300" at
the end is actually a running count of signatures searched linearly,
in case there is a problem with the hash function.
4 What can I do with the binary pattern file from DispSig?
----------------------------------------------------------
You can feed it into SrchSig; this might make sense if you wanted to
know if, e.g. the signature for printf was the same for version 2 as
it is for version 3. In this case, you would use DispSig on the
version 2 signature file, and SrchSig on the version 3 file.
You can also disassemble it, using debug (it comes with MS-DOS). For
example
debug strcmp.bin
-u100 l 17
1754:0100 55 PUSH BP
1754:0101 8BEC MOV BP,SP
1754:0103 56 PUSH SI
1754:0104 57 PUSH DI
1754:0105 8CD8 MOV AX,DS
1754:0107 8EC0 MOV ES,AX
1754:0109 FC CLD
1754:010A 33C0 XOR AX,AX
1754:010C 8BD8 MOV BX,AX
1754:010E 8B7E06 MOV DI,[BP+06]
1754:0111 8BF7 MOV SI,DI
1754:0113 32C0 XOR AL,AL
1754:0115 B9F42B MOV CX,2BF4
-q
Note that the "2B" at the end is actually past the end of the
signature. (Signatures are 23 bytes (17 in hex) long, so only
addresses 100-116 are valid). Remember that most 16 bit operands will
be "wildcarded", so don't believe the resultant addresses.
5 How can I create a binary pattern file for SrchSig?
-----------------------------------------------------
Again, you can use debug. Suppose you have found an interesing piece
of code at address 05BE (this example comes from a hello world
program):
-u 5be
15FF:05BE 55 PUSH BP
15FF:05BF 8BEC MOV BP,SP
15FF:05C1 83EC08 SUB SP,+08
15FF:05C4 57 PUSH DI
15FF:05C5 56 PUSH SI
15FF:05C6 BE1E01 MOV SI,011E
15FF:05C9 8D4606 LEA AX,[BP+06]
15FF:05CC 8946FC MOV [BP-04],AX
15FF:05CF 56 PUSH SI
15FF:05D0 E8E901 CALL 07BC
15FF:05D3 83C402 ADD SP,+02
15FF:05D6 8BF8 MOV DI,AX
15FF:05D8 8D4606 LEA AX,[BP+06]
15FF:05DB 50 PUSH AX
15FF:05DC FF7604 PUSH [BP+04]
-mcs:5be l 17 cs:100
-u100 l 17
15FF:0100 55 PUSH BP
15FF:0101 8BEC MOV BP,SP
15FF:0103 83EC08 SUB SP,+08
15FF:0106 57 PUSH DI
15FF:0107 56 PUSH SI
15FF:0108 BE1E01 MOV SI,011E
15FF:010B 8D4606 LEA AX,[BP+06]
15FF:010E 8946FC MOV [BP-04],AX
15FF:0111 56 PUSH SI
15FF:0112 E8E901 CALL 02FE
15FF:0115 83C41F ADD SP,+1F
-nfoo.bin
-rcx
CS 268A
:17
-w
Writing 0017 bytes
-q
c>dir foo.bin
foo.bin 23 3-25-94 12:04
c>
The binary file has to be exactly 23 bytes long; that's why we
changed cx to the value 17 (hex 17 = decimal 23). If you are studying
a large file (> 64K) remember to set bx to 0 as well. The m (block
move) command moves the code of interest to cs:100, which is where
debug will write the file from. The "rcx" changes the length of the
save, and the "nfoo.bin" sets the name of the file to be saved. Now
we can feed this into srchsig:
srchsig dccb2s.sig foo.bin
Pattern:
55 8B EC 83 EC 08 57 56 BE F4 F4 8D 46 06 89 46 FC 56 E8 F4 F4 83 C4
Pattern hashed to 278 (0x116), symbol sleep
Pattern mismatch: found following pattern
55 8B EC 83 EC 04 56 57 8D 46 FC 50 E8 F4 F4 59 80 7E FE 5A 76 05 BF
300
Hmmm. Not a Borland C version 2 small model signature. Perhaps its a
Microsoft Version 5 signature:
Pattern:
55 8B EC 83 EC 08 57 56 BE F4 F4 8D 46 06 89 46 FC 56 E8 F4 F4 83 C4
Pattern hashed to 31 (0x1F), symbol printf
Pattern matched
Yes, it was good old printf. Of course, no need for you to guess, DCC
will figure out the vendor, version number, and model for you.

245
tools/dispsrch/srchsig.cpp Normal file
View File

@ -0,0 +1,245 @@
/* Quick program to see if a pattern is in a sig file. Pattern is supplied
in a small .bin or .com style file */
#include "perfhlib.h"
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
/* statics */
uint8_t buf[100];
int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* Sig file being read */
FILE *fpat; /* Pattern file being read */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *g; /* g[] */
#define SYMLEN 16
#define PATLEN 23
typedef struct HT_tag {
/* Hash table structure */
char htSym[SYMLEN];
uint8_t htPat[PATLEN];
} HT;
HT *ht; /* Declare a pointer to a hash table */
/* prototypes */
void grab(int n);
uint16_t readFileShort(void);
void cleanup(void);
extern void fixWildCards(uint8_t pat[]); /* In fixwild.c */
void pattSearch(void);
PerfectHash g_pattern_hasher;
int main(int argc, char *argv[]) {
uint16_t w, len;
int h, i;
int patlen;
if (argc <= 2) {
printf("Usage: srchsig <SigFilename> <PattFilename>\n");
printf("Searches the signature file for the given pattern\n");
printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
exit(1);
}
if ((f = fopen(argv[1], "rb")) == NULL) {
printf("Cannot open signature file %s\n", argv[1]);
exit(2);
}
if ((fpat = fopen(argv[2], "rb")) == NULL) {
printf("Cannot open pattern file %s\n", argv[2]);
exit(2);
}
/* Read the parameters */
grab(4);
if (memcmp("dccs", buf, 4) != 0) {
printf("Not a dccs file!\n");
exit(3);
}
numKeys = readFileShort();
numVert = readFileShort();
PatLen = readFileShort();
SymLen = readFileShort();
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
g_pattern_hasher.setHashParams(
numKeys, /* The number of symbols */
PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas
and Majewski for details */
T1base = g_pattern_hasher.readT1();
T2base = g_pattern_hasher.readT2();
g = g_pattern_hasher.readG();
/* Read T1 and T2 tables */
grab(2);
if (memcmp("T1", buf, 2) != 0) {
printf("Expected 'T1'\n");
exit(3);
}
len = PatLen * 256 * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T1base, 1, len, f) != len) {
printf("Could not read T1\n");
exit(5);
}
grab(2);
if (memcmp("T2", buf, 2) != 0) {
printf("Expected 'T2'\n");
exit(3);
}
w = readFileShort();
if (w != len) {
printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T2base, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
/* Now read the function g[] */
grab(2);
if (memcmp("gg", buf, 2) != 0) {
printf("Expected 'gg'\n");
exit(3);
}
len = numVert * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(g, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
/* This is now the hash table */
/* First allocate space for the table */
if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0) {
printf("Could not allocate hash table\n");
exit(1);
}
grab(2);
if (memcmp("ht", buf, 2) != 0) {
printf("Expected 'ht'\n");
exit(3);
}
w = readFileShort();
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6);
}
for (i = 0; i < numKeys; i++) {
if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen) {
printf("Could not read\n");
exit(11);
}
}
/* Read the pattern to buf */
if ((patlen = fread(buf, 1, 100, fpat)) == 0) {
printf("Could not read pattern\n");
exit(11);
}
if (patlen != PATLEN) {
printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN);
exit(12);
}
/* Fix the wildcards */
fixWildCards(buf);
printf("Pattern:\n");
for (i = 0; i < PATLEN; i++)
printf("%02X ", buf[i]);
printf("\n");
h = g_pattern_hasher.hash(buf);
printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
if (memcmp(ht[h].htPat, buf, PATLEN) == 0) {
printf("Pattern matched");
} else {
printf("Pattern mismatch: found following pattern\n");
for (i = 0; i < PATLEN; i++)
printf("%02X ", ht[h].htPat[i]);
printf("\n");
pattSearch(); /* Look for it the hard way */
}
cleanup();
free(ht);
fclose(f);
fclose(fpat);
return 0;
}
void pattSearch(void) {
int i;
for (i = 0; i < numKeys; i++) {
if ((i % 100) == 0)
printf("\r%d ", i);
if (memcmp(ht[i].htPat, buf, PATLEN) == 0) {
printf("\nPattern matched offset %d (0x%X)\n", i, i);
}
}
printf("\n");
}
void cleanup(void) {
/* Free the storage for variable sized tables etc */
if (T1base)
free(T1base);
if (T2base)
free(T2base);
if (g)
free(g);
}
void grab(int n) {
if (fread(buf, 1, n, f) != (size_t)n) {
printf("Could not read\n");
exit(11);
}
}
uint16_t readFileShort(void) {
uint8_t b1, b2;
if (fread(&b1, 1, 1, f) != 1) {
printf("Could not read\n");
exit(11);
}
if (fread(&b2, 1, 1, f) != 1) {
printf("Could not read\n");
exit(11);
}
return (b2 << 8) + b1;
}
/* Following two functions not needed unless creating tables */
void getKey(int i, uint8_t **keys) {}
/* Display key i */
void dispKey(int i) {}

View File

@ -0,0 +1,14 @@
CFLAGS = -Zi -c -AL -W3 -D__MSDOS__
srchsig.exe: srchsig.obj perfhlib.obj fixwild.obj
link /CO srchsig perfhlib fixwild;
srchsig.obj: srchsig.c dcc.h perfhlib.h
cl $(CFLAGS) $*.c
perfhlib.obj: perfhlib.c dcc.h perfhlib.h
cl $(CFLAGS) $*.c
fixwild.obj: fixwild.c dcc.h
cl $(CFLAGS) $*.c

View File

@ -0,0 +1,11 @@
set(SRC
makedsig
fixwild.cpp
LIB_PatternCollector.cpp
LIB_PatternCollector.h
TPL_PatternCollector.cpp
TPL_PatternCollector.h
)
add_executable(makedsig ${SRC})
target_link_libraries(makedsig dcc_hash)
qt5_use_modules(makedsig Core)

View File

@ -0,0 +1,240 @@
#include "LIB_PatternCollector.h"
#include "msvc_fixes.h"
#include <cstring>
#include <algorithm>
/** \note there is an untested assumption that the *first* segment definition
with class CODE will be the one containing all useful functions in the
LEDATA records. Functions such as _exit() have more than one segment
declared with class CODE (MSC8 libraries) */
extern void fixWildCards(uint8_t pat[]);
void readNN(int n, FILE *fl)
{
if (fseek(fl, (long)n, SEEK_CUR) != 0)
{
printf("Could not seek file\n");
exit(2);
}
}
void LIB_PatternCollector::readString(FILE *fl)
{
uint8_t len;
len = readByte(fl);
if (fread(buf, 1, len, fl) != len)
{
printf("Could not read string len %d\n", len);
exit(2);
}
buf[len] = '\0';
offset += len;
}
int LIB_PatternCollector::readSyms(FILE *fl)
{
int i;
int count = 0;
int firstSym = 0; /* First symbol this module */
uint8_t b, c, type;
uint16_t w, len;
codeLNAMES = NONE; /* Invalidate indexes for code segment */
codeSEGDEF = NONE; /* Else won't be assigned */
offset = 0; /* For diagnostics, really */
if ((leData = (uint8_t *)malloc(0xFF80)) == 0)
{
printf("Could not malloc 64k bytes for LEDATA\n");
exit(10);
}
while (not feof(fl))
{
type = readByte(fl);
len = readWord(fl);
/* Note: uncommenting the following generates a *lot* of output */
/*printf("Offset %05lX: type %02X len %d\n", offset-3, type, len);//*/
switch (type)
{
case 0x96: /* LNAMES */
while (len > 1)
{
readString(fl);
++lnum;
if (strcmp((char *)buf, "CODE") == 0)
{
/* This is the class name we're looking for */
codeLNAMES= lnum;
}
len -= strlen((char *)buf)+1;
}
b = readByte(fl); /* Checksum */
break;
case 0x98: /* Segment definition */
b = readByte(fl); /* Segment attributes */
if ((b & 0xE0) == 0)
{
/* Alignment field is zero. Frame and offset follow */
readWord(fl);
readByte(fl);
}
w = readWord(fl); /* Segment length */
b = readByte(fl); /* Segment name index */
++segnum;
b = readByte(fl); /* Class name index */
if ((b == codeLNAMES) and (codeSEGDEF == NONE))
{
/* This is the segment defining the code class */
codeSEGDEF = segnum;
}
b = readByte(fl); /* Overlay index */
b = readByte(fl); /* Checksum */
break;
case 0x90: /* PUBDEF: public symbols */
b = readByte(fl); /* Base group */
c = readByte(fl); /* Base segment */
len -= 2;
if (c == 0)
{
w = readWord(fl);
len -= 2;
}
while (len > 1)
{
readString(fl);
w = readWord(fl); /* Offset */
b = readByte(fl); /* Type index */
if (c == codeSEGDEF)
{
char *p;
HASHENTRY entry;
p = (char *)buf;
if (buf[0] == '_') /* Leading underscore? */
{
p++; /* Yes, remove it*/
}
i = std::min(size_t(SYMLEN-1), strlen(p));
memcpy(entry.name, p, i);
entry.name[i] = '\0';
entry.offset = w;
/*printf("%04X: %s is sym #%d\n", w, keys[count].name, count);//*/
keys.push_back(entry);
count++;
}
len -= strlen((char *)buf) + 1 + 2 + 1;
}
b = readByte(fl); /* Checksum */
break;
case 0xA0: /* LEDATA */
{
b = readByte(fl); /* Segment index */
w = readWord(fl); /* Offset */
len -= 3;
/*printf("LEDATA seg %d off %02X len %Xh, looking for %d\n", b, w, len-1, codeSEGDEF);//*/
if (b != codeSEGDEF)
{
readNN(len,fl); /* Skip the data */
break; /* Next record */
}
if (fread(&leData[w], 1, len-1, fl) != len-1)
{
printf("Could not read LEDATA length %d\n", len-1);
exit(2);
}
offset += len-1;
maxLeData = std::max<uint16_t>(maxLeData, w+len-1);
readByte(fl); /* Checksum */
break;
}
default:
readNN(len,fl); /* Just skip the lot */
if (type == 0x8A) /* Mod end */
{
/* Now find all the patterns for public code symbols that
we have found */
for (i=firstSym; i < count; i++)
{
uint16_t off = keys[i].offset;
if (off == (uint16_t)-1)
{
continue; /* Ignore if already done */
}
if (keys[i].offset > maxLeData)
{
printf(
"Warning: no LEDATA for symbol #%d %s "
"(offset %04X, max %04X)\n",
i, keys[i].name, off, maxLeData);
/* To make things consistant, we set the pattern for
this symbol to nulls */
memset(&keys[i].pat, 0, PATLEN);
continue;
}
/* Copy to temp buffer so don't overrun later patterns.
(e.g. when chopping a short pattern).
Beware of short patterns! */
if (off+PATLEN <= maxLeData)
{
/* Available pattern is >= PATLEN */
memcpy(buf, &leData[off], PATLEN);
}
else
{
/* Short! Only copy what is available (and malloced!) */
memcpy(buf, &leData[off], maxLeData-off);
/* Set rest to zeroes */
memset(&buf[maxLeData-off], 0, PATLEN-(maxLeData-off));
}
fixWildCards((uint8_t *)buf);
/* Save into the hash entry. */
memcpy(keys[i].pat, buf, PATLEN);
keys[i].offset = (uint16_t)-1; // Flag it as done
//printf("Saved pattern for %s\n", keys[i].name);
}
while (readByte(fl) == 0);
readNN(-1,fl); /* Unget the last byte (= type) */
lnum = 0; /* Reset index into lnames */
segnum = 0; /* Reset index into snames */
firstSym = count; /* Remember index of first sym this mod */
codeLNAMES = NONE; /* Invalidate indexes for code segment */
codeSEGDEF = NONE;
memset(leData, 0, maxLeData); /* Clear out old junk */
maxLeData = 0; /* No data read this module */
}
else if (type == 0xF1)
{
/* Library end record */
return count;
}
}
}
free(leData);
keys.clear();
return count;
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "PatternCollector.h"
struct LIB_PatternCollector : public PatternCollector
{
protected:
unsigned long offset;
uint8_t lnum = 0; /* Count of LNAMES so far */
uint8_t segnum = 0; /* Count of SEGDEFs so far */
uint8_t codeLNAMES; /* Index of the LNAMES for "CODE" class */
uint8_t codeSEGDEF; /* Index of the first SEGDEF that has class CODE */
#define NONE 0xFF /* Improbable segment index */
uint8_t *leData; /* Pointer to 64K of alloc'd data. Some .lib files
have the symbols (PUBDEFs) *after* the data
(LEDATA), so you need to keep the data here */
uint16_t maxLeData; /* How much data we have in there */
/* read a length then string to buf[]; make it an asciiz string */
void readString( FILE *fl);
public:
/* Read the .lib file, and put the keys into the array *keys[]. Returns the count */
int readSyms(FILE *fl);
};

View File

@ -0,0 +1,303 @@
#include "TPL_PatternCollector.h"
#include "msvc_fixes.h"
#include <cstring>
/** \note Fundamental problem: there seems to be no information linking the names
in the system unit ("V" category) with their routines, except trial and
error. I have entered a few. There is no guarantee that the same pmap
offset will map to the same routine in all versions of turbo.tpl. They
seem to match so far in version 4 and 5.0 */
#define roundUp(w) ((w + 0x0F) & 0xFFF0)
extern void fixWildCards(uint8_t pat[]);
void TPL_PatternCollector::enterSym(FILE *f, const char *name, uint16_t pmapOffset)
{
uint16_t pm, cm, codeOffset, pcode;
uint16_t j;
/* Enter a symbol with given name */
allocSym(count);
strcpy(keys[count].name, name);
pm = pmap + pmapOffset; /* Pointer to the 4 byte pmap structure */
fseek(f, unitBase+pm, SEEK_SET);/* Go there */
cm = readShort(f); /* CSeg map offset */
codeOffset = readShort(f); /* How far into the code segment is our rtn */
j = cm / 8; /* Index into the cmap array */
pcode = csegBase+csegoffs[j]+codeOffset;
fseek(f, unitBase+pcode, SEEK_SET); /* Go there */
grab(f,PATLEN); /* Grab the pattern to buf[] */
fixWildCards(buf); /* Fix the wild cards */
memcpy(keys[count].pat, buf, PATLEN); /* Copy to the key array */
count++; /* Done one more */
}
void TPL_PatternCollector::allocSym(int count)
{
keys.resize(count);
}
void TPL_PatternCollector::readCmapOffsets(FILE *f)
{
uint16_t cumsize, csize;
uint16_t i;
/* Read the cmap table to find the start address of each segment */
fseek(f, unitBase+cmap, SEEK_SET);
cumsize = 0;
csegIdx = 0;
for (i=cmap; i < pmap; i+=8)
{
readShort(f); /* Always 0 */
csize = readShort(f);
if (csize == 0xFFFF) continue; /* Ignore the first one... unit init */
csegoffs[csegIdx++] = cumsize;
cumsize += csize;
grab(f,4);
}
}
void TPL_PatternCollector::enterSystemUnit(FILE *f)
{
/* The system unit is special. The association between keywords and
pmap entries is not stored in the .tpl file (as far as I can tell).
So we hope that they are constant pmap entries.
*/
fseek(f, 0x0C, SEEK_SET);
cmap = readShort(f);
pmap = readShort(f);
fseek(f, offStCseg, SEEK_SET);
csegBase = roundUp(readShort(f)); /* Round up to next 16 bdry */
printf("CMAP table at %04X\n", cmap);
printf("PMAP table at %04X\n", pmap);
printf("Code seg base %04X\n", csegBase);
readCmapOffsets(f);
enterSym(f,"INITIALISE", 0x04);
enterSym(f,"UNKNOWN008", 0x08);
enterSym(f,"EXIT", 0x0C);
enterSym(f,"BlockMove", 0x10);
unknown(f,0x14, 0xC8);
enterSym(f,"PostIO", 0xC8);
enterSym(f,"UNKNOWN0CC", 0xCC);
enterSym(f,"STACKCHK", 0xD0);
enterSym(f,"UNKNOWN0D4", 0xD4);
enterSym(f,"WriteString", 0xD8);
enterSym(f,"WriteInt", 0xDC);
enterSym(f,"UNKNOWN0E0", 0xE0);
enterSym(f,"UNKNOWN0E4", 0xE4);
enterSym(f,"CRLF", 0xE8);
enterSym(f,"UNKNOWN0EC", 0xEC);
enterSym(f,"UNKNOWN0F0", 0xF0);
enterSym(f,"UNKNOWN0F4", 0xF4);
enterSym(f,"ReadEOL", 0xF8);
enterSym(f,"Read", 0xFC);
enterSym(f,"UNKNOWN100", 0x100);
enterSym(f,"UNKNOWN104", 0x104);
enterSym(f,"PostWrite", 0x108);
enterSym(f,"UNKNOWN10C", 0x10C);
enterSym(f,"Randomize", 0x110);
unknown(f,0x114, 0x174);
enterSym(f,"Random", 0x174);
unknown(f,0x178, 0x1B8);
enterSym(f,"FloatAdd", 0x1B8); /* A guess! */
enterSym(f,"FloatSub", 0x1BC); /* disicx - dxbxax -> dxbxax*/
enterSym(f,"FloatMult", 0x1C0); /* disicx * dxbxax -> dxbxax*/
enterSym(f,"FloatDivide", 0x1C4); /* disicx / dxbxax -> dxbxax*/
enterSym(f,"UNKNOWN1C8", 0x1C8);
enterSym(f,"DoubleToFloat",0x1CC); /* dxax to dxbxax */
enterSym(f,"UNKNOWN1D0", 0x1D0);
enterSym(f,"WriteFloat", 0x1DC);
unknown(f,0x1E0, 0x200);
}
void TPL_PatternCollector::readString(FILE *f)
{
uint8_t len;
len = readByte(f);
grab(f,len);
buf[len] = '\0';
}
void TPL_PatternCollector::unknown(FILE *f, unsigned j, unsigned k)
{
/* Mark calls j to k (not inclusive) as unknown */
unsigned i;
for (i=j; i < k; i+= 4)
{
sprintf((char *)buf, "UNKNOWN%03X", i);
enterSym(f,(char *)buf, i);
}
}
void TPL_PatternCollector::nextUnit(FILE *f)
{
/* Find the start of the next unit */
uint16_t dsegBase, sizeSyms, sizeOther1, sizeOther2;
fseek(f, unitBase+offStCseg, SEEK_SET);
dsegBase = roundUp(readShort(f));
sizeSyms = roundUp(readShort(f));
sizeOther1 = roundUp(readShort(f));
sizeOther2 = roundUp(readShort(f));
unitBase += dsegBase + sizeSyms + sizeOther1 + sizeOther2;
fseek(f, unitBase, SEEK_SET);
if (fread(buf, 1, 4, f) == 4)
{
buf[4]='\0';
printf("Start of unit: found %s\n", buf);
}
}
void TPL_PatternCollector::setVersionSpecifics()
{
version = buf[3]; /* The x of TPUx */
switch (version)
{
case '0': /* Version 4.0 */
offStCseg = 0x14; /* Offset to the LL giving the Cseg start */
charProc = 'T'; /* Indicates a proc in the dictionary */
charFunc = 'U'; /* Indicates a function in the dictionary */
skipPmap = 6; /* Bytes to skip after Func to get pmap offset */
break;
case '5': /* Version 5.0 */
offStCseg = 0x18; /* Offset to the LL giving the Cseg start */
charProc = 'T'; /* Indicates a proc in the dictionary */
charFunc = 'U'; /* Indicates a function in the dictionary */
skipPmap = 1; /* Bytes to skip after Func to get pmap offset */
break;
default:
printf("Unknown version %c!\n", version);
exit(1);
}
}
void TPL_PatternCollector::savePos(FILE *f)
{
if (positionStack.size() >= 20)
{
printf("Overflowed filePosn array\n");
exit(1);
}
positionStack.push_back(ftell(f));
}
void TPL_PatternCollector::restorePos(FILE *f)
{
if (positionStack.empty() == 0)
{
printf("Underflowed filePosn array\n");
exit(1);
}
fseek(f, positionStack.back(), SEEK_SET);
positionStack.pop_back();
}
void TPL_PatternCollector::enterUnitProcs(FILE *f)
{
uint16_t i, LL;
uint16_t hash, hsize, dhdr, pmapOff;
char cat;
char name[40];
fseek(f, unitBase+0x0C, SEEK_SET);
cmap = readShort(f);
pmap = readShort(f);
fseek(f, unitBase+offStCseg, SEEK_SET);
csegBase = roundUp(readShort(f)); /* Round up to next 16 bdry */
printf("CMAP table at %04X\n", cmap);
printf("PMAP table at %04X\n", pmap);
printf("Code seg base %04X\n", csegBase);
readCmapOffsets(f);
fseek(f, unitBase+pmap, SEEK_SET); /* Go to first pmap entry */
if (readShort(f) != 0xFFFF) /* FFFF means none */
{
sprintf(name, "UNIT_INIT_%d", ++unitNum);
enterSym(f,name, 0); /* This is the unit init code */
}
fseek(f, unitBase+0x0A, SEEK_SET);
hash = readShort(f);
//printf("Hash table at %04X\n", hash);
fseek(f, unitBase+hash, SEEK_SET);
hsize = readShort(f);
//printf("Hash table size %04X\n", hsize);
for (i=0; i <= hsize; i+= 2)
{
dhdr = readShort(f);
if (dhdr)
{
savePos(f);
fseek(f, unitBase+dhdr, SEEK_SET);
do
{
LL = readShort(f);
readString(f);
strcpy(name, (char *)buf);
cat = readByte(f);
if ((cat == charProc) or (cat == charFunc))
{
grab(f,skipPmap); /* Skip to the pmap */
pmapOff = readShort(f); /* pmap offset */
printf("pmap offset for %13s: %04X\n", name, pmapOff);
enterSym(f,name, pmapOff);
}
//printf("%13s %c ", name, cat);
if (LL)
{
//printf("LL seek to %04X\n", LL);
fseek(f, unitBase+LL, SEEK_SET);
}
} while (LL);
restorePos(f);
}
}
}
int TPL_PatternCollector::readSyms(FILE *f)
{
grab(f,4);
if ((strncmp((char *)buf, "TPU0", 4) != 0) and ((strncmp((char *)buf, "TPU5", 4) != 0)))
{
printf("Not a Turbo Pascal version 4 or 5 library file\n");
fclose(f);
exit(1);
}
setVersionSpecifics();
enterSystemUnit(f);
unitBase = 0;
do
{
nextUnit(f);
if (feof(f)) break;
enterUnitProcs(f);
} while (1);
return count;
}

View File

@ -0,0 +1,38 @@
#ifndef TPL_PATTERNCOLLECTOR_H
#define TPL_PATTERNCOLLECTOR_H
#include "PatternCollector.h"
#include <stdio.h>
#include <stdint.h>
#include <vector>
struct TPL_PatternCollector : public PatternCollector {
protected:
uint16_t cmap, pmap, csegBase, unitBase;
uint16_t offStCseg, skipPmap;
int count = 0;
int cAllocSym = 0;
int unitNum = 0;
char version, charProc, charFunc;
uint16_t csegoffs[100];
uint16_t csegIdx;
std::vector<long int> positionStack;
void enterSym(FILE *f,const char *name, uint16_t pmapOffset);
void allocSym(int count);
void readCmapOffsets(FILE *f);
void enterSystemUnit(FILE *f);
void readString(FILE *f);
void unknown(FILE *f,unsigned j, unsigned k);
void nextUnit(FILE *f);
void setVersionSpecifics(void);
void savePos(FILE *f);
void restorePos(FILE *f);
void enterUnitProcs(FILE *f);
public:
/* Read the .tpl file, and put the keys into the array *keys[]. Returns the count */
int readSyms(FILE *f);
};
#endif // TPL_PATTERNCOLLECTOR_H

528
tools/makedsig/fixwild.cpp Normal file
View File

@ -0,0 +1,528 @@
/*
*$Log: fixwild.c,v $
* Revision 1.10 93/10/28 11:10:10 emmerik
* Addressing mode [reg+nnnn] is now wildcarded
*
* Revision 1.9 93/10/26 13:40:11 cifuente
* op0F(byte pat[])
*
* Revision 1.8 93/10/26 13:01:29 emmerik
* Completed the odd opcodes, like 0F XX and F7. Result: some library
* functions that were not recognised before are recognised now.
*
* Revision 1.7 93/10/11 11:37:01 cifuente
* First walk of HIGH_LEVEL icodes.
*
* Revision 1.6 93/10/01 14:36:21 emmerik
* Added $ log, and made independant of dcc.h
*
*
*/
/* * * * * * * * * * * * *\
* *
* Fix Wild Cards Code *
* *
\* * * * * * * * * * * * */
#include "msvc_fixes.h"
#include <memory.h>
#include <stdint.h>
#ifndef PATLEN
#define PATLEN 23
#define WILD 0xF4
#endif
static int pc; /* Indexes into pat[] */
/* prototypes */
static bool ModRM(uint8_t pat[]); /* Handle the mod/rm byte */
static bool TwoWild(uint8_t pat[]); /* Make the next 2 bytes wild */
static bool FourWild(uint8_t pat[]); /* Make the next 4 bytes wild */
void fixWildCards(uint8_t pat[]); /* Main routine */
/* Handle the mod/rm case. Returns true if pattern exhausted */
static bool ModRM(uint8_t pat[])
{
uint8_t op;
/* A standard mod/rm byte follows opcode */
op = pat[pc++]; /* The mod/rm byte */
if (pc >= PATLEN) return true; /* Skip Mod/RM */
switch (op & 0xC0)
{
case 0x00: /* [reg] or [nnnn] */
if ((op & 0xC7) == 6)
{
/* Uses [nnnn] address mode */
pat[pc++] = WILD;
if (pc >= PATLEN) return true;
pat[pc++] = WILD;
if (pc >= PATLEN) return true;
}
break;
case 0x40: /* [reg + nn] */
if ((pc+=1) >= PATLEN) return true;
break;
case 0x80: /* [reg + nnnn] */
/* Possibly just a long constant offset from a register,
but often will be an index from a variable */
pat[pc++] = WILD;
if (pc >= PATLEN) return true;
pat[pc++] = WILD;
if (pc >= PATLEN) return true;
break;
case 0xC0: /* reg */
break;
}
return false;
}
/* Change the next two bytes to wild cards */
static bool TwoWild(uint8_t pat[])
{
pat[pc++] = WILD;
if (pc >= PATLEN) return true; /* Pattern exhausted */
pat[pc++] = WILD;
if (pc >= PATLEN) return true;
return false;
}
/* Change the next four bytes to wild cards */
static bool FourWild(uint8_t pat[])
{
TwoWild(pat);
return TwoWild(pat);
}
/* Chop from the current point by wiping with zeroes. Can't rely on anything
after this point */
static void chop(uint8_t pat[])
{
if (pc >= PATLEN) return; /* Could go negative otherwise */
memset(&pat[pc], 0, PATLEN - pc);
}
static bool op0F(uint8_t pat[])
{
/* The two byte opcodes */
uint8_t op = pat[pc++];
switch (op & 0xF0)
{
case 0x00: /* 00 - 0F */
if (op >= 0x06) /* Clts, Invd, Wbinvd */
return false;
else
{
/* Grp 6, Grp 7, LAR, LSL */
return ModRM(pat);
}
case 0x20: /* Various funnies, all with Mod/RM */
return ModRM(pat);
case 0x80:
pc += 2; /* Word displacement cond jumps */
return false;
case 0x90: /* Byte set on condition */
return ModRM(pat);
case 0xA0:
switch (op)
{
case 0xA0: /* Push FS */
case 0xA1: /* Pop FS */
case 0xA8: /* Push GS */
case 0xA9: /* Pop GS */
return false;
case 0xA3: /* Bt Ev,Gv */
case 0xAB: /* Bts Ev,Gv */
return ModRM(pat);
case 0xA4: /* Shld EvGbIb */
case 0xAC: /* Shrd EvGbIb */
if (ModRM(pat)) return true;
pc++; /* The #num bits to shift */
return false;
case 0xA5: /* Shld EvGb CL */
case 0xAD: /* Shrd EvGb CL */
return ModRM(pat);
default: /* CmpXchg, Imul */
return ModRM(pat);
}
case 0xB0:
if (op == 0xBA)
{
/* Grp 8: bt/bts/btr/btc Ev,#nn */
if (ModRM(pat)) return true;
pc++; /* The #num bits to shift */
return false;
}
return ModRM(pat);
case 0xC0:
if (op <= 0xC1)
{
/* Xadd */
return ModRM(pat);
}
/* Else BSWAP */
return false;
default:
return false; /* Treat as double byte opcodes */
}
}
/* Scan through the instructions in pat[], looking for opcodes that may
have operands that vary with different instances. For example, load and
store from statics, calls to other procs (even relative calls; they may
call procs loaded in a different order, etc).
Note that this procedure is architecture specific, and assumes the
processor is in 16 bit address mode (real mode).
PATLEN bytes are scanned.
*/
void fixWildCards(uint8_t pat[])
{
uint8_t op, quad, intArg;
pc=0;
while (pc < PATLEN)
{
op = pat[pc++];
if (pc >= PATLEN) return;
quad = op & 0xC0; /* Quadrant of the opcode map */
if (quad == 0)
{
/* Arithmetic group 00-3F */
if ((op & 0xE7) == 0x26) /* First check for the odds */
{
/* Segment prefix: treat as 1 byte opcode */
continue;
}
if (op == 0x0F) /* 386 2 byte opcodes */
{
if (op0F(pat)) return;
continue;
}
if (op & 0x04)
{
/* All these are constant. Work out the instr length */
if (op & 2)
{
/* Push, pop, other 1 byte opcodes */
continue;
}
else
{
if (op & 1)
{
/* Word immediate operands */
pc += 2;
continue;
}
else
{
/* Byte immediate operands */
pc++;
continue;
}
}
}
else
{
/* All these have mod/rm bytes */
if (ModRM(pat)) return;
continue;
}
}
else if (quad == 0x40)
{
if ((op & 0x60) == 0x40)
{
/* 0x40 - 0x5F -- these are inc, dec, push, pop of general
registers */
continue;
}
else
{
/* 0x60 - 0x70 */
if (op & 0x10)
{
/* 70-7F 2 byte jump opcodes */
pc++;
continue;
}
else
{
/* Odds and sods */
switch (op)
{
case 0x60: /* pusha */
case 0x61: /* popa */
case 0x64: /* overrides */
case 0x65:
case 0x66:
case 0x67:
case 0x6C: /* insb DX */
case 0x6E: /* outsb DX */
continue;
case 0x62: /* bound */
pc += 4;
continue;
case 0x63: /* arpl */
if (TwoWild(pat)) return;
continue;
case 0x68: /* Push byte */
case 0x6A: /* Push byte */
case 0x6D: /* insb port */
case 0x6F: /* outsb port */
/* 2 byte instr, no wilds */
pc++;
continue;
}
}
}
}
else if (quad == 0x80)
{
switch (op & 0xF0)
{
case 0x80: /* 80 - 8F */
/* All have a mod/rm byte */
if (ModRM(pat)) return;
/* These also have immediate values */
switch (op)
{
case 0x80:
case 0x83:
/* One byte immediate */
pc++;
continue;
case 0x81:
/* Immediate 16 bit values might be constant, but
also might be relocatable. Have to make them
wild */
if (TwoWild(pat)) return;
continue;
}
continue;
case 0x90: /* 90 - 9F */
if (op == 0x9A)
{
/* far call */
if (FourWild(pat)) return;
continue;
}
/* All others are 1 byte opcodes */
continue;
case 0xA0: /* A0 - AF */
if ((op & 0x0C) == 0)
{
/* mov al/ax to/from [nnnn] */
if (TwoWild(pat)) return;
continue;
}
else if ((op & 0xFE) == 0xA8)
{
/* test al,#byte or test ax,#word */
if (op & 1) pc += 2;
else pc += 1;
continue;
}
case 0xB0: /* B0 - BF */
{
if (op & 8)
{
/* mov reg, #16 */
/* Immediate 16 bit values might be constant, but also
might be relocatable. For now, make them wild */
if (TwoWild(pat)) return;
}
else
{
/* mov reg, #8 */
pc++;
}
continue;
}
}
}
else
{
/* In the last quadrant of the op code table */
switch (op)
{
case 0xC0: /* 386: Rotate group 2 ModRM, byte, #byte */
case 0xC1: /* 386: Rotate group 2 ModRM, word, #byte */
if (ModRM(pat)) return;
/* Byte immediate value follows ModRM */
pc++;
continue;
case 0xC3: /* Return */
case 0xCB: /* Return far */
chop(pat);
return;
case 0xC2: /* Ret nnnn */
case 0xCA: /* Retf nnnn */
pc += 2;
chop(pat);
return;
case 0xC4: /* les Gv, Mp */
case 0xC5: /* lds Gv, Mp */
if (ModRM(pat)) return;
continue;
case 0xC6: /* Mov ModRM, #nn */
if (ModRM(pat)) return;
/* Byte immediate value follows ModRM */
pc++;
continue;
case 0xC7: /* Mov ModRM, #nnnn */
if (ModRM(pat)) return;
/* Word immediate value follows ModRM */
/* Immediate 16 bit values might be constant, but also
might be relocatable. For now, make them wild */
if (TwoWild(pat)) return;
continue;
case 0xC8: /* Enter Iw, Ib */
pc += 3; /* Constant word, byte */
continue;
case 0xC9: /* Leave */
continue;
case 0xCC: /* Int 3 */
continue;
case 0xCD: /* Int nn */
intArg = pat[pc++];
if ((intArg >= 0x34) and (intArg <= 0x3B))
{
/* Borland/Microsoft FP emulations */
if (ModRM(pat)) return;
}
continue;
case 0xCE: /* Into */
continue;
case 0xCF: /* Iret */
continue;
case 0xD0: /* Group 2 rotate, byte, 1 bit */
case 0xD1: /* Group 2 rotate, word, 1 bit */
case 0xD2: /* Group 2 rotate, byte, CL bits */
case 0xD3: /* Group 2 rotate, word, CL bits */
if (ModRM(pat)) return;
continue;
case 0xD4: /* Aam */
case 0xD5: /* Aad */
case 0xD7: /* Xlat */
continue;
case 0xD8:
case 0xD9:
case 0xDA:
case 0xDB: /* Esc opcodes */
case 0xDC: /* i.e. floating point */
case 0xDD: /* coprocessor calls */
case 0xDE:
case 0xDF:
if (ModRM(pat)) return;
continue;
case 0xE0: /* Loopne */
case 0xE1: /* Loope */
case 0xE2: /* Loop */
case 0xE3: /* Jcxz */
pc++; /* Short jump offset */
continue;
case 0xE4: /* in al,nn */
case 0xE6: /* out nn,al */
pc++;
continue;
case 0xE5: /* in ax,nn */
case 0xE7: /* in nn,ax */
pc += 2;
continue;
case 0xE8: /* Call rel */
if (TwoWild(pat)) return;
continue;
case 0xE9: /* Jump rel, unconditional */
if (TwoWild(pat)) return;
chop(pat);
return;
case 0xEA: /* Jump abs */
if (FourWild(pat)) return;
chop(pat);
return;
case 0xEB: /* Jmp short unconditional */
pc++;
chop(pat);
return;
case 0xEC: /* In al,dx */
case 0xED: /* In ax,dx */
case 0xEE: /* Out dx,al */
case 0xEF: /* Out dx,ax */
continue;
case 0xF0: /* Lock */
case 0xF2: /* Repne */
case 0xF3: /* Rep/repe */
case 0xF4: /* Halt */
case 0xF5: /* Cmc */
case 0xF8: /* Clc */
case 0xF9: /* Stc */
case 0xFA: /* Cli */
case 0xFB: /* Sti */
case 0xFC: /* Cld */
case 0xFD: /* Std */
continue;
case 0xF6: /* Group 3 byte test/not/mul/div */
case 0xF7: /* Group 3 word test/not/mul/div */
case 0xFE: /* Inc/Dec group 4 */
if (ModRM(pat)) return;
continue;
case 0xFF: /* Group 5 Inc/Dec/Call/Jmp/Push */
/* Most are like standard ModRM */
if (ModRM(pat)) return;
continue;
default: /* Rest are single byte opcodes */
continue;
}
}
}
}

179
tools/makedsig/makedsig.cpp Normal file
View File

@ -0,0 +1,179 @@
/* Program for making the DCC signature file */
#include "LIB_PatternCollector.h"
#include "TPL_PatternCollector.h"
#include "perfhlib.h" /* Symbol table prototypes */
#include "msvc_fixes.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <algorithm>
/* Symbol table constnts */
#define C 2.2 /* Sparseness of graph. See Czech, Havas and Majewski for details */
/* prototypes */
void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll); /* Save the info */
static int numKeys; /* Number of useful codeview symbols */
static void printUsage(bool longusage) {
if(longusage)
printf(
"This program is to make 'signatures' of known c and tpl library calls for the dcc program.\n"
"It needs as the first arg the name of a library file, and as the second arg, the name "
"of the signature file to be generated.\n"
"Example: makedsig CL.LIB dccb3l.sig\n"
" or makedsig turbo.tpl dcct4p.sig\n"
);
else
printf("Usage: makedsig <libname> <signame>\n"
"or makedsig -h for help\n");
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc,argv);
FILE *f2; // output file
FILE *srcfile; // .lib file
int s;
if(app.arguments().size()<2) {
printUsage(false);
return 0;
}
QString arg2 = app.arguments()[1];
if (arg2.startsWith("-h") or arg2.startsWith("-?"))
{
printUsage(true);
return 0;
}
PatternCollector *collector;
if(arg2.endsWith("tpl")) {
collector = new TPL_PatternCollector;
} else if(arg2.endsWith(".lib")) {
collector = new LIB_PatternCollector;
} else {
qCritical() << "Unsupported file type.";
return -1;
}
if ((srcfile = fopen(argv[1], "rb")) == NULL)
{
printf("Cannot read %s\n", argv[1]);
exit(2);
}
if ((f2 = fopen(argv[2], "wb")) == NULL)
{
printf("Cannot write %s\n", argv[2]);
exit(2);
}
fprintf(stderr, "Seed: ");
scanf("%d", &s);
srand(s);
PerfectHash p_hash;
numKeys = collector->readSyms(srcfile); /* Read the keys (symbols) */
printf("Num keys: %d; vertices: %d\n", numKeys, (int)(numKeys*C));
/* Set the parameters for the hash table */
p_hash.setHashParams( numKeys, /* The number of symbols */
PATLEN, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */
numKeys*C); /* C is the sparseness of the graph. See Czech,
Havas and Majewski for details */
/* The following two functions are in perfhlib.c */
p_hash.map(collector); /* Perform the mapping. This will call getKey() repeatedly */
p_hash.assign(); /* Generate the function g */
saveFile(f2,p_hash,collector); /* Save the resultant information */
fclose(srcfile);
fclose(f2);
}
/* * * * * * * * * * * * *\
* *
* S a v e t h e s i g f i l e *
* *
\* * * * * * * * * * * * */
void writeFile(FILE *fl,const char *buffer, int len)
{
if ((int)fwrite(buffer, 1, len, fl) != len)
{
printf("Could not write to file\n");
exit(1);
}
}
void writeFileShort(FILE *fl,uint16_t w)
{
uint8_t b;
b = (uint8_t)(w & 0xFF);
writeFile(fl,(char *)&b, 1); /* Write a short little endian */
b = (uint8_t)(w>>8);
writeFile(fl,(char *)&b, 1);
}
void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll)
{
int i, len;
const uint16_t *pTable;
writeFile(fl,"dccs", 4); /* Signature */
writeFileShort(fl,numKeys); /* Number of keys */
writeFileShort(fl,(short)(numKeys * C)); /* Number of vertices */
writeFileShort(fl,PATLEN); /* Length of key part of entries */
writeFileShort(fl,SYMLEN); /* Length of symbol part of entries */
/* Write out the tables T1 and T2, with their sig and byte lengths in front */
writeFile(fl,"T1", 2); /* "Signature" */
pTable = p_hash.readT1();
len = PATLEN * 256;
writeFileShort(fl,len * sizeof(uint16_t));
for (i=0; i < len; i++)
{
writeFileShort(fl,pTable[i]);
}
writeFile(fl,"T2", 2);
pTable = p_hash.readT2();
writeFileShort(fl,len * sizeof(uint16_t));
for (i=0; i < len; i++)
{
writeFileShort(fl,pTable[i]);
}
/* Write out g[] */
writeFile(fl,"gg", 2); /* "Signature" */
pTable = p_hash.readG();
len = (short)(numKeys * C);
writeFileShort(fl,len * sizeof(uint16_t));
for (i=0; i < len; i++)
{
writeFileShort(fl,pTable[i]);
}
/* Now the hash table itself */
writeFile(fl,"ht ", 2); /* "Signature" */
writeFileShort(fl,numKeys * (SYMLEN + PATLEN + sizeof(uint16_t))); /* byte len */
for (i=0; i < numKeys; i++)
{
writeFile(fl,(char *)&coll->keys[i], SYMLEN + PATLEN);
}
}

188
tools/makedsig/makedsig.txt Normal file
View File

@ -0,0 +1,188 @@
MAKEDSIG
1 What is MakeDsig?
2 How does it work?
3 How do I use MakeDsig?
4 What's in a signature file?
5 What other tools are useful for signature work?
1 What is MakeDsig?
-------------------
MakeDsig is a program that reads a library (.lib) file from a
compiler, and generates a signature file for use by DCC. Without
signature files, dcc cannot recognise library functions, and so will
attempt to decompile them, and cannot name them. This makes the
resultant decompiled code bulkier and difficult to understand.
2 How does it work?
-------------------
Library files contain complete functions, relocation information,
function names, and more. MakeDsig reads a library file, and for each
function found, it saves the name, and creates a signature. These
are stored in an array. When all functions are done, tables for the
perfect hashing function are generated. During this process,
duplicate keys (functions that produce identical signatures) may be
detected; if so, one of the keys will be zeroed.
The signature file contains information needed by dcc to hash the
signatures, as well as the symbols and signatures. Dcc reads the various
sections of the signature file to be able to hash signatures. The
signatures, not the symbols, are hashed, since dcc gets a signature
from the executable file, and needs to know quickly if there is a
symbolic name for it.
3 How do I use MakeDsig?
------------------------
You can always find out by just executing it with no arguments, or
MakeDsig -h for more details.
Basically, you just give it the names of the files that it needs:
MakeDsig <libname> <signame>
It will ask you for a seed; enter any number, e.g. 1.
You need the library file for the appropriate compiler. For example,
to analyse executable programs created from Turbo C 2.1 small model,
you need the cs.lib file that comes with that compiler.
You also need to know the correct name for the signature file, i.e.
<signame>. Dcc will detect certain compiler vendors and version
numbers, and will look for a signature file named like this:
d c c <vendor> <version> <model> . s i g
Here are the current vendors:
Vendor Vendor letter
Microsoft C/C++ m
Borland C/C++ b
Logitech (Modula) l
Turbo Pascal t
Here are the model codes:
small/tiny s
medium m
compact c
large l
Turbo Pascal p
The version codes are fairly self explanatory:
Microsoft C 5.1 5
Microsoft C 8 8
Borland C 2.0 2
Borland C 3.0 3
Turbo Pascal 3.0 3 Note: currently no way to make dcct3p.sig
Turbo Pascal 4.0 4 Use Makedstp, not makedsig
Turbo Pascal 5.0 5 Use Makedstp, not makedsig
Some examples: the signature file for Borland C version 2.0, small
model, would be dccb2s.sig. To generate it, you would supply as the
library file cs.lib that came with that compiler. Suppose it was in
the \bc\lib directory. To generate the signature file required to
work with files produced by this compiler, you would type
makedsig \bc\lib\cs.lib dccb2s.sig
This will create dccb2s.sig in the current directory. For dcc to use
this file, place it in the same directory as dcc itself, or point the
environment variable DCC to the directory containing it.
Another example: to make the signature file for Microsoft Visual
C/C++ (C 8.0), large model, and assuming the libraries are in
the directory \msvc\lib, you would type
makedsig \msvc\lib\llibce.lib dccm8l.sig
Note that the signature files for Turbo Pascal from version 4 onwards
are generated by makedstp, not makedsig. The latter program reads a
special file called turbo.tpl, as there are no normal .lib files for
turbo pascal. Dcc will recognise turbo pascal 3.0 files, and look
for dcct3p.sig. Because all the library routines are contained in
every Turbo Pascal executable, there are no library files or even a
turbo.tpl file, so the signature file would have to be constructed by
guesswork. You can still use dcc on these files; just ignore the
warning about not finding the signature file.
For executables that dcc does not recognise, it will look for the
signature file dccxxx.sig. That way, if you have a new compiler, you
can at least have dcc detect library calls, even if it attempts to
decompile them all, and has not identified the main program.
Logitech Modula V1.0 files are recognised, and the signature file
dccl1x.sig is looked for. This was experimental in nature, and is not
recommended for serious analysis at this stage.
4 What's in a signature file?
-----------------------------
The details of a signature file are best documented in the source for
makedsig; see the function saveFile(). Briefly:
1) a 4 byte pattern identifying the file as a signature file: "dccs".
2) a two byte integer containing the number of keys (signatures)
3) a two byte integer containing the number of vertices on the graph
used to generate the hash table. See the source code and/or the
Czech, Havas and Majewski articles for details
4) a two byte integer containing the pattern length
5) a two byte integer containing the symbolic name length
The next sections all have the following structure:
1) 2 char ID
2) a two byte integer containing the size of the body
3) the body.
There are 4 sections: "T1", "T2", "gg", and "ht". T1 and T2 are the
tables associated with the hash function. (The hash function is a
random function, meaning that it involves tables. T1 and T2 are the
tables used by the hash function). "gg" is another table associated
with the graph needed by the perfect hashing function algorithm.
"ht" contains the actual hash table. The body of this section is an
array of records of this structure:
typedef struct _hashEntry
{
char name[SYMLEN]; /* The symbol name */
byte pat [PATLEN]; /* The pattern */
word offset; /* Offset (needed temporarily) */
} HASHENTRY;
This part of the signature file can be browsed with a binary dump
program; a PATLEN length signature will follow the (null padded)
symbol name. There are tools for searching signature files, e.g.
srchsig, dispsig, and readsig. See below.
5 What other tools are useful for signature work?
-------------------------------------------------
Makedstp - makes signature files from turbo.tpl. Needed to make
signature files for Turbo Pascal version 4.0 and later.
SrchSig - tells you whether a given pattern exists in a signature
file, and gives its name. You need a binary file with the signature
in it, exactly the right length. This can most easily be done with
debug (comes with MS-DOS).
DispSig - given the name of a function, displays its signature, and
stores the signature into a binary file as well. (You can use this
file with srchsig on another signature file, if you want).
ReadSig - reads a signature file, checking for correct structure, and
displaying duplicate signatures. With the -a switch, it will display
all signatures, with their symbols.
The file perfhlib.c is used by various of these tools to do the work
of the perfect hashing functions. It could be used as part of other
tools that use signature files, or just perfect hashing functions for
that matter.

Some files were not shown because too many files have changed in this diff Show More