Skip to content

Commit

Permalink
Add "data instructions"
Browse files Browse the repository at this point in the history
This commit add "data instructions", a kind instruction that is not an
ISA instruction but merely byte value. These allows users to build data
(structures) and code in an uniform way and also simplify embedding data
into instruction stream (when generating trampolines / PLTs, compiled
code headers and so on - all this is common in JIT compilers)
  • Loading branch information
janvrany committed Aug 5, 2024
1 parent b6bc53f commit 7817215
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/ArchC-Core-Tests/AcAssemblerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,39 @@ AcAssemblerTest >> testTRAP [
self assert: result binaryEncoding equals: 16r0c000048.
self assert: result disassemble equals: 'twi 0x0, 0, 0x48'
]

{ #category : #data }
AcAssemblerTest >> test_byte [
| result |

result := AcProcessorDescriptions powerpc assembler parse: '.byte 0x7F'.
self assert: result binaryEncoding equals: 16r7F.
self assert: result disassemble equals: '.byte 0x' , (16r7F printStringRadix: 16).
]

{ #category : #data }
AcAssemblerTest >> test_u16 [
| result |

result := AcProcessorDescriptions powerpc assembler parse: '.2byte 0x7FE0'.
self assert: result binaryEncoding equals: 16r7FE0.
self assert: result disassemble equals: '.2byte 0x' , (16r7FE0 printStringRadix:16).
]

{ #category : #data }
AcAssemblerTest >> test_u32 [
| result |

result := AcProcessorDescriptions powerpc assembler parse: '.4byte 0x7FE00008'.
self assert: result binaryEncoding equals: 16r7FE00008.
self assert: result disassemble equals: '.4byte 0x' , (16r7FE00008 printStringRadix:16).
]

{ #category : #data }
AcAssemblerTest >> test_u64 [
| result |

result := AcProcessorDescriptions powerpc assembler parse: '.8byte 0x7FE00008CAFEAFFE'.
self assert: result binaryEncoding equals: 16r7FE00008CAFEAFFE.
self assert: result disassemble equals: '.8byte 0x' , (16r7FE00008CAFEAFFE printStringRadix:16).
]
15 changes: 15 additions & 0 deletions src/ArchC-Core/AcByte.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class {
#name : #AcByte,
#superclass : #AcInt,
#category : #'ArchC-Core-Core'
}

{ #category : #accessing }
AcByte >> bitWidth [
^ 8
]

{ #category : #accessing }
AcByte >> name [
^ '.byte'
]
69 changes: 69 additions & 0 deletions src/ArchC-Core/AcDataInstruction.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Class {
#name : #AcDataInstruction,
#superclass : #AcInstruction,
#instVars : [
'isa',
'binaryEncoding',
'relocation'
],
#category : #'ArchC-Core-Core'
}

{ #category : #queries }
AcDataInstruction class >> isAbstract [
^ self == AcDataInstruction.
]

{ #category : #'instance creation' }
AcDataInstruction class >> value: value [
self assert: self isAbstract not description: 'Cannot instantiate abstract class'.

^ self basicNew
value: value;
yourself.
]

{ #category : #accessing }
AcDataInstruction >> binaryEncoding [
^ binaryEncoding
]

{ #category : #accessing }
AcDataInstruction >> isa [
^ isa
]

{ #category : #accessing }
AcDataInstruction >> isa: anAcProcessorDefinition [
self assert:(isa isNil or: [ anAcProcessorDefinition == isa ]).

isa := anAcProcessorDefinition.
]

{ #category : #accessing }
AcDataInstruction >> relocation [
^ relocation
]

{ #category : #accessing }
AcDataInstruction >> relocation: anAcRelocationOrNil [
"Set or clear relocation entry associated with this instruction."

self assert: (anAcRelocationOrNil isNil or: [ anAcRelocationOrNil isAcRelocation ]).
self assert: (anAcRelocationOrNil isNil or: [ anAcRelocationOrNil isValidForInstruction: self])
description: 'Invalid data relocation'.
self assert: (relocation isNil or: [ anAcRelocationOrNil isNil ])
description: 'A relocation already associated'.

relocation := anAcRelocationOrNil
]

{ #category : #accessing }
AcDataInstruction >> value [
^ self subclassResponsibility
]

{ #category : #accessing }
AcDataInstruction >> value: value [
^ self subclassResponsibility
]
54 changes: 54 additions & 0 deletions src/ArchC-Core/AcInt.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Class {
#name : #AcInt,
#superclass : #AcDataInstruction,
#category : #'ArchC-Core-Core'
}

{ #category : #queries }
AcInt class >> isAbstract [
"Return if this class is an abstract class.
True is returned here for myself only; false for subclasses.
Abstract subclasses must redefine this again."

^ self == AcInt.
]

{ #category : #'instance creation' }
AcInt class >> new [
^ self value: 0
]

{ #category : #'encoding / decoding' }
AcInt >> assembler [
^ (self name asParser trim,
( "hex literal"('0x' asParser, #hex asParser plus flatten ==> [ :prefAndnumeral | Integer readFrom: prefAndnumeral second base: 16 ])
/ "dec literal"(#digit asParser plus flatten ==> [ :numeralString | numeralString asInteger ])
/ "{varname}" (${ asParser, (PPPredicateObjectParser anyExceptAnyOf: #($})) star flatten, $} asParser ==> [ :expr | expr second])))
==> [ :nameAndValue | self class value: nameAndValue second ].

"
AcByte basicNew assembler parse: '.byte 0x1'
AcByte basicNew assembler parse: '.byte 255'
AcByte basicNew assembler parse: '.byte {byteval}'
"
]

{ #category : #'encoding / decoding' }
AcInt >> disassemble [
^ self name , ' 0x' , (self value printStringRadix: 16)
]

{ #category : #'encoding / decoding' }
AcInt >> emitOn: aStream [
self binaryEncoding toByteArrayEndian: self isa endian on: aStream
]

{ #category : #accessing }
AcInt >> value [
^ self binaryEncoding unsignedValue
]

{ #category : #accessing }
AcInt >> value: value [
binaryEncoding := value toBitVector: self bitWidth
]
15 changes: 15 additions & 0 deletions src/ArchC-Core/AcInt16.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class {
#name : #AcInt16,
#superclass : #AcInt,
#category : #'ArchC-Core-Core'
}

{ #category : #accessing }
AcInt16 >> bitWidth [
^ 16
]

{ #category : #accessing }
AcInt16 >> name [
^ '.2byte'
]
15 changes: 15 additions & 0 deletions src/ArchC-Core/AcInt32.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class {
#name : #AcInt32,
#superclass : #AcInt,
#category : #'ArchC-Core-Core'
}

{ #category : #accessing }
AcInt32 >> bitWidth [
^ 32
]

{ #category : #accessing }
AcInt32 >> name [
^ '.4byte'
]
15 changes: 15 additions & 0 deletions src/ArchC-Core/AcInt64.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Class {
#name : #AcInt64,
#superclass : #AcInt,
#category : #'ArchC-Core-Core'
}

{ #category : #accessing }
AcInt64 >> bitWidth [
^ 64
]

{ #category : #accessing }
AcInt64 >> name [
^ '.8byte'
]
5 changes: 4 additions & 1 deletion src/ArchC-Core/AcProcessorDescription.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ AcProcessorDescription >> initialize [
" tgtimm := nil."
" architectureName := nil."
" abi := nil."
instructionMnemonics := OrderedCollection new.
instructionMnemonics := OrderedCollection with:(AcByte basicNew assembler
/AcInt16 basicNew assembler
/AcInt32 basicNew assembler
/AcInt64 basicNew assembler)
" opcodeDecoder := nil."
]

Expand Down

0 comments on commit 7817215

Please sign in to comment.