ios - How to get value of UnsafeMutableRawPointer? -
i'm trying address pointed unsafemutablerawpointer, i'm unable so. i'm new swift might missing or doing wrong. preferably cast raw value cchar.
note passers-by: of answer won't make sense, doesn't answer initial question above, rather question(s) arose in chat op.
took me few hours, i've learned assembly, can answer questions.
ccharc char... literally. representschartype ofc. it'stypealiasint8. it's single byte. can't use pointer type, 8 bytes (on 64 bit machines).you don't need
unsafemutablerawpointerboilerplate, , certainly don''t need access raw value. can pass arrays directly pointers expected.when function declared taking unsafepointer argument, can accept of following: ...
- a [type] value, passed pointer start of array.
you experiencing issues mutation of
0x8(%rdi)did not seem reflected on swift side. issue here you're writing @ offset of 8 bytes,print(a.load(as: void_star.self))reading first byte. you're reading byte never modified.
i did further exploration. here's loot adventure:
exampleswift.swift:
@_silgen_name("incrementbyvalue") func incrementbyvalue(_: int64) @_silgen_name("incrementbyreference") func incrementbyreference(_: inout int64) @_silgen_name("return1234") func return1234() -> int64 @_silgen_name("incrementelements") func incrementelements(of _: unsaferawpointer, count _: int) var a: int64 = 100 print("\"a\" before \"incrementbyvalue(a)\": \(a)") incrementbyvalue(a) print("\"a\" after \"incrementbyvalue(a)\": \(a)\n") var b: int64 = 200 print("\"b\" before \"incrementbyvalue(b)\": \(b)") incrementbyreference(&b) print("\"b\" after \"incrementbyvalue(b)\": \(b)\n") print("return1234() returned: \(return1234())\n") var array: [int64] = array(0...5) print("\"array\" before incrementelements(of: array, count: array.count): \n\t\(array)") incrementelements(of: array, count: array.count) print("\"array\" after incrementelements(of: array, count: array.count): \n\t\(array)\n") exampleasm.s:
.text .globl _incrementbyvalue .globl _incrementbyreference .globl _return1234 .globl _incrementelements // test routine demonstrates operating on value _incrementbyvalue: // %rdi contains copy of argument passed in. // changes here won't reflected in swift incq %rdi ret // test routine demonstrates operating on reference _incrementbyreference: // %rdi contains reference tp argument passed in. // changes reference won't reflected in swift, // changes referenced memory will. incq (%rdi) ret // test routine demonstrates use of %rax returning value _return1234: movq $1234, %rax // return value in rax ret //a test routine demonstrates operating on array _incrementelements: // %rdi: pointer first of n int64 elements // %rsi: array count, n movq %rsi, %rcx // set loop counter (%rcx) n aloop: incq (%rdi) // increment value pointer %rdi add $8, %rdi // advance pointer 8 bytes loop aloop // loop aloop if rcx > 0 ret compile, link , run with:
llvm-g++ -c exampleasm.s && swiftc -c exampleswift.swift && ld exampleasm.o exampleswift.o -o examplebinary -force_load /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -framework corefoundation -macosx_version_min 10.12.0 -syslibroot /applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.12.sdk -lobjc -lsystem -arch x86_64 -l /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/lib/swift/macosx -rpath /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/lib/swift/macosx -no_objc_category_merging && ./examplebinary output:
"a" before "incrementbyvalue(a)": 100 "a" after "incrementbyvalue(a)": 100 "b" before "incrementbyvalue(b)": 200 "b" after "incrementbyvalue(b)": 201 return1234() returned: 1234 "array" before incrementelements(of: array, count: array.count): [0, 1, 2, 3, 4, 5] "array" after incrementelements(of: array, count: array.count): [1, 2, 3, 4, 5, 6] 
Comments
Post a Comment